using IBigBreenService; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Common.CommonEnum; using WIDESEA_Common.LocationEnum; using WIDESEA_Common.OrderEnum; using WIDESEA_Common.TaskEnum; using WIDESEA_Core; using WIDESEA_Core.BaseRepository; using WIDESEA_Model.Models; namespace BigGreenService { public class BigGreenService : IBigGreenService { private readonly IRepository _stockInfoDetailRepository; private readonly IRepository _outBoundOrderRepository; private readonly IRepository _locationInfoRepository; private readonly IRepository _outBoundOrderDetailRepository; private readonly IRepository _inboundOrderDetailRepository; private readonly IRepository _taskHtyRepository; private readonly IRepository _taskRepository; private readonly IRepository _stockInfoRepository; public BigGreenService(IRepository stockInfoDetailRepository, IRepository outBoundOrderRepository, IRepository locationInfoRepository,IRepository outBoundOrderDetailRepository, IRepository inboundOrderDetailRepository,IRepository taskRepository,IRepository taskHtyRepository, IRepository stockInfoRepository) { _stockInfoDetailRepository = stockInfoDetailRepository; _outBoundOrderRepository = outBoundOrderRepository; _locationInfoRepository = locationInfoRepository; _outBoundOrderDetailRepository = outBoundOrderDetailRepository; _inboundOrderDetailRepository = inboundOrderDetailRepository; _taskRepository = taskRepository; _taskHtyRepository = taskHtyRepository; _stockInfoRepository = stockInfoRepository; } public WebResponseContent GetBigGreenData() { //计算总库存数量 var totalStockQuantity = _stockInfoDetailRepository.Db.Queryable().Sum(x => (decimal?)x.StockQuantity) ?? 0; //计算待出库订单 var targetStatus = new List { (int)OutOrderStatusEnum.出库中, (int)OutOrderStatusEnum.未开始 }; var unOutBound =_outBoundOrderRepository.Db.Queryable().Where(x =>targetStatus.Contains(x.OrderStatus)).Count(); //计算库位利用率 var freeLocation =_locationInfoRepository.Db.Queryable().Where(x=>x.LocationStatus==(int)LocationStatusEnum.Free).Count(); var inStockLocation =_locationInfoRepository.Db.Queryable().Where(x => x.LocationStatus == (int)LocationStatusEnum.InStock || x.LocationStatus == (int)LocationStatusEnum.Pallet).Count(); int totalLocation = freeLocation + inStockLocation; decimal locationUtilizationRate = totalLocation == 0 ? 0 : Math.Round((decimal)inStockLocation / totalLocation, 4)*100; //计算入库任务和出库任务完成数量 var inboundCount =_taskHtyRepository.Db.Queryable().Where(x => x.TaskType >= 500 && x.TaskType < 900).Count(); var outboundCount =_taskHtyRepository.Db.Queryable().Where(x => x.TaskType >= 100 && x.TaskType < 500).Count(); //计算有货料箱数量 var inStockPallet = _stockInfoRepository.Db.Queryable().Where(x => x.PalletType ==(int) PalletTypeEnum.None).Count(); //计算空箱数量 var freeStockPallet = _stockInfoRepository.Db.Queryable().Where(x => x.PalletType == (int)PalletTypeEnum.Empty).Count(); // 4. 获取近7日每日出入库明细(核心修改:调用上面的方法) var dailyInOutBoundList = Get7DaysDailyInOutBound(); //任务 List tasks = _taskRepository.QueryData(); var bigGreenData = new BigGreenDataDto { TotalStockQuantity=totalStockQuantity, UnOutBoundOrderCount = unOutBound, LocationUtilizationRate = locationUtilizationRate, DailyInOutBoundList = dailyInOutBoundList, TaskList = tasks, InboundCount = inboundCount, OutboundCount = outboundCount, InStockPallet = inStockPallet, FreeStockPallet = freeStockPallet }; return WebResponseContent.Instance.OK(data: bigGreenData); } /// /// 获取近7日每日出入库明细 /// /// 每日出入库明细列表 private List Get7DaysDailyInOutBound() { // 1. 定义时间范围(近7个自然日) DateTime endDate = DateTime.Today; DateTime startDate = endDate.AddDays(-6); // 生成近7日所有日期(确保无缺失,即使某一天无数据也返回0) var all7Days = Enumerable.Range(0, 7) .Select(i => endDate.AddDays(-i).ToString("MM-dd")) .Reverse() // 按日期升序排列(从7天前到今日) .ToList(); // 2. 查询每日出库明细(按日期分组) var dailyOutboundList = _outBoundOrderDetailRepository.Db .Queryable() .Where(x => x.CreateDate >= startDate && x.CreateDate < endDate.AddDays(1)) .GroupBy(x => x.CreateDate) // 按日期格式化分组 .Select(x => new { Date = x.CreateDate.ToString( "MM-dd"), DailyOutbound = SqlFunc.AggregateSum((decimal?)x.OverOutQuantity) ?? 0 }) .ToList() .ToDictionary(k => k.Date, v => v.DailyOutbound); // 转为字典方便匹配 // 3. 查询每日入库明细(按日期分组) var dailyInboundList = _inboundOrderDetailRepository.Db .Queryable() .Where(x => x.CreateDate != null // 过滤空日期 && x.CreateDate >= startDate && x.CreateDate < endDate.AddDays(1)) .GroupBy(x => x.CreateDate).Distinct() // 按日期格式化分组 .Select(x => new { Date = x.CreateDate.ToString("MM-dd"), DailyInbound = SqlFunc.AggregateSum((decimal?)x.OverInQuantity) ?? 0 }) .ToList() .ToDictionary(k => k.Date, v => v.DailyInbound); // 转为字典方便匹配 // 4. 合并每日数据(确保7天日期完整,无数据补0) var dailyInOutBoundList = all7Days.Select(date => new DailyInOutBoundDto { Date = date, DailyOutboundQuantity = dailyOutboundList.ContainsKey(date) ? dailyOutboundList[date] : 0, DailyInboundQuantity = dailyInboundList.ContainsKey(date) ? dailyInboundList[date] : 0, }).ToList(); return dailyInOutBoundList; } /// /// 大屏/汇总数据返回DTO(大绿数据汇总) /// public class BigGreenDataDto { /// /// 入库完成数量 /// public int InboundCount { get; set; } /// /// 出库完成数量 /// public int OutboundCount { get; set; } /// /// 总库存数量 /// public decimal TotalStockQuantity { get; set; } /// /// 待出库订单数量(出库中+未开始) /// public int UnOutBoundOrderCount { get; set; } /// /// 空闲库位数量 /// public int FreeLocationCount { get; set; } /// /// 占用库位数量(已存库) /// public int InStockLocationCount { get; set; } /// /// 库位利用率(小数形式,如0.85对应85%) /// public decimal LocationUtilizationRate { get; set; } /// /// 近7日每日出入库明细 /// public List DailyInOutBoundList { get; set; } = new List(); /// /// 近7日净入库量(入库-出库) /// public decimal NetStock7Days { get; set; } /// /// 近7日净入库量(入库-出库) /// public decimal totalStockChangeRate { get; set; } /// /// 任务列表 /// public List TaskList { get; set; } = new List(); public int InStockPallet { get; set; } public int FreeStockPallet { get; set; } } /// /// 每日出入库明细DTO /// public class DailyInOutBoundDto { /// /// 日期(格式:yyyy-MM-dd) /// public string Date { get; set; } /// /// 当日出库总量 /// public decimal DailyOutboundQuantity { get; set; } /// /// 当日入库总量 /// public decimal DailyInboundQuantity { get; set; } } } }