MRP物料分时间查询优化
This commit is contained in:
parent
9e14c50dba
commit
520f321b95
@ -221,55 +221,55 @@ namespace DOAN.Service.MES.mm.paintedparts_call
|
||||
.ToList();
|
||||
|
||||
// 计算每个 ProductionCode 的 DeliveryNum 总和
|
||||
var deliveryNumDict = workorders
|
||||
var totalDeliveryDict = workorders
|
||||
.GroupBy(x => x.ProductionCode)
|
||||
.ToDictionary(g => g.Key, g => g.Sum(x => x.DeliveryNum));
|
||||
|
||||
// 生成 MmCallRequests
|
||||
List<MmCallMrp> mmCallMrpList = new();
|
||||
|
||||
// 1. 按工单顺序排序 (假设按ProductionCode排序,可根据实际需求调整)
|
||||
var sortedWorkorders = workorders.OrderBy(x => x.ProductionCode).ToList();
|
||||
//// 1. 按工单顺序排序 (假设按ProductionCode排序,可根据实际需求调整)
|
||||
//var sortedWorkorders = workorders.OrderBy(x => x.ProductionCode).ToList();
|
||||
|
||||
// 2. 计算总交货量并拆分为四组
|
||||
int totalDeliveryNum = sortedWorkorders.Sum(x => x.DeliveryNum).Value;
|
||||
int groupTarget = totalDeliveryNum / 4;
|
||||
//// 2. 计算总交货量并拆分为四组
|
||||
//int totalDeliveryNum = sortedWorkorders.Sum(x => x.DeliveryNum).Value;
|
||||
//int groupTarget = totalDeliveryNum / 4;
|
||||
|
||||
List<List<dynamic>> groups = new List<List<dynamic>>();
|
||||
List<dynamic> currentGroup = new List<dynamic>();
|
||||
int currentSum = 0;
|
||||
//List<List<dynamic>> groups = new List<List<dynamic>>();
|
||||
//List<dynamic> currentGroup = new List<dynamic>();
|
||||
//int currentSum = 0;
|
||||
|
||||
foreach (var wo in sortedWorkorders)
|
||||
{
|
||||
currentGroup.Add(wo);
|
||||
currentSum += wo.DeliveryNum.Value;
|
||||
//foreach (var wo in sortedWorkorders)
|
||||
//{
|
||||
// currentGroup.Add(wo);
|
||||
// currentSum += wo.DeliveryNum.Value;
|
||||
|
||||
if (currentSum >= groupTarget * (groups.Count + 1))
|
||||
{
|
||||
groups.Add(currentGroup);
|
||||
currentGroup = new List<dynamic>();
|
||||
currentSum = 0;
|
||||
}
|
||||
}
|
||||
// if (currentSum >= groupTarget * (groups.Count + 1))
|
||||
// {
|
||||
// groups.Add(currentGroup);
|
||||
// currentGroup = new List<dynamic>();
|
||||
// currentSum = 0;
|
||||
// }
|
||||
//}
|
||||
|
||||
// 处理剩余工单
|
||||
if (currentGroup.Any())
|
||||
{
|
||||
if (groups.Count < 4)
|
||||
{
|
||||
groups.Add(currentGroup);
|
||||
}
|
||||
else
|
||||
{
|
||||
groups[3].AddRange(currentGroup);
|
||||
}
|
||||
}
|
||||
//// 处理剩余工单
|
||||
//if (currentGroup.Any())
|
||||
//{
|
||||
// if (groups.Count < 4)
|
||||
// {
|
||||
// groups.Add(currentGroup);
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// groups[3].AddRange(currentGroup);
|
||||
// }
|
||||
//}
|
||||
|
||||
// 确保有四组
|
||||
while (groups.Count < 4)
|
||||
{
|
||||
groups.Add(new List<dynamic>());
|
||||
}
|
||||
//// 确保有四组
|
||||
//while (groups.Count < 4)
|
||||
//{
|
||||
// groups.Add(new List<dynamic>());
|
||||
//}
|
||||
|
||||
// 3. 定义四个时间段
|
||||
var timePeriods = new List<(DateTime Start, DateTime End)>
|
||||
@ -281,24 +281,16 @@ namespace DOAN.Service.MES.mm.paintedparts_call
|
||||
};
|
||||
|
||||
// 4. 分时段计算物料需求
|
||||
for (int i = 0; i < groups.Count && i < timePeriods.Count; i++)
|
||||
foreach (var (startTime, endTime) in timePeriods)
|
||||
{
|
||||
var group = groups[i];
|
||||
var (startTime, endTime) = timePeriods[i];
|
||||
|
||||
if (!group.Any())
|
||||
continue;
|
||||
|
||||
// 获取该组的ProductionCode列表并转换为HashSet以提高查找效率
|
||||
// 显式指定泛型类型参数确保从dynamic转换为string
|
||||
var groupProductionCodes = new HashSet<string>(
|
||||
group.Select<dynamic, string>(x => x.ProductionCode.ToString()).Distinct()
|
||||
);
|
||||
var groupProductionCodes = new HashSet<string>(lineProductionCodeList);
|
||||
|
||||
// 计算该组的DeliveryNum字典
|
||||
var groupDeliveryDict = group
|
||||
.GroupBy(x => x.ProductionCode)
|
||||
.ToDictionary(g => g.Key, g => g.Sum(x => x.DeliveryNum));
|
||||
//// 计算该组的DeliveryNum字典
|
||||
//var groupDeliveryDict = group
|
||||
// .GroupBy(x => x.ProductionCode)
|
||||
// .ToDictionary(g => g.Key, g => g.Sum(x => x.DeliveryNum));
|
||||
|
||||
// 按linecode与MaterialCode进行聚合统计
|
||||
var aggregatedData = mmCallMaterialBoms
|
||||
@ -318,15 +310,13 @@ namespace DOAN.Service.MES.mm.paintedparts_call
|
||||
MaterialName = g.Key.subInvName,
|
||||
TotalQuantity = g.Sum(bom =>
|
||||
{
|
||||
if (
|
||||
!groupDeliveryDict.TryGetValue(
|
||||
bom.InvCode,
|
||||
out int workOrderDeliveryNum
|
||||
)
|
||||
)
|
||||
int totalDeliveryNum = workorders
|
||||
.Where(w => w.ProductionCode == bom.InvCode)
|
||||
.Sum(w => w.DeliveryNum) ?? 0;
|
||||
|
||||
if (totalDeliveryNum <= 0)
|
||||
{
|
||||
Context.Ado.RollbackTran();
|
||||
throw new Exception($"{bom.InvCode}交货数量异常");
|
||||
return 0m; // 没有交货量则跳过
|
||||
}
|
||||
|
||||
decimal iusequantity;
|
||||
@ -336,7 +326,7 @@ namespace DOAN.Service.MES.mm.paintedparts_call
|
||||
throw new Exception($"{bom.subInvCode}BOM使用数量异常");
|
||||
}
|
||||
|
||||
return workOrderDeliveryNum * iusequantity;
|
||||
return totalDeliveryNum * iusequantity;
|
||||
})
|
||||
})
|
||||
.ToList();
|
||||
@ -364,6 +354,16 @@ namespace DOAN.Service.MES.mm.paintedparts_call
|
||||
);
|
||||
}
|
||||
}
|
||||
mmCallMrpList = mmCallMrpList
|
||||
.Where(it => !string.IsNullOrEmpty(it.MaterialCode) &&
|
||||
(it.MaterialCode.Contains("-") || it.MaterialCode.Contains("—")))
|
||||
.ToList();
|
||||
|
||||
foreach (var mrp in mmCallMrpList.Take(10))
|
||||
{
|
||||
Console.WriteLine($"MaterialCode: {mrp.MaterialCode}");
|
||||
}
|
||||
|
||||
// 删除旧数据并插入新数据
|
||||
Context
|
||||
.Deleteable<MmCallMrp>()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user