public async Task <StatementExpendCategoryDto> GetExpendCategoryStatisticsAsync(StatementDateInputDto input) { var dto = new StatementExpendCategoryDto(); var statements = await _statementRepo .Select .Where(s => s.IsDeleted == false) .Where(s => s.Type.Equals("expend")) .WhereIf(input.UserId != null, s => s.CreateUserId == input.UserId) .WhereIf(input.Year != null, s => s.Year == input.Year) .WhereIf(input.Month != null, s => s.Month == input.Month) .WhereIf(input.Day != null, s => s.Day == input.Day) .ToListAsync(); decimal total = 0; // 根据CategoryId分组,并统计总额 var childDetails = statements.GroupBy(s => s.CategoryId).Select(g => { var info = _categoryRepo.GetAsync(g.Key.Value).Result; var parentInfo = _categoryRepo.GetCategoryParentAsync(g.Key.Value).Result; var amount = g.Sum(s => s.Amount); total += amount; return(new { CategoryId = g.Key, Amount = amount, Info = info, ParentInfo = parentInfo }); }); var childDtos = new List <ChildGroupDto>(); var parentDtos = childDetails.GroupBy(p => new { p.ParentInfo.Id, p.ParentInfo.Name }).Select(g => { var childDto = new ChildGroupDto(); var childTotal = g.Sum(s => s.Amount); childDto.ParentName = g.Key.Name; childDto.Childs = childDetails.Where(d => d.Info.ParentId == g.Key.Id).Select(d => new { Id = d.Info.Id, Name = d.Info.Name, Data = d.Amount, Percent = Math.Round(d.Amount / childTotal, 4) * 100, CategoryIconPath = _fileRepo.GetFileUrl(d.Info.IconUrl) }); childDtos.Add(childDto); return(new StatisticsDto { Id = g.Key.Id, Name = g.Key.Name, Data = Math.Round(g.Sum(s => s.Amount) / total, 4) * 100 }); }).ToList(); dto.ParentCategoryStas = parentDtos; dto.ChildCategoryStas = childDtos; return(dto); }
public async Task AddAsync(Guid id, Guid userId, string category, string name, string description, DateTime createdAt) { var activityCategory = await _categoryRepository.GetAsync(category); if (activityCategory == null) { throw new ActioException("category_not_found", $"Category: {category} was not found"); } var activity = new Activity(id, name, activityCategory, description, userId, createdAt); await _activityRepository.AddAsync(activity); }
public async Task AddAsync(Guid id, Guid userId, string category, string name, string description, DateTime createdAt) { var activityCategoty = await _categoryRepo.GetAsync(name); if (activityCategoty == null) { throw new MicroException("category_nf", $"Category: {category} was nf"); } var activity = new Activity(id, userId, category, name, description, createdAt); await _activityRepo.AddAsync(activity); }