using Aliyun.OSS; using RIZO.Model.MES.recipe; using RIZO.Model.MES.recipe.Dto; using Infrastructure.Attribute; using Mapster; using MDM.Controllers.Process; using MDM.Model; using MDM.Model.Material; using MDM.Model.Process; using MDM.Model.Process.Dto; using MDM.Models.Process; using MDM.Models.Process.Dto; using MDM.Repository; using MDM.Service; using MDM.Services.IProcessService; namespace MDM.Services.Process { /// /// 工序Service业务层处理 /// [AppService(ServiceType = typeof(IProcessOperationService), ServiceLifetime = LifeTime.Transient)] public class ProcessOperationService : BaseService, IProcessOperationService { /// /// 查询工序列表 /// /// /// public PagedInfo GetList(ProcessOperationQueryDto parm) { var predicate = Expressionable.Create() .AndIF(!string.IsNullOrEmpty(parm.FkRoutingCode), m => m.FkRoutingCode.Contains(parm.FkRoutingCode)) .AndIF(!string.IsNullOrEmpty(parm.OperationCode), m => m.OperationCode.Contains(parm.OperationCode)) .AndIF(!string.IsNullOrEmpty(parm.OperationName), m => m.OperationName.Contains(parm.OperationName)) .AndIF(!string.IsNullOrEmpty(parm.ParallelGroupCode), m => m.ParallelGroupCode.Contains(parm.ParallelGroupCode)) ; var response = Queryable() .Select(it => new ProcessOperation() { OperationId = it.OperationId, FkRoutingCode = it.FkRoutingCode, OperationCode = it.OperationCode, OperationName = it.OperationName, OperationSeq = it.OperationSeq, LastOperationSeq = it.LastOperationSeq, MaxStationCount = it.MaxStationCount, OperationType = it.OperationType, Description = it.Description, StandardTime = it.StandardTime, ControlStrategy = SqlFunc.Subqueryable().Where(s => s.StrategyCode == it.ControlStrategy).Select(s => s.StrategyName), IsSkippable = it.IsSkippable, IsReworkable = it.IsReworkable, IsParallel = it.IsParallel, ParallelGroupCode = it.ParallelGroupCode, DefaultNextOperationCode = it.DefaultNextOperationCode, Status = it.Status, CreatedBy = it.CreatedBy, CreatedTime = it.CreatedTime, UpdatedBy = it.UpdatedBy, UpdatedTime = it.UpdatedTime }, true) .Where(predicate.ToExpression()) .ToPage(parm); return response; } /// /// 获取详情,同时显示工序绑定的流程信息,配方信息 /// /// /// public ProcessOperationInfoDto GetInfo(int OperationId) { var response = Queryable() .Where(x => x.OperationId == OperationId) .Select(it => new ProcessOperationInfoDto() { OperationId = it.OperationId, FkRoutingCode = it.FkRoutingCode, OperationCode = it.OperationCode, OperationName = it.OperationName, OperationSeq = it.OperationSeq, LastOperationSeq = it.LastOperationSeq, MaxStationCount = it.MaxStationCount, OperationType = it.OperationType, Description = it.Description, StandardTime = it.StandardTime, ControlStrategy = it.ControlStrategy, IsSkippable = it.IsSkippable, IsReworkable = it.IsReworkable, IsParallel = it.IsParallel, ParallelGroupCode = it.ParallelGroupCode, DefaultNextOperationCode = it.DefaultNextOperationCode, Status = it.Status, CreatedBy = it.CreatedBy, CreatedTime = it.CreatedTime, UpdatedBy = it.UpdatedBy, UpdatedTime = it.UpdatedTime, //绑定的流程信息 OperationFlows = SqlFunc.Subqueryable().Where(s => s.FkRoutingCode == it.FkRoutingCode && s.FkOperationCode == it.OperationCode).ToList(), ////绑定的配方参数信息 //OperationRecipeParameters = SqlFunc.Subqueryable() //.LeftJoin((refpr, ver) => refpr.RecipeCode == ver.RecipeCode) //.LeftJoin((refpr, ver, param) => ver.RecipeCode == param.RecipeCode && ver.Version == param.Version) //.Where((refpr, ver, param) => refpr.FkRoutingCode == it.FkRoutingCode && refpr.FkOperationCode == it.OperationCode) //.ToList((refpr, ver, param) => new PfRecipeParametersDto //{ // Id = param.Id, // RecipeCode = param.RecipeCode, // Version = param.Version, // ParamName = param.ParamName, // Unit = param.Unit, // UpperLimit = param.UpperLimit, // LowerLimit = param.LowerLimit, // StandardValue = param.StandardValue, // Remark = param.Remark //}), ////绑定的采集参数 ProcessOperationCollectParameters = SqlFunc.Subqueryable().Where(s => s.FkRoutingCode == it.FkRoutingCode && s.FkOperationCode == it.OperationCode).ToList(), // 绑定的物料参数 ProcessOperationFlowMaterialParamters = SqlFunc.Subqueryable().Where(s => s.FkRoutingCode == it.FkRoutingCode && s.FkOperationCode == it.OperationCode).ToList(), }).First(); return response; } /// /// 添加工序 /// /// /// public ProcessOperation AddProcessOperation(ProcessOperation model) { return Context.Insertable(model).ExecuteReturnEntity(); } /// /// 修改工序 /// /// /// public int UpdateProcessOperation(ProcessOperation model) { //var response = Update(w => w.OperationId == model.OperationId, it => new ProcessOperation() //{ // FkRoutingCode = model.FkRoutingCode, // OperationName = model.OperationName, // OperationSeq = model.OperationSeq, // OperationType = model.OperationType, // Description = model.Description, // StandardTime = model.StandardTime, // ControlStrategy = model.ControlStrategy, // IsSkippable = model.IsSkippable, // IsReworkable = model.IsReworkable, // IsParallel = model.IsParallel, // ParallelGroupCode = model.ParallelGroupCode, // DefaultNextOperationCode = model.DefaultNextOperationCode, // Status = model.Status, // CreatedBy = model.CreatedBy, // CreatedTime = model.CreatedTime, // UpdatedBy = model.UpdatedBy, // UpdatedTime = model.UpdatedTime, //}); //return response; return Update(model, true); } public List SearchControlstrategyDict(ProcessControlStrategyDictQueryDto parm) { var predicate = Expressionable.Create() .AndIF(!string.IsNullOrEmpty(parm.StrategyName), m => m.StrategyName.Contains(parm.StrategyName)) .AndIF(!string.IsNullOrEmpty(parm.StrategyCode), m => m.StrategyCode.Contains(parm.StrategyCode)) ; var response = Context.Queryable() .Where(predicate.ToExpression()) .ToList() .Adapt, List>(); return response; } public List QueryProcessOprerationTransitionDict(ProcessOprerationTransitionDictQueryDto parm) { var predicate = Expressionable.Create() .AndIF(!string.IsNullOrEmpty(parm.TranstionName), m => m.TranstionName.Contains(parm.TranstionName)) .AndIF(!string.IsNullOrEmpty(parm.TransitionCode), m => m.TransitionCode.Contains(parm.TransitionCode)) ; var response = Context.Queryable() .Where(predicate.ToExpression()) .ToList(); return response; } public List GetFlow(string flow_type_code) { var response = Context.Queryable() .WhereIF(!string.IsNullOrEmpty(flow_type_code), m => m.FlowTypeCode.Contains(flow_type_code)) .ToList(); return response; } public int BindFlow(ProcessOperationBindFlowDto parm) { return 0; } public int OperationAddFlow(ProcessOperationFlowDto parm) { var model = parm.Adapt(); model.CreatedTime = DateTime.Now; var response = Context.Insertable(model).ExecuteCommand(); return response; } public int OperationDeleteFlow(int operation_flow_id) { var response = Context.Deleteable(m => m.id == operation_flow_id).ExecuteCommand(); return response; } public List SelectRouting(string routing) { var response = Context.Queryable().WhereIF(!string.IsNullOrEmpty(routing), it => it.RoutingCode.Contains(routing)).ToList(); return response; } public List GetOperationFlowList(string routing_code, string operation_code) { return Context.Queryable().Where(it => it.FkRoutingCode == routing_code && it.FkOperationCode == operation_code).ToList(); } public int OperationAddDatacollection(ProcessOperationCollectParameterDto parm) { var model = parm.Adapt(); model.CreatedTime = DateTime.Now; var response = Context.Insertable(model).ExecuteCommand(); return response; } public int OperationUpdateFlow(ProcessOperationFlowDto2 model) { var response = Context.Updateable() .SetColumns(it => new ProcessOperationFlow { FlowCode = model.FlowCode, FlowTypeCode = model.FlowTypeCode, FlowTypeName = model.FlowTypeName, UpdatedTime = DateTime.Now, }) .Where(it => it.id == model.id) .ExecuteCommand(); return response; } public int OperationDeleteDatacollection(int operation_datacollection_id) { var response = Context.Deleteable(m => m.Id == operation_datacollection_id).ExecuteCommand(); return response; } public List SearchMaterialInfo(string material_info) { var response = Context.Queryable() .WhereIF(!string.IsNullOrEmpty(material_info), m => m.Name.Contains(material_info) || m.Code.Contains(material_info)) .ToList(); return response; } public List SearchMaterialErrorproofDict(string error_proof_rule_code) { var response = Context.Queryable() .WhereIF(!string.IsNullOrEmpty(error_proof_rule_code), m => m.ErrorProofRuleCode.Contains(error_proof_rule_code)) .ToList(); return response; } public int OperationAddMaterialParamter(ProcessOperationFlowMaterialParamterDto parm) { var model = parm.Adapt(); model.CreatedTime = DateTime.Now; var response = Context.Insertable(model).ExecuteCommand(); return response; } public int OperationDeleteMaterialParamter(int operation_material_paramter_id) { var response = Context.Deleteable(m => m.id == operation_material_paramter_id).ExecuteCommand(); return response; } public PagedInfo QueryProcessOperationRecipe(ProcessOperationQuery2Dto query) { return Context.Queryable() .LeftJoin((refpr, ver) => refpr.RecipeCode == ver.RecipeCode) .LeftJoin((refpr, ver, param) => ver.RecipeCode == param.RecipeCode && ver.Version == param.Version) .Where((refpr, ver, param) => refpr.FkRoutingCode == query.FkRoutingCode && refpr.FkOperationCode == query.OperationCode) .Select((refpr, ver, param) => new PfRecipeParametersDto { Id = param.Id, RecipeCode = param.RecipeCode, Version = param.Version, ParamName = param.ParamName, Unit = param.Unit, UpperLimit = param.UpperLimit, LowerLimit = param.LowerLimit, StandardValue = param.StandardValue, Remark = param.Remark }).ToPage(query); } public int DeleteInfo(int[] idsArr) { try { // 查询要删除的主记录 var parms = Context.Queryable() .Where(it => idsArr.Contains(it.OperationId)) .ToList(); if (parms == null || !parms.Any()) { return 0; // 没有找到要删除的记录 } // 准备要删除的关联数据集合 var collectParamsToDelete = new List(); var workstationParamsToDelete = new List(); var materialParamsToDelete = new List(); foreach (var item in parms) { // 收集参数 - 使用 AddRange 而不是 Add var deletedParamList = Context.Queryable() .Where(it => it.FkOperationCode == item.OperationCode && it.FkRoutingCode == item.FkRoutingCode) .ToList(); collectParamsToDelete.AddRange(deletedParamList); // 工作站流程采集参数 var deletedStationParamList = Context.Queryable() .Where(it => it.FkOperationCode == item.OperationCode && it.FkRoutingCode == item.FkRoutingCode) .ToList(); workstationParamsToDelete.AddRange(deletedStationParamList); // 物料参数 var deletedFlowMaterialList = Context.Queryable() .Where(it => it.FkOperationCode == item.OperationCode && it.FkRoutingCode == item.FkRoutingCode) .ToList(); materialParamsToDelete.AddRange(deletedFlowMaterialList); } int deletedCount = 0; UseTran2(() => { // 先删除关联表数据(外键约束顺序) if (collectParamsToDelete.Any()) { deletedCount += Context.Deleteable(collectParamsToDelete).ExecuteCommand(); } if (workstationParamsToDelete.Any()) { deletedCount += Context.Deleteable(workstationParamsToDelete).ExecuteCommand(); } if (materialParamsToDelete.Any()) { deletedCount += Context.Deleteable(materialParamsToDelete).ExecuteCommand(); } // 最后删除主表数据 deletedCount += Context.Deleteable(parms).ExecuteCommand(); }); return deletedCount; } catch (Exception ex) { throw new Exception($"删除失败: {ex.Message}", ex); } } } }