using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Text; using System.Threading.Tasks; using Aspose.Cells.Drawing; using DbModels; using JNPF.Common.Core.Manager; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.Systems.Interfaces.System; using JNPF.VisualDev; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Tnb.BasicData.Entities; using Tnb.BasicData; using Tnb.EquipMgr.Entities; using Tnb.ProductionMgr.Entities; using Tnb.ProductionMgr.Entities.Dto.PrdManage; using Tnb.ProductionMgr.Entities.Dto; using Tnb.ProductionMgr.Interfaces; using JNPF.Common.Enums; using JNPF.Common.Extension; using JNPF.Common.Security; using JNPF.FriendlyException; using Tnb.ProductionMgr.Entities.Enums; using JNPF.VisualDev.Entitys.Dto.VisualDevModelData; using JNPF.VisualDev.Interfaces; using JNPF.VisualDev.Entitys; using Aop.Api.Domain; using Senparc.Weixin.MP.AdvancedAPIs.Card; using Aspose.Cells.Drawing.Texts; using JNPF.Systems.Entitys.Permission; using WebSocketSharp.Frame; using JNPF.Logging; using System.Dynamic; using Tnb.EquipMgr.Entities.Dto; using JNPF.Common.Filter; using JNPF.Systems.Entitys.System; using Newtonsoft.Json; using Tnb.BasicData.Entities.Dto; using NPOI.SS.Formula.Functions; namespace Tnb.ProductionMgr { [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] [OverideVisualDev(ModuleId)] public class PrdMoTaskService : IOverideVisualDevService, IPrdMoTaskService, IDynamicApiController, ITransient { private const string ModuleId = "25567924238373"; private const string MoModuleId = "25018860321301"; private readonly ISqlSugarRepository _repository; private readonly IUserManager _userManager; private readonly IDictionaryDataService _dictionaryDataService; private readonly IRunService _runService; private readonly IVisualDevService _visualDevService; private static Dictionary _dicDefect = new Dictionary(); private static Dictionary _dicWorkLine = new Dictionary(); private static Dictionary _dicProcess = new Dictionary(); private readonly ISqlSugarClient _db; public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc(); public PrdMoTaskService( ISqlSugarRepository repository, IUserManager userManager, IDictionaryDataService dictionaryDataService, IRunService runService, IVisualDevService visualDevService ) { _repository = repository; _userManager = userManager; _dictionaryDataService = dictionaryDataService; _runService = runService; _visualDevService = visualDevService; _db = _repository.AsSugarClient(); OverideFuncs.DeleteAsync = Delete; OverideFuncs.GetListAsync = GetList; } #region Get /// /// 根据产品ID获取模具列表 /// /// 产品ID /// /// ///
return results: ///
[ ///
{ ///
mold_code:模具编号 ///
mold_name:模具名称 ///
item_name:产品名称 ///
item_code:产品编号 ///
cavity_qty:模穴数 ///
} ///
] ///
[HttpGet("{materialId}")] public async Task GetMoldListByItemId(string materialId) { var result = new List(); result = await _db.Queryable() .InnerJoin((a, b) => a.mold_id == b.id) .InnerJoin((a, b, c) => a.material_id == c.id) .Where((a, b, c) => a.material_id == materialId) .Select((a, b, c) => new Tnb.ProductionMgr.Entities.Dto.MoldListOutput { mold_id = b.id, mold_code = b.mold_code, mold_name = b.mold_name, mold_type_code = b.mold_type_code, material_name = c.name, material_code = c.code, available_stations = SqlFunc.Subqueryable().Where(it => it.mold_id == a.id).Count(), }).ToListAsync(); return result; } /// /// 根据模具Id获取设备列表 /// /// /// [HttpGet("{moldId}")] public async Task GetEquipmentListByMoldId(string moldId) { var items = await _db.Queryable().InnerJoin((a, b) => a.equipment_id == b.id) .Where((a, b) => a.mold_id == moldId) .Select((a, b) => new Entities.Dto.EquipmentListOutput { eqp_id = b.id, eqp_code = b.code, eqp_type_code = SqlFunc.Subqueryable().Where(iit => iit.id == b.equip_type_id).Select(iit => iit.code), eqp_machine_num = b.eqp_machine_num, tonnage = b.tonnage, task_list_qty = SqlFunc.Subqueryable().Where(x => x.eqp_id == b.id).Count(), estimated_end_date = SqlFunc.Subqueryable().Where(x => x.eqp_id == b.id).OrderByDesc(o => o.plan_end_date).Select(x => x.plan_end_date) }).Mapper(x => { x.first_date = x.estimated_end_date.HasValue ? x.estimated_end_date.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""; }).ToListAsync(); return items; } /// /// 工单调整-生产任务重新排序 /// /// 设备ID /// 排序后生产任务列表 /// /// returns: ///
[ ///
{ ///
no:生产序号 ///
mo_id:工单编号 ///
group_flag:同组标识 ///
plan_qty:计划生产数量 ///
comple_qty:完成数量 ///
item_name:产品名称 ///
mold_code:模具编号 ///
} ///
] ///
[HttpGet("{eqpId}")] public async Task PrdTaskSort(string eqpId) { var taskStatusDic = await _dictionaryDataService.GetDicByTypeId(DictConst.PrdTaskStatusTypeId); var list = await _repository.AsSugarClient().Queryable() .LeftJoin((a, b) => a.mo_id == b.id) .LeftJoin((a, b, c) => a.material_id == c.id) .LeftJoin((a, b, c, d) => a.mold_id == d.id) .Where((a, b, c, d) => a.eqp_id == eqpId) .OrderBy((a, b, c, d) => a.estimated_start_date) .Select((a, b, c, d) => new PrdTaskSortOutput { mo_status_code = a.mo_task_code, status = a.mo_task_status, group_flag = a.group_flag, plan_qty = b.plan_qty, comple_qty = a.complete_qty, item_name = c.name, mold_code = d.mold_code, }) .ToListAsync(); var data = list.Select((x, idx) => new PrdTaskSortOutput { no = idx + 1, mo_status_code = x.mo_status_code, status = taskStatusDic.ContainsKey(x.status) ? taskStatusDic[x.status].ToString() : "", group_flag = x.group_flag, plan_qty = x.plan_qty, comple_qty = x.comple_qty, item_name = x.item_name, mold_code = x.mold_code, }) .ToList(); return data; } /// /// 查看生产任务操作记录 /// /// 任务ID /// [HttpGet("{taskId}")] public async Task GetMoOperRecord(string taskId) { var list = await _repository.AsSugarClient().Queryable().Where(it => it.mo_task_id == taskId).ToListAsync(); var data = list.Adapt>(); var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.PrdTaskStatusTypeId); _repository.AsSugarClient().ThenMapper(data, x => x.statusName = dic.ContainsKey(x.status) ? dic[x.status].ToString() : ""); return data; } /// /// 获取自检报废批次记录 /// /// 任务单Id /// [HttpGet("{moTaskId}")] public async Task GetScarpStatRecord(string moTaskId) { var output = new PrdMoTaskDefectOutput(); var db = _repository.AsSugarClient(); if (_dicDefect.Count < 1) { _dicDefect = await db.Queryable().ToDictionaryAsync(x => x.id, x => x.defect_type_name); } output.mo_task_code = (await db.Queryable().FirstAsync(it => it.id == moTaskId))?.mo_task_code; var defects = await db.Queryable().Where(it => it.mo_task_id == moTaskId).ToListAsync(); if (defects?.Count > 0) { output.batchItems = defects.GroupBy(g => new { g.batch }).Select(t => new BatchItem { scrap_qty = t.Sum(d => d.defective_item_qty), batch = t.Key.batch, create_time = t.Key.batch.ParseToDateTime().ToString("yyyy-MM-dd HH:mm:ss"), categoryItems = t.GroupBy(g => g.defective_cagetory_id).Select(c => new CategoryItem { name = _dicDefect[c.Key]?.ToString(), qty = c.Sum(d => d.defective_item_qty), defectItems = c.Select(d => new DefectItem { name = d.defective_item, qty = d.defective_item_qty, }).ToList(), }).ToList(), }).ToList(); } return output; } /// /// 根据工单Id获取任务单列表 /// /// /// /// output: ///
{ ///
mo_task_code:生产任务单号 ///
material_code:物料编码 ///
mold_code:模具编码 ///
mold_name:模具名称 ///
eqp_code:设备编码 ///
eqp_name:设备名称 ///
eqp_type_code:设备型号 ///
mo_task_qty:任务单数量 ///
mo_task_status:任务状态 ///
scheduled_qty:计划生产数量 ///
complete_qty:完成数量 ///
estimated_start_date:预计开工时间 ///
estimated_end_date:预计完工时间 ///
} ///
[HttpGet("{moId}")] public async Task GetPrdTaskInfoByMoId(string moId, int schedType = 1) { List result = new(); var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.PrdTaskStatusTypeId); if (schedType == 1) { result = await _db.Queryable().LeftJoin((a, b) => a.mo_id == b.id) .LeftJoin((a, b, c) => a.material_id == c.id) .LeftJoin((a, b, c, d) => a.mold_id == d.id) .LeftJoin((a, b, c, d, e) => a.eqp_id == e.id) .Where((a, b, c, d, e) => a.mo_id == moId) .Select((a, b, c, d, e) => new PrdMoTaskOutput { mo_task_code = a.mo_task_code, mo_task_status = a.mo_task_status, mold_code = d.mold_code, mold_name = d.mold_name, material_code = c.code, material_name = c.name, eqp_code = e.code, eqp_name = e.name, eqp_type_code = SqlFunc.Subqueryable().Where(it => it.id == e.equip_type_id).Select(it => it.code), mo_task_qty = SqlFunc.Subqueryable().Where(a => a.mo_id == moId).Count(), estimated_start_date = a.estimated_start_date.Value.ToString("yyyy-MM-dd HH"), estimated_end_date = a.estimated_end_date.Value.ToString("yyyy-MM-dd HH"), plan_qty = a.scheduled_qty, complete_qty = a.complete_qty, process_code = SqlFunc.Subqueryable().Where(it => it.id == a.process_id).Select(it => it.process_code), process_name = SqlFunc.Subqueryable().Where(it => it.id == a.process_id).Select(it => it.process_name), }) .Mapper(it => { it.mo_task_status = dic.ContainsKey(it.mo_task_status) ? dic[it.mo_task_status].ToString() : ""; }) .ToListAsync(); } else { result = await _db.Queryable().LeftJoin((a, b) => a.mo_id == b.id) .LeftJoin((a, b, c) => a.material_id == c.id) .LeftJoin((a, b, c, d) => a.workline_id == d.Id) .Where((a, b, c, d) => b.id == moId || b.parent_id == moId) .Select((a, b, c, d) => new PrdMoTaskOutput { mo_task_code = a.mo_task_code, mo_task_status = a.mo_task_status, workline_code = d.EnCode, workline_name = d.FullName, material_code = c.code, material_name = c.name, mo_task_qty = SqlFunc.Subqueryable().Where(a => a.mo_id == moId).Count(), estimated_start_date = a.estimated_start_date.Value.ToString("yyyy-MM-dd HH"), estimated_end_date = a.estimated_end_date.Value.ToString("yyyy-MM-dd HH"), plan_qty = b.scheduled_qty, complete_qty = a.complete_qty, }) .Mapper(it => { it.mo_task_status = dic.ContainsKey(it.mo_task_status) ? dic[it.mo_task_status].ToString()! : ""; }) .ToListAsync(); } return result; } /// ///组装、包装 获取待下发任务列表 /// /// [HttpGet] public async Task GetPackScheldToBeIssueList([FromQuery] PackScheldToBeIssueListInput input) { var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.PrdTaskStatusTypeId); var result = await _db.Queryable() .LeftJoin((a, b) => a.workline_id == b.Id) .LeftJoin((a, b, c) => a.mo_id == c.id) .LeftJoin((a, b, c, d) => a.bom_id == d.id) .WhereIF(!string.IsNullOrEmpty(input.mo_task_code), (a, b, c, d) => a.mo_task_code.Contains(input.mo_task_code)) .Where((a, b, c, d) => a.schedule_type == 2 && string.IsNullOrEmpty(a.parent_id)) .Select((a, b, c, d) => new PackSechelToBeIssueListOutput { mo_task_id = a.id, mo_task_code = a.mo_task_code, material_id = a.material_id, material_code = SqlFunc.Subqueryable().Where(it => it.id == a.material_id).Select(it => it.code), material_name = SqlFunc.Subqueryable().Where(it => it.id == a.material_id).Select(it => it.name), workline_id = a.workline_id, workline_code = b.EnCode, workline_name = b.FullName, mo_task_status = a.mo_task_status, scheduled_qty = SqlFunc.Subqueryable().Where(it => it.mo_id == a.mo_id).Sum(it => it.scheduled_qty), plan_qty = SqlFunc.Subqueryable().Where(it => it.id == a.mo_id).Select(it => it.plan_qty), estimated_start_date = a.estimated_start_date, estimated_end_date = a.estimated_end_date, bom_id = d.id, bom_version = d.version }) .Mapper(it => it.mo_task_status = dic.ContainsKey(it.mo_task_status) ? dic[it.mo_task_status].ToString()! : "") .ToListAsync(); return result; } /// /// 根据生产任务ID获取子任务列表 /// /// /// [HttpGet("{mo_task_id}")] public async Task GetSubMoTaskListByTaskId(string mo_task_id) { var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.PrdTaskStatusTypeId); var result = await _db.Queryable() .LeftJoin((a, b) => a.material_id == b.id) .LeftJoin((a, b, c) => a.workline_id == c.Id) .LeftJoin((a, b, c, d) => a.mo_id == d.id) .Where((a, b, c, d) => a.parent_id == mo_task_id) .Select((a, b, c, d) => new PackSechelToBeIssueListOutput { mo_task_id = a.id, mo_task_code = a.mo_task_code, material_code = b.code, material_name = b.name, workline_code = c.EnCode, workline_name = c.FullName, mo_task_status = a.mo_task_status, scheduled_qty = a.scheduled_qty, plan_qty = d.plan_qty, process_task_qty = a.process_task_qty, bom_version = SqlFunc.Subqueryable().Where(it => it.material_id == a.material_id).Select(it => it.version) }) .Mapper(it => it.mo_task_status = dic.ContainsKey(it.mo_task_status) ? dic[it.mo_task_status].ToString()! : "") .OrderBy(a => a.mo_task_code, OrderByType.Asc) .ToListAsync(); return result; } // /// // /// 获取组装、包装 待排产工单树形列表 // /// // /// // [HttpGet] // public async Task GetUnSchedulingList([FromQuery] VisualDevModelListQueryInput input) // { // List trees = new(); // VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(MoModuleId, true); // var data = await _runService.GetListResult(templateEntity, input); // // if (data?.list?.Count > 0) // { // var parentIdField = nameof(PrdMo.parent_id); // var nodes = data.list.Where(it => it.ContainsKey(parentIdField) && it[parentIdField].IsNullOrEmpty()).ToList(); // foreach (var row in nodes) // { // var pkName = "material_id_id"; // var dic = row.ToDictionary(x => x.Key, x => x.Value); // // PrdMoTreeOutput node = DictionaryToObject(row); // node.parentId = "0"; // node.mo_id = node.id; // if (dic.ContainsKey(pkName)) // { // var materialId = dic[pkName]?.ToString(); // var material = await _db.Queryable().FirstAsync(it => it.id == materialId); // node.material_id_id = materialId; // node.material_code = material?.code; // node.material_name = material?.name; // node.material_standard = material?.material_standard; // } // // // var scheldQtyCol = nameof(PrdMo.scheduled_qty); // // node.scheduled_qty = _db.Queryable().Where(it => it.mo_id == dic["id"].ToString()).Sum(it => it.scheduled_qty)?.ToString(); // var queryObj = new { parent_id = node.id }; // input.superQueryJson = ""; // input.queryJson = queryObj.ToJsonString(); // var subData = await _runService.GetListResult(templateEntity, input); // if (subData?.list?.Count > 0) // { // var childNodes = new List(); // foreach (var item in subData.list) // { // dic = item.ToDictionary(x => x.Key, x => x.Value); // PrdMoTreeOutput subNode = DictionaryToObject(item); // subNode.parentId = node.id; // subNode.mo_id = subNode.id; // if (dic.ContainsKey(pkName)) // { // var materialId = dic[pkName]?.ToString(); // var material = await _db.Queryable().FirstAsync(it => it.id == materialId); // subNode.material_id_id = materialId; // subNode.material_code = material?.code; // subNode.material_name = material?.name; // subNode.material_standard = material?.material_standard; // } // node.scheduled_qty = _db.Queryable().Where(it => it.mo_id == dic["id"].ToString()).Sum(it => it.scheduled_qty)?.ToString(); // childNodes.Add(subNode); // } // trees.AddRange(childNodes); // } // trees.Add(node); // } // } // var treeList = trees.ToTree(); // var list = treeList.Skip(input.currentPage - 1).Take(input.pageSize).ToList(); // SqlSugarPagedList pagedList = new() // { // list = treeList, // pagination = new Pagination // { // CurrentPage = input.currentPage, // PageSize = input.pageSize, // Total = treeList.Count // } // }; // return PageResult.SqlSugarPageResult(pagedList); // } /// /// 获取组装、包装 待排产工单树形列表 /// /// [HttpGet] public async Task GetUnSchedulingList([FromQuery] VisualDevModelListQueryInput input) { Dictionary queryJson = input.queryJson!=null ? JsonConvert.DeserializeObject>(input.queryJson) : null; List moStatusList = new List(); if (queryJson!=null && queryJson.ContainsKey("mo_stauts")) { moStatusList = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(queryJson["mo_status"])); } List moTypeList = new List(); if (queryJson!=null && queryJson.ContainsKey("mo_type")) { moTypeList = JsonConvert.DeserializeObject>(JsonConvert.SerializeObject(queryJson["mo_type"])); } string moCode = queryJson!=null && queryJson.ContainsKey("mo_code") ? queryJson["mo_code"].ToString() : ""; string combineMoCode = queryJson!=null && queryJson.ContainsKey("combine_mo_code") ? queryJson["combine_mo_code"].ToString() : ""; var db = _repository.AsSugarClient(); var result = await db.Queryable() .LeftJoin((a, b) => a.material_id == b.id) .LeftJoin((a, b, c) => a.mo_type == c.Id) .LeftJoin((a, b, c, d) => a.mo_status == d.Id) .LeftJoin((a, b, c, d, e) => a.create_id == e.Id) .WhereIF(moStatusList != null && moStatusList.Count > 0, (a, b, c, d, e) => moStatusList.Contains(a.mo_status)) .WhereIF(moTypeList != null && moTypeList.Count > 0, (a, b, c, d, e) => moTypeList.Contains(a.mo_type)) .WhereIF(!string.IsNullOrEmpty(moCode),(a,b,c,d,e)=>a.mo_code.Contains(moCode)) .WhereIF(!string.IsNullOrEmpty(combineMoCode),(a,b,c,d,e)=>a.combine_mo_code.Contains(combineMoCode)) .OrderByDescending((a,b,c,d,e)=>a.create_time) .Select((a, b, c, d, e) => new PrdMoTreeOutput { org_id = a.org_id, mo_id = a.id, mo_code = a.mo_code, material_code = b.code, material_name = b.name, material_standard = b.material_standard, mo_type = c.FullName, mo_status = d.FullName, plan_qty = a.plan_qty.Value.ToString(), input_qty = a.input_qty.Value.ToString(), complete_qty = a.complete_qty.Value.ToString(), scrap_qty = a.scrap_qty.Value.ToString(), plan_start_date = a.plan_start_date.Value.ToString("yyyy-MM-dd"), plan_end_date = a.plan_end_date.Value.ToString("yyyy-MM-dd"), is_create_dispatch = a.is_create_dispatch==0 ? "否" : "是", production_linecode = a.production_linecode, is_merge = a.is_merge==0 ? "否" : "是", combine_mo_code = a.combine_mo_code, time_stamp = a.time_stamp, create_id = e.RealName, create_time = a.create_time.Value.ToString("yyyy-MM-dd : HH:mm:ss"), material_id = b.code, material_id_id = a.material_id, scheduled_qty = a.scheduled_qty.Value.ToString(), parent_id = "0", id = a.id, hasChildren = false, num = 0, isLeaf = true, }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } #endregion #region Post /// /// 生产工单-生产排产 /// /// ///
{ ///
Id:生产任务主键Id ///
MoType:工单类型 1、注塑/挤出 2、组装/包装 ///
MoId:工单Id ///
ItemId:产品编号 ///
ItemName:产品名称 ///
MoldId:模具Id ///
MoldName:模具名称 ///
EqpId:设备Id ///
EqpName:设备名称 ///
LineId:产线编号 ///
LineName:产线名称 ///
} /// /// [HttpPost] public async Task ProductionScheduling(ProductionSchedulingCrInput input) { var db = _repository.AsSugarClient(); var row = -1; if (input.schedule_type.HasValue) { if (input.schedule_type.Value == 1) //注塑、基础排产 { var moTask = input.Adapt(); moTask.id = SnowflakeIdHelper.NextId(); moTask.create_id = _userManager.UserId; moTask.create_time = DateTime.Now; moTask.mo_task_status = DictConst.ToBeScheduledEncode; moTask.scheduled_qty = input.scheduled_qty; var mo = await db.Queryable().FirstAsync(it => it.id == input.mo_id); var moCode = mo?.mo_code; var taskCode = await db.Queryable().Where(it => !string.IsNullOrEmpty(it.mo_task_code) && it.mo_task_code.Contains(moCode)).OrderByDescending(it => it.mo_task_code).Select(it => it.mo_task_code).FirstAsync(); if (taskCode.IsNullOrEmpty()) { moTask.mo_task_code = $"{moCode}-01"; } else { var pos = taskCode.IndexOf("-", StringComparison.Ordinal); if (pos > -1) { var num = taskCode.AsSpan().Slice(pos + 1).ToString().ParseToInt(); var code = taskCode.AsSpan().Slice(0, pos).ToString(); var n = (num + 1).ToString().PadLeft(2, '0'); moTask.mo_task_code = $"{code}-{n}"; } } try { await db.Ado.BeginTranAsync(); row = await db.Insertable(moTask).ExecuteCommandAsync(); //根据工单号获取当前工单包含的已排产数 var schedQty = db.Queryable().Where(it => it.mo_id == input.mo_id)?.Sum(d => d.scheduled_qty); if (mo != null) {//判断如果当前 工单的已排产数大于工单计划数量则更新工单状态为 已排产 if (schedQty.HasValue && schedQty.Value >= mo.plan_qty) { mo.mo_status = DictConst.AlreadyId; } else { if (schedQty.HasValue) mo.scheduled_qty = schedQty.Value; } row = await db.Updateable(mo).ExecuteCommandAsync(); } var material = await db.Queryable().FirstAsync(it => it.id == moTask.material_id); var taskLog = new PrdTaskLog(); taskLog.id = SnowflakeIdHelper.NextId(); taskLog.mo_code = (await db.Queryable().FirstAsync(it => it.id == input.mo_id))?.mo_code!; taskLog.eqp_code = (await db.Queryable().FirstAsync(it => it.id == input.eqp_id))?.code!; taskLog.mold_code = (await db.Queryable().FirstAsync(it => it.id == input.mold_id))?.mold_code!; taskLog.item_code = material?.code!; taskLog.item_standard = material?.material_standard!; taskLog.status = DictConst.ToBeScheduledEncode; taskLog.operator_name = _userManager.RealName; taskLog.create_id = _userManager.UserId; taskLog.create_time = DateTime.Now; taskLog.mo_task_id = moTask.id; taskLog.mo_task_code = moTask.mo_task_code; await db.Insertable(taskLog).ExecuteCommandAsync(); //将生产任务插入到自检报废记录表 //var sacipRecord = new PrdMoTaskDefectRecord(); //sacipRecord.id = SnowflakeIdHelper.NextId(); //sacipRecord.material_code = material?.code!; //sacipRecord.material_name = material?.name!; //sacipRecord.eqp_code = (await db.Queryable().FirstAsync(it => it.id == moTask.eqp_id))?.code!; //sacipRecord.mold_name = (await db.Queryable().FirstAsync(it => it.id == moTask.mold_id))?.mold_name!; //sacipRecord.estimated_start_date = moTask.plan_start_date; //sacipRecord.estimated_end_date = moTask.plan_end_date; //sacipRecord.plan_qty = moTask.plan_qty; //sacipRecord.scrap_qty = moTask.scrap_qty; //sacipRecord.status = moTask.mo_task_status; //sacipRecord.create_id = _userManager.UserId; //sacipRecord.create_time = DateTime.Now; //sacipRecord.mo_task_id = moTask.id; //sacipRecord.mo_task_code = moTask.mo_task_code; //await db.Insertable(sacipRecord).ExecuteCommandAsync(); await db.Ado.CommitTranAsync(); } catch (Exception ex) { await db.Ado.RollbackTranAsync(); } } else if (input.schedule_type.Value == 2) //组装、包装排产 { } } return row > 0; } /// /// 组装包装排产 /// /// /// /// [HttpPost] public async Task PackSchedling(PackSchedlingCrInput input) { var row = -1; try { await _db.Ado.BeginTranAsync(); var moTask = input.Adapt(); moTask.id = SnowflakeIdHelper.NextId(); moTask.create_id = _userManager.UserId; moTask.bom_id = input.bom_id; moTask.workroute_id = (await _db.Queryable().FirstAsync(it => it.id == input.bom_id))?.route_id; moTask.workline_id = input.workline_id; moTask.create_time = DateTime.Now; moTask.schedule_type = 2; moTask.mo_task_status = DictConst.ToBeScheduledEncode; moTask.estimated_start_date = input.estimated_start_date; moTask.estimated_end_date = input.estimated_end_date; moTask.scheduled_qty = input.scheduled_qty; var mo = await _db.Queryable().FirstAsync(it => it.id == input.mo_id); var moCode = mo?.mo_code; var taskCode = await _db.Queryable().Where(it => string.IsNullOrEmpty(it.parent_id) && !string.IsNullOrEmpty(it.mo_task_code) && it.mo_task_code.Contains(moCode)) .OrderByDescending(it => it.mo_task_code) .Select(it => it.mo_task_code) .FirstAsync(); if (taskCode is null || taskCode.IsNullOrEmpty()) { moTask.mo_task_code = $"{moCode}-01"; } else { var pos = taskCode?.IndexOf("-", StringComparison.Ordinal); if (pos.HasValue && pos.Value > -1) { var num = taskCode.AsSpan().Slice(pos.Value + 1).ToString().ParseToInt(); var code = taskCode.AsSpan().Slice(0, pos.Value).ToString(); var n = (num + 1).ToString().PadLeft(2, '0'); moTask.mo_task_code = $"{code}-{n}"; } } row = await _db.Insertable(moTask).ExecuteCommandAsync(); var material_h = await _db.Queryable().FirstAsync(it => it.id == moTask.material_id); //添加生产任务操作记录日志 var taskLog = new PrdTaskLog(); taskLog.id = SnowflakeIdHelper.NextId(); taskLog.mo_code = (await _db.Queryable().FirstAsync(it => it.id == input.mo_id))?.mo_code!; taskLog.item_code = material_h?.code!; taskLog.item_standard = material_h?.material_standard!; taskLog.status = DictConst.ToBeScheduledEncode; taskLog.operator_name = _userManager.RealName; taskLog.create_id = _userManager.UserId; taskLog.create_time = DateTime.Now; taskLog.mo_task_id = moTask.id; taskLog.mo_task_code = moTask.mo_task_code!; row = await _db.Insertable(taskLog).ExecuteCommandAsync(); //将生产任务插入到自检报废记录表 //var sacipRecord = new PrdMoTaskDefectRecord(); //sacipRecord.id = SnowflakeIdHelper.NextId(); //sacipRecord.material_code = material?.code!; //sacipRecord.material_name = material?.name!; //sacipRecord.eqp_code = (await db.Queryable().FirstAsync(it => it.id == moTask.eqp_id))?.code!; //sacipRecord.mold_name = (await db.Queryable().FirstAsync(it => it.id == moTask.mold_id))?.mold_name!; //sacipRecord.estimated_start_date = moTask.plan_start_date; //sacipRecord.estimated_end_date = moTask.plan_end_date; //sacipRecord.plan_qty = moTask.plan_qty; //sacipRecord.scrap_qty = moTask.scrap_qty; //sacipRecord.status = moTask.mo_task_status; //sacipRecord.create_id = _userManager.UserId; //sacipRecord.create_time = DateTime.Now; //sacipRecord.mo_task_id = moTask.id; //sacipRecord.mo_task_code = moTask.mo_task_code; //await db.Insertable(sacipRecord).ExecuteCommandAsync(); //根据工单号获取当前工单包含的已排产数 var schedQty = _db.Queryable().Where(it => it.mo_id == input.mo_id)?.Sum(d => d.scheduled_qty); // if (mo != null) // {//判断如果当前 工单的已排产数大于工单计划数量则更新工单状态为 已排产 // if (schedQty.HasValue && schedQty.Value >= mo.plan_qty) // { // mo.mo_status = DictConst.AlreadyId; // await _db.Updateable(mo).ExecuteCommandAsync(); // } // else // { // if (schedQty.HasValue) // { // mo.scheduled_qty += schedQty.Value; // await _db.Updateable(mo).ExecuteCommandAsync(); // } // } // } if (mo != null) { if (mo.scheduled_qty == null) { await _db.Updateable() .SetColumns(x => x.mo_status == DictConst.AlreadyId) .SetColumns(x => x.scheduled_qty == input.scheduled_qty) .Where(x=>x.id==mo.id) .ExecuteCommandAsync(); } else { await _db.Updateable().SetColumns(x => x.scheduled_qty == x.scheduled_qty + input.scheduled_qty) .Where(x=>x.id==mo.id) .ExecuteCommandAsync(); } } var subTaskList = await _db.Queryable() .LeftJoin((a, b) => a.id == b.mbom_id) .LeftJoin((a, b, c) => a.route_id == c.id) .LeftJoin((a, b, c, d) => b.process_id == d.process_id && c.id == d.route_id) .LeftJoin((a, b, c, d, e) => a.id == e.mbom_id && e.mbom_process_id == b.id) .Where((a, b, c, d, e) => a.id == input.bom_id) .Select((a, b, c, d, e) => new SubBomListOutput { version = a.version, unit_id = a.unit_id, route_id = c.id, process_id = b.process_id, material_id = SqlFunc.Subqueryable().Where(it => it.id == e.material_id).Select(it => it.id), num = e.num, ordinal = d.ordinal, }) .Mapper(it => it.output_qty = it.num.ParseToInt()) .ToListAsync(); if (subTaskList?.Count > 0) { List subMoTasks = new(); foreach (var item in subTaskList) { PrdMoTask subMoTask = new(); subMoTask.mo_id = input.mo_id; subMoTask.material_id = item.material_id; subMoTask.schedule_type = 2; subMoTask.parent_id = moTask.id; subMoTask.bom_id = input.bom_id; subMoTask.process_id = item.process_id; subMoTask.mo_task_status = DictConst.ToBeScheduledEncode; subMoTask.workroute_id = item.route_id; subMoTask.workline_id = input.workline_id; subMoTask.material_id = item.material_id; subMoTask.scheduled_qty = input.scheduled_qty; subMoTask.process_task_qty = item.output_qty * moTask.scheduled_qty; subMoTask.estimated_start_date = input.estimated_start_date; subMoTask.estimated_end_date = input.estimated_end_date; subMoTask.create_id = _userManager.UserId; subMoTask.create_time = DateTime.Now; subMoTasks.Add(subMoTask); } //根据生产任务编号生成子任务编号 if (moTask.mo_task_code!.IsNotEmptyOrNull()) { for (int i = 1; i <= subMoTasks.Count; i++) { string n = i.ToString(); subMoTasks[i - 1].mo_task_code = $"{moTask.mo_task_code}-{n.PadLeft(2, '0')}"; } } row = await _db.Insertable(subMoTasks).ExecuteCommandAsync(); } await _db.Ado.CommitTranAsync(); } catch (Exception ex) { JNPF.Logging.Log.Error("组装包装排产时报错", ex); await _db.Ado.RollbackTranAsync(); } return row > 0; } /// /// 生产任务下发,开始 、结束、完成 /// /// 输入参数 /// /// /// [HttpPost] public async Task PrdTaskRelease(PrdTaskReleaseUpInput input) { var row = -1; if (input is null) { throw new ArgumentNullException(nameof(input)); } if (input.TaskIds is null) { throw new ArgumentNullException(nameof(input.TaskIds)); } if (input.Behavior.IsNullOrWhiteSpace()) { throw new ArgumentException($"{nameof(input.Behavior)} not be null or empty"); } //var taskList = await _db.Queryable().Where(it => input.TaskIds.Contains(it.id) && it.mo_task_status == DictConst.ToBeScheduledEncode).ToListAsync(); //if (taskList?.Count > 0) { string SetTaskStatus(PrdTaskBehavior behavior) => behavior switch { PrdTaskBehavior.Release => DictConst.ToBeStartedEnCode, PrdTaskBehavior.Start => DictConst.InProgressEnCode, PrdTaskBehavior.Closed => DictConst.ClosedEnCode, PrdTaskBehavior.Compled => DictConst.ComplatedEnCode, _ => throw new NotImplementedException(), }; PrdTaskBehavior behavior = input.Behavior.ToEnum(); var status = SetTaskStatus(behavior); var db = _repository.AsSugarClient(); var list = await db.Queryable().Where(it => input.TaskIds.Contains(it.id)).Select(it => it).ToListAsync(); if (behavior == PrdTaskBehavior.Compled) { if (list?.Count > 0) { var schedQtySum = list.Sum(x => x.scheduled_qty); var planQtySum = list.Sum(x => x.plan_qty); if (schedQtySum < planQtySum) { throw new AppFriendlyException("任务数量必须大于等于生产计划数量,才可完成", 500); } } } var taskReportLogs = new List(); var prdTaskList = await db.Queryable().Where(it => input.TaskIds.Contains(it.id)).ToListAsync(); if (prdTaskList?.Count > 0) { prdTaskList.ForEach(x => x.mo_task_status = status); row = await db.Updateable(prdTaskList).ExecuteCommandAsync(); if (row > 0) { taskReportLogs.AddRange(prdTaskList); } } if (row > 0) { //更新子任务 var subMoTaskList = await db.Queryable().Where(it => input.TaskIds.Contains(it.parent_id)).ToListAsync(); if (subMoTaskList?.Count > 0) { var subTaskIds = subMoTaskList.Select(it => it.id).ToList(); row = await db.Updateable() .SetColumns(it => new PrdMoTask { mo_task_status = status }) .Where(it => subTaskIds.Contains(it.id)) .ExecuteCommandAsync(); if (row > 0) { taskReportLogs.AddRange(subMoTaskList); } } } //插入操作记录日志 var prdMOTasks = await _db.Queryable().Where(it => input.TaskIds.Contains(it.id) && string.IsNullOrEmpty(it.parent_id)).ToListAsync(); if (prdMOTasks?.Count > 0) { List taskLogEntities = new(); foreach (var taskId in input.TaskIds) { var taskLog = await db.Queryable().FirstAsync(it => it.mo_task_id == taskId); if (taskLog is null) { var taskItem = list?.Find(x => x.id == taskId); taskLog = new PrdTaskLog(); taskLog.id = SnowflakeIdHelper.NextId(); if (taskItem != null) { if (taskItem.mo_id!.IsNotEmptyOrNull()) { taskLog.mo_code = (await db.Queryable().FirstAsync(it => it.id == taskItem.mo_id))?.mo_code!; } if (taskItem.eqp_id!.IsNotEmptyOrNull()) { taskLog.eqp_code = (await db.Queryable().FirstAsync(it => it.id == taskItem.eqp_id))?.code!; } if (taskItem.mold_id!.IsNotEmptyOrNull()) { taskLog.mold_code = (await db.Queryable().FirstAsync(it => it.id == taskItem.mold_id))?.mold_code!; } if (taskItem.material_id!.IsNotEmptyOrNull()) { var material = await db.Queryable().FirstAsync(it => it.id == taskItem.material_id); taskLog.item_code = material?.code!; taskLog.item_standard = material?.material_standard!; } taskLog.operator_name = _userManager.RealName; taskLog.status = status; taskLog.create_id = _userManager.UserId; taskLog.create_time = DateTime.Now; taskLog.mo_task_id = taskItem.id; } taskLogEntities.Add(taskLog); } else { var records = await db.Queryable().Where(it => it.mo_task_id == taskLog.mo_task_id).ToListAsync(); if (records != null && !records.Select(x => x.status).Contains(status)) { taskLog.id = SnowflakeIdHelper.NextId(); taskLog.status = status; taskLogEntities.Add(taskLog); } } } if (taskLogEntities?.Count > 0) { row = await db.Insertable(taskLogEntities).ExecuteCommandAsync(); } List prdReportLogs = new(); List prdTaskDefectLogs = new(); if (taskReportLogs?.Count > 0) { foreach (var taskInfo in taskReportLogs) { //组装生产提报对象 var material = (await db.Queryable().FirstAsync(it => it.id == taskInfo.material_id)); var mo = await db.Queryable().FirstAsync(it => it.id == taskInfo.mo_id); var record = taskInfo.Adapt(); record.id = SnowflakeIdHelper.NextId(); record.masterial_code = material?.code; record.masterial_name = material?.name; record.plan_start_date = taskInfo.estimated_start_date; record.plan_end_date = taskInfo.estimated_end_date; record.plan_qty = taskInfo.plan_qty; record.eqp_code = (await db.Queryable().FirstAsync(it => it.id == taskInfo.eqp_id))?.code; record.mo_task_type = mo?.mo_type; record.status = taskInfo.mo_task_status; record.mo_task_id = taskInfo.id; //record.completed_qty = (await db.Queryable().Where(it => it.mo_task_code == taskInfo.mo_task_code).SumAsync(it => it.reported_work_qty)).Value; prdReportLogs.Add(record); //组装自检报废对象 var sacipRecord = new PrdMoTaskDefectRecord(); sacipRecord.id = SnowflakeIdHelper.NextId(); sacipRecord.material_code = material?.code!; sacipRecord.material_name = material?.name!; sacipRecord.eqp_code = (await db.Queryable().FirstAsync(it => it.id == taskInfo.eqp_id))?.code!; sacipRecord.mold_name = (await db.Queryable().FirstAsync(it => it.id == taskInfo.mold_id))?.mold_name!; sacipRecord.estimated_start_date = taskInfo.estimated_start_date; sacipRecord.estimated_end_date = taskInfo.estimated_end_date; sacipRecord.plan_qty = taskInfo.plan_qty; sacipRecord.scrap_qty = taskInfo.scrap_qty.HasValue ? taskInfo.scrap_qty.Value : 0; sacipRecord.status = taskInfo.mo_task_status; sacipRecord.create_id = _userManager.UserId; sacipRecord.create_time = DateTime.Now; sacipRecord.mo_task_id = taskInfo.id; sacipRecord.mo_task_code = taskInfo.mo_task_code; sacipRecord.mo_task_type = mo?.mo_type; sacipRecord.status = taskInfo.mo_task_status; prdTaskDefectLogs.Add(sacipRecord); } } var reportTaskIds = prdReportLogs.Select(it => it.mo_task_id).ToList(); if (reportTaskIds?.Count > 0) { var items = await db.Queryable().Where(it => reportTaskIds.Contains(it.mo_task_id)).ToListAsync(); if (items == null || items.Count < 1) { row = await db.Insertable(prdReportLogs).ExecuteCommandAsync(); } } var defectTaskIds = prdTaskDefectLogs.Select(it => it.mo_task_id).ToList(); if (defectTaskIds?.Count > 0) { var items = await db.Queryable().Where(it => defectTaskIds.Contains(it.mo_task_id)).ToListAsync(); if (items == null || items.Count < 1) { row = await db.Insertable(prdTaskDefectLogs).ExecuteCommandAsync(); } } } } //else // throw new AppFriendlyException("只有待下发状态的任务才可下发", 500); return (row > 0); } /// /// 生产任务单修改 /// /// 生产任务单修改输入参数 /// /// [HttpPost] public async Task IcmoModify(IcmoUpInput input) { var row = -1; var db = _repository.AsSugarClient(); if (input.icmo_id.IsNullOrWhiteSpace()) throw new ArgumentNullException(nameof(input.icmo_id)); var icmoItem = await db.Queryable().FirstAsync(it => it.id == input.icmo_id); switch (input.category) { case 1: //设备 if (icmoItem != null) { var eqpItem = await db.Queryable().FirstAsync(it => it.id == icmoItem.eqp_id); icmoItem.eqp_id = eqpItem.id; icmoItem.eqp_type_code = db.Queryable().First(it => it.id == eqpItem.equip_type_id)?.code!; //if (input.scheduled_qty > icmoItem.plan_qty) //{ // throw new AppFriendlyException("任务单数量不能大于计划数量", 500); //} //row = await db.Updateable().SetColumns(it => new PrdMo { input_qty = input.scheduled_qty }).Where(it => it.id == input.mo_id).ExecuteCommandAsync(); } break; case 2: //模具 if (icmoItem != null) { var moldItem = await db.Queryable().FirstAsync(it => it.id == icmoItem.mold_id); icmoItem.mold_id = moldItem.id; icmoItem.mold_code = moldItem.mold_code; icmoItem.mold_name = moldItem.mold_name; icmoItem.mold_cavity_qty = moldItem.cavity_qty; } break; } row = await db.Updateable(icmoItem).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync(); return (row > 0); } // /// // /// 生产提报 // /// // /// // /// // /// input: // ///
{ // ///
icmo_id:生产任务ID // ///
icmo_code:任务单号 // ///
prd_qty:生产数量 // ///
reported_work_qty:已报工数量 // ///
reported_qty:提报数量 // ///
icmo_qty:生产任务量 // ///
} // ///
// [HttpPost] // public async Task PrdReport(PrdReportCrInput input) // { // var row = -1; // var db = _repository.AsSugarClient(); // var report = await db.Queryable().FirstAsync(it => it.mo_task_id == input.mo_task_id); // // //if (report is not null) // //{ // // report.mo_task_code = input.mo_task_code; // // report.mo_task_id = input.mo_task_id; // // report.reported_work_qty += input.reported_qty; // // report.prd_qty += input.reported_qty; // //} // //else // { // report = input.Adapt(); // report.id = SnowflakeIdHelper.NextId(); // report.reported_qty = input.reported_qty; // report.create_id = _userManager.UserId; // report.create_time = DateTime.Now; // // } // row = await db.Insertable(report).ExecuteCommandAsync(); // var master = await db.Queryable().FirstAsync(it => it.mo_task_id == input.mo_task_id); // if (master != null) // { // master.reported_work_qty += input.reported_qty; // master.completed_qty += input.reported_qty; // await db.Updateable(master).ExecuteCommandAsync(); // } // return row > 0; // } /// /// 生产提报 /// /// /// /// input: ///
{ ///
icmo_id:生产任务ID ///
icmo_code:任务单号 ///
prd_qty:生产数量 ///
reported_work_qty:已报工数量 ///
reported_qty:提报数量 ///
icmo_qty:生产任务量 ///
} ///
[HttpPost] public async Task PrdReport(PrdReportCrInput input) { var db = _repository.AsSugarClient(); DbResult result = await _repository.AsSugarClient().Ado.UseTranAsync(async () => { var row = -1; var report = await db.Queryable().FirstAsync(it => it.mo_task_id == input.mo_task_id); report = input.Adapt(); report.id = SnowflakeIdHelper.NextId(); report.reported_qty = input.reported_qty; report.create_id = _userManager.UserId; report.create_time = DateTime.Now; row = await db.Insertable(report).ExecuteCommandAsync(); var prdMoTask = await db.Queryable().SingleAsync(x => x.id == input.mo_task_id); if (prdMoTask.reported_work_qty == null) { await db.Updateable() // .SetColumns(x => x.complete_qty == x.complete_qty + input.reported_qty) .SetColumns(x => x.reported_work_qty == input.reported_qty) .Where(x => x.id == input.mo_task_id).ExecuteCommandAsync(); } else { await db.Updateable() // .SetColumns(x => x.complete_qty == x.complete_qty + input.reported_qty) .SetColumns(x => x.reported_work_qty == x.reported_work_qty + input.reported_qty) .Where(x => x.id == input.mo_task_id).ExecuteCommandAsync(); } var master = await db.Queryable().FirstAsync(it => it.mo_task_id == input.mo_task_id); if (master != null) { master.reported_work_qty += input.reported_qty; master.completed_qty += input.reported_qty; await db.Updateable(master).ExecuteCommandAsync(); } }); return result.IsSuccess; } /// /// 自检报废提交 /// /// 自检报废输入参数 /// true/false /// /// input: ///
{ ///
"icmo_code": 任务单号, ///
"scrap_qty": 报废数量, ///
"remark": 备注, ///
"categoryItems": [ ///
{ ///
"defective_category":次品分类, ///
"category_qty": 分类数量, ///
"items": [ ///
{ ///
"defective_item": 次品项, ///
"defective_item_qty": 次品项数量 ///
} ///
] ///
} ///
] ///
} ///
[HttpPost] public async Task SelfTestScrapped(SelfTestScrappedInput input) { var db = _repository.AsSugarClient(); var result = await db.Ado.UseTranAsync(async () => { List destDefects = new(); var batch = DateTime.Now.ToString("yyyyMMddHHmmss"); foreach (var categoryItem in input.categoryItems) { foreach (var dItem in categoryItem.items) { var defect = new PrdMoTaskDefect(); defect.id = SnowflakeIdHelper.NextId(); defect.mo_task_id = input.mo_task_id; defect.batch = batch; defect.defective_cagetory_id = categoryItem.category_id; defect.defective_item = dItem.defective_item; defect.defective_item_qty = dItem.defective_item_qty; defect.create_id = _userManager.UserId; defect.scrap_qty = input.scrap_qty; destDefects.Add(defect); } } await db.Insertable(destDefects).ExecuteCommandAsync(); var scrapQty = db.Queryable().Where(it => it.mo_task_id == input.mo_task_id)?.Sum(d => d.defective_item_qty); if (scrapQty.HasValue && scrapQty.Value > 0)//更新生产任务表报废数量 { await db.Updateable().SetColumns(it => new PrdMoTask { scrap_qty = scrapQty.Value }).Where(it => it.id == input.mo_task_id).ExecuteCommandAsync(); } var defectRecord = await db.Queryable().FirstAsync(it => it.mo_task_id == input.mo_task_id); if (defectRecord != null) { defectRecord.scrap_qty += input.scrap_qty; await db.Updateable(defectRecord).ExecuteCommandAsync(); } var reportMaster = await db.Queryable().FirstAsync(it => it.mo_task_id == input.mo_task_id); if (reportMaster != null) { reportMaster.completed_qty += input.scrap_qty; await db.Updateable(reportMaster).ExecuteCommandAsync(); } }); return result.IsSuccess; } /// /// 修改组装、包装,生产任务信息 /// /// /// [HttpPost] public async Task UpdatePackMoTask(PackPrdTaskUpInput input) { if (input == null) throw new ArgumentNullException("input"); var row = await _db.Updateable().SetColumns(it => new PrdMoTask { workline_id = input.workline_id, bom_id = input.bom_id, estimated_start_date = input.estimated_start_date, estimated_end_date = input.estimated_end_date, scheduled_qty = input.scheduled_qty, }) .Where(it => it.id == input.mo_task_id).ExecuteCommandAsync(); return row > 0; } /// /// 生成子工单 /// /// [HttpPost] public async Task GenSubMo(GenSubMoCrInput input) { if (input is null) throw new ArgumentNullException("input"); if (input.ids is null || input.ids.Count == 0) throw new ArgumentException($"{nameof(input.ids)} not be null or count zero"); var curMo = await _db.Queryable().FirstAsync(it => it.id == input.mo_id); if (curMo == null) throw new ArgumentNullException("创建子工单时的父工单不能为null"); var outMaterials = await _db.Queryable().InnerJoin((a, b) => a.id == b.mbom_id) .InnerJoin((a, b, c) => a.id == c.mbom_id && b.id == c.mbom_process_id) .Where((a, b, c) => a.id == input.bom_id && input.ids.Contains(b.process_id)) .Select((a, b, c) => new { material_id = c.material_id, num = c.num, }) .ToListAsync(); if (outMaterials?.Count > 0) { var ids = outMaterials.Select(x => x.material_id).ToList(); await _db.Deleteable().Where(it => it.parent_id == input.mo_id).ExecuteCommandAsync(); if (ids?.Count > 0) { var dicOutMaterialNum = outMaterials.DistinctBy(x => x.material_id).ToDictionary(x => x.material_id, x => x.num.ParseToInt()); List subMoList = new(); var outputMaterials = await _db.Queryable().Where(it => ids.Contains(it.id)).ToListAsync(); foreach (var om in outputMaterials) { PrdMo subMo = new(); subMo.material_id = om.id; subMo.material_code = om.code; subMo.plan_qty = dicOutMaterialNum.ContainsKey(om.id) ? dicOutMaterialNum[om.id] * curMo.plan_qty : 0; subMo.mo_type = curMo.mo_type; subMo.parent_id = curMo.id; subMo.plan_start_date = curMo.plan_start_date; subMo.plan_end_date = curMo.plan_end_date; subMo.create_id = _userManager.UserId; subMo.create_time = DateTime.Now; subMo.mo_status = DictConst.WaitProductId; subMoList.Add(subMo); } //生成子工单编码 for (int i = 0; i < subMoList.Count; i++) { var num = (i + 1).ToString().PadLeft(2, '0'); subMoList[i].mo_code = $"{curMo.mo_code}-{num}"; } var row = await _db.Insertable(subMoList).ExecuteCommandAsync(); if (row < 1) throw Oops.Oh(ErrorCode.COM1000); } } } #endregion private static Dictionary dicProperties = new Dictionary(StringComparer.OrdinalIgnoreCase); private static T DictionaryToObject(IDictionary dictionary) where T : class, new() { var name = typeof(T).Name; T instance = new(); if (!dicProperties.TryGetValue(name, out string[] properies)) { properies = instance.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public).Select(p => p.Name).ToArray(); dicProperties[name] = properies; } foreach (var pn in properies) { if (dictionary.ContainsKey(pn)) { instance.PropertySetValue(pn, dictionary[pn]); } } return instance; } private async Task GetList(VisualDevModelListQueryInput input) { var db = _repository.AsSugarClient(); VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleId, true); var data = await _runService.GetListResult(templateEntity, input); if (data?.list?.Count > 0) { foreach (var row in data.list) { var dic = row.ToDictionary(x => x.Key, x => x.Value); var pkName = "material_id_id"; if (dic.ContainsKey(pkName)) { var materialId = dic[pkName]?.ToString(); var material = await db.Queryable().FirstAsync(it => it.id == materialId); if (material != null) { row["material_id"] = $"{material.code}/{material.name}"; } } //模具 if (dic.ContainsKey("mold_id")) { var moldId = dic["mold_id"]?.ToString(); var mold = await db.Queryable().FirstAsync(it => it.id == moldId); if (mold != null) { row["mold_id"] = $"{mold.mold_code}/{mold.mold_name}"; } } //设备 if (dic.ContainsKey("eqp_id")) { var eqpId = dic["eqp_id"]?.ToString(); var eqp = await db.Queryable().FirstAsync(it => it.id == eqpId); if (eqp != null) { row["eqp_id"] = $"{eqp.code}/{eqp.name}"; } } } } return data!; } /// /// 删除 /// /// /// private async Task Delete(string id) { var db = _repository.AsSugarClient(); var result = await db.Ado.UseTranAsync(async () => { var row = -1; var prdTask = await db.Queryable().FirstAsync(it => it.id == id); row = await db.Deleteable().Where(it => it.id == id).ExecuteCommandAsync(); if (row > 0) { var prdMo = await db.Queryable().FirstAsync(it => it.id == prdTask.mo_id); if (prdMo is not null) { prdMo.scheduled_qty += prdTask.scheduled_qty; prdMo.mo_status = DictConst.ScheduledId; row = await db.Updateable(prdMo).ExecuteCommandAsync(); } } return row > 0; }); if (!result.IsSuccess) throw Oops.Oh(ErrorCode.COM1002); } public async Task> GetListByEqpId(string eqpId) { return await _db.Queryable().Where(it => it.eqp_id == eqpId && it.mo_task_status == DictConst.InProgressEnCode).ToListAsync(); } } }