/// <summary> /// Converte um produto de Model para Dto /// </summary> /// <param name="entregaDto"></param> /// <param name="mensagemErro"></param> /// <returns></returns> public bool ConverterModelParaDto(ref PedidoEntregaDto entregaDto, ref string mensagemErro) { try { entregaDto.Obs = string.IsNullOrWhiteSpace(Obs) ? "" : Obs.Trim(); entregaDto.Conferido = this.Conferido; entregaDto.IdEndereco = this.IdEndereco; entregaDto.IdFuncionario = this.IdFuncionario; entregaDto.IdPedido = this.IdPedido; entregaDto.ValorRetorno = this.ValorRetorno; entregaDto.DataAlteracao = this.DataAlteracao; entregaDto.DataInclusao = this.DataInclusao; entregaDto.Id = this.Id; entregaDto.Inativo = this.Inativo; // Converter endereço ClienteEnderecoDto clienteEnderecoDto = new ClienteEnderecoDto(); if (!ClienteEndereco.ConverterModelParaDto(ref clienteEnderecoDto, ref mensagemErro)) { return(false); } entregaDto.ClienteEndereco = clienteEnderecoDto; return(true); } catch (Exception ex) { mensagemErro = ex.Message; return(false); } }
/// <summary> /// Converte um endereço de cliente de DTO para Model /// </summary> /// <param name="enderecoDto"></param> /// <param name="mensagemErro"></param> /// <returns></returns> public bool ConverterDtoParaModel(ClienteEnderecoDto enderecoDto, ref string mensagemErro) { try { this.NumeroEndereco = string.IsNullOrWhiteSpace(enderecoDto.NumeroEndereco) ? "" : enderecoDto.NumeroEndereco.Trim(); this.ComplementoEndereco = string.IsNullOrWhiteSpace(enderecoDto.ComplementoEndereco) ? "" : enderecoDto.ComplementoEndereco.Trim(); this.DataAlteracao = enderecoDto.DataAlteracao; this.DataInclusao = enderecoDto.DataInclusao; this.Id = enderecoDto.Id; this.Inativo = enderecoDto.Inativo; this.IdCep = enderecoDto.IdCep; this.IdCliente = enderecoDto.IdCliente; Endereco.Bairro = string.IsNullOrWhiteSpace(enderecoDto.Endereco.Bairro) ? "" : enderecoDto.Endereco.Bairro.Trim(); Endereco.Cep = string.IsNullOrWhiteSpace(enderecoDto.Endereco.Cep) ? "" : enderecoDto.Endereco.Cep.Trim(); Endereco.Cidade = string.IsNullOrWhiteSpace(enderecoDto.Endereco.Cidade) ? "" : enderecoDto.Endereco.Cidade.Trim(); Endereco.Logradouro = string.IsNullOrWhiteSpace(enderecoDto.Endereco.Logradouro) ? "" : enderecoDto.Endereco.Logradouro.Trim(); Endereco.Id = enderecoDto.Endereco.Id; return(true); } catch (Exception ex) { mensagemErro = ex.Message; return(false); } }
/// <summary> /// Converte um endereço de cliente de Model para Dto /// </summary> /// <param name="enderecoDto"></param> /// <param name="mensagemErro"></param> /// <returns></returns> public bool ConverterModelParaDto(ref ClienteEnderecoDto enderecoDto, ref string mensagemErro) { try { enderecoDto.NumeroEndereco = string.IsNullOrWhiteSpace(NumeroEndereco) ? "" : NumeroEndereco.Trim(); enderecoDto.ComplementoEndereco = string.IsNullOrWhiteSpace(ComplementoEndereco) ? "" : ComplementoEndereco.Trim(); enderecoDto.DataAlteracao = DataAlteracao; enderecoDto.DataInclusao = DataInclusao; enderecoDto.Id = Id; enderecoDto.Inativo = Inativo; enderecoDto.IdCep = Endereco.Id; enderecoDto.IdCliente = IdCliente; enderecoDto.Endereco.Bairro = string.IsNullOrWhiteSpace(Endereco.Bairro) ? "" : Endereco.Bairro.Trim(); enderecoDto.Endereco.Cep = string.IsNullOrWhiteSpace(Endereco.Cep) ? "" : Endereco.Cep.Trim().Replace("-", ""); enderecoDto.Endereco.Cidade = string.IsNullOrWhiteSpace(Endereco.Cidade) ? "" : Endereco.Cidade.Trim(); enderecoDto.Endereco.Logradouro = string.IsNullOrWhiteSpace(Endereco.Logradouro) ? "" : Endereco.Logradouro.Trim(); enderecoDto.Endereco.Id = Endereco.Id; return(true); } catch (Exception ex) { mensagemErro = ex.Message; return(false); } }
public ActionResult Incluir(ClienteEnderecoModel model) { //Se não tiver login, encaminhar para a tela de login if (string.IsNullOrWhiteSpace(SessaoUsuario.SessaoLogin.Identificacao)) { return(RedirectToAction("Login", "Usuario")); } //Validar a model recebida if (!ModelState.IsValid) { return(View(model)); } //Converter para DTO ClienteEnderecoDto clienteEnderecoDto = new ClienteEnderecoDto(); string mensagemErro = ""; if (!model.ConverterModelParaDto(ref clienteEnderecoDto, ref mensagemErro)) { ModelState.AddModelError("", $"Erro ao converter para Dto: {mensagemErro}"); return(View(model)); } clienteEnderecoDto.Id = Guid.NewGuid(); //Preparar requisição e retorno RetornoDto retorno = new RetornoDto(); RequisicaoEntidadeDto <ClienteEnderecoDto> requisicaoDto = new RequisicaoEntidadeDto <ClienteEnderecoDto>() { EntidadeDto = clienteEnderecoDto, Identificacao = SessaoUsuario.SessaoLogin.Identificacao, IdUsuario = SessaoUsuario.SessaoLogin.IdUsuario }; //Consumir o serviço ClienteEnderecoBll clienteEnderecoBll = new ClienteEnderecoBll(true); clienteEnderecoBll.Incluir(requisicaoDto, ref retorno); //Verificar o retorno if (retorno.Retorno == false) { //Se houver erro, exibir na tela de inclusão ModelState.AddModelError("", retorno.Mensagem); return(View(model)); } TempData["Retorno"] = "INCLUIDO"; //Retornar para o cliente return(RedirectToAction("Visualizar", "Cliente", new { id = model.IdCliente })); }
public ActionResult Editar(ClienteEnderecoModel model) { //Se não tiver login, encaminhar para a tela de login if (string.IsNullOrWhiteSpace(SessaoUsuario.SessaoLogin.Identificacao)) { return(RedirectToAction("Login", "Usuario")); } //Valida a entidade recebida if (!ModelState.IsValid) { return(View(model)); } //Converte para DTO ClienteEnderecoDto clienteDto = new ClienteEnderecoDto(); string mensagemErro = ""; if (!model.ConverterModelParaDto(ref clienteDto, ref mensagemErro)) { ViewBag.MensagemErro = mensagemErro; return(View("Erro")); } //Preparar requisição e retorno RetornoDto retorno = new RetornoDto(); RequisicaoEntidadeDto <ClienteEnderecoDto> requisicaoDto = new RequisicaoEntidadeDto <ClienteEnderecoDto>() { EntidadeDto = clienteDto, Identificacao = SessaoUsuario.SessaoLogin.Identificacao, IdUsuario = SessaoUsuario.SessaoLogin.IdUsuario }; //Consumir o serviço ClienteEnderecoBll clienteBll = new ClienteEnderecoBll(true); clienteBll.Editar(requisicaoDto, ref retorno); //Tratar o retorno if (retorno.Retorno == false) { ModelState.AddModelError("", retorno.Mensagem); return(View(model)); } TempData["Retorno"] = "ALTERADO"; //Retornar para o cliente return(RedirectToAction("Visualizar", "Cliente", new { id = model.IdCliente })); }
/// <summary> /// Obtém uma lista de entregas com filtros aplicados, podendo ser paginada /// </summary> /// <param name="requisicaoDto"></param> /// <param name="retornoDto"></param> /// <returns></returns> public override bool ObterListaFiltrada(RequisicaoObterListaDto requisicaoDto, ref RetornoObterListaDto <PedidoEntregaDto> retornoDto) { if (!base.ObterListaFiltrada(requisicaoDto, ref retornoDto)) { return(false); } string mensagemErro = ""; IQueryable <PedidoEntregaVo> query; // Obter a query primária if (!this.ObterQueryBd(out query, ref mensagemErro)) { retornoDto.Mensagem = $"Houve um problema ao listar as entregas: {mensagemErro}"; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } // Aplicar os filtros foreach (var filtro in requisicaoDto.ListaFiltros) { switch (filtro.Key) { case "IDENDERECO": Guid idEndereco; if (!Guid.TryParse(filtro.Value, out idEndereco)) { retornoDto.Mensagem = $"Problema ao converter o filtro de endereço."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => p.IdEndereco == idEndereco); break; case "DATAINCLUSAOINICIO": DateTime data; if (!DateTime.TryParse(filtro.Value, out data)) { retornoDto.Mensagem = $"Fala ao converter o filtro de 'data de inclusão'."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => DbFunctions.TruncateTime(p.DataInclusao) >= data); break; case "DATAINCLUSAOFIM": DateTime dataFim; if (!DateTime.TryParse(filtro.Value, out dataFim)) { retornoDto.Mensagem = $"Fala ao converter o filtro de 'data de inclusão'."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => DbFunctions.TruncateTime(p.DataInclusao) <= dataFim); break; case "IDPEDIDO": Guid idPedido; if (!Guid.TryParse(filtro.Value, out idPedido)) { retornoDto.Mensagem = $"Problema ao converter o filtro de pedido."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => p.IdPedido == idPedido); break; case "IDFUNCIONARIO": Guid idFuncinario; if (!Guid.TryParse(filtro.Value, out idFuncinario)) { retornoDto.Mensagem = $"Problema ao converter o filtro de pedido."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => p.IdFuncionario == idFuncinario); break; case "VALORRETORNO": float valor; if (!float.TryParse(filtro.Value, out valor)) { retornoDto.Mensagem = $"Problema ao converter o filtro de preço."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => p.ValorRetorno == valor); break; case "CONFERIDO": bool conferido; if (!bool.TryParse(filtro.Value, out conferido)) { retornoDto.Mensagem = $"Fala ao converter o filtro de 'inativo'."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => p.Conferido == conferido); break; case "INATIVO": bool filtroInativo; if (!bool.TryParse(filtro.Value, out filtroInativo)) { retornoDto.Mensagem = $"Fala ao converter o filtro de 'inativo'."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } query = query.Where(p => p.Inativo == filtroInativo); break; default: retornoDto.Mensagem = $"O filtro {filtro.Key} não está definido para esta pesquisa."; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, Guid.Empty, retornoDto.Mensagem); return(false); } } requisicaoDto.CampoOrdem = string.IsNullOrWhiteSpace(requisicaoDto.CampoOrdem) ? "" : requisicaoDto.CampoOrdem.ToUpper().Trim(); switch (requisicaoDto.CampoOrdem) { case "DATAINCLUSAO": query = query.OrderBy(p => p.DataInclusao).ThenBy(p => p.IdFuncionario); break; case "IDFUNCIONARIO": query = query.OrderBy(p => p.IdFuncionario).ThenBy(p => p.DataInclusao); break; case "DATAINCLUSAODESCRESCENTE": query = query.OrderByDescending(p => p.DataInclusao).ThenBy(p => p.IdFuncionario); break; default: query = query.OrderByDescending(p => p.DataInclusao).ThenBy(p => p.IdFuncionario); break; } double totalItens = query.Count(); if (totalItens == 0) { retornoDto.Mensagem = "Nenhum resultado encontrado."; retornoDto.Retorno = true; return(true); } if (!requisicaoDto.NaoPaginarPesquisa) { double paginas = totalItens <= requisicaoDto.NumeroItensPorPagina ? 1 : totalItens / requisicaoDto.NumeroItensPorPagina; retornoDto.NumeroPaginas = (int)Math.Ceiling(paginas); int pular = (requisicaoDto.Pagina - 1) * requisicaoDto.NumeroItensPorPagina; query = query.Skip(pular).Take(requisicaoDto.NumeroItensPorPagina); } // Obter entregas e os IDs dos endereços List <PedidoEntregaVo> listaVo = query.ToList(); List <Guid> listaIds = listaVo.Select(p => p.IdEndereco).ToList(); // Obter os endereços pelo ID RetornoObterListaDto <ClienteEnderecoDto> retornoEnderecoDto = new RetornoObterListaDto <ClienteEnderecoDto>(); RequisicaoListaGuidsDto requisicaoEnderecoDto = new RequisicaoListaGuidsDto() { Identificacao = requisicaoDto.Identificacao, IdUsuario = requisicaoDto.IdUsuario, ListaGuids = listaIds }; ClienteEnderecoBll clienteEnderecoBll = new ClienteEnderecoBll(false); if (!clienteEnderecoBll.ObterListaPorId(requisicaoEnderecoDto, ref retornoEnderecoDto)) { retornoDto.Mensagem = "Erro ao obter os endereços: " + retornoEnderecoDto.Mensagem; retornoDto.Retorno = false; } // Converter da dto ClienteEnderecoDto enderedoNaoEncontrado = new ClienteEnderecoDto(); enderedoNaoEncontrado.Endereco.Logradouro = "Endereço não encontrado"; foreach (var pedidoEntrega in listaVo) { PedidoEntregaDto pedidoEntregaDto = new PedidoEntregaDto(); if (!ConverterVoParaDto(pedidoEntrega, ref pedidoEntregaDto, ref mensagemErro)) { retornoDto.Mensagem = "Erro ao converter para DTO: " + mensagemErro; retornoDto.Retorno = false; logBll.ResgistrarLog(requisicaoDto, LogRecursos.ObterListaPedidoEntrega, pedidoEntrega.Id, retornoDto.Mensagem); return(false); } ClienteEnderecoDto endereco = retornoEnderecoDto.ListaEntidades.Where(p => p.Id == pedidoEntregaDto.IdEndereco).FirstOrDefault(); pedidoEntregaDto.ClienteEndereco = (endereco == null) ? enderedoNaoEncontrado : endereco; retornoDto.ListaEntidades.Add(pedidoEntregaDto); } retornoDto.Mensagem = "Ok"; retornoDto.Retorno = true; return(true); }