using System; using System.Collections.Generic; using System.Dynamic; using System.Linq; using System.Text; using System.Threading.Tasks; using Aspose.Cells.Drawing; using JNPF.Common.Core.Manager; using JNPF.Common.Enums; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Logging; using JNPF.Systems.Interfaces.System; using Microsoft.AspNetCore.Mvc; using SqlSugar; using Tnb.BasicData; using Tnb.EquipMgr.Entities; 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) { dynamic info = new ExpandoObject(); var planMoldRelation = await _db.Queryable().FirstAsync(it => it.maintain_plan_id == planId); if (planMoldRelation != null) { var mold = await _db.Queryable().FirstAsync(it => it.id == planMoldRelation.mold_id); if (mold != null) { info.mold_code = mold.mold_code; info.mold_name = mold.mold_name; var moldEqpRelation = await _db.Queryable().FirstAsync(it => it.mold_id == mold.id); if (moldEqpRelation != null) { var eqp = await _db.Queryable().FirstAsync(it => it.id == moldEqpRelation.equipment_id); info.eqp_code = eqp.code; info.eqp_name = eqp.name; } var itemGroupRelation = await _db.Queryable().FirstAsync(it => it.mold_id == mold.id); if (itemGroupRelation != null) { var itemGroup = await _db.Queryable().FirstAsync(it => it.id == itemGroupRelation.item_group_id); if (itemGroup != null) { info.item_group_name = itemGroup.name; } var itemRelation = await _db.Queryable().FirstAsync(it => it.item_group_id == itemGroupRelation.item_group_id); if (itemRelation != null) { var checkItem = await _db.Queryable().FirstAsync(it => it.id == itemRelation.item_id); if (checkItem != null) { info.item_name = checkItem.name; } } } } } return info; } /// /// 模具保养计划执行-开始模具保养 /// /// /// { /// plan_id:执行计划id /// } /// /// [HttpPost] public async Task MaintainStart(MoldMaintainRunUpInput input) { try { await _db.Ado.BeginTranAsync(); var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.MaintainStatusTypeId); var plan = await _db.Queryable().FirstAsync(it => it.id == input.plan_id); if (plan != null) { plan.status = DictConst.MoldMaintainStatusDBYCode; var row = await _db.Updateable(plan).ExecuteCommandAsync(); if (row < 1) throw Oops.Oh(ErrorCode.COM1001); ToolMoldMaintainRunRecord record = new(); record.plan_code = plan.plan_code; record.mode = plan.mode; record.plan_status = dic.ContainsKey(plan.plan_code) ? dic[plan.plan_code].ToString() : ""; record.designer = _userManager.RealName; record.designer_time = DateTime.Now; var moldPlanRelation = await _db.Queryable().FirstAsync(it => it.maintain_plan_id == input.plan_id); if (moldPlanRelation != null) { var mold = await _db.Queryable().FirstAsync(it => it.id == moldPlanRelation.mold_id); record.mold_code = mold?.mold_code; record.mold_name = mold?.mold_name; var moldGroupRelation = await _db.Queryable().FirstAsync(it => it.mold_id == mold.id); if (moldGroupRelation != null) { var maintainGroup = await _db.Queryable().FirstAsync(it => it.id == moldGroupRelation.item_group_id); record.group_name = maintainGroup.name; var itemGrpRelation = await _db.Queryable().FirstAsync(it => it.item_group_id == maintainGroup.id); if (itemGrpRelation != null) { var checkItem = await _db.Queryable().FirstAsync(it => it.id == itemGrpRelation.item_id); record.check_item_name = checkItem.name; } } } record.plan_start_time = DateTime.Now; row = await _db.Insertable(record).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 MaintainFinish(MoldMaintainRunUpInput input) { } } }