diff --git a/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmCallRequestsController.cs b/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmCallRequestsController.cs index 9a54ef5..466430c 100644 --- a/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmCallRequestsController.cs +++ b/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmCallRequestsController.cs @@ -103,7 +103,29 @@ namespace DOAN.Admin.WebApi.Controllers return ToResponse(response); } + /// + /// 生成产线油漆件MRP + /// + /// + [HttpPost("GenerateLineMmCallMRP")] + [AllowAnonymous] + [Log(Title = "生成产线油漆件MRP", BusinessType = BusinessType.OTHER)] + public IActionResult GenerateLineMmCallMRP([FromBody] MmCallRequestsDto parm) + { + try + { + var modal = parm.Adapt().ToCreate(HttpContext); + var response = _MmCallRequestsService.GenerateLineMmCallMRP(modal); + + return SUCCESS(response); + } + catch (Exception ex) + { + return ToResponse(new ApiResult(500,ex.Message)); + } + + } } diff --git a/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmGeneratePaintBOMController.cs b/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmGeneratePaintBOMController.cs index 56fcce3..209e745 100644 --- a/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmGeneratePaintBOMController.cs +++ b/DOAN.Admin.WebApi/Controllers/MES/mm/paintedparts_call/MmGeneratePaintBOMController.cs @@ -26,11 +26,23 @@ namespace DOAN.Admin.WebApi.Controllers _MmGeneratePaintBOMService = mmGeneratePaintBOMService; } + /// + /// 生成油漆件BOM + /// + /// [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)); + } + } diff --git a/DOAN.Model/MES/mm/paintedparts_call/MmCallMaterialBom.cs b/DOAN.Model/MES/mm/paintedparts_call/MmCallMaterialBom.cs new file mode 100644 index 0000000..63abca7 --- /dev/null +++ b/DOAN.Model/MES/mm/paintedparts_call/MmCallMaterialBom.cs @@ -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 +{ + /// + /// BOM档案 + /// + //[Tenant("1")] + [SugarTable("mm_call_material_bom")] + public class MmCallMaterialBom + { + /// + /// 母件编码 + /// + [SugarColumn(ColumnName = "InvCode")] + public string InvCode { get; set; } + + /// + /// 母件名称 + /// + [SugarColumn(ColumnName = "InvName")] + public string InvName { get; set; } + + /// + /// 子件编码 + /// + [SugarColumn(ColumnName = "subInvCode")] + public string subInvCode { get; set; } + + /// + /// 子件名称 + /// + [SugarColumn(ColumnName = "subInvName")] + public string subInvName { get; set; } + + /// + /// 使用数量 + /// + [SugarColumn(ColumnName = "iusequantity")] + public string Iusequantity { get; set; } + + /// + /// BOM版本号 + /// + [SugarColumn(ColumnName = "BOMVersion")] + public string BOMVersion { get; set; } + + /// + /// 创建人 + /// + [SugarColumn(ColumnName = "created_by")] + public string CREATED_BY { get; set; } + + /// + /// 创建时间 + /// + [SugarColumn(ColumnName = "created_time")] + public DateTime? CREATED_TIME { get; set; } + } +} diff --git a/DOAN.Service/MES/mm/paintedparts_call/IService/IMmCallRequestsService.cs b/DOAN.Service/MES/mm/paintedparts_call/IService/IMmCallRequestsService.cs index 27bb69c..721a5b0 100644 --- a/DOAN.Service/MES/mm/paintedparts_call/IService/IMmCallRequestsService.cs +++ b/DOAN.Service/MES/mm/paintedparts_call/IService/IMmCallRequestsService.cs @@ -21,5 +21,7 @@ namespace DOAN.Service.MES.mm.paintedparts_call.IService int UpdateMmCallRequests(MmCallRequests parm); + // 生成产线油漆件MRP + int GenerateLineMmCallMRP(MmCallRequests parm); } } diff --git a/DOAN.Service/MES/mm/paintedparts_call/MmCallRequestsService.cs b/DOAN.Service/MES/mm/paintedparts_call/MmCallRequestsService.cs index 7f3e732..ee0aafe 100644 --- a/DOAN.Service/MES/mm/paintedparts_call/MmCallRequestsService.cs +++ b/DOAN.Service/MES/mm/paintedparts_call/MmCallRequestsService.cs @@ -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; } - /// /// 获取详情 /// @@ -42,9 +45,7 @@ namespace DOAN.Service.MES.mm.paintedparts_call /// 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() + .Where(x => x.LineCode == lineCode && x.WorkorderDate == checkDate) + .Select(x => new { x.ProductionCode, x.DeliveryNum }) + .ToList(); + + List lineProductionCodeList = workorders + .Select(x => x.ProductionCode) + .Distinct() + .ToList(); + if (!lineProductionCodeList.Any()) + { + Context.Ado.RollbackTran(); + throw new Exception("未找到对应的工单"); + } + + // 查询 BOM 表 + List mmCallMaterialBoms = Context + .Queryable() + .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 = 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() + .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; + } + } } -} \ No newline at end of file +} diff --git a/DOAN.Service/MES/mm/paintedparts_call/MmGeneratePaintBOMService.cs b/DOAN.Service/MES/mm/paintedparts_call/MmGeneratePaintBOMService.cs index 3ab4db6..602baa9 100644 --- a/DOAN.Service/MES/mm/paintedparts_call/MmGeneratePaintBOMService.cs +++ b/DOAN.Service/MES/mm/paintedparts_call/MmGeneratePaintBOMService.cs @@ -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, IMmGeneratePaintBOMService { /// @@ -19,9 +24,82 @@ namespace DOAN.Service.MES.mm.paintedparts_call /// 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 invCodes = Context + .AsTenant() + .QueryableWithAttr() + .Where(it => it.ClnvDefine8 == ClnvDefine8) + .Select(it => it.InvCode) + .ToList(); + + // 2. 若无数据,直接返回(避免无意义的DELETE操作) + if (!invCodes.Any()) + { + Context.Ado.CommitTran(); + return 0; + } + + // 3. 分批筛选油漆BOM(避免SQL参数爆炸) + List bomList = new List(); + 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() + .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; + } } } }