22
schangxiang@126.com
2025-05-20 5b189017d143be6366f56ffcdd3c3699a381e034
PipeLineLems/server/src/CMS.Plugin.PipeLineLems.Application/Implements/MesAppService.cs
@@ -1,158 +1,173 @@
using CMS.Plugin.FormulaManagement.Abstractions;
using CMS.Plugin.MesSuite.Abstractions.Events;
using CMS.Plugin.OrderManagement.Abstractions;
using CMS.Plugin.OrderManagement.Abstractions.Enums;
using CMS.Plugin.OrderManagement.Abstractions.Models;
using CMS.Plugin.OrderManagement.Abstractions;
using CMS.Plugin.PipeLineLems.Application.Contracts.Dtos.MyTestEntityNames;
using CMS.Plugin.PipeLineLems.Application.Contracts.Dtos.WorkPlan;
using CMS.Plugin.PipeLineLems.Application.Contracts.Services;
using CMS.Plugin.PipeLineLems.Domain.MyTestEntityNames;
using CMS.Plugin.PipeLineLems.Domain.Shared;
using CMS.Plugin.PipeLineLems.Domain.Shared.MyTestEntityNames;
using CMS.Plugin.PipeLineLems.Domain.WorkPlan;
using CMS.Plugin.ProductManagement.Abstractions;
using Microsoft.Extensions.DependencyInjection;
using Volo.Abp;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Data;
using Volo.Abp.ObjectExtending;
using CMS.Plugin.PipeLineLems.Application.Contracts.Dtos.WorkPlan;
using Volo.Abp.EventBus;
using Volo.Abp.Users;
namespace CMS.Plugin.PipeLineLems.Application.Implements;
public class MesAppService : IMesAppService
public class MesAppService : CMSPluginAppService,IMesAppService
{
    private readonly IServiceProvider _serviceProvider;
    private readonly WorkPlanAppService _workPlanAppService;
    private readonly SharedService _sharedService;
    //private readonly IEventBus _eventBus;
    public MesAppService(IServiceProvider serviceProvider
        , WorkPlanAppService workPlanAppService
        //, IEventBus eventBus
        , SharedService sharedService
        )
    {
        _serviceProvider = serviceProvider;
        // _eventBus = eventBus;
        _workPlanAppService = workPlanAppService;
        _sharedService = sharedService;
    }
    public async Task<MesOrderResponse> CreateAsync(List<WorkPlanInput> input)
    public async Task<MesOrderResponse> CreateAsync(List<WorkPlanInput> input, string userId, string userAccount)
    {
        if (input == null)
        {
            throw new UserFriendlyException("输入参数不能为空");
        }
        var response = await _sharedService.CommonCreatebyApsAsync(input, _serviceProvider, _workPlanAppService, userId, userAccount);
        if (input.Count == 0)
        {
            throw new UserFriendlyException("输入参数Data不能为空");
        }
        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)
        {
            var taskCode = gTask.Key;
            var order = await orderManager.GetByCodeAsync(taskCode);
            if (order != null)
            {
                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)
                //{
                //    throw new UserFriendlyException($"工单[{orderItem.TaskCode}]创建失败");
                //}
                //orderModels.Add(orderModelResult);
            }
        }
        // 发布事件
        //await _eventBus.PublishAsync(new EntityChangedEto("MESCREATE", input, null, EntityChangeType.Add, true));
        var response = new MesOrderResponse
        {
            Code = "000000",
            Data = orderModels,
            Fail = false,
            Mesg = "处理成功",
            Success = true,
            Time = DateTime.UtcNow
        };
        return response;
    }
    //public async Task<MesOrderResponse> CreateAsync(List<WorkPlanInput> input)
    //{
    //    if (input == null)
    //    {
    //        throw new UserFriendlyException("输入参数不能为空");
    //    }
    //    if (input.Count == 0)
    //    {
    //        throw new UserFriendlyException("输入参数Data不能为空");
    //    }
    //    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)
    //    {
    //        var taskCode = gTask.Key;
    //        var order = await orderManager.GetByCodeAsync(taskCode);
    //        if (order != null)
    //        {
    //            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)
    //            //{
    //            //    throw new UserFriendlyException($"工单[{orderItem.TaskCode}]创建失败");
    //            //}
    //            //orderModels.Add(orderModelResult);
    //        }
    //    }
    //    //保存到表中
    //    foreach (var item in input)
    //    {
    //        var insertObj = ObjectMapper.Map<WorkPlanInput, WorkPlanCreateDto>(item);
    //        //insertObj.OrgMaterialCode = "1111";
    //        insertObj.Sort = 1;
    //        await _workPlanAppService.CreateAsync(insertObj);
    //    }
    //    // 发布事件
    //    //await _eventBus.PublishAsync(new EntityChangedEto("MESCREATE", input, null, EntityChangeType.Add, true));
    //    var response = new MesOrderResponse
    //    {
    //        Code = "000000",
    //        Data = orderModels,
    //        Fail = false,
    //        Mesg = "处理成功",
    //        Success = true,
    //        Time = DateTime.UtcNow
    //    };
    //    return response;
    //}
}