public async Task <IList <ProjectCompanyFactShare> > GetFactByProjectCompanyId(int companyId, DateTime?date = null) { var projectId = (await companyRepository.Where(x => x.Id == companyId).FirstOrDefaultAsync()).ProjectId; var checkdate = date?.Date ?? DateTime.Today; var shares = await repository.Where(c => c.OwnerProjectCompany.ProjectId == projectId) .Where(x => x.ShareStartDate <= checkdate) .Where(x => x.ShareFinishDate >= checkdate || x.ShareFinishDate == null) .Include(x => x.OwnerProjectCompany, c => c.DependentProjectCompany, t => t.OwnerProjectCompany.DomesticCompany).ToListAsync(); return(factShareCalculator.GetFactShares(shares) .Where(x => x.OwnerProjectCompanyId == companyId) .ToList()); }
internal IList <ProjectCompanyFactShare> GetFactShares(int companyId, int year, IEnumerable <ProjectCompanyShare> shares) { IEnumerable <ProjectCompanyFactShare> factShares = new List <ProjectCompanyFactShare>(); if (shares != null && shares.Any()) { IList <DateTime> accum = new List <DateTime>(); // Заполняем началом и концом года accum.Add(new DateTime(year, 1, 1)); accum.Add(new DateTime(year, 12, 31)); foreach (var share in shares) { accum.Add(share.ShareStartDate); // Добавляем окрестности владения для проверки пустот accum.Add(share.ShareStartDate.AddDays(-1)); accum.Add(share.ShareStartDate.AddDays(1)); if (share.ShareFinishDate.HasValue) { accum.Add(share.ShareFinishDate.Value); // Добавляем окрестности владения для проверки пустот accum.Add(share.ShareFinishDate.Value.AddDays(-1)); accum.Add(share.ShareFinishDate.Value.AddDays(1)); } } // выбрасываем повторы accum = accum.Distinct().ToList(); //Выбрасываем если попал другой год из окрестности accum = accum.Where(ac => ac.Year == year).ToList(); //accum.OrderBy(x => x); // owner var ownerId = shares.First().OwnerProjectCompanyId; // dependent var dependent = shares.First().DependentProjectCompanyId; foreach (DateTime datePoint in accum) { var sharesAcc = dbContext.ProjectCompanyShares.Where(c => c.OwnerProjectCompany.Id == ownerId) .Where(c => c.DependentProjectCompany.Id == dependent) .Where(x => x.ShareStartDate <= datePoint) .Where(x => x.ShareFinishDate >= datePoint || x.ShareFinishDate == null).ToList(); if (sharesAcc != null && sharesAcc.Any()) { var factPointShares = factShareCalculator.GetFactShares(sharesAcc).Where(x => x.OwnerProjectCompanyId == companyId); factShares = factShares.Concat(factPointShares); } else { factShares = factShares.Append <ProjectCompanyFactShare>(new ProjectCompanyFactShare { ShareDirectPart = 0 }); } } } return(factShares.ToList()); }
[HttpGet, Route("{projectId}/ownership")] //todo: set filter. public async Task <ProjectOwnershipViewModel> GetProjectOwnership(int projectId, DateTime?date = null) { var project = await((IProjectService)Service).GetProjectOwnership(projectId); var data = Mapper.Map <ProjectOwnershipViewModel>(project); var shares = await shareService.GetAllByProjectId(projectId, date ?? DateTime.Today); var factsShares = factSharesService.GetFactShares(shares); data.Links = factsShares .Where(x => x.ShareDirectPart > 0) .Select(x => new ProjectOwnershipLinkViewModel() { SourceId = x.OwnerProjectCompanyId, TargetId = x.DependentProjectCompanyId, Share = x.ShareDirectPart }).ToArray(); return(data); }