diff --git a/Src/Dlhis.Canteen/Order/Controllers/BalancesController.cs b/Src/Dlhis.Canteen/Order/Controllers/BalancesController.cs index 608fbfbc..8ed0dd64 100644 --- a/Src/Dlhis.Canteen/Order/Controllers/BalancesController.cs +++ b/Src/Dlhis.Canteen/Order/Controllers/BalancesController.cs @@ -142,4 +142,24 @@ public class BalancesController : ControllerBase { return await _balanceService.GetConsumeNumberAsync(); } + + /// + /// 消费走势 + /// + /// + [HttpGet("consumptiontrends")] + public async Task> GetConsumptionTrendsAsync([FromQuery] ConsumptionTrendRequest request) + { + return await _balanceService.GetConsumptionTrendsAsync(request); + } + + /// + /// 餐补走势 + /// + /// + [HttpGet("supplementtrend")] + public async Task> GetSupplementTrendsAsync([FromQuery] ConsumptionTrendRequest request) + { + return await _balanceService.GetSupplementTrendsAsync(request); + } } diff --git a/Src/Dlhis.Canteen/Order/Request/ConsumptionTrendRequest.cs b/Src/Dlhis.Canteen/Order/Request/ConsumptionTrendRequest.cs new file mode 100644 index 00000000..7f223506 --- /dev/null +++ b/Src/Dlhis.Canteen/Order/Request/ConsumptionTrendRequest.cs @@ -0,0 +1,12 @@ +using Dlhis.Entity.Canteen.Enums; +using System.ComponentModel.DataAnnotations; + +namespace Dlhis.Canteen.Order.Request +{ + public class ConsumptionTrendRequest + { + [Required] + [EnumDataType(typeof(ConsumptionTrendEnumType))] + public ConsumptionTrendEnumType Type { get; set; } + } +} diff --git a/Src/Dlhis.Canteen/Order/Response/ConsumptionTrendResponse.cs b/Src/Dlhis.Canteen/Order/Response/ConsumptionTrendResponse.cs new file mode 100644 index 00000000..b490dd19 --- /dev/null +++ b/Src/Dlhis.Canteen/Order/Response/ConsumptionTrendResponse.cs @@ -0,0 +1,20 @@ +namespace Dlhis.Canteen.Order.Response +{ + public class ConsumptionTrendResponse + { + /// + /// 时间 + /// + public string DateTime { get; set; } + + /// + /// 当日/月消费总额 + /// + public decimal CurrentAmount { get; set; } + + /// + /// 消费总额 + /// + public decimal TotalAmount { get; set; } + } +} diff --git a/Src/Dlhis.Canteen/Order/Services/IBalanceService.cs b/Src/Dlhis.Canteen/Order/Services/IBalanceService.cs index 6c78934d..8ba54785 100644 --- a/Src/Dlhis.Canteen/Order/Services/IBalanceService.cs +++ b/Src/Dlhis.Canteen/Order/Services/IBalanceService.cs @@ -1,6 +1,7 @@ using Dlhis.Canteen.Order.Request; using Dlhis.Canteen.Order.Response; using Kurisu.Core.DataAccess.Dto; +using Microsoft.AspNetCore.Mvc; namespace Dlhis.Canteen.Order.Services; @@ -25,4 +26,8 @@ public interface IBalanceService : IScopeDependency Task> BalanceRecordAnalysisAsync(BalanceRecordAnalysisRequest request); Task GetConsumeNumberAsync(); + + Task> GetConsumptionTrendsAsync(ConsumptionTrendRequest request); + + Task> GetSupplementTrendsAsync(ConsumptionTrendRequest request); } diff --git a/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs b/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs index 256e9caf..08f58d1b 100644 --- a/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs +++ b/Src/Dlhis.Canteen/Order/Services/Implements/BalanceService.cs @@ -13,7 +13,9 @@ using Kurisu.Core.User.Abstractions; using Kurisu.SqlSugar.Attributes; using Kurisu.SqlSugar.Extensions; using Kurisu.SqlSugar.Services; +using Microsoft.AspNetCore.Mvc; using SqlSugar; +using System.Globalization; namespace Dlhis.Canteen.Order.Services.Implements; @@ -584,6 +586,104 @@ public class BalanceService : IBalanceService return res; } + public async Task> GetConsumptionTrendsAsync(ConsumptionTrendRequest request) + { + var bs = await _dbContext.Queryable() + .Where(x => x.Type == BalanceRecordTypeEnumType.Out) + .ToListAsync(); + //消费总额 + var totalAmount = Math.Round(Math.Abs(bs.Sum(x => x.Amount)), 2); + + var list = new List(); + if (request.Type == ConsumptionTrendEnumType.Month) + { + //月与总额对比 + var months = GetMonthsOfYear(); + + foreach (var month in months) + { + var currentAmount = Math.Round(Math.Abs(bs.Where(x => x.CostDateTime.ToString("yyyy-MM") == month) + .Sum(x => x.Amount)), 2); + + var res = new ConsumptionTrendResponse + { + DateTime = month, + CurrentAmount = currentAmount, + TotalAmount = totalAmount + }; + list.Add(res); + } + } + else + { + //日与总额对比 + var days = GetDaysOfMonth(); + + foreach (var day in days) + { + var currentAmount = Math.Round(Math.Abs(bs.Where(x => x.CostDateTime.ToString("yyyy-MM-dd") == day) + .Sum(x => x.Amount)), 2); + + var res = new ConsumptionTrendResponse + { + DateTime = day, + CurrentAmount = currentAmount, + TotalAmount = totalAmount + }; + list.Add(res); + } + } + return list; + } + + public async Task> GetSupplementTrendsAsync(ConsumptionTrendRequest request) + { + //todo + return new List(); + } + + /// + /// 获取当前年所有月 + /// + /// + private List GetMonthsOfYear() + { + List months = new List(); + // 获取当前年份 + int year = DateTime.Now.Year; + // 遍历12个月份并添加到列表中 + for (int month = 1; month <= 12; month++) + { + DateTime monthDate = new DateTime(year, month, 1); + months.Add(monthDate.ToString("yyyy-MM")); + } + return months; + } + + /// + /// 获取当前月所有天 + /// + /// + private List GetDaysOfMonth() + { + List days = new List(); + + // 获取当前年份和月份 + int year = DateTime.Now.Year; + int month = DateTime.Now.Month; + + // 获取当前月份的第一天和最后一天 + DateTime firstDayOfMonth = new DateTime(year, month, 1); + DateTime lastDayOfMonth = firstDayOfMonth.AddMonths(1).AddDays(-1); + + // 遍历当前月的所有日期并添加到列表中 + for (DateTime date = firstDayOfMonth; date <= lastDayOfMonth; date = date.AddDays(1)) + { + days.Add(date.ToString("yyyy-MM-dd")); + } + + return days; + } //************************************************************** help methods ****************************************************************** diff --git a/Src/Dlhis.Entity/Canteen/Enums/ConsumptionTrendEnumType.cs b/Src/Dlhis.Entity/Canteen/Enums/ConsumptionTrendEnumType.cs new file mode 100644 index 00000000..e8573b48 --- /dev/null +++ b/Src/Dlhis.Entity/Canteen/Enums/ConsumptionTrendEnumType.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Dlhis.Entity.Canteen.Enums +{ + public enum ConsumptionTrendEnumType + { + /// + /// 日 + /// + [Description("日")] + Day = 1, + + /// + /// 月 + /// + [Description("月")] + Month = 2, + } +}