Пример #1
0
        public async Task <StatementTotalDto> GetStatisticsTotalAsync(StatementDateInputDto input)
        {
            // var userId = input.UserId ?? CurrentUser.Id;
            var statements = await _statementRepo
                             .Select
                             .Where(s => s.IsDeleted == false)
                             .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)
                             .ToListAsync();

            var dto = new StatementTotalDto();

            statements.ForEach(s =>
            {
                switch (s.Type)
                {
                case "expend":
                    dto.MonthExpend += s.Amount;
                    break;

                case "income":
                    dto.MonthIcome += s.Amount;
                    break;

                case "repayment":
                    dto.MonthRepayment += s.Amount;
                    break;

                case "transfer":
                    dto.MonthTransfer += s.Amount;
                    break;
                }
                if (input.Day != null && input.Day == s.Day)
                {
                    switch (s.Type)
                    {
                    case "expend":
                        dto.DayExpend += s.Amount;
                        break;

                    case "income":
                        dto.DayIcome += s.Amount;
                        break;

                    case "repayment":
                        dto.DayRepayment += s.Amount;
                        break;

                    case "transfer":
                        dto.DayTransfer += s.Amount;
                        break;
                    }
                }
            });
            return(dto);
        }
Пример #2
0
 public async Task <ServiceResult <IEnumerable <StatementExpendTrendDto> > > GetMonthExpendTrendStatisticsAsync([FromQuery] StatementDateInputDto input)
 {
     return(ServiceResult <IEnumerable <StatementExpendTrendDto> > .Successed(await _statementService.GetMonthExpendTrendStatisticsAsync(input, 5)));
 }
Пример #3
0
 public async Task <ServiceResult <StatementExpendCategoryDto> > GetExpendCategoryStatisticsAsync([FromQuery] StatementDateInputDto input)
 {
     return(ServiceResult <StatementExpendCategoryDto> .Successed(await _statementService.GetExpendCategoryStatisticsAsync(input)));
 }
Пример #4
0
 public async Task <ServiceResult <StatementTotalDto> > GetMonthStatisticsAsync([FromQuery] StatementDateInputDto input)
 {
     return(ServiceResult <StatementTotalDto> .Successed(await _statementService.GetStatisticsTotalAsync(input)));
 }
Пример #5
0
        public async Task <IEnumerable <StatementExpendTrendDto> > GetMonthExpendTrendStatisticsAsync(StatementDateInputDto input, int count)
        {
            var dateList = new List <WeeksOfMonth>();

            // 获取当前月份前count个月份(包含当前月份)
            for (int i = 0; i < count; i++)
            {
                var currentDate = new DateTime(input.Year.Value, input.Month.Value, 1).AddMonths(-i);
                dateList.Add(new WeeksOfMonth
                {
                    Number    = i + 1,
                    StartDate = currentDate,
                    EndDate   = currentDate.AddMonths(1).AddDays(-1)
                });
            }

            var startDate = dateList.OrderBy(d => d.Number).LastOrDefault().StartDate.Date;//获取最小的月份(Number最大)
            var EndDate   = dateList.OrderBy(d => d.Number).FirstOrDefault().EndDate.Date;

            var statements = await _statementRepo
                             .Select
                             .Where(s => s.IsDeleted == false)
                             .WhereIf(input.Type.IsNotNullOrEmpty(), s => s.Type == input.Type)
                             .WhereIf(input.UserId != null, s => s.CreateUserId == input.UserId)
                             .WhereIf(input.Year != null, s => s.Year >= startDate.Year && s.Year <= EndDate.Year)
                             .WhereIf(input.Month != null, s => s.Time >= startDate && s.Time <= EndDate.AddDays(1).AddSeconds(-1))
                             .ToListAsync();

            var dtos = dateList.Select(d => new StatementExpendTrendDto
            {
                Name      = $"{d.StartDate.Year}/{d.StartDate.Month}",
                Data      = statements.Where(s => s.Year == d.StartDate.Year && s.Month == d.StartDate.Month).Select(t => new { t.Amount }).Sum(t => t.Amount),
                StartDate = d.StartDate.Date,
                EndDate   = d.EndDate.Date
            });

            return(dtos);
        }
Пример #6
0
        public async Task <IEnumerable <StatementExpendTrendDto> > GetWeekExpendTrendStatisticsAsync(StatementDateInputDto input)
        {
            var dateList  = DateTimeUtil.GetWeeksOfMonth(input.Year.Value, input.Month.Value);
            var startDate = dateList.OrderBy(d => d.Number).FirstOrDefault().StartDate.Date;
            var EndDate   = dateList.OrderBy(d => d.Number).LastOrDefault().EndDate.Date.AddDays(1).AddSeconds(-1);// 加上23:59:59

            var statements = await _statementRepo
                             .Select
                             .Where(s => s.IsDeleted == false)
                             .WhereIf(input.Type.IsNotNullOrEmpty(), s => s.Type == input.Type)
                             .WhereIf(input.UserId != null, s => s.CreateUserId == input.UserId)
                             .WhereIf(input.Year != null, s => s.Year == input.Year)
                             .WhereIf(input.Month != null, s => s.Time >= startDate && s.Time <= EndDate)
                             .ToListAsync();

            var dtos = dateList.Select(d => new StatementExpendTrendDto
            {
                Name      = $"{d.StartDate.Month}/{d.StartDate.Day}-{d.EndDate.Month}/{d.EndDate.Day}",
                Data      = statements.Where(s => s.Time >= d.StartDate && s.Time <= d.EndDate.AddDays(1).AddSeconds(-1)).Select(t => new { t.Amount }).Sum(t => t.Amount),
                StartDate = d.StartDate.Date,
                EndDate   = d.EndDate.Date
            });

            return(dtos);
        }
Пример #7
0
        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);
        }