public PessoaModel InsertGestor(PessoaViewModel pessoaViewModel) { pessoaViewModel.Pessoa.Cpf = Methods.RemoveSpecialsCaracts(pessoaViewModel.Cpf); var pessoa = GetByCpf(pessoaViewModel.Pessoa.Cpf); if (pessoa != null) { throw new ServiceException("Já possui um cadastro seu no sistema. Solicite a um Gestor de Saúde " + pessoaViewModel.AreaAtuacao + " para autorizar seu CPF para acessar o sistema."); } using (var transaction = _context.Database.BeginTransaction()) { try { pessoa = Insert(pessoaViewModel.Pessoa); if (pessoaViewModel.AreaAtuacao.Equals("Municipal")) { PessoaTrabalhaMunicipioService _pessoaTrabalhaMunicipioService = new PessoaTrabalhaMunicipioService(_context); _pessoaTrabalhaMunicipioService .Insert(new PessoaTrabalhaMunicipioModel { IdPessoa = pessoa.Idpessoa, IdMunicipio = pessoaViewModel.IdMunicipio, SituacaoCadastro = EmpresaExameModel.SITUACAO_CADASTRO_SOLICITADA, EhSecretario = false, EhResponsavel = true }); } else if (pessoaViewModel.AreaAtuacao.Equals("Estadual")) { PessoaTrabalhaEstadoService _pessoaTrabalhaEstadoService = new PessoaTrabalhaEstadoService(_context); _pessoaTrabalhaEstadoService .Insert(new PessoaTrabalhaEstadoModel { IdPessoa = pessoa.Idpessoa, IdEstado = pessoaViewModel.IdEstado, SituacaoCadastro = EmpresaExameModel.SITUACAO_CADASTRO_SOLICITADA, IdEmpresaExame = EmpresaExameModel.EMPRESA_ESTADO_MUNICIPIO, //valor padrão EhSecretario = false, EhResponsavel = true }); } transaction.Commit(); } catch (Exception e) { transaction.Rollback(); throw e; } } return(pessoa); }
public void Import(IFormFile file, UsuarioViewModel agente) { var _pessoaTrabalhaMunicipioService = new PessoaTrabalhaMunicipioService(_context); var _pessoaTrabalhaEstadoContext = new PessoaTrabalhaEstadoService(_context); var _municipioGeoService = new MunicipioGeoService(_context); var _virusBacteriaService = new VirusBacteriaService(_context); var _pessoaService = new PessoaService(_context); var _empresaExameService = new EmpresaExameService(_context); var _situacaoPessoaService = new SituacaoVirusBacteriaService(_context); var _municipioService = new MunicipioService(_context); var _estadoService = new EstadoService(_context); var _exameService = new ExameService(_context); var secretarioMunicipio = _pessoaTrabalhaMunicipioService.GetByIdPessoa(agente.UsuarioModel.IdPessoa); var secretarioEstado = _pessoaTrabalhaEstadoContext.GetByIdPessoa(agente.UsuarioModel.IdPessoa); var examesPlanilha = new List <ExameViewModel>(); var indices = new IndiceItemArquivoImportacao(); var listVirusBacteria = _virusBacteriaService.GetAll(); string[] line = { }; Dictionary <string, MunicipioGeoModel> mapCidade = new Dictionary <string, MunicipioGeoModel>(); Dictionary <string, EstadoModel> mapEstado = new Dictionary <string, EstadoModel>(); Dictionary <string, EstadoModel> mapSiglaEstado = new Dictionary <string, EstadoModel>(); MunicipioModel municipioSecretario = null; if (secretarioMunicipio != null) { municipioSecretario = _municipioService.GetById(secretarioMunicipio.IdMunicipio); } using (var reader = new StreamReader(file.OpenReadStream(), Encoding.UTF7)) { var cabecalho = reader.ReadLine(); indices = IndexaColunasArquivoGal(cabecalho) ?? IndexaColunasArquivoUFS(cabecalho); if (indices == null) { throw new ServiceException("Essa planilha não possui as informações necessárias para fazer a importação, " + "por favor verifique a planilha e tente novamente."); } if (indices.EhPlanilhaGal) { line = reader.ReadLine().Split(';'); while (reader.Peek() >= 0 && String.Concat(line).Length > 0) { EstadoModel estadoPaciente = null; if (line[indices.IndiceEstadoPaciente].Length > 2) { if (mapEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]) == null) { mapEstado.Add(line[indices.IndiceEstadoPaciente], _estadoService.GetByName(line[indices.IndiceEstadoPaciente])); } estadoPaciente = mapEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]); } else if (line[indices.IndiceEstadoPaciente].Length == 2) { if (mapSiglaEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]) == null) { mapSiglaEstado.Add(line[indices.IndiceEstadoPaciente], _estadoService.GetByUf(line[indices.IndiceEstadoPaciente])); } estadoPaciente = mapSiglaEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]); } if (mapCidade.GetValueOrDefault(line[indices.IndiceCidadePaciente]) == null) { mapCidade.Add(line[indices.IndiceCidadePaciente], _municipioGeoService.GetByName(line[indices.IndiceCidadePaciente], estadoPaciente.CodigoUf)); } MunicipioGeoModel cidadePaciente = mapCidade.GetValueOrDefault(line[indices.IndiceCidadePaciente]); if (mapCidade.GetValueOrDefault(line[indices.IndiceCidadeEmpresa]) == null) { mapCidade.Add(line[indices.IndiceCidadeEmpresa], _municipioGeoService.GetByName(line[indices.IndiceCidadeEmpresa], estadoPaciente.CodigoUf)); } MunicipioGeoModel cidadeEmpresa = mapCidade.GetValueOrDefault(line[indices.IndiceCidadeEmpresa]); var exame = new ExameViewModel { Paciente = new PessoaModel { Nome = line[indices.IndiceNomePaciente], Cidade = line[indices.IndiceCidadePaciente], Cep = line[indices.IndiceCepPaciente].Length > 0 ? Methods.RemoveSpecialsCaracts(line[indices.IndiceCepPaciente]) : "00000000", Bairro = line[indices.IndiceBairroPaciente].Length > 0 && line[indices.IndiceBairroPaciente].Length < 60 ? line[indices.IndiceBairroPaciente] : "NAO INFORMADO", Estado = line[indices.IndiceEstadoPaciente].Length > 2 ? mapEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]).Uf : line[indices.IndiceEstadoPaciente].ToUpper(), FoneCelular = line[indices.IndiceFoneCelularPaciente], DataNascimento = !line[indices.IndiceDataNascimentoPaciente].Equals("") ? Convert.ToDateTime(line[indices.IndiceDataNascimentoPaciente]) : DateTime.MinValue, Longitude = cidadePaciente != null?cidadePaciente.Longitude.ToString() : "0", Latitude = cidadePaciente != null?cidadePaciente.Latitude.ToString() : "0", IdAreaAtuacao = 0, OutrasComorbidades = "", OutrosSintomas = "", Cpf = line[indices.IndiceTipoDocumento1Paciente].Equals("CPF") && Methods.ValidarCpf(line[indices.IndiceDocumento1Paciente]) ? Methods.RemoveSpecialsCaracts(line[indices.IndiceDocumento1Paciente]) : line[indices.IndiceTipoDocumento2Paciente].Equals("CPF") && Methods.ValidarCpf(line[indices.IndiceDocumento2Paciente]) ? Methods.RemoveSpecialsCaracts(line[indices.IndiceDocumento2Paciente]) : "", Sexo = line[indices.IndiceSexoPaciente].Equals("FEMININO") ? "F" : "M", Rua = line[indices.IndiceRuaPaciente].Length > 0 && line[indices.IndiceRuaPaciente].Length < 60 ? line[indices.IndiceRuaPaciente].Split('-')[0] : "NÃO INFORMADO", Numero = line[indices.IndiceRuaPaciente].Length > 0 && line[indices.IndiceRuaPaciente].Split('-').Length >= 2 ? (Methods.SoContemNumeros(line[indices.IndiceRuaPaciente].Split('-')[1].Trim()) ? line[indices.IndiceRuaPaciente].Split('-')[1].Trim() : "") : "", Complemento = line[indices.IndiceRuaPaciente].Length > 0 && line[indices.IndiceRuaPaciente].Split('-').Length == 3 ? (line[indices.IndiceRuaPaciente].Split('-')[2].Trim().Length < 100 ? line[indices.IndiceRuaPaciente].Split('-')[2].Trim() : "") : "", Cns = line[indices.IndiceCnsPaciente], Profissao = "NÃO INFORMADA", }, Exame = new ExameModel { IdAgenteSaude = agente.UsuarioModel.IdPessoa, DataExame = Convert.ToDateTime(line[indices.IndiceDataExame]), IdEstado = secretarioMunicipio != null?Convert.ToInt32(municipioSecretario.Uf) : secretarioEstado.IdEstado, IdAreaAtuacao = 0, CodigoColeta = line[indices.IndiceCodigoColeta], MetodoExame = "F", IdVirusBacteria = GetIdVirusBacteriaItemImportacao(line[indices.IndiceTipoExame], listVirusBacteria), DataInicioSintomas = line[indices.IndiceDataInicioSintomas].Equals("") ? Convert.ToDateTime(line[indices.IndiceDataExame]) : Convert.ToDateTime(line[indices.IndiceDataInicioSintomas]), IgG = line[indices.IndiceMetodoExame].ToUpper().Contains(IndiceItemArquivoImportacao.METODO_IGG) && !line[indices.IndiceMetodoExame].ToUpper().Contains(IndiceItemArquivoImportacao.METODO_IGM) ? GetMetodoExameImportacao(line[indices.IndiceMetodoExame], IndiceItemArquivoImportacao.METODO_IGG, line[indices.IndiceResultadoExame].Length > 0 ? line[indices.IndiceResultadoExame] : line[indices.IndiceObservacaoExame]) : "N", IgM = line[indices.IndiceMetodoExame].ToUpper().Contains(IndiceItemArquivoImportacao.METODO_IGM) && !line[indices.IndiceMetodoExame].ToUpper().Contains(IndiceItemArquivoImportacao.METODO_IGG) ? GetMetodoExameImportacao(line[indices.IndiceMetodoExame], IndiceItemArquivoImportacao.METODO_IGM, line[indices.IndiceResultadoExame].Length > 0 ? line[indices.IndiceResultadoExame] : line[indices.IndiceObservacaoExame]) : "N", Pcr = line[indices.IndiceMetodoExame].ToUpper().Contains(IndiceItemArquivoImportacao.METODO_PCR) ? GetMetodoExameImportacao(line[indices.IndiceMetodoExame], IndiceItemArquivoImportacao.METODO_PCR, line[indices.IndiceResultadoExame].Length > 0 ? line[indices.IndiceResultadoExame] : line[indices.IndiceObservacaoExame]) : "N", IgGIgM = line[indices.IndiceMetodoExame].ToUpper().Contains(IndiceItemArquivoImportacao.METODO_IGG) && line[indices.IndiceMetodoExame].ToUpper().Contains(IndiceItemArquivoImportacao.METODO_IGM) ? GetMetodoExameImportacao(line[indices.IndiceMetodoExame], IndiceItemArquivoImportacao.METODO_IGG_IGM, line[indices.IndiceResultadoExame].Length > 0 ? line[indices.IndiceResultadoExame] : line[indices.IndiceObservacaoExame]) : "N", }, EmpresaExame = new EmpresaExameModel { Cnpj = "NÃO INFORMADO", Nome = line[indices.IndiceNomeEmpresa], Cnes = line[indices.IndiceCnesEmpresa], Cidade = line[indices.IndiceCidadeEmpresa], Latitude = cidadeEmpresa != null?cidadeEmpresa.Latitude.ToString() : "0", Longitude = cidadeEmpresa != null?cidadeEmpresa.Longitude.ToString() : "0", Estado = line[indices.IndiceEstadoEmpresa], Rua = "NÃO INFORMADO", Bairro = "NÃO INFORMADO", Cep = "00000000", FoneCelular = "00000000000", }, }; if (secretarioMunicipio != null) { exame.Exame.IdMunicipio = secretarioMunicipio.IdMunicipio; } else { exame.Exame.IdMunicipio = null; } examesPlanilha.Add(exame); line = reader.ReadLine().Split(';'); } } else { line = reader.ReadLine().Split(';'); //MunicipioGeoModel cidadePaciente; while (reader.Peek() >= 0 && String.Concat(line).Length > 0) { EstadoModel estadoPaciente = null; if (line[indices.IndiceEstadoPaciente].Length > 2) { if (mapEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]) == null) { mapEstado.Add(line[indices.IndiceEstadoPaciente], _estadoService.GetByName(line[indices.IndiceEstadoPaciente])); } estadoPaciente = mapEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]); } else if (line[indices.IndiceEstadoPaciente].Length == 2) { if (mapSiglaEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]) == null) { mapSiglaEstado.Add(line[indices.IndiceEstadoPaciente], _estadoService.GetByUf(line[indices.IndiceEstadoPaciente])); } estadoPaciente = mapSiglaEstado.GetValueOrDefault(line[indices.IndiceEstadoPaciente]); } if (mapCidade.GetValueOrDefault(line[indices.IndiceCidadePaciente]) == null) { mapCidade.Add(line[indices.IndiceCidadePaciente], _municipioGeoService.GetByName(line[indices.IndiceCidadePaciente], estadoPaciente.CodigoUf)); } MunicipioGeoModel cidadePaciente = mapCidade.GetValueOrDefault(line[indices.IndiceCidadePaciente]); if (mapCidade.GetValueOrDefault(line[indices.IndiceCidadeEmpresa]) == null) { mapCidade.Add(line[indices.IndiceCidadeEmpresa], _municipioGeoService.GetByName(line[indices.IndiceCidadeEmpresa], estadoPaciente.CodigoUf)); } MunicipioGeoModel cidadeEmpresa = mapCidade.GetValueOrDefault(line[indices.IndiceCidadeEmpresa]); var exame = new ExameViewModel { Paciente = new PessoaModel { Nome = line[indices.IndiceNomePaciente], Cidade = line[indices.IndiceCidadePaciente], Cep = line[indices.IndiceCepPaciente].Length > 0 ? Methods.RemoveSpecialsCaracts(line[indices.IndiceCepPaciente]) : "00000000", Bairro = line[indices.IndiceBairroPaciente].Length > 0 && line[indices.IndiceBairroPaciente].Length < 60 ? line[indices.IndiceBairroPaciente] : "NAO INFORMADO", Estado = line[indices.IndiceEstadoPaciente].Length > 2 ? estadoPaciente.Uf : line[indices.IndiceEstadoPaciente].ToUpper(), FoneCelular = line[indices.IndiceFoneCelularPaciente], DataNascimento = !line[indices.IndiceDataNascimentoPaciente].Equals("") ? Convert.ToDateTime(line[indices.IndiceDataNascimentoPaciente]) : DateTime.MinValue, Longitude = cidadePaciente != null?cidadePaciente.Longitude.ToString() : "0", Latitude = cidadePaciente != null?cidadePaciente.Latitude.ToString() : "0", IdAreaAtuacao = 0, OutrasComorbidades = "", OutrosSintomas = "", Sexo = "M", Rua = line[indices.IndiceRuaPaciente].Length > 0 && line[indices.IndiceRuaPaciente].Length < 60 ? line[indices.IndiceRuaPaciente] : "NÃO INFORMADO", Numero = line[indices.IndicenNumeroResidenciaPaciente].Length > 0 ? line[indices.IndicenNumeroResidenciaPaciente] : "", Complemento = line[indices.IndiceComplementoPaciente].Length > 0 ? line[indices.IndiceComplementoPaciente] : "", Cpf = line[indices.IndiceCpfPaciente].Length > 0 && Methods.ValidarCpf(line[indices.IndiceCpfPaciente]) ? line[indices.IndiceCpfPaciente] : "", Profissao = line[indices.IndiceProfissaoPaciente], Hipertenso = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DOENCA_HIPERTENSAO), Diabetes = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DOENCA_DIABETES), Obeso = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DOENCA_OBESIDADE), Cardiopatia = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DOENCA_CARDIOPATIA), Imunodeprimido = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DEONCA_IMUNODEPRIMIDO), Cancer = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DEONCA_CANCER), DoencaRespiratoria = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DOENCA_RESPIRATORIA), DoencaRenal = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DOENCA_RENAL), Epilepsia = VerificaSintomaOuDoencaImportacao(line, indices.IndicesDoencaPacienteUfs, IndiceItemArquivoImportacao.DEONCA_EPILESIA), }, Exame = new ExameModel { IdAgenteSaude = agente.UsuarioModel.IdPessoa, DataExame = Convert.ToDateTime(line[indices.IndiceDataExame]), IdEstado = secretarioMunicipio != null?Convert.ToInt32(municipioSecretario.Uf) : secretarioEstado.IdEstado, IdAreaAtuacao = 0, CodigoColeta = line[indices.IndiceCodigoColeta], MetodoExame = "F", PerdaOlfatoPaladar = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_PERDA_OLFATO), Febre = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_FEBRE), Tosse = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_TOSSE), Coriza = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_CORIZA), DificuldadeRespiratoria = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_DIFICULDADE_RESPIRATORIA), DorGarganta = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_DOR_DE_GARGANTA), Diarreia = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_DIARREIA), DorOuvido = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_DOR_DE_OUVIDO), Nausea = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_NAUSEAS), DorAbdominal = VerificaSintomaOuDoencaImportacao(line, indices.IndicesSintomasPacienteUfs, IndiceItemArquivoImportacao.SINTOMA_DORES_E_DESCONFORTO), IdVirusBacteria = GetIdVirusBacteriaItemImportacao("COVID-19", listVirusBacteria), DataInicioSintomas = Convert.ToDateTime(line[indices.IndiceDataExame]), IgG = "N", IgM = line[indices.IndiceRealizouTeste].ToUpper().Contains("POSITIVO") ? "S" : "N", Pcr = "N", IgGIgM = "N", IdEmpresaSaude = EmpresaExameModel.EMPRESA_ESTADO_MUNICIPIO, }, }; if (secretarioMunicipio != null) { exame.Exame.IdMunicipio = secretarioMunicipio.IdMunicipio; } else { exame.Exame.IdMunicipio = null; } examesPlanilha.Add(exame); line = reader.ReadLine().Split(';'); } } } foreach (var exameView in examesPlanilha) { var exameGravado = _context.Exame .Where(e => e.CodigoColeta.Equals(exameView.Exame.CodigoColeta)) .Select(ex => new { IdExame = ex.IdExame, IdPessoa = ex.IdPaciente, IdEmpresa = ex.IdEmpresaSaude }).FirstOrDefault(); if (exameGravado == null || (exameGravado != null && exameGravado.IdEmpresa < 1)) { var empresa = _empresaExameService.GetByCNES(exameView.EmpresaExame.Cnes); if (empresa == null) { empresa = _empresaExameService.Insert(exameView.EmpresaExame); } exameView.Exame.IdEmpresaSaude = empresa.Id; } if (exameGravado == null) { _exameService.Insert(exameView); } else { _exameService.Update(exameView); } } }