生成油漆件BOM 功能与 生成产线油漆件MRP功能实现

This commit is contained in:
赵正易 2025-08-04 14:28:13 +08:00
parent a3eaebdf65
commit 08ea99512d
6 changed files with 304 additions and 21 deletions

View File

@ -103,7 +103,29 @@ namespace DOAN.Admin.WebApi.Controllers
return ToResponse(response);
}
/// <summary>
/// 生成产线油漆件MRP
/// </summary>
/// <returns></returns>
[HttpPost("GenerateLineMmCallMRP")]
[AllowAnonymous]
[Log(Title = "生成产线油漆件MRP", BusinessType = BusinessType.OTHER)]
public IActionResult GenerateLineMmCallMRP([FromBody] MmCallRequestsDto parm)
{
try
{
var modal = parm.Adapt<MmCallRequests>().ToCreate(HttpContext);
var response = _MmCallRequestsService.GenerateLineMmCallMRP(modal);
return SUCCESS(response);
}
catch (Exception ex)
{
return ToResponse(new ApiResult(500,ex.Message));
}
}
}

View File

@ -26,11 +26,23 @@ namespace DOAN.Admin.WebApi.Controllers
_MmGeneratePaintBOMService = mmGeneratePaintBOMService;
}
/// <summary>
/// 生成油漆件BOM
/// </summary>
/// <returns></returns>
[HttpGet("generateBOM")]
public IActionResult GenerateMaterialBOM()
{
var response = _MmGeneratePaintBOMService.GenerateMaterialBOM();
return SUCCESS(response);
try
{
var response = _MmGeneratePaintBOMService.GenerateMaterialBOM();
return SUCCESS(response);
}
catch (Exception ex)
{
return ToResponse(new ApiResult(500, ex.Message));
}
}

View File

@ -0,0 +1,64 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DOAN.Model.MES.ERP
{
/// <summary>
/// BOM档案
/// </summary>
//[Tenant("1")]
[SugarTable("mm_call_material_bom")]
public class MmCallMaterialBom
{
/// <summary>
/// 母件编码
/// </summary>
[SugarColumn(ColumnName = "InvCode")]
public string InvCode { get; set; }
/// <summary>
/// 母件名称
/// </summary>
[SugarColumn(ColumnName = "InvName")]
public string InvName { get; set; }
/// <summary>
/// 子件编码
/// </summary>
[SugarColumn(ColumnName = "subInvCode")]
public string subInvCode { get; set; }
/// <summary>
/// 子件名称
/// </summary>
[SugarColumn(ColumnName = "subInvName")]
public string subInvName { get; set; }
/// <summary>
/// 使用数量
/// </summary>
[SugarColumn(ColumnName = "iusequantity")]
public string Iusequantity { get; set; }
/// <summary>
/// BOM版本号
/// </summary>
[SugarColumn(ColumnName = "BOMVersion")]
public string BOMVersion { get; set; }
/// <summary>
/// 创建人
/// </summary>
[SugarColumn(ColumnName = "created_by")]
public string CREATED_BY { get; set; }
/// <summary>
/// 创建时间
/// </summary>
[SugarColumn(ColumnName = "created_time")]
public DateTime? CREATED_TIME { get; set; }
}
}

View File

@ -21,5 +21,7 @@ namespace DOAN.Service.MES.mm.paintedparts_call.IService
int UpdateMmCallRequests(MmCallRequests parm);
// 生成产线油漆件MRP
int GenerateLineMmCallMRP(MmCallRequests parm);
}
}

View File

@ -1,14 +1,18 @@
using System;
using SqlSugar;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using System.Collections.Generic;
using System.Linq;
using DOAN.Model;
using DOAN.Model.Dto;
using DOAN.Repository;
using System.Linq;
using DOAN.Service.MES.mm.paintedparts_call.IService;
using DOAN.Model.MES.ERP;
using DOAN.Model.MES.mm.paintedparts_call;
using DOAN.Model.MES.mm.paintedparts_call.Dto;
using DOAN.Model.MES.product;
using DOAN.Repository;
using DOAN.Service.MES.mm.paintedparts_call.IService;
using Infrastructure.Attribute;
using Infrastructure.Extensions;
using JinianNet.JNTemplate;
using SqlSugar;
namespace DOAN.Service.MES.mm.paintedparts_call
{
@ -34,7 +38,6 @@ namespace DOAN.Service.MES.mm.paintedparts_call
return response;
}
/// <summary>
/// 获取详情
/// </summary>
@ -42,9 +45,7 @@ namespace DOAN.Service.MES.mm.paintedparts_call
/// <returns></returns>
public MmCallRequests GetInfo(int Id)
{
var response = Queryable()
.Where(x => x.Id == Id)
.First();
var response = Queryable().Where(x => x.Id == Id).First();
return response;
}
@ -82,7 +83,111 @@ namespace DOAN.Service.MES.mm.paintedparts_call
//return response;
return Update(model, true);
}
public int GenerateLineMmCallMRP(MmCallRequests parm)
{
try
{
Context.Ado.BeginTran();
string lineCode = parm.LineCode;
if (string.IsNullOrEmpty(lineCode))
{
Context.Ado.RollbackTran();
throw new Exception("产线编码不能为空");
}
if (!parm.RequestTime.HasValue)
{
Context.Ado.RollbackTran();
throw new Exception("请选择日期");
}
DateTime checkDate = parm.RequestTime.Value;
// 查询工单及对应的 DeliveryNum
var workorders = Context
.Queryable<ProWorkorder>()
.Where(x => x.LineCode == lineCode && x.WorkorderDate == checkDate)
.Select(x => new { x.ProductionCode, x.DeliveryNum })
.ToList();
List<string> lineProductionCodeList = workorders
.Select(x => x.ProductionCode)
.Distinct()
.ToList();
if (!lineProductionCodeList.Any())
{
Context.Ado.RollbackTran();
throw new Exception("未找到对应的工单");
}
// 查询 BOM 表
List<MmCallMaterialBom> mmCallMaterialBoms = Context
.Queryable<MmCallMaterialBom>()
.Where(it => lineProductionCodeList.Contains(it.InvCode))
.Where(it => !string.IsNullOrEmpty(it.subInvCode))
.ToList();
// 计算每个 ProductionCode 的 DeliveryNum 总和
var deliveryNumDict = workorders
.GroupBy(x => x.ProductionCode)
.ToDictionary(g => g.Key, g => g.Sum(x => x.DeliveryNum));
// 生成 MmCallRequests
List<MmCallRequests> mmCallRequests = new();
foreach (MmCallMaterialBom item in mmCallMaterialBoms)
{
if (!deliveryNumDict.TryGetValue(item.InvCode, out int? workOrderDeliveryNum))
{
Context.Ado.RollbackTran();
throw new Exception($"{item.InvCode}交货数量异常");
}
decimal iusequantity;
if (!decimal.TryParse(item.Iusequantity, out iusequantity))
{
Context.Ado.RollbackTran();
throw new Exception($"{item.subInvCode}BOM使用数量异常");
}
mmCallRequests.Add(
new MmCallRequests
{
LineCode = lineCode,
RequestTime = checkDate,
AcceptTime = null,
MaterialCode = item.subInvCode,
MaterialName = item.subInvName,
Quantity = (int?)(workOrderDeliveryNum * iusequantity),
CreatedBy = "系统自动生成",
CreatedTime = DateTime.Now,
}
);
}
// 删除旧数据并插入新数据
Context
.Deleteable<MmCallRequests>()
.Where(it => it.RequestTime == checkDate)
.Where(it => it.LineCode == lineCode)
.ExecuteCommand();
int result = Context.Insertable(mmCallRequests).ExecuteCommand();
if(result > 0)
{
Context.Ado.CommitTran();
return 1;
}
else
{
Context.Ado.RollbackTran();
return 0;
}
}
catch (Exception)
{
Context.Ado.RollbackTran();
throw;
}
}
}
}
}

View File

@ -1,16 +1,21 @@
using DOAN.Model.MES.mm.paintedparts_call;
using DOAN.Service.MES.mm.paintedparts_call.IService;
using Infrastructure.Attribute;
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DOAN.Model.MES.ERP;
using DOAN.Model.MES.mm.paintedparts_call;
using DOAN.Service.MES.mm.paintedparts_call.IService;
using Infrastructure.Attribute;
using JinianNet.JNTemplate;
using Mapster;
namespace DOAN.Service.MES.mm.paintedparts_call
{
[AppService(ServiceType = typeof(IMmGeneratePaintBOMService), ServiceLifetime = LifeTime.Transient)]
[AppService(
ServiceType = typeof(IMmGeneratePaintBOMService),
ServiceLifetime = LifeTime.Transient
)]
public class MmGeneratePaintBOMService : BaseService<MmCallRequests>, IMmGeneratePaintBOMService
{
/// <summary>
@ -19,9 +24,82 @@ namespace DOAN.Service.MES.mm.paintedparts_call
/// <returns></returns>
public int GenerateMaterialBOM()
{
// 涂装客户编号
const string ClnvDefine8 = "123456";
// 自动生成相关配置
const string CREATED_BY = "系统自动生成";
const string BOM_TABLE_NAME = "mm_call_material_bom";
try
{
Context.Ado.BeginTran();
return 0;
// 1. 查询ERP中的有关涂装油漆件物料清单
List<string> invCodes = Context
.AsTenant()
.QueryableWithAttr<CustDevMesInventory>()
.Where(it => it.ClnvDefine8 == ClnvDefine8)
.Select(it => it.InvCode)
.ToList();
// 2. 若无数据直接返回避免无意义的DELETE操作
if (!invCodes.Any())
{
Context.Ado.CommitTran();
return 0;
}
// 3. 分批筛选油漆BOM避免SQL参数爆炸
List<MmCallMaterialBom> bomList = new List<MmCallMaterialBom>();
int batchSize = 1000; // 根据数据库参数限制调整
for (int i = 0; i < invCodes.Count; i += batchSize)
{
var currentBatch = invCodes.Skip(i).Take(batchSize).ToList();
var batchResult = Context
.AsTenant()
.QueryableWithAttr<CustDevMesBom>()
.Where(it =>
currentBatch.Contains(it.McInvCode) // 确保McInvCode有索引
|| currentBatch.Contains(it.subcInvCode) // 确保subcInvCode有索引
)
.Select(it => new MmCallMaterialBom
{
InvCode = it.McInvCode,
InvName = it.McInvName,
subInvCode = it.subcInvCode,
subInvName = it.subcInvName,
Iusequantity = it.Iusequantity,
BOMVersion = it.BOMVersion,
CREATED_BY = CREATED_BY,
CREATED_TIME = DateTime.Now
})
.ToList();
bomList.AddRange(batchResult);
}
// 4. 清空并插入新数据
Context.Ado.ExecuteCommand($"DELETE FROM {BOM_TABLE_NAME}");
int result = Context.Insertable(bomList).ExecuteCommand();
if (result > 0)
{
Context.Ado.CommitTran();
return result;
}
else
{
Context.Ado.RollbackTran();
return 0;
}
}
catch (Exception ex)
{
Context.Ado.RollbackTran();
// 记录日志(需替换为实际日志框架)
// ILogger.LogError(ex, $"GenerateMaterialBOM异常: {ex.Message}");
Console.Error.WriteLine($"GenerateMaterialBOM异常: {ex.Message}\n{ex.StackTrace}");
throw;
}
}
}
}