public ResultadoCalculoAgregado ObterResultadosCalculosPorPessoa(long idArvore, DateTime dataInicial, DateTime dataFinal) { var arvores = arvoreRepository.Filter($"IdArvoreSuperior = {idArvore} and TipoArvore = {(int) TipoArvore.Indicador}"); decimal somaPesos = 0; var result = new ResultadoCalculoAgregado(); if (arvores.Any()) { foreach (var arvore in arvores) { var obj = new Tuple <ResultadoCalculoAgregado, decimal, long, long, DateTime, DateTime>(result, arvore.ValorPercentualPeso.Value, arvore.IdProjeto, arvore.IdIndicador.Value, dataInicial, dataFinal); var thread = new Thread(new ParameterizedThreadStart(ObterResultadosCalculosThread)); thread.Start(obj); somaPesos += arvore.ValorPercentualPeso.Value; } while (result.Resultados.Count < arvores.Count()) { continue; } result.ValorPonderadoIndividualPessoa = decimal.Round(result.Resultados.Sum(item => item.PesoPorAtingimento) / somaPesos, 2); result.ValorPonderadoCorporativo = ObterResultadosCalculosPorIndicadoresCorporativos(idArvore, dataInicial, dataFinal).ValorPonderadoCorporativo; long idUsuario = arvores.First().IdUsuario.Value; var queryPesosUsuario = DatabaseProvider.NewQuery($"select coalesce(ValorPesoIndividual, 0 ) vpi, coalesce(ValorPesoCorporativo, 0) vpc, ValorMinimoPonderado, ValorMaximoPonderado from Usuario where IdUsuario = {idUsuario}"); queryPesosUsuario.ExecuteQuery(); if (!queryPesosUsuario.IsNull("VALORMINIMOPONDERADO") && result.ValorPonderadoIndividualPessoa < queryPesosUsuario.GetDecimal("VALORMINIMOPONDERADO")) { result.ValorPonderadoIndividualPessoa = queryPesosUsuario.GetDecimal("VALORMINIMOPONDERADO"); } else if (!queryPesosUsuario.IsNull("VALORMAXIMOPONDERADO") && result.ValorPonderadoIndividualPessoa > queryPesosUsuario.GetDecimal("VALORMAXIMOPONDERADO")) { result.ValorPonderadoIndividualPessoa = queryPesosUsuario.GetDecimal("VALORMAXIMOPONDERADO"); } result.ValorPonderadoFinalPessoa = decimal.Round(result.ValorPonderadoIndividualPessoa * (queryPesosUsuario.GetDecimal("VPI") / 100) + (result.ValorPonderadoCorporativo * (queryPesosUsuario.GetDecimal("VPC") / 100)), 2); } return(result); }
public ResultadoCalculoAgregado ObterResultadosCalculosPorIndicadoresCorporativos(long idArvore, DateTime dataInicial, DateTime dataFinal) { var result = new ResultadoCalculoAgregado(); var query = DatabaseProvider.NewQuery($"select a.IdProjeto, a.IdIndicador, a.ValorPercentualPeso from Arvore a where a.IdProjeto = (select IdProjeto from Arvore where IdArvore = {idArvore}) and a.TipoArvore = {(int)TipoArvore.Corporativo}"); query.ExecuteQuery(); decimal somaPesos = 0; query.ForEach(() => { var obj = new Tuple <ResultadoCalculoAgregado, decimal, long, long, DateTime, DateTime>(result, query.GetDecimal("VALORPERCENTUALPESO"), query.GetLong("IDPROJETO"), query.GetLong("IDINDICADOR"), dataInicial, dataFinal); var thread = new Thread(new ParameterizedThreadStart(ObterResultadosCalculosThread)); thread.Start(obj); somaPesos += query.GetDecimal("VALORPERCENTUALPESO"); }); while (result.Resultados.Count < query.Count) { continue; } result.ValorPonderadoCorporativo = decimal.Round(result.Resultados.Sum(item => item.PesoPorAtingimento) / somaPesos, 2); return(result); }