Ejemplo n.º 1
0
        public ActionResult <RetornoValidaCidadao> ValidaExistencia([FromHeader] string ibge, [FromBody] Cidadao model)
        {
            try
            {
                ibge = _config.GetConnectionString(Connection.GetConnection(ibge));
                #region Valida se cidadão já existe
                string filtro = string.Empty;
                if (!string.IsNullOrWhiteSpace(model.csi_cpfpac))
                {
                    filtro += $@"( PAC.CSI_CPFPAC LIKE '%{model.csi_cpfpac}%'";
                }

                if (!string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_idepac))
                {
                    filtro += $@" OR PAC.CSI_IDEPAC LIKE '%{model.csi_idepac}%'";
                }
                else if (string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_idepac))
                {
                    filtro += $@"( PAC.CSI_IDEPAC LIKE '%{model.csi_idepac}%'";
                }

                if (!string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_ncartao))
                {
                    filtro += $@" OR PAC.CSI_CARTAO LIKE '%{model.csi_ncartao}%'";
                }
                else if (string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_ncartao))
                {
                    filtro += $@"( PAC.CSI_CARTAO LIKE '%{model.csi_ncartao}%'";
                }

                if (model.csi_dtnasc != null && !string.IsNullOrWhiteSpace(filtro))
                {
                    filtro += $@") OR PAC.CSI_DTNASC = '{model.csi_dtnasc?.ToString("dd.MM.yyyy")}'";
                }
                else if (string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_ncartao))
                {
                    filtro += $@" PAC.CSI_DTNASC = '{model.csi_dtnasc?.ToString("dd.MM.yyyy")}'";
                }

                if (!string.IsNullOrWhiteSpace(model.csi_nompac) && !string.IsNullOrWhiteSpace(filtro))
                {
                    filtro += $@" (SELECT UPPER(RETORNO) FROM TIRA_ACENTOS(PAC.CSI_NOMPAC)) LIKE '%{Helper.RemoveAcentos(model.csi_nompac.ToUpper())}%'";
                }
                else if (string.IsNullOrWhiteSpace(filtro) && !string.IsNullOrWhiteSpace(model.csi_ncartao))
                {
                    filtro += $@" (SELECT UPPER(RETORNO) FROM TIRA_ACENTOS(PAC.CSI_NOMPAC)) LIKE '%{Helper.RemoveAcentos(model.csi_nompac.ToUpper())}%'";
                }

                filtro = " WHERE " + filtro;

                var retorno = _cidadaoRepository.ValidaExistenciaCidadaoParam(ibge, filtro);

                //caso esteja editando o registro
                if (model.csi_codpac != null)
                {
                    retorno = retorno.Where(x => x.csi_codpac != model.csi_codpac).ToList();
                }

                RetornoValidaCidadao item = new RetornoValidaCidadao();
                item.cidadao = retorno;
                var possuicpfcns = retorno.Where(x => x.csi_ncartao == null ?
                                                 x.csi_cpfpac == model.csi_cpfpac :
                                                 x.csi_ncartao == model.csi_ncartao)
                                   .ToList();
                if (possuicpfcns.Count > 0)
                {
                    item.permiteEnviar = false;
                }
                else
                {
                    item.permiteEnviar = true;
                }

                #endregion

                return(Ok(item));
            }
            catch (Exception ex)
            {
                var response = TrataErro.GetResponse(ex.Message, true);
                return(StatusCode((int)HttpStatusCode.InternalServerError, response));
            }
        }