using System.Dynamic; using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.Common.Filter; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Logging; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.System; using JNPF.Systems.Interfaces.System; using Microsoft.AspNetCore.Mvc; using SqlSugar; using StackExchange.Profiling.Internal; using Tnb.BasicData; using Tnb.EquipMgr.Entities; using Tnb.EquipMgr.Entities.Consts; using Tnb.EquipMgr.Entities.Dto; using Tnb.EquipMgr.Interfaces; namespace Tnb.EquipMgr { /// /// 模具维修任务执行 /// [ApiDescriptionSettings(Tag = ModuleConsts.Tag, Area = ModuleConsts.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] public class ToolMoldMaintainPlanRunService : IToolMoldMaintainPlanRunService, IDynamicApiController, ITransient { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; private readonly IDictionaryDataService _dictionaryDataService; public ToolMoldMaintainPlanRunService(ISqlSugarRepository repository, IUserManager userManager, IDictionaryDataService dictionaryDataService) { _db = repository.AsSugarClient(); _userManager = userManager; _dictionaryDataService = dictionaryDataService; } /// /// 根据计划id,获取相关联模具、设备、信息 /// /// /// [HttpGet] public async Task GetMaintainInfoFromByPlanId([FromRoute] string planId) { List result = new(); var planMoldRelations = await _db.Queryable() .LeftJoin((a, b) => a.maintain_plan_id == b.id)//ToolMoldMaintainPlan .LeftJoin((a, b, c) => a.mold_id == c.id) .LeftJoin((a, b, c, d) => d.plan_code == b.plan_code && d.mold_code == c.mold_code) .Where(a => a.maintain_plan_id == planId) .Select((a, b, c, d) => new { a.mold_id, d.plan_start_time, d.designer }) .ToListAsync(); List moldids = planMoldRelations.Select(x => x.mold_id).ToList(); List molds = await _db.Queryable().Where(it => moldids.Contains(it.id)).ToListAsync(); foreach (var planMoldRelation in planMoldRelations) { ToolMolds? mold = molds.Where(p => p.id == planMoldRelation.mold_id).FirstOrDefault(); if (mold != null) { dynamic info = new ExpandoObject(); info.mold_id = mold.id; info.mold_code = mold.mold_code; info.mold_name = mold.mold_name; info.mold_status = (await _dictionaryDataService.GetInfo(mold.mold_status!))?.FullName; info.maintain_qty = mold.maintain_qty; info.designer = planMoldRelation.designer ?? ""; info.plan_start_time = planMoldRelation.plan_start_time == null ? "" : ((DateTime)planMoldRelation.plan_start_time).ToString("yyyy-MM-dd"); ToolMoldsEquipment moldEqpRelation = await _db.Queryable().FirstAsync(it => it.mold_id == mold.id); if (moldEqpRelation != null) { EqpEquipment eqp = await _db.Queryable().FirstAsync(it => it.id == moldEqpRelation.equipment_id); info.eqp_code = eqp.code; info.eqp_name = eqp.name; } result.Add(info); } } return result; } [HttpGet] public async Task GetMaintainInfo([FromQuery] MaintainInfoQueryinput input) { Dictionary dicstatus = new() { { "UnMaintain", "待保养" }, { "Completed", "已完成" } }; List result = new(); List plans = await _db.Queryable().ToListAsync(); List ToolMolds = await _db.Queryable().ToListAsync(); List ToolMoldsEquipments = await _db.Queryable().ToListAsync(); List EqpEquipments = await _db.Queryable().ToListAsync(); List dic = await _db.Queryable().Where(p => p.DictionaryTypeId == "26149299883285").ToListAsync(); List users = await _db.Queryable().ToListAsync(); List records = _db.Queryable().ToList(); List runrecords = _db.Queryable().ToList(); foreach (ToolMoldMaintainPlan plan in plans) { var planMoldRelations = await _db.Queryable() .LeftJoin((a, b) => a.maintain_plan_id == b.id) .LeftJoin((a, b, c) => b.plan_code == c.plan_code) .Where(a => a.maintain_plan_id == plan.id) .Select((a, b, c) => new { a.mold_id, c.plan_start_time, }).ToListAsync(); if (planMoldRelations?.Count > 0) { List mids = planMoldRelations.Select(x => x.mold_id).ToList(); List molds = ToolMolds.Where(it => mids.Contains(it.id)) .WhereIF(!string.IsNullOrEmpty(input.keyword), p => p.mold_code!.Contains(input.keyword!) || p.mold_name!.Contains(input.keyword!)) .ToList(); if (molds?.Count > 0) { for (int i = 0, cnt = molds.Count; i < cnt; i++) { ToolMolds mold = molds[i]; if (!string.IsNullOrEmpty(input.status)) { string moldstatus = records.Where(p => p.mold_id == mold.id && p.plan_id == plan.id).Any() ? "已完成" : "待保养"; if (input.status != moldstatus) { continue; } } dynamic info = new ExpandoObject(); info.mold_id = mold.id; info.mold_code = mold.mold_code; info.mold_name = mold.mold_name; info.mold_status = dic.Where(p => p.Id == mold.mold_status).Any() ? dic.Where(p => p.Id == mold.mold_status).First().FullName : ""; info.maintain_qty = mold.maintain_qty; info.plan_start_time = plan.plan_start_date == null ? "" : ((DateTime)plan.plan_start_date!).ToString("yyyy-MM-dd"); info.createtime = plan.create_time == null ? "" : ((DateTime)plan.create_time).ToString("yyyy-MM-dd"); info.status = records.Where(p => p.mold_id == mold.id && p.plan_id == plan.id).Any() ? "已完成" : "待保养";//plan.status == "UnMaintain" ? "待保养" : "已完成"; info.createuser = string.IsNullOrEmpty(plan.create_id) ? "" : users.Where(p => p.Id == plan.create_id).First().RealName; info.plan_id = plan.id; info.starttime = ""; if (runrecords.Where(p => p.mold_code == mold.mold_code && p.plan_code == plan.plan_code).Any()) { ToolMoldMaintainRunRecord run = runrecords.Where(p => p.mold_code == mold.mold_code && p.plan_code == plan.plan_code).First(); info.starttime = run.plan_start_time != null ? ((DateTime)run.plan_start_time).ToString("yyyy-MM-dd") : ""; } ToolMoldsEquipment? moldEqpRelation = ToolMoldsEquipments.Where(it => it.mold_id == mold.id).FirstOrDefault(); if (moldEqpRelation != null) { EqpEquipment? eqp = EqpEquipments.Where(it => it.id == moldEqpRelation.equipment_id).FirstOrDefault(); if (eqp != null) { info.eqp_code = eqp.code; info.eqp_name = eqp.name; } } result.Add(info); } } } } if (!string.IsNullOrEmpty(input.sort)) { if (input.sort == "createtime") { result = result.OrderByDescending(p => p.createtime).ToList(); } if (input.sort == "plan_start_time") { result = result.OrderByDescending(p => p.plan_start_time).ToList(); }; } return result; } [HttpPost] public async Task GetPdaMaintainInfo(PdaMaintainInput input) { DateTime? start_time = input.start_time; DateTime? end_time = input.end_time; if (string.IsNullOrEmpty(input.sidx)) { input.sidx = "b.create_time"; input.sort = "desc"; } else { input.sidx = "b." + input.sidx; input.sort = "desc"; } List records = await _db.Queryable().Select(p => p.plan_id + p.mold_id).ToListAsync(); SqlSugarPagedList result = await _db.Queryable() .LeftJoin((a, b) => a.maintain_plan_id == b.id) .LeftJoin((a, b, c) => a.mold_id == c.id) .LeftJoin((a, b, c, d) => b.plan_code == d.plan_code && c.mold_code == d.mold_code) // .LeftJoin((a, b, c, d, e) => e.plan_id == b.id && e.mold_id == c.id) .LeftJoin((a, b, c, d, e) => b.create_id == e.Id) .LeftJoin((a, b, c, d, e, f) => c.mold_status == f.Id) .Where((a, b, c, d, e, f) => b.create_time != null) .WhereIF(!string.IsNullOrEmpty(input.maintain_info), (a, b, c, d, e, f) => c.mold_code!.Contains(input.maintain_info) || c.mold_name!.Contains(input.maintain_info)) .WhereIF(start_time != null, (a, b, c, d, e, f) => b.create_time != null && b.create_time >= start_time) .WhereIF(end_time != null, (a, b, c, d, e, f) => b.create_time != null && b.create_time <= end_time) .WhereIF(input.status == "待保养", (a, b, c, d, e, f) => !records.Contains(a.maintain_plan_id + a.mold_id)) .WhereIF(input.status == "已完成", (a, b, c, d, e, f) => records.Contains(a.maintain_plan_id + a.mold_id)) .Select((a, b, c, d, e, f) => new PadMainListOutput { plan_id = b.id, mold_id = c.id, mold_code = c.mold_code!, mold_name = c.mold_name!, mold_status = f.FullName!, status = input.status, createuser = e.RealName, createtime = b.create_time == null ? "" : b.create_time.Value.ToString(DbTimeFormat.SS), plan_start_time = b.plan_start_date == null ? "" : b.plan_start_date.Value.ToString(DbTimeFormat.SS), starttime = d.plan_start_time == null ? "" : d.plan_start_time.Value.ToString(DbTimeFormat.SS), finishtime= d.plan_end_time == null ? "" : d.plan_end_time.Value.ToString(DbTimeFormat.SS), }).OrderBy($"{input.sidx} {input.sort}").ToPagedListAsync(input?.currentPage ?? 1, input?.pageSize ?? 50); return PageResult.SqlSugarPageResult(result); } /// /// 根据计划Id、模具ID获取,保养组及项目信息 /// /// /// 参数: ///
{ ///
plan_id:计划Id ///
mold_id:模具Id ///
} /// /// /// returns: ///
{ ///
plan_id:计划ID ///
mold_id:模具ID ///
item_group_id:保养组ID ///
item_group_name:保养组名称 ///
item_id:保养项ID ///
item_name:保养项名称 ///
} ///
/// [HttpGet] public async Task GetCheckItemAndGrpByMoldId([FromQuery] CheckItemQueryinput input) { if (input == null) { throw new ArgumentNullException(nameof(input)); } List? items = await _db.Queryable() .InnerJoin((a, b) => a.mold_id == b.mold_id) .InnerJoin((a, b, c) => b.item_group_id == c.item_group_id) .InnerJoin((a, b, c, d) => c.item_group_id == d.id) .InnerJoin((a, b, c, d, e) => c.item_id == e.id) .Where((a) => a.maintain_plan_id == input.plan_id && a.mold_id == input.mold_id) .Select((a, b, c, d, e) => new CheckItemOutput { plan_id = a.maintain_plan_id, mold_id = a.mold_id, item_group_id = d.id, item_group_name = d.name, item_id = e.id, item_name = e.name, }) .ToListAsync(); //新增功能 ToolMoldMaintainPlanRelation ToolMoldMaintainPlanRelation = _db.Queryable().Where((a) => a.maintain_plan_id == input.plan_id && a.mold_id == input.mold_id && !string.IsNullOrEmpty(a.group_id)).First(); if (ToolMoldMaintainPlanRelation != null) { items = items.Where(a => a.item_group_id == ToolMoldMaintainPlanRelation.group_id).ToList(); } var checkItems = await _db.Queryable().Where(it => it.plan_id == input.plan_id && it.mold_id == input.mold_id).Select(it => new { it.plan_id, it.item_id, it.item_group_id, it.mold_id, }).ToListAsync(); var dicCheckItems = checkItems.GroupBy(g => $"{g.plan_id}{g.mold_id}{g.item_group_id}{g.item_id}").ToDictionary(x => x.Key, x => x.FirstOrDefault()); if (items?.Count > 0 && checkItems?.Count > 0) { foreach (CheckItemOutput? item in items) { string key = $"{item.plan_id}{item.mold_id}{item.item_group_id}{item.item_id}"; if (dicCheckItems.ContainsKey(key) && dicCheckItems[key] != null) { item.status = 1; } } } return items; } [HttpPost] public async Task GetMaintainItem(MoldMaintainRunUpInput input) { var data = await _db.Queryable() .LeftJoin((a, b) => a.item_id == b.id) .LeftJoin((a, b, c) => a.item_group_id == c.id) .Where((a, b, c) => a.plan_id == input.plan_id && a.mold_id == input.mold_id) .Select((a, b, c) => new MaintainItemResult { group_name = c.name!, item_name = b.name!, result = a.result, }) .ToListAsync(); return data; } /// /// 模具保养计划执行-开始模具保养 /// /// /// { /// plan_id:执行计划id /// } /// /// [HttpPost] public async Task MaintainStart(MoldMaintainRunUpInput input) { if (input == null) { throw new ArgumentNullException("input"); } bool flag = _db.Queryable() .LeftJoin((a, b) => a.plan_code == b.plan_code) .LeftJoin((a, b, c) => a.mold_code == c.mold_code) .Where((a, b, c) => b.id == input.plan_id && c.id == input.mold_id).Any(); if (flag) { return; } try { await _db.Ado.BeginTranAsync(); Dictionary dic = await _dictionaryDataService.GetDicByTypeId(DictConst.MaintainStatusTypeId); ToolMolds mold = await _db.Queryable().FirstAsync(it => it.id == input.mold_id); if (mold != null) { mold.mold_status = MoldUseStatus.MOLD_USE_STATUS_MAINTAIN_ID; bool isOk = await _db.Updateable(mold).Where(it => it.id == input.mold_id).ExecuteCommandHasChangeAsync(); if (!isOk) { throw Oops.Oh(ErrorCode.COM1001); } ToolMoldMaintainPlan? plan = await _db.Queryable().LeftJoin((a, b) => a.maintain_plan_id == b.id) .Where(a => a.mold_id == input.mold_id && a.maintain_plan_id == input.plan_id).Select((a, b) => b).FirstAsync(); if (plan is not null) { //插入保养计划记录 ToolMoldMaintainRunRecord record = new() { plan_code = plan.plan_code, mode = plan.mode, plan_status = dic.ContainsKey(plan.plan_code) ? dic[plan.plan_code].ToString() : "", designer = _userManager.RealName, designer_time = DateTime.Now, mold_code = mold.mold_code, mold_name = mold.mold_name, plan_start_time = string.IsNullOrEmpty(input.starttime) ? DateTime.Now : DateTime.Parse(input.starttime) }; int row = await _db.Insertable(record).ExecuteCommandAsync(); if (row < 1) { throw Oops.Oh(ErrorCode.COM1001); } IEnumerable groupids = _db.Queryable().Where(a => !string.IsNullOrEmpty(a.group_id) && a.mold_id == input.mold_id && a.maintain_plan_id == input.plan_id).ToList().Select(p => p.group_id); /* var maintainInfos = await _db.Queryable() .LeftJoin((a, b) => a.item_group_id == b.id) .LeftJoin((a, b, c) => b.id == c.item_group_id) .LeftJoin((a, b, c, d) => c.item_id == d.id) .Where(a => a.mold_id == input.mold_id) .Select((a, b, c, d) => new { group_id = b.id, group_name = b.name, check_item_id = d.id, check_item_name = d.name }) .ToListAsync(); */ var maintainInfos = await _db.Queryable() .LeftJoin((a, b) => a.item_group_id == b.id) .LeftJoin((a, b, c) => b.id == c.item_group_id) .LeftJoin((a, b, c, d) => c.item_id == d.id) .WhereIF(groupids.Count() > 0, (a) => groupids.Contains(a.item_group_id)) .Where(a => a.mold_id == input.mold_id) .Select((a, b, c, d) => new { group_id = b.id, group_name = b.name, check_item_id = d.id, check_item_name = d.name }).ToListAsync(); if (maintainInfos?.Count > 0) { List recordDs = new(); foreach (var info in maintainInfos) { ToolMoldMaintainRunRecordD record_d = new() { mainid = record.id, group_id = info.group_id, group_name = info.group_name, check_item_id = info.check_item_id, check_item_name = info.check_item_name }; recordDs.Add(record_d); } row = await _db.Insertable(recordDs).ExecuteCommandAsync(); if (row < 1) { throw Oops.Oh(ErrorCode.COM1001); } } } } await _db.Ado.CommitTranAsync(); } catch (Exception ex) { Log.Error("开始模具保养失败", ex); await _db.Ado.RollbackTranAsync(); throw; } } /// /// 模具保养完成 /// /// [HttpPost] public async Task FinishMaintain(MoldMaintainRunUpInput input) { if (input == null) { throw new ArgumentNullException("input"); } if (input.items == null || input.items.Count == 0) { throw new ArgumentException($"parameter {nameof(input.items)} not be null or empty"); } var plan_code = _db.Queryable().Where(p => p.id == input.plan_id).Select(p=>p.plan_code).First(); var mold_code = _db.Queryable().Where(p => p.id == input.mold_id).Select(p => p.mold_code).First(); if (!string.IsNullOrEmpty(plan_code)&& !string.IsNullOrEmpty(mold_code)) { _ = await _db.Updateable().SetColumns(it => new ToolMoldMaintainRunRecord { plan_end_time = DateTime.Now }).Where(it => it.plan_code == plan_code && it.mold_code== mold_code).ExecuteCommandAsync(); } List records = new(); foreach (MaintainItemInfo item in input.items) { ToolMoldMaintainItemRecord record = new() { plan_id = input.plan_id, mold_id = input.mold_id, item_group_id = item.item_group_id, item_id = item.item_id, status = 1, result = item.result }; records.Add(record); } _ = await _db.Insertable(records).ExecuteCommandAsync(); _ = await _db.Updateable().SetColumns(it => new ToolMolds { mold_status = MoldUseStatus.MOLD_USE_STATUS_ZK_ID }).Where(it => it.id == input.mold_id).ExecuteCommandAsync(); int count = await _db.Queryable().Where(p => p.maintain_plan_id == input.plan_id).Select(p => p.mold_id).Distinct().CountAsync(); int finish = await _db.Queryable().Where(p => p.plan_id == input.plan_id).Select(p => p.mold_id).Distinct().CountAsync(); if (count == finish) { _ = await _db.Updateable().SetColumns(it => new ToolMoldMaintainPlan { status = MoldPlanMaintainStatus.MOLDPLAN_MAINTAIN_STATUS_COMPLETED_CODE }).Where(it => it.id == input.plan_id).ExecuteCommandAsync(); } } [HttpPost] public async Task MaintainItemFinish(MoldMaintainRunUpInput input) { if (input == null) { throw new ArgumentNullException("input"); } if (input.items == null || input.items.Count == 0) { throw new ArgumentException($"parameter {nameof(input.items)} not be null or empty"); } List records = new(); foreach (MaintainItemInfo item in input.items) { ToolMoldMaintainItemRecord record = new() { plan_id = input.plan_id, mold_id = input.mold_id, item_group_id = item.item_group_id, item_id = item.item_id, status = 1 }; records.Add(record); } int row = await _db.Insertable(records).ExecuteCommandAsync(); if (row < 1) { throw Oops.Oh(ErrorCode.COM1001); } } /// /// 模具保养计划执行-保养完成 /// /// /// [HttpPost] public async Task MaintainFinish(MoldMaintainRunUpInput input) { List? items = await _db.Queryable() .InnerJoin((a, b) => a.mold_id == b.mold_id) .InnerJoin((a, b, c) => b.item_group_id == c.item_group_id) .InnerJoin((a, b, c, d) => c.item_group_id == d.id) .InnerJoin((a, b, c, d, e) => c.item_id == e.id) .Where((a) => a.maintain_plan_id == input.plan_id && a.mold_id == input.mold_id) .Select((a, b, c, d, e) => new CheckItemOutput { plan_id = a.maintain_plan_id, mold_id = a.mold_id, item_group_id = d.id, item_group_name = d.name, item_id = e.id, item_name = e.name, }) .ToListAsync(); //新增功能 ToolMoldMaintainPlanRelation ToolMoldMaintainPlanRelation = _db.Queryable().Where((a) => a.maintain_plan_id == input.plan_id && a.mold_id == input.mold_id && !string.IsNullOrEmpty(a.group_id)).First(); if (ToolMoldMaintainPlanRelation != null) { items = items.Where(a => a.item_group_id == ToolMoldMaintainPlanRelation.group_id).ToList(); } var checkItems = await _db.Queryable().Where(it => it.plan_id == input.plan_id && it.mold_id == input.mold_id).Select(it => new { it.plan_id, it.item_id, it.item_group_id, it.mold_id, }).ToListAsync(); var dicCheckItems = checkItems.GroupBy(g => $"{g.plan_id}{g.mold_id}{g.item_group_id}{g.item_id}").ToDictionary(x => x.Key, x => x.FirstOrDefault()); List? maintainedItems = items.Where(it => dicCheckItems.ContainsKey($"{it.plan_id}{it.mold_id}{it.item_group_id}{it.item_id}") && dicCheckItems[$"{it.plan_id}{it.mold_id}{it.item_group_id}{it.item_id}"] != null).ToList(); if ((items?.Count > 0 && maintainedItems?.Count > 0) || maintainedItems == null || maintainedItems.Count < 1) { if (maintainedItems.Count < items.Count || maintainedItems == null || maintainedItems.Count < 1) { throw new AppFriendlyException("当前模具有未完成的保养项目", 500); } } int row = await _db.Updateable().SetColumns(it => new ToolMolds { mold_status = MoldUseStatus.MOLD_USE_STATUS_ZK_ID }).Where(it => it.id == input.mold_id).ExecuteCommandAsync(); List allMoldStatus = await _db.Queryable().InnerJoin((a, b) => a.mold_id == b.id) .Where((a, b) => a.maintain_plan_id == input.plan_id) .Select((a, b) => b.mold_status) .ToListAsync(); if (allMoldStatus?.Count > 0 && allMoldStatus.All(x => x == MoldUseStatus.MOLD_USE_STATUS_ZK_ID)) { row = await _db.Updateable().SetColumns(it => new ToolMoldMaintainPlan { status = MoldPlanMaintainStatus.MOLDPLAN_MAINTAIN_STATUS_COMPLETED_CODE }).Where(it => it.id == input.plan_id).ExecuteCommandAsync(); } if (row < 1) { throw Oops.Oh(ErrorCode.COM1001); } } } }