public async Task <KPI11ShowData[]> GetDataKP11(DataKPI11 data, string userId) { var createKPI11 = await GetDataKP11Original(data, userId); var ret = createKPI11 //.Where(it => it.yearToData == year) .Select(it => new KPI11ShowData() { Value = it.ActualYTD, PrevYearValue = it.PreviousValueActualYTD, AssVA = this.dboAssVA.First(a => a.idassva == it.IDManager) }).ToArray(); return(ret); }
public async Task <createKPI11[]> GetDataKP11Original(DataKPI11 data, string userId) { var yearStart = data.StartDate; var yearEnd = data.EndDate; var dates = await this.GetDatesStartEndKPI11(); if (yearStart == null) { yearStart = dates[0]; } if (yearEnd == null) { yearEnd = dates[dates.Length - 1]; } var Managers = string.Join(",", data.ManagerIds.SelectMany(it => it.Value).ToArray()); string Clients = ""; if ((data.Clients?.Count ?? 0) > 0) { var maxClients = data.Clients.Max(it => it.Key); switch (maxClients) { case 2: //top var allClients = await dboClients.Select(it => it.idclient).ToArrayAsync(); Clients = string.Join(",", allClients); break; case 1: { var ids = data.Clients[maxClients].ToArray(); ids = ids.Select(it => it / DatabaseContext.DiffCategory).ToArray(); var dataClients = await this. dboClientsCategory .Where(it => ids.Contains(it.idcategory)) .Select(it => it.idclient) .ToArrayAsync(); Clients = string.Join(',', ids); } break; case 0: { var ids = data.Clients[maxClients].ToArray(); ids = ids.Select(it => it / DatabaseContext.DiffClients).ToArray(); Clients = string.Join(',', data.Clients[maxClients].ToArray()); } break; default: throw new ArgumentException("no clients defined for " + maxClients); } } var createKPI11 = await this.createKPI11.FromSqlInterpolated( $"exec createKPI11 {userId},{yearStart.Year},{yearStart.Month},{yearEnd.Year},{yearEnd.Month}, {Managers}, {Clients}").ToArrayAsync(); return(createKPI11); //var idAssVA = new List<long>(); //var managers = new Dictionary<long, List<long>>(); //if (data.Managers?.Length > 0) //{ // foreach (var item in data.Managers) // { // var val = item.Value; // if ((val?.Count ?? 0) == 0) // continue; // switch (item.Key) // { // case 0: // { // idAssVA.AddRange(val); // } // break; // case 1: // { // //TODO: make it recursive // var ids = await dboAssVA // .Where(it => it.idmanager != null && val.Contains(it.idmanager.Value)) // .Select(it => new { it.idassva, it.idmanager }) // .ToArrayAsync(); // foreach (var m in ids) // { // var idM = m.idassva; // if (!managers.ContainsKey(idM)) // { // managers.Add(idM, new List<long>()); // } // managers[idM].Add(m.idassva); // idAssVA.Add(m.idassva); // } // } // break; // case 2: // { // var idsM2 = await dboAssVA // .Where(it => it.idmanager != null && val.Contains(it.idmanager.Value)) // .Select(it => new { it.idassva, it.idmanager }) // .ToArrayAsync(); // foreach (var m in idsM2) // { // var idM = m.idassva; // if (!managers.ContainsKey(idM)) // { // managers.Add(idM, new List<long>()); // } // var idAss = await dboAssVA // .Where(it => it.idmanager == idM) // .Select(it=>it.idassva) // .ToArrayAsync(); // managers[idM].AddRange(idAss); // idAssVA.AddRange(idAss); // } // } // break; // default: // throw new ArgumentException($"cannot have key {item.Key} for clients dictionary"); // } // } //} //idAssVA = idAssVA.Distinct().ToList(); //var idAssVAClientsCounties = await dboAssVAClientsCounties // .Where(it => idAssVA.Contains(it.idassva)) // .Select(it =>new { it.idassva, it.idassvaclientscounties }) // .ToArrayAsync() // ; //var idass = idAssVAClientsCounties.Select(it => it.idassva).Distinct().ToArray(); //var idassvaclientscounties= idAssVAClientsCounties.Select(it => it.idassvaclientscounties).Distinct().ToArray(); //var values = await dboACTPL // .Where(it => idassvaclientscounties.Contains(it.idassvaclientscounties)) // .Select(it => new { it.idassvaclientscounties, it.actual }) // .ToArrayAsync(); ////TODO: actual //var ret = new List<KPI11ShowData>(); //foreach(var item in managers) //{ // var arrIDAssVA = item.Value.ToArray(); // KPI11ShowData retItem = new KPI11ShowData(); // retItem.AssVA = await dboAssVA.FirstOrDefaultAsync(it => it.idassva == item.Key); // var idAssVAClientsCountiesItem = idAssVAClientsCounties // .Where(it => arrIDAssVA.Contains( it.idassva )) // .Select(it=>it.idassvaclientscounties) // .ToArray(); // retItem.Value = values // .Where(it => idAssVAClientsCountiesItem.Contains(it.idassvaclientscounties)) // .Select(it=>it.actual) // .Sum(); // ret.Add(retItem); // //retItem.Value = values.Where(it=>it.idassvaclientscounties == item.id) //} //return ret.ToArray(); }