using IBigBreenService;
|
using Microsoft.IdentityModel.Tokens;
|
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
|
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_Core.Helper;
|
using WIDESEA_Model.Models;
|
using WIDESEA_Model.Models.Basic;
|
|
namespace BigGreenService
|
{
|
public class BigGreenService : IBigGreenService
|
{
|
private readonly IRepository<Dt_StockInfoDetail> _stockInfoDetailRepository;
|
private readonly IRepository<Dt_OutboundOrder> _outBoundOrderRepository;
|
private readonly IRepository<Dt_LocationInfo> _locationInfoRepository;
|
private readonly IRepository<Dt_OutboundOrderDetail> _outBoundOrderDetailRepository;
|
private readonly IRepository<Dt_InboundOrderDetail> _inboundOrderDetailRepository;
|
private readonly IRepository<Dt_Task_Hty> _taskHtyRepository;
|
private readonly IRepository<Dt_Task> _taskRepository;
|
private readonly IRepository<Dt_StockInfo> _stockInfoRepository;
|
private readonly IRepository<Dt_MaterialExpirationDate> _materialExpirationDateRepository;
|
|
public BigGreenService(IRepository<Dt_StockInfoDetail> stockInfoDetailRepository, IRepository<Dt_OutboundOrder> outBoundOrderRepository, IRepository<Dt_LocationInfo> locationInfoRepository, IRepository<Dt_OutboundOrderDetail> outBoundOrderDetailRepository, IRepository<Dt_InboundOrderDetail> inboundOrderDetailRepository, IRepository<Dt_Task> taskRepository, IRepository<Dt_Task_Hty> taskHtyRepository, IRepository<Dt_StockInfo> stockInfoRepository, IRepository<Dt_MaterialExpirationDate> materialExpirationDateRepository)
|
{
|
_stockInfoDetailRepository = stockInfoDetailRepository;
|
_outBoundOrderRepository = outBoundOrderRepository;
|
_locationInfoRepository = locationInfoRepository;
|
_outBoundOrderDetailRepository = outBoundOrderDetailRepository;
|
_inboundOrderDetailRepository = inboundOrderDetailRepository;
|
_taskRepository = taskRepository;
|
_taskHtyRepository = taskHtyRepository;
|
_stockInfoRepository = stockInfoRepository;
|
_materialExpirationDateRepository = materialExpirationDateRepository;
|
}
|
public WebResponseContent GetBigGreenData()
|
{
|
//计算总库存数量
|
var totalStockQuantity = _stockInfoDetailRepository.Db.Queryable<Dt_StockInfoDetail>().Sum(x => (decimal?)x.StockQuantity) ?? 0;
|
|
//计算待出库订单
|
var targetStatus = new List<int>
|
{
|
(int)OutOrderStatusEnum.出库中,
|
(int)OutOrderStatusEnum.未开始
|
};
|
var unOutBound = _outBoundOrderRepository.Db.Queryable<Dt_OutboundOrder>().Where(x => targetStatus.Contains(x.OrderStatus)).Count();
|
|
//计算库位利用率
|
var freeLocation = _locationInfoRepository.Db.Queryable<Dt_LocationInfo>().Where(x => x.LocationStatus == (int)LocationStatusEnum.Free).Count();
|
var inStockLocation = _locationInfoRepository.Db.Queryable<Dt_LocationInfo>().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<Dt_Task_Hty>().Where(x => x.TaskType >= 500 && x.TaskType < 900 && x.CreateDate.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")).Count();
|
var outboundCount = _taskHtyRepository.Db.Queryable<Dt_Task_Hty>().Where(x => x.TaskType >= 100 && x.TaskType < 500 && x.CreateDate.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd")).Count();
|
|
//计算有货料箱数量
|
var inStockPallet = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletType == (int)PalletTypeEnum.None && !string.IsNullOrEmpty(x.LocationCode)).Count();
|
//计算空箱数量
|
var freeStockPallet = _stockInfoRepository.Db.Queryable<Dt_StockInfo>().Where(x => x.PalletType == (int)PalletTypeEnum.Empty && !string.IsNullOrEmpty(x.LocationCode)).Count();
|
// 4. 获取近7日每日出入库明细(核心修改:调用上面的方法)
|
var dailyInOutBoundList = Get7DaysDailyInOutBound();
|
|
var nearExpirationList = GetMaterialsNearExpiration();
|
//获取作业统计
|
var completeTask = SimpleStatistics();
|
//任务
|
List<Dt_Task> 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,
|
CompleteTask = completeTask,
|
NearExpirationList = nearExpirationList
|
};
|
return WebResponseContent.Instance.OK(data: bigGreenData);
|
}
|
|
/// <summary>
|
/// 获取近7日每日出入库明细
|
/// </summary>
|
/// <returns>每日出入库明细列表</returns>
|
private List<DailyInOutBoundDto> 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<Dt_OutboundOrderDetail>()
|
.Where(x => x.CreateDate >= startDate
|
&& x.CreateDate < endDate.AddDays(1))
|
|
.Select(x => new
|
{
|
Date = x.CreateDate.ToString("MM-dd"),
|
x.OverOutQuantity
|
})
|
.ToList()
|
.GroupBy(x => x.Date)
|
.ToDictionary(k => k.Key, g => g.Sum(x => (decimal?)x.OverOutQuantity) ?? 0); // 转为字典方便匹配
|
|
// 3. 查询每日入库明细(按日期分组)
|
var dailyInboundList = _inboundOrderDetailRepository.Db
|
.Queryable<Dt_InboundOrderDetail>()
|
.Where(x => x.CreateDate >= startDate
|
&& x.CreateDate < endDate.AddDays(1))
|
.Select(x => new
|
{
|
Date = x.CreateDate.ToString("MM-dd"),
|
x.OverInQuantity
|
})
|
.ToList()
|
.GroupBy(x => x.Date)
|
.ToDictionary(k => k.Key, g => g.Sum(x => (decimal?)x.OverInQuantity) ?? 0); // 转为字典方便匹配
|
|
// 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;
|
}
|
|
public List<SimpleStatisticsDTO> SimpleStatistics()
|
{
|
DateTime sevenDaysAgo = DateTime.Now.AddDays(-7);
|
|
var stats = _taskHtyRepository
|
.QueryData(x => x.TaskStatus == (int)TaskStatusEnum.Finish && x.CreateDate >= sevenDaysAgo)
|
.GroupBy(t =>
|
(int)t.TaskType >= 100 && (int)t.TaskType <= 299 ? "出库" :
|
(int)t.TaskType >= 500 && (int)t.TaskType <= 699 ? "入库" : "其他"
|
)
|
.Where(g => g.Key == "出库" || g.Key == "入库")
|
.Select(g => new SimpleStatisticsDTO
|
{
|
TaskType = g.Key,
|
Count = g.Count()
|
})
|
.ToList();
|
|
return stats;
|
}
|
|
/// <summary>
|
/// 大屏/汇总数据返回DTO(大绿数据汇总)
|
/// </summary>
|
public class BigGreenDataDto
|
{
|
/// <summary>
|
/// 入库完成数量
|
/// </summary>
|
public int InboundCount { get; set; }
|
/// <summary>
|
/// 出库完成数量
|
/// </summary>
|
public int OutboundCount { get; set; }
|
/// <summary>
|
/// 总库存数量
|
/// </summary>
|
public decimal TotalStockQuantity { get; set; }
|
|
/// <summary>
|
/// 待出库订单数量(出库中+未开始)
|
/// </summary>
|
public int UnOutBoundOrderCount { get; set; }
|
|
/// <summary>
|
/// 空闲库位数量
|
/// </summary>
|
public int FreeLocationCount { get; set; }
|
|
/// <summary>
|
/// 占用库位数量(已存库)
|
/// </summary>
|
public int InStockLocationCount { get; set; }
|
|
/// <summary>
|
/// 库位利用率(小数形式,如0.85对应85%)
|
/// </summary>
|
public decimal LocationUtilizationRate { get; set; }
|
|
/// <summary>
|
/// 近7日每日出入库明细
|
/// </summary>
|
public List<DailyInOutBoundDto> DailyInOutBoundList { get; set; } = new List<DailyInOutBoundDto>();
|
|
/// <summary>
|
/// 近7日净入库量(入库-出库)
|
/// </summary>
|
public decimal NetStock7Days { get; set; }
|
|
/// <summary>
|
/// 近7日净入库量(入库-出库)
|
/// </summary>
|
public decimal TotalStockChangeRate { get; set; }
|
|
/// <summary>
|
/// 任务列表
|
/// </summary>
|
public List<Dt_Task> TaskList { get; set; } = new List<Dt_Task>();
|
|
public int InStockPallet { get; set; }
|
|
public int FreeStockPallet { get; set; }
|
|
public List<SimpleStatisticsDTO> CompleteTask { get; set; }
|
|
public NearExpirationDTO NearExpirationList { get; set; }
|
}
|
|
/// <summary>
|
/// 每日出入库明细DTO
|
/// </summary>
|
public class DailyInOutBoundDto
|
{
|
/// <summary>
|
/// 日期(格式:yyyy-MM-dd)
|
/// </summary>
|
public string Date { get; set; }
|
|
/// <summary>
|
/// 当日出库总量
|
/// </summary>
|
public decimal DailyOutboundQuantity { get; set; }
|
|
/// <summary>
|
/// 当日入库总量
|
/// </summary>
|
public decimal DailyInboundQuantity { get; set; }
|
|
}
|
|
public class SimpleStatisticsDTO
|
{
|
public string TaskType { get; set; }
|
public int Count { get; set; }
|
}
|
|
public class NearExpirationDTO
|
{
|
public int DaysToExpiration { get; set; }
|
|
public List<Dt_StockInfoDetail> Details { get; set; }
|
|
public string LocationCode { get; set; }
|
|
public string PalletCode { get; set; }
|
}
|
|
///<summary>
|
///获取近30天要过期的物料
|
/// </summary>
|
public NearExpirationDTO GetMaterialsNearExpiration()
|
{
|
// 初始化返回DTO
|
var resultDTO = new NearExpirationDTO
|
{
|
Details = new List<Dt_StockInfoDetail>(),
|
LocationCode = string.Empty,
|
PalletCode = string.Empty,
|
DaysToExpiration = 0 // 初始化天数
|
};
|
|
DateTime currentTime = DateTime.Now;
|
DateTime thirtyDaysLater = currentTime.AddDays(30);
|
|
// 筛选30天内过期的库存明细
|
var nearExpirationList = _stockInfoDetailRepository.Db.Queryable<Dt_StockInfoDetail>()
|
.Where(x => (x.ValidDate.Value - x.CreateDate).TotalDays <= 30)
|
.ToList();
|
|
// 无符合条件的明细,直接返回
|
if (!nearExpirationList.Any())
|
{
|
return resultDTO;
|
}
|
|
|
var firstStockId = nearExpirationList.First().StockId;
|
|
var stock = _stockInfoRepository.Db.Queryable<Dt_StockInfo>()
|
.First(x => x.Id == firstStockId);
|
|
|
if (stock == null)
|
{
|
return resultDTO;
|
}
|
|
|
resultDTO.LocationCode = stock.LocationCode;
|
resultDTO.PalletCode = stock.PalletCode;
|
|
|
int minDaysToExpiration = int.MaxValue;
|
foreach (var detail in nearExpirationList)
|
{
|
|
TimeSpan totalDaysToExpiration = detail.ValidDate.Value - detail.CreateDate;
|
double remainingDays = totalDaysToExpiration.TotalDays;
|
int daysToExpiration = (int)Math.Ceiling(Math.Max(0, remainingDays));
|
|
|
if (daysToExpiration < minDaysToExpiration)
|
{
|
minDaysToExpiration = daysToExpiration;
|
}
|
|
|
resultDTO.Details.Add(detail);
|
}
|
|
|
resultDTO.DaysToExpiration = minDaysToExpiration;
|
|
return resultDTO;
|
}
|
}
|
}
|