From 4d916744ba1ba9066d1ecf809f43ddddb817b26b Mon Sep 17 00:00:00 2001 From: schangxiang@126.com <schangxiang@126.com> Date: 周日, 11 5月 2025 14:11:31 +0800 Subject: [PATCH] 2 --- PipeLineLems/server/src/CMS.Plugin.PipeLineLems.Application/Implements/SharedService.cs | 289 +++++++++++++++++++++++++++++++++++++++------------------ 1 files changed, 197 insertions(+), 92 deletions(-) diff --git a/PipeLineLems/server/src/CMS.Plugin.PipeLineLems.Application/Implements/SharedService.cs b/PipeLineLems/server/src/CMS.Plugin.PipeLineLems.Application/Implements/SharedService.cs index 2e16c41..63676a1 100644 --- a/PipeLineLems/server/src/CMS.Plugin.PipeLineLems.Application/Implements/SharedService.cs +++ b/PipeLineLems/server/src/CMS.Plugin.PipeLineLems.Application/Implements/SharedService.cs @@ -7,16 +7,23 @@ using Microsoft.Extensions.DependencyInjection; using Volo.Abp; using Microsoft.Extensions.DependencyInjection; +using NPOI.Util; +using CMS.Plugin.PipeLineLems.Domain.CallMaterialOrder; +using CMS.Plugin.PipeLineLems.Application.Contracts.Services; +using CMS.Plugin.PipeLineLems.Application.Contracts.Dtos.CallMaterialOrder; +using Volo.Abp.Uow; namespace CMS.Plugin.PipeLineLems.Application.Implements; /// <summary> /// 浣滀笟璁″垝琛ㄥ簲鐢ㄦ湇鍔� /// </summary> -public class SharedService: CMSPluginAppService +public class SharedService : CMSPluginAppService { - - public async Task<MesOrderResponse> CommonCreatebyApsAsync(List<WorkPlanInput> input, IServiceProvider _serviceProvider, WorkPlanAppService workPlanAppService) + + public async Task<MesOrderResponse> CommonCreatebyApsAsync(List<WorkPlanInput> input, IServiceProvider _serviceProvider, + WorkPlanAppService workPlanAppService + ) { if (input == null) { @@ -28,111 +35,174 @@ throw new UserFriendlyException("杈撳叆鍙傛暟Data涓嶈兘涓虹┖"); } + //鏍¢獙鏁版嵁 + //1銆佸師鏂欐爣璇嗙浉鍚岀殑涓嶈兘瀛樺湪浜庝袱涓� 绠℃缂栧彿涓� + //var validationResult = ValidateUniqueDataIdentifierPerPipeSection(input); + //if (!validationResult.isValid) + //{ + // 澶勭悊楠岃瘉澶辫触鐨勬儏鍐� + // throw new UserFriendlyException($"楠岃瘉澶辫触: {validationResult.errorMessage}"); + // 杩斿洖閿欒淇℃伅缁欏鎴风鎴栬繘琛屽叾浠栧鐞� + //} + + + var callMaterialOrderAppService = _serviceProvider.GetRequiredService<ICallMaterialOrderAppService>(); + var orderManager = _serviceProvider.GetRequiredService<IOrderManager>(); var productProvider = _serviceProvider.GetRequiredService<IProductProvider>(); var formulaProvider = _serviceProvider.GetRequiredService<IFormulaProvider>(); List<OrderModel> orderModels = new List<OrderModel>(); - //鎸夌収浠诲姟缂栧彿鍒嗙粍 - var groupTask = input.GroupBy(x => x.TaskCode); - foreach (var gTask in groupTask) + + #region 浜嬪姟 + + using var scope = _serviceProvider.CreateScope(); + var unitOfWorkManager = scope.ServiceProvider.GetRequiredService<IUnitOfWorkManager>(); + using var uow = unitOfWorkManager.Begin(requiresNew: true); + + try { - var taskCode = gTask.Key; - var order = await orderManager.GetByCodeAsync(taskCode); - if (order != null) + #region 鏁版嵁澶勭悊 + + //鎸夌収 鍘熸枡鏍囪瘑 鍒嗙粍 + var groupTask = input.GroupBy(x => x.DataIdentifier); + foreach (var gTask in groupTask) { - throw new UserFriendlyException($"浠诲姟缂栧彿[{taskCode}]宸插瓨鍦�"); - } - //var product = await productProvider.FindByNameAsync(orderItem.PipeSectionName); - //if (product == null) - //{ - // throw new UserFriendlyException($"浜у搧鍚嶇О[{orderItem.PipeSectionName}]涓嶅瓨鍦�"); - //} - //var formula = await formulaProvider.GetFormulaAsync(product.Id); - //if (formula == null) - //{ - // throw new UserFriendlyException($"浜у搧鍨嬪彿[{orderItem.MaterialCode}]鏃犲叧鑱旈厤鏂�"); - //} - //棣栧厛瑕佸垱寤� 鎵撶爜鍒囧壊鐨勫伐鍗� - var productForCut = await productProvider.FindByNameAsync("鍒囧壊鍘熸枡绠�"); - if (productForCut == null) - { - throw new UserFriendlyException($"浜у搧鍚嶇О[鍒囧壊鍘熸枡绠涓嶅瓨鍦�"); - } - var formulaForCut = await formulaProvider.GetFormulaAsync(productForCut.Id); - if (formulaForCut == null) - { - throw new UserFriendlyException($"浜у搧鍚嶇О[鍒囧壊鍘熸枡绠鏃犲叧鑱旈厤鏂�"); - } - //鍒嗙粍鏁版嵁 - var group = gTask.ToList().GroupBy(x => x.DataIdentifier); - foreach (var item in group) - { - OrderModel orderModelForCut = new OrderModel() - { - Id = Guid.NewGuid(), - Code = "Cut_" + taskCode, - Source = "APS鎺ㄩ��", - PlanStartTime = gTask.ToList().First().PlannedStartTime, - PlanFinishTime = gTask.ToList().First().PlannedEndTime, - PlanQty = (ulong)item.ToList().Count, - Status = OrderStatus.NotActive, - Product = new AssociationProductModel() { Id = productForCut.Id, Name = productForCut.Name, Model = productForCut.Model, ShortNumber = productForCut.ShortNumber }, - Formula = new AssociationFormulaModel() { Id = formulaForCut.Id, Code = formulaForCut.Code, Name = formulaForCut.Name } - }; - orderModelForCut.ExtraProperties["OuterDiameter"] = gTask.ToList().First().OuterDiameter;//澶栧緞 - orderModelForCut.ExtraProperties["Material"] = gTask.ToList().First().Material;//鏉愯川 - orderModelForCut.ExtraProperties["Length"] = gTask.ToList().First().Length;//闀垮害 - - var orderForCut = await orderManager.GetByCodeAsync(orderModelForCut.Code); - if (orderForCut != null) - { - //throw new UserFriendlyException($"宸ュ崟[{orderModelForCut.Code}]宸插瓨鍦�"); - //涓嶅啀鎶涘紓甯革紝鐩存帴璺宠繃 - break; - } - - var orderModelResultForCut = await orderManager.CreateAsync(orderModelForCut); - if (orderModelResultForCut == null) - { - throw new UserFriendlyException($"宸ュ崟[{orderModelForCut.Code}]鍒涘缓澶辫触"); - } - orderModels.Add(orderModelResultForCut); - - - //OrderModel orderModel = new OrderModel(); - //orderModel.Id = Guid.NewGuid(); - //orderModel.Code = orderItem.TaskCode; - //orderModel.Source = "APS鎺ㄩ��"; - //orderModel.Product = new AssociationProductModel() { Id = product.Id, Name = product.Name, Model = product.Model, ShortNumber = product.ShortNumber }; - ////orderModel.Formula = new AssociationFormulaModel() { Id = formula.Id, Code = formula.Code, Name = formula.Name }; - //orderModel.PlanStartTime = orderItem.PlannedStartTime; - //orderModel.PlanFinishTime = orderItem.PlannedEndTime; - //orderModel.PlanQty = (ulong?)orderItem.OrderQty; - //orderModel.Status = OrderStatus.NotActive; - //orderModel.ExtraProperties["Source"] = "ddd"; - //var source = orderModel.ExtraProperties["Source"]; - //var orderModelResult = await orderManager.CreateAsync(orderModel); - //if (orderModelResult == null) + //var product = await productProvider.FindByNameAsync(orderItem.PipeSectionName); + //if (product == null) //{ - // throw new UserFriendlyException($"宸ュ崟[{orderItem.TaskCode}]鍒涘缓澶辫触"); + // throw new UserFriendlyException($"浜у搧鍚嶇О[{orderItem.PipeSectionName}]涓嶅瓨鍦�"); //} - //orderModels.Add(orderModelResult); + + //var formula = await formulaProvider.GetFormulaAsync(product.Id); + //if (formula == null) + //{ + // throw new UserFriendlyException($"浜у搧鍨嬪彿[{orderItem.MaterialCode}]鏃犲叧鑱旈厤鏂�"); + //} + //棣栧厛瑕佸垱寤� 鎵撶爜鍒囧壊鐨勫伐鍗� + var productForCut = await productProvider.FindByNameAsync("鍒囧壊鍘熸枡绠�"); + if (productForCut == null) + { + throw new UserFriendlyException($"浜у搧鍚嶇О[鍒囧壊鍘熸枡绠涓嶅瓨鍦�"); + } + var formulaForCut = await formulaProvider.GetFormulaAsync(productForCut.Id); + if (formulaForCut == null) + { + throw new UserFriendlyException($"浜у搧鍚嶇О[鍒囧壊鍘熸枡绠鏃犲叧鑱旈厤鏂�"); + } + //鎸夌収 绠℃缂栧彿 鍒嗙粍 + var group = gTask.ToList().GroupBy(x => x.PipeSpecCode); + foreach (var item in group) + { + var prodOrderNo = "Order_" + item.Key; + var order = await orderManager.GetByCodeAsync(prodOrderNo); + if (order != null) + { + throw new UserFriendlyException($"鐢熶骇宸ュ崟[{prodOrderNo}]宸插瓨鍦�"); + } + + OrderModel orderModelForCut = new OrderModel() + { + Id = Guid.NewGuid(), + Code = prodOrderNo, + Source = "APS鎺ㄩ��", + PlanStartTime = gTask.ToList().First().PlannedStartTime, + PlanFinishTime = gTask.ToList().First().PlannedEndTime, + PlanQty = 1, + Status = OrderStatus.NotActive, + Product = new AssociationProductModel() { Id = productForCut.Id, Name = productForCut.Name, Model = productForCut.Model, ShortNumber = productForCut.ShortNumber }, + Formula = new AssociationFormulaModel() { Id = formulaForCut.Id, Code = formulaForCut.Code, Name = formulaForCut.Name } + }; + orderModelForCut.ExtraProperties["OuterDiameter"] = gTask.ToList().First().OuterDiameter;//澶栧緞 + orderModelForCut.ExtraProperties["Material"] = gTask.ToList().First().Material;//鏉愯川 + orderModelForCut.ExtraProperties["Length"] = gTask.ToList().First().Length;//闀垮害 + orderModelForCut.ExtraProperties["DataIdentifier"] = gTask.ToList().First().DataIdentifier;//鍘熸枡鏍囪瘑 + orderModelForCut.ExtraProperties["MaterialMode"] = gTask.ToList().First().MaterialMode;//鍘熸枡绫诲瀷 + orderModelForCut.ExtraProperties["PipeFittingCode"] = gTask.ToList().First().PipeFittingCode;//绠℃缂栧彿 + + var orderForCut = await orderManager.GetByCodeAsync(orderModelForCut.Code); + if (orderForCut != null) + { + //throw new UserFriendlyException($"宸ュ崟[{orderModelForCut.Code}]宸插瓨鍦�"); + //涓嶅啀鎶涘紓甯革紝鐩存帴璺宠繃 + break; + } + + var orderModelResultForCut = await orderManager.CreateAsync(orderModelForCut); + if (orderModelResultForCut == null) + { + throw new UserFriendlyException($"宸ュ崟[{orderModelForCut.Code}]鍒涘缓澶辫触"); + } + orderModels.Add(orderModelResultForCut); + + + //OrderModel orderModel = new OrderModel(); + //orderModel.Id = Guid.NewGuid(); + //orderModel.Code = orderItem.TaskCode; + //orderModel.Source = "APS鎺ㄩ��"; + //orderModel.Product = new AssociationProductModel() { Id = product.Id, Name = product.Name, Model = product.Model, ShortNumber = product.ShortNumber }; + ////orderModel.Formula = new AssociationFormulaModel() { Id = formula.Id, Code = formula.Code, Name = formula.Name }; + //orderModel.PlanStartTime = orderItem.PlannedStartTime; + //orderModel.PlanFinishTime = orderItem.PlannedEndTime; + //orderModel.PlanQty = (ulong?)orderItem.OrderQty; + //orderModel.Status = OrderStatus.NotActive; + //orderModel.ExtraProperties["Source"] = "ddd"; + //var source = orderModel.ExtraProperties["Source"]; + //var orderModelResult = await orderManager.CreateAsync(orderModel); + //if (orderModelResult == null) + //{ + // throw new UserFriendlyException($"宸ュ崟[{orderItem.TaskCode}]鍒涘缓澶辫触"); + //} + //orderModels.Add(orderModelResult); + } + + + //淇濆瓨鍒� scms_callmaterialorders 琛ㄤ腑 + CallMaterialOrderCreateDto insertObjForOrder = new CallMaterialOrderCreateDto() + { + CallMaterialStatus = Domain.Shared.Enums.CallMaterialStatusEnum.鏈墽琛�, + DataIdentifier = gTask.Key, + MaterialMode = gTask.ToList().First().MaterialMode, + Quantity = 1 + }; + + await callMaterialOrderAppService.CreateAsync(insertObjForOrder); } + + + + //淇濆瓨鍒� scms_workplans 琛ㄤ腑 + foreach (var item in input) + { + var insertObj = ObjectMapper.Map<WorkPlanInput, WorkPlanCreateDto>(item); + //insertObj.OrgMaterialCode = "1111"; + insertObj.CallMaterialStatus = Domain.Shared.Enums.CallMaterialStatusEnum.鏈墽琛�; + insertObj.WorkPlanStatus = Domain.Shared.Enums.WorkPlanStatusEnum.鏈墽琛�; + insertObj.Sort = 1; + await workPlanAppService.CreateAsync(insertObj); + } + + #endregion + + await uow.CompleteAsync(); } - - - //淇濆瓨鍒拌〃涓� - foreach (var item in input) + catch (Exception) { - var insertObj = ObjectMapper.Map<WorkPlanInput, WorkPlanCreateDto>(item); - //insertObj.OrgMaterialCode = "1111"; - insertObj.Sort = 1; - await workPlanAppService.CreateAsync(insertObj); + + throw; } + finally + { + await uow.RollbackAsync(); + } + + + + #endregion + // 鍙戝竷浜嬩欢 @@ -150,4 +220,39 @@ return response; } + /// <summary> + /// 楠岃瘉鍘熸枡鏍囪瘑鐩稿悓鐨勮褰曚笉鑳藉瓨鍦ㄤ簬涓や釜涓嶅悓鐨勭娈电紪鍙蜂腑 + /// </summary> + /// <param name="inputs">浣滀笟璁″垝杈撳叆鍙傛暟鍒楄〃</param> + /// <returns>楠岃瘉缁撴灉锛屽寘鍚槸鍚﹂�氳繃楠岃瘉鍙婇敊璇俊鎭�</returns> + public static (bool isValid, string errorMessage) ValidateUniqueDataIdentifierPerPipeSection(List<WorkPlanInput> inputs) + { + if (inputs == null || !inputs.Any()) + { + return (true, string.Empty); + } + + // 浣跨敤Lookup鍒嗙粍锛岄敭涓哄師鏂欐爣璇嗭紝鍊间负瀵瑰簲鐨勭娈电紪鍙烽泦鍚� + var dataIdentifierGroups = inputs.ToLookup(x => x.DataIdentifier, x => x.PipeSpecCode); + + foreach (var group in dataIdentifierGroups) + { + // 蹇界暐绌虹殑鍘熸枡鏍囪瘑 + if (string.IsNullOrEmpty(group.Key)) + { + continue; + } + + // 鑾峰彇褰撳墠鍘熸枡鏍囪瘑瀵瑰簲鐨勫敮涓�绠℃缂栧彿闆嗗悎 + var uniquePipeSections = group.Distinct().ToList(); + + // 濡傛灉瀛樺湪澶氫釜涓嶅悓鐨勭娈电紪鍙凤紝鍒欒繚鍙嶈鍒� + if (uniquePipeSections.Count > 1) + { + return (false, $"鍘熸枡鏍囪瘑 '{group.Key}' 瀛樺湪浜庡涓笉鍚岀殑绠℃缂栧彿涓�: {string.Join(", ", uniquePipeSections)}"); + } + } + + return (true, string.Empty); + } } -- Gitblit v1.9.3