using Admin.NET.Application.Entity;
using Admin.NET.Application.Service.ReportCenter.WmsOtherReport.Dto;
namespace Admin.NET.Application;
///
/// 收货完成统计 服务
///
[ApiDescriptionSettings(ApplicationConst.ReportCenterGroupName, Order = 100)]
public class WmsTakeGoodStatsService : IDynamicApiController, ITransient
{
private readonly SqlSugarRepository _rep;
private readonly SqlSugarRepository _wmsRecordReceivingDeliveryRep;
public WmsTakeGoodStatsService(
SqlSugarRepository wmsRecordReceivingDeliveryRep,
SqlSugarRepository rep)
{
_wmsRecordReceivingDeliveryRep = wmsRecordReceivingDeliveryRep;
_rep = rep;
}
///
/// 收货完成情况
///
///
///
[HttpGet]
[ApiDescriptionSettings(Name = "Page")]
[Description("WmsTakeGoodStats/Page")]
public async Task> Page([FromQuery] WmsTakeGoodStatsInput input)
{
var today = DateTime.Today; // 获取当前日期
// 查询当天的 ASN 单明细表,按照 ASN 单号、物料号、供应商名称进行分组
var result = await Task.Run(() =>
{
var query = _rep.AsQueryable()
.Where(a => a.CreateTime >= today) // 过滤条件,只取当天的记录
.Select()
.ToList();
return query;
});
//查找今天收获的所有记录
var records = await _wmsRecordReceivingDeliveryRep.AsQueryable().Where(x => x.CreateTime >= today).ToListAsync();
var returnQuery = new SqlSugarPagedList();
var wmsTakeGoodStatsOutputList = new List();
foreach (var item in result)
{
var newWmsTakeGoodStatsOutput = new WmsTakeGoodStatsOutput();
if (item.AsnStatus == OrderStatusEnum.新建)
{
newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
// newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
//newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.待收货;
newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
}
else if (item.AsnStatus == OrderStatusEnum.处理中)
{
newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
if (today < item.PlannedEndTime)
{
newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.收货中;
wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
}
else if (today > item.PlannedEndTime)
{
newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.延迟未收货;
wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
}
}
else if (item.AsnStatus == OrderStatusEnum.已完成 || item.AsnStatus == OrderStatusEnum.已关闭)
{
if (item.AsnStatus == OrderStatusEnum.已完成)
{
newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
// 找到所有相同 ASN 单号的记录
var completedItems = result.Where(r => r.AsnNo == item.AsnNo).ToList();
newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
// 找到最小和最大的 PlannedStartTime
var minStartTime = completedItems.Min(r => r.PlannedStartTime);
var maxEndTime = completedItems.Max(r => r.PlannedEndTime);
// 设置 ASNBeginTime 和 ASNEndTime
newWmsTakeGoodStatsOutput.ASNBeginTime = minStartTime;
newWmsTakeGoodStatsOutput.ASNEndTime = maxEndTime;
newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.收货完成;
newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
}
else
{
newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.已关闭;
newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
}
}
else if (item.AsnStatus == OrderStatusEnum.已取消)
{
newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.已取消;
newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
}
}
// 使用 LINQ 对 wmsTakeGoodStatsOutputList 进行分组
var groupedResults = wmsTakeGoodStatsOutputList
.GroupBy(x => new { x.AsnNo, x.MaterialCode, x.SupplierName })
.Select(group => new WmsTakeGoodStatsOutput()
{
SupplierName = group.Key.SupplierName,
AsnNo = group.Key.AsnNo,
MaterialCode = group.Key.MaterialCode,
//ASNStatus = group.Max(x => x.ASNStatus),
ProjectNo = group.Min(x => x.ProjectNo),
PlanBeginTime = group.Min(x => x.PlanBeginTime),
AllQty = group.Sum(x => x.AllQty),
ASNNumber = group.Sum(x => x.ASNNumber),
//ASNBeginTime = group.Min(x => x.ASNBeginTime),
//ASNEndTime = group.Max(x => x.ASNEndTime),
IsCancel = group.Count(x => x.ASNStatus == ReceiptStatusEnum.已取消) == group.Count() ? true : false,
IsClose = group.Count(x => x.ASNStatus == ReceiptStatusEnum.已关闭) == group.Count() ? true : false,
}); ;
input.Page = 1;
input.PageSize = 10000;
var ls = groupedResults.ToPagedList(input.Page, input.PageSize);
foreach (var item in ls.Items)
{
//计算 开始收获时间+结束收获时间
var myrecords = records.Where(x => x.OrderNo == item.AsnNo && x.MaterialCode == item.MaterialCode && x.SupplierName == item.SupplierName).OrderBy(x => x.Id).ToList();
if (myrecords.Count > 0)
{
item.ASNBeginTime = myrecords.Select(x => x.CreateTime).Min();
//判断他的状态
if (item.AllQty == item.ASNNumber)
{
item.ASNStatus = ReceiptStatusEnum.收货完成;
item.ASNEndTime = myrecords.Select(x => x.CreateTime).Max();
}
else if (item.AllQty > item.ASNNumber)
{
if (item.ASNNumber > 0)
{
item.ASNStatus = ReceiptStatusEnum.收货中;
}
else
{
item.ASNBeginTime = null;
item.ASNStatus = ReceiptStatusEnum.待收货;
//判断是不是延迟未收货
if (item.PlanBeginTime < DateTime.Now)
{
item.ASNStatus = ReceiptStatusEnum.延迟未收货;
}
}
}
}
else
{
item.ASNBeginTime = null;
item.ASNStatus = ReceiptStatusEnum.待收货;
//判断是不是延迟未收货
if (item.PlanBeginTime < DateTime.Now)
{
item.ASNStatus = ReceiptStatusEnum.延迟未收货;
}
}
//判断是否取消或关闭
if (item.IsCancel)
{
item.ASNStatus = ReceiptStatusEnum.已取消;
}
if (item.IsClose)
{
item.ASNStatus = ReceiptStatusEnum.已关闭;
}
}
return ls;
}
}