コード例 #1
0
        public ActionResult Criar(FuncionarioVM funcVM)
        {
            Funcionario funcionario = _bus.Obter(funcVM.Cpf) ?? new Funcionario();

            funcionario.Usuario.Login = (funcVM.Login ?? string.Empty).Trim();
            funcionario.Cpf           = (funcVM.Cpf ?? string.Empty).Trim();
            funcionario.Nome          = (funcVM.Nome ?? string.Empty).Trim();
            funcionario.Email         = (funcVM.Email ?? string.Empty).Trim();

            funcionario.Arquivo.ContentType    = funcVM.ArquivoContentType;
            funcionario.Arquivo.Extensao       = funcVM.ArquivoExtensao;
            funcionario.Arquivo.Id             = funcVM.ArquivoId;
            funcionario.Arquivo.Nome           = funcVM.ArquivoNome;
            funcionario.Arquivo.TemporarioNome = funcVM.ArquivoTemporarioNome;

            funcVM.ListaCargos  = funcVM.ListaCargos ?? new List <String>();
            funcVM.ListaSetores = funcVM.ListaSetores ?? new List <Setor>();
            funcVM.papeis       = funcVM.papeis ?? new List <PapeisVME>();

            funcionario.Cargos.RemoveAll(x => !funcVM.ListaCargos.Exists(y => y == x.Id.ToString()));
            funcionario.Cargos.AddRange(
                _busLista.Cargos
                .Where(x =>
                       funcVM.ListaCargos.Contains(x.Id.ToString()) &&
                       !funcionario.Cargos.Exists(y => y.Id == x.Id)));

            funcVM.ListaSetores.ForEach(x => x.Nome = _busLista.SetoresComSigla.Single(y => y.Id == x.Id).Nome);
            funcionario.Setores.RemoveAll(x => !funcVM.ListaSetores.Exists(y => y.Id == x.Id));
            funcionario.Setores.AddRange(
                funcVM.ListaSetores
                .Where(x => !funcionario.Setores.Exists(y => y.Id == x.Id)));

            foreach (var item in funcVM.papeis)
            {
                if (item.IsAtivo)
                {
                    if (!funcionario.Papeis.Exists(x => x.Id == item.Papel.Id) &&
                        _bus.PapeisFuncionario.Exists(y => y.Id == item.Papel.Id))
                    {
                        funcionario.Papeis.Add(_bus.PapeisFuncionario.Single(y => y.Id == item.Papel.Id));
                    }
                }
                else
                {
                    funcionario.Papeis.RemoveAll(x => x.Id == item.Papel.Id);
                }
            }

            if (_bus.Salvar(funcionario, funcVM.Senha, funcVM.ConfirmarSenha))
            {
                return(RedirectToAction("Criar", Validacao.QueryParamSerializer()));
            }

            CriarVM viewModel = new CriarVM(_busLista.Cargos, _busLista.SetoresComSigla);

            viewModel.Funcionario = funcionario;
            viewModel.CpfValido   = true;

            viewModel.Papeis = _bus.PapeisFuncionario.
                               Select(x => new PapeisVME()
            {
                Papel = x, IsAtivo = viewModel.Funcionario.Papeis.Any(y => y.Id == x.Id)
            }).ToList();

            viewModel.TextoPermissoes = TextoPermissoes(viewModel.Papeis);

            return(View(viewModel));
        }