using JNPF.Common.Extension; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.System; using JNPF.Systems.Interfaces.System; using Mapster; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Tnb.BasicData; using Tnb.BasicData.Entities; using Tnb.EquipMgr.Entities; using Tnb.PerMgr.Entities; using Tnb.ProductionMgr.Entities; using Tnb.ProductionMgr.Entities.Dto.PrdManage; using Tnb.ProductionMgr.Interfaces; using Tnb.ProductionMgr.Entities.Entity; namespace Tnb.ProductionMgr { /// /// 组装、包装生产提报服务 /// [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] public class PrdPackReportService : IPrdPackReportService, IDynamicApiController, ITransient { private readonly ISqlSugarClient _db; private readonly IDictionaryDataService _dictionaryDataService; private static Dictionary> _dicWorkLine = new Dictionary>(); public PrdPackReportService(ISqlSugarRepository repository, IDictionaryDataService dictionaryDataService) { _db = repository.AsSugarClient(); _dictionaryDataService = dictionaryDataService; } /// /// 获取组装包装生产任务记录,树形结构 /// /// [HttpPost] public async Task GetList(PrdPackReportQueryInput input) { if (input == null) throw new ArgumentNullException("input"); if (string.IsNullOrEmpty(input.stationId)) { return new { pagination = new PageResult(), list = Array.Empty() }; } List trees = new(); var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.PrdTaskStatusTypeId); var list = await _db.Queryable().Where(it => it.Category == "workline").ToListAsync(); if (_dicWorkLine.Count < 1) { _dicWorkLine = list.ToDictionary(x => x.Id, x => Tuple.Create(x.EnCode, x.FullName)); } bool start = false; bool end=false; DateTime[] startTimes = new DateTime[2]; DateTime[] endTimes = new DateTime[2]; if (input.estimated_start_date != null && input.estimated_start_date.Count() == 2) { start=true; startTimes[0] = GetDateTimeMilliseconds(input.estimated_start_date![0]); startTimes[1] = GetDateTimeMilliseconds(input.estimated_start_date![1]); } if (input.estimated_end_date != null && input.estimated_end_date.Count() == 2) { end = true; endTimes[0] = GetDateTimeMilliseconds(input.estimated_end_date![0]); endTimes[1] = GetDateTimeMilliseconds(input.estimated_end_date![1]); } var items = await _db.Queryable() .LeftJoin((a, b) => a.process_id == b.id) .LeftJoin((a, b, c) => a.mo_id == c.id) .LeftJoin((a,b,c,d)=>a.id==d.parent_id) .WhereIF(!string.IsNullOrEmpty(input.mo_task_code), a => a.mo_task_code == input.mo_task_code.Trim()) // .WhereIF(start, a => a.estimated_start_date != null&& startTimes[0] <= a.estimated_start_date && startTimes[1] >= a.estimated_start_date) // .WhereIF(end, a => a.estimated_end_date != null && endTimes[0] <= a.estimated_end_date && endTimes[1] >= a.estimated_end_date) .WhereIF(!string.IsNullOrEmpty(input.workline), a => list.Where(p => p.EnCode.Contains(input.workline) || p.FullName.Contains(input.workline)).Select(p => p.Id).ToList().Contains(a.workline_id!)) .Where(a => string.IsNullOrEmpty(a.parent_id) && a.schedule_type == 2 && a.mo_task_status != "ToBeScheduled") .Where((a,b,c,d)=>d.workstation_id==input.stationId) .OrderByDescending(a=>a.create_time) .Select((a, b, c) => new PrdMoTask { id = a.id, mo_task_code = a.mo_task_code, workline_id = a.workline_id, process_id = a.process_id, plan_start_date = a.estimated_start_date, plan_end_date = a.estimated_end_date, plan_qty = a.scheduled_qty, // complete_qty = SqlFunc.Subqueryable().Where(it => it.mo_task_code == a.mo_task_code).Sum(it => it.reported_work_qty), complete_qty = a.last_process_complete_qty, mo_task_status = a.mo_task_status, }) .ToPagedListAsync(input.currentPage, input.pageSize); if (start) items.list= items.list.Where(a => startTimes[0] <= a.plan_start_date && startTimes[1] >= a.plan_start_date).ToList(); if (end) items.list= items.list.Where(a => endTimes[0] <= a.plan_end_date && endTimes[1] >= a.plan_end_date).ToList(); _db.ThenMapper(items.list, it => { it.mo_task_status = it.mo_task_status.IsNotEmptyOrNull() && dic.ContainsKey(it.mo_task_status) ? dic[it.mo_task_status].ToString() : ""; }); if (items != null && items.list != null && items.list.Any()) { foreach (var item in items.list) { var node = item.Adapt(); node.parentId = "0"; if (item.workline_id.IsNotEmptyOrNull()) { var workLine = _dicWorkLine.ContainsKey(item.workline_id) ? (Tuple)_dicWorkLine[item.workline_id] : null; if (workLine != null) { node.workline_id = $"{workLine.Item1}/{workLine.Item2}"; } } node.plan_start_date = item.plan_start_date.HasValue ? item.plan_start_date.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""; node.plan_end_date = item.plan_end_date.HasValue ? item.plan_end_date.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""; await GetChild(item.id, trees, dic,input.stationId); trees.Add(node); } } var treeList = trees.ToTree(); if (!string.IsNullOrEmpty(input.process)) { List< PackReportTreeOutput > removelist = new List< PackReportTreeOutput >(); foreach (var item in treeList) { bool flag = false; if (item.process_id != null && item.process_id.Contains(input.process)) flag = true; if (item.children != null&& item.children.Count>0) { var childs = item.children.Adapt>(); if (childs.Where(p => p.process_id.Contains(input.process)).Any()) flag = true; } if (!flag) removelist.Add(item); } removelist.ForEach(p => treeList.Remove(p)); } SqlSugarPagedList pagedList = new() { list = treeList, pagination = new Pagination { CurrentPage = input.currentPage, PageSize = input.pageSize, Total = treeList.Count } }; return PageResult.SqlSugarPageResult(pagedList); } private static DateTime GetDateTimeMilliseconds(long timestamp) { long begtime = timestamp * 10000; DateTime dt_1970 = new DateTime(1970, 1, 1, 8, 0, 0); long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度 long time_tricks = tricks_1970 + begtime;//日志日期刻度 DateTime dt = new DateTime(time_tricks);//转化为DateTime return dt; } private async Task GetChild(string parentId, List nodes, Dictionary dic,string stationId) { var items = await _db.Queryable() .LeftJoin((a, b) => a.process_id == b.id) .LeftJoin((a, b, c) => a.mo_id == c.id) .LeftJoin((a, b, c, d) => a.mbom_process_id == d.id) .Where(a => a.parent_id == parentId && a.mo_task_status != "ToBeScheduled" && a.workstation_id==stationId) .OrderBy((a, b, c, d) => d.ordinal) .Select((a, b, c) => new PrdMoTask { id = a.id, mo_task_code = a.mo_task_code, workline_id = a.workline_id, process_id = a.process_id, process_code = b.process_code, process_name = b.process_name, plan_start_date = a.estimated_start_date, plan_end_date = a.estimated_end_date, plan_qty = a.scheduled_qty, //complete_qty = SqlFunc.Subqueryable().Where(it => it.mo_task_code == a.mo_task_code).Sum(it => it.reported_work_qty), complete_qty = SqlFunc.IsNull(a.reported_work_qty,0) + SqlFunc.IsNull(a.scrap_qty,0), mo_task_status = a.mo_task_status, }).ToListAsync(); _db.ThenMapper(items, it => { it.mo_task_status = it.mo_task_status.IsNotEmptyOrNull() && dic.ContainsKey(it.mo_task_status) ? dic[it.mo_task_status].ToString() : ""; }); if (items?.Count > 0) { var nsChild = items.Adapt>(); for (int i = 0; i < nsChild.Count; i++) { nsChild[i].parentId = parentId; nsChild[i].process_id = $"{items[i].process_code}/{items[i].process_name}"; nsChild[i].plan_start_date = items[i].plan_start_date.HasValue ? items[i].plan_start_date.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""; nsChild[i].plan_end_date = items[i].plan_end_date.HasValue ? items[i].plan_end_date.Value.ToString("yyyy-MM-dd HH:mm:ss") : ""; if (nsChild[i].workline_id.IsNotEmptyOrNull()) { var workLine = _dicWorkLine.ContainsKey(nsChild[i].workline_id) ? (Tuple)_dicWorkLine[nsChild[i].workline_id] : null; if (workLine != null) { nsChild[i].workline_id = $"{workLine.Item1}/{workLine.Item2}"; } } } nodes.AddRange(nsChild); foreach (var item in items) { await GetChild(item.id, nodes, dic,stationId); } } } /// /// pda端根据工位获取注塑挤出列表 /// /// [HttpPost] public async Task GetInjectionMoldingList(PrdPackReportQueryInput input) { if (string.IsNullOrEmpty(input.stationId)) { return new { pagination = new PageResult(), list = Array.Empty() }; } var result = await _db.Queryable() .LeftJoin((a, b) => a.material_id == b.id) .LeftJoin((a, b, c) => a.process_id == c.id) .LeftJoin((a, b, c, e) => e.DictionaryTypeId == DictConst.PrdTaskStatusTypeId && a.mo_task_status == e.EnCode) .LeftJoin((a,b,c,e,f)=>a.eqp_id==f.id) .LeftJoin((a,b,c,e,f,g)=>a.mold_id==g.id) .Where((a, b) => a.workstation_id == input.stationId && a.mo_task_status != DictConst.ToBeScheduledEncode && a.schedule_type==1) .OrderByDescending((a, b) => a.create_time) .Select((a, b, c, e,f,g) => new PADPackageTaskPageOutput { id = a.id, mo_task_code = a.mo_task_code, mo_id = a.mo_id, material_id = a.material_id, material_code = b.code, material_name = b.name, workline_id = a.workline_id, // workline_name = d.FullName, bom_id = a.bom_id, mo_task_status = e.FullName, complete_qty = SqlFunc.IsNull(a.reported_work_qty,0) + SqlFunc.IsNull(a.scrap_qty,0), scrap_qty = a.scrap_qty, scheduled_qty = a.scheduled_qty, reported_work_qty = a.reported_work_qty, estimated_start_date = a.estimated_start_date==null ? "" : a.estimated_start_date.Value.ToString(DbTimeFormat.SS), estimated_end_date = a.estimated_end_date==null ? "" : a.estimated_end_date.Value.ToString(DbTimeFormat.SS), parent_id = a.parent_id, process_id = a.process_id, process_name = c.process_name, mbom_process_id = a.mbom_process_id, equip_id = a.eqp_id, equip_code = f.code, equip_name = f.name, mold_id = a.mold_id, mold_code = g.mold_code, mold_name = g.mold_name, }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } /// /// pda端根据工位获取组装包装列表 /// /// [HttpPost] public async Task GetPadList(PrdPackReportQueryInput input) { if (string.IsNullOrEmpty(input.stationId)) { return new { pagination = new PageResult(), list = Array.Empty() }; } var result = await _db.Queryable() .LeftJoin((a, b) => a.material_id == b.id) .LeftJoin((a, b, c) => a.process_id == c.id) .LeftJoin((a, b, c, d) => a.workline_id == d.Id) .LeftJoin((a, b, c, d, e) => e.DictionaryTypeId == DictConst.PrdTaskStatusTypeId && a.mo_task_status == e.EnCode) .Where((a, b) => a.workstation_id == input.stationId && a.mo_task_status != DictConst.ToBeScheduledEncode && a.schedule_type==2) .OrderByDescending((a, b) => a.create_time) .Select((a, b, c, d, e) => new PADPackageTaskPageOutput { id = a.id, mo_task_code = a.mo_task_code, mo_id = a.mo_id, material_id = a.material_id, material_code = b.code, material_name = b.name, workline_id = a.workline_id, workline_name = d.FullName, bom_id = a.bom_id, mo_task_status = e.FullName, complete_qty = SqlFunc.IsNull(a.reported_work_qty,0) + SqlFunc.IsNull(a.scrap_qty,0), scrap_qty = a.scrap_qty, scheduled_qty = a.scheduled_qty, reported_work_qty = a.reported_work_qty, estimated_start_date = a.estimated_start_date==null ? "" : a.estimated_start_date.Value.ToString(DbTimeFormat.SS), estimated_end_date = a.estimated_end_date==null ? "" : a.estimated_end_date.Value.ToString(DbTimeFormat.SS), parent_id = a.parent_id, process_id = a.process_id, process_name = c.process_name, mbom_process_id = a.mbom_process_id, }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } /// /// pda端根据工位获取任务单列表 /// /// [HttpPost] public async Task GetPadPrdMoTaskList(PrdMoTaskListOutput input) { if (string.IsNullOrEmpty(input.stationId)) { return new { pagination = new PageResult(), list = Array.Empty() }; } Dictionary queryJson = string.IsNullOrEmpty(input.queryJson) ? new Dictionary() : input.queryJson.ToObject>(); string mo_task_code = queryJson.ContainsKey("mo_task_code") ? queryJson["mo_task_code"].ToString() : ""; string status = queryJson.ContainsKey("status") ? queryJson["status"].ToString() : ""; DateTime? start_time = queryJson.ContainsKey("start_time") ? queryJson["start_time"].ToString()=="" ? null : Convert.ToDateTime(queryJson["start_time"]) : null; DateTime? end_time = queryJson.ContainsKey("end_time") ? queryJson["end_time"].ToString()=="" ? null : Convert.ToDateTime(queryJson["end_time"]) : null; List statusList = new List(); if (!string.IsNullOrEmpty(status)) { switch (status) { case "1": statusList.Add(DictConst.InProgressEnCode); statusList.Add(DictConst.MoStatusPauseCode); break; case "2": statusList.Add(DictConst.ToBeScheduledEncode); statusList.Add(DictConst.ToBeStartedEnCode); break; case "3": statusList.Add(DictConst.ComplatedEnCode); statusList.Add(DictConst.ClosedEnCode); break; } } // string mo_task_status = queryJson.ContainsKey("mo_task_status") ? queryJson["mo_task_status"].ToString() : ""; if (string.IsNullOrEmpty(input.sidx)) { input.sidx = "create_time"; input.sort = "desc"; } var result = await _db.Queryable() .LeftJoin((a, b) => a.material_id == b.id) .LeftJoin((a, b, c) => a.process_id == c.id) .LeftJoin((a, b, c, d) => a.workline_id == d.Id) .LeftJoin((a, b, c, d, e) => e.DictionaryTypeId == DictConst.PrdTaskStatusTypeId && a.mo_task_status == e.EnCode) .LeftJoin((a,b,c,d,e,f)=>a.eqp_id==f.id) .LeftJoin((a,b,c,d,e,f,g)=>a.mold_id==g.id) .LeftJoin((a,b,c,d,e,f,g,h)=>a.material_id==h.output_material_id && a.eqp_id==h.equip_id && a.mold_id==h.molds_id && h.enabled==1) .LeftJoin((a,b,c,d,e,f,g,h,i)=>a.process_id==i.process_id && i.enabled==1) .LeftJoin((a,b,c,d,e,f,g,h,i,j)=>a.mo_id==j.id) .LeftJoin((a,b,c,d,e,f,g,h,i,j,k)=>a.material_id==k.material_id && k.auxiliary_unit_id=="kg") .LeftJoin((a,b,c,d,e,f,g,h,i,j,k,l)=>a.eqp_id==l.equip_id && l.enabled==1 && l.label_point=="提报装箱称重点位") .Where((a, b) => a.workstation_id == input.stationId && (a.mo_task_status == DictConst.ToBeStartedEnCode || a.mo_task_status == DictConst.MoStatusPauseCode || a.mo_task_status == DictConst.ComplatedEnCode || a.mo_task_status == DictConst.InProgressEnCode) ) .WhereIF(!string.IsNullOrEmpty(mo_task_code),a=>a.mo_task_code.Contains(mo_task_code)) //.WhereIF(!string.IsNullOrEmpty(mo_task_status),a=>a.mo_task_status==mo_task_status) .WhereIF(statusList.Count>0,a=>statusList.Contains(a.mo_task_status)) .WhereIF(status=="3" && start_time!=null,a=>a.act_end_date>=start_time) .WhereIF(status=="3" && end_time!=null,a=>a.act_end_date<=end_time) .Select((a, b, c, d, e,f,g,h,i,j,k,l) => new PADPackageTaskPageOutput { id = a.id, mo_task_code = a.mo_task_code, mo_id = a.mo_id, mo_code = j.mo_code, material_id = a.material_id, material_code = b.code, material_name = b.name, workline_id = a.workline_id, workline_name = d.FullName, bom_id = a.bom_id, mo_task_status = e.FullName, complete_qty = SqlFunc.IsNull(a.reported_work_qty,0) + SqlFunc.IsNull(a.scrap_qty,0), scrap_qty = a.scrap_qty, scheduled_qty = a.scheduled_qty, reported_work_qty = a.reported_work_qty, estimated_start_date = a.estimated_start_date==null ? "" : a.estimated_start_date.Value.ToString(DbTimeFormat.SS), estimated_end_date = a.estimated_end_date==null ? "" : a.estimated_end_date.Value.ToString(DbTimeFormat.SS), parent_id = a.parent_id, process_id = a.process_id, process_name = c.process_name, mbom_process_id = a.mbom_process_id, create_time = a.create_time, equip_id = a.eqp_id, equip_code = f.code, equip_name = f.name, mold_id = a.mold_id, mold_code = g.mold_code, mold_name = g.mold_name, schedule_type = a.schedule_type, mold_cavity = g.mold_cavity, moulding_cycle = h.moulding_cycle, standard_time = i.standard_time, act_start_date = a.act_start_date==null ? "" : a.act_start_date.Value.ToString(DbTimeFormat.SS), act_end_date = a.act_end_date==null ? "" : a.act_end_date.Value.ToString(DbTimeFormat.SS), plan_end_date = a.plan_end_date==null ? "" : a.plan_end_date.Value.ToString(DbTimeFormat.SS), tube = f.tube, minpacking = b.minpacking, main_num = k.number_of_primary_unit, deputy_num = k.number_of_auxiliary_unit, third_equip_code = f.third_equip_code, weight_name = l.label_name }) .MergeTable() .OrderBy($"{input.sidx} {input.sort}") .ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } /// /// 上模校验 /// /// [HttpPost] public async Task CheckMold(CheckMoldInput input) { PrdMoTask prdMoTask = await _db.Queryable().SingleAsync(x => x.id == input.mo_task_id); BasQrcode basQrcode = await _db.Queryable().Where(x=>x.source_name=="TOOL_MOLDS" && x.code==input.mold_qrcode).FirstAsync(); if (prdMoTask != null && basQrcode!=null) { return prdMoTask.mold_id == basQrcode.source_id; } return false; } } }