Files
tnb.server/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainRunService.cs
2023-05-30 08:34:44 +08:00

228 lines
11 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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.Common.Extension;
using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Logging;
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
{
/// <summary>
/// 模具维修任务执行
/// </summary>
[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<ToolMoldMaintainRule> repository, IUserManager userManager, IDictionaryDataService dictionaryDataService)
{
_db = repository.AsSugarClient();
_userManager = userManager;
_dictionaryDataService = dictionaryDataService;
}
/// <summary>
/// 根据计划id,获取相关联模具、设备、信息
/// </summary>
/// <param name="planId"></param>
/// <returns></returns>
[HttpGet]
public async Task<dynamic> GetMaintainInfoFromByPlanId([FromRoute] string planId)
{
dynamic info = new ExpandoObject();
var planMoldRelation = await _db.Queryable<ToolMoldMaintainPlanRelation>()
.LeftJoin<ToolMoldMaintainPlan>((a, b) => a.maintain_plan_id == b.id)//ToolMoldMaintainPlan
.LeftJoin<ToolMoldMaintainRunRecord>((a, b, c) => b.plan_code == c.plan_code)
.Where(a => a.maintain_plan_id == planId)
.Select((a, b, c) => new
{
mold_id = a.mold_id,
plan_start_time = c.plan_start_time,
})
.FirstAsync();
if (planMoldRelation != null)
{
var mold = await _db.Queryable<ToolMolds>().FirstAsync(it => it.id == planMoldRelation.mold_id);
if (mold != null)
{
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.plan_start_time = planMoldRelation.plan_start_time;
var moldEqpRelation = await _db.Queryable<ToolMoldsEquipment>().FirstAsync(it => it.mold_id == mold.id);
if (moldEqpRelation != null)
{
var eqp = await _db.Queryable<EqpEquipment>().FirstAsync(it => it.id == moldEqpRelation.equipment_id);
info.eqp_code = eqp.code;
info.eqp_name = eqp.name;
}
}
}
return info;
}
/// <summary>
/// 根据模具ID获取保养组及项目信息
/// </summary>
/// <param name="moldId">模具ID</param>
/// <returns></returns>
[HttpGet]
public async Task<dynamic> GetCheckItemAndGrpByMoldId([FromRoute] string moldId)
{
if (moldId.IsNullOrEmpty()) throw new ArgumentException($"parameter {nameof(moldId)} not be null or empty");
var itemGroupRelation = await _db.Queryable<ToolMoldMaintainGroupRelation>().FirstAsync(it => it.mold_id == moldId);
if (itemGroupRelation != null)
{
var checkItems = await _db.Queryable<ToolMoldMaintainGroup, ToolMoldMaintainGroupItem, ToolMoldMaintainItem>((a, b, c) => new JoinQueryInfos
(
JoinType.Left, a.id == b.item_group_id,
JoinType.Left, b.item_id == c.id
))
.Where(a => a.id == itemGroupRelation.item_group_id)
.Select((a, b, c) => new
{
item_group_id = a.id,
item_group_name = a.name,
item_id = c.id,
item_name = c.name,
}).ToListAsync();
return checkItems;
}
return Enumerable.Empty<dynamic>();
}
/// <summary>
/// 模具保养计划执行-开始模具保养
/// </summary>
/// <param name="input">
/// {
/// plan_id:执行计划id
/// }
/// </param>
/// <returns></returns>
[HttpPost]
public async Task MaintainStart(MoldMaintainRunUpInput input)
{
try
{
await _db.Ado.BeginTranAsync();
var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.MaintainStatusTypeId);
var plan = await _db.Queryable<ToolMoldMaintainPlan>().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<ToolMoldMaintainPlanRelation>().FirstAsync(it => it.maintain_plan_id == input.plan_id);
if (moldPlanRelation != null)
{
var mold = await _db.Queryable<ToolMolds>().FirstAsync(it => it.id == moldPlanRelation.mold_id);
record.mold_code = mold?.mold_code;
record.mold_name = mold?.mold_name;
var moldGroupRelation = await _db.Queryable<ToolMoldMaintainGroupRelation>().FirstAsync(it => it.mold_id == mold.id);
if (moldGroupRelation != null)
{
var maintainGroup = await _db.Queryable<ToolMoldMaintainGroup>().FirstAsync(it => it.id == moldGroupRelation.item_group_id);
record.group_name = maintainGroup.name;
var itemGrpRelation = await _db.Queryable<ToolMoldMaintainGroupItem>().FirstAsync(it => it.item_group_id == maintainGroup.id);
if (itemGrpRelation != null)
{
var checkItem = await _db.Queryable<ToolMoldMaintainItem>().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;
}
}
public async Task MaintainItemFinish(MoldMaintainRunUpInput input)
{
if (input == null) throw new ArgumentNullException("input");
if (input.itemIds == null || input.itemIds.Count == 0) throw new ArgumentException($"parameter {nameof(input.itemIds)} not be null or empty");
var row = await _db.Updateable<ToolMoldMaintainItem>().SetColumns(it => new ToolMoldMaintainItem { status = input.status }).Where(it => input.itemIds.Contains(it.id)).ExecuteCommandAsync();
if (row < 1) throw Oops.Oh(ErrorCode.COM1001);
}
/// <summary>
/// 模具保养计划执行-保养完成
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
public async Task MaintainFinish(MoldMaintainRunUpInput input)
{
if (input == null) throw new ArgumentNullException("input");
var grpIds = await _db.Queryable<ToolMoldMaintainGroupRelation>()
.LeftJoin<ToolMolds>((a, b) => a.mold_id == b.id)
.Where(a => a.mold_id == input.mold_id)
.Select((a, b) => a.item_group_id)
.Distinct()
.ToListAsync();
var itemIds = await _db.Queryable<ToolMoldMaintainGroupItem>().Where(it => grpIds.Contains(it.item_group_id)).Select(it => it.item_id).ToListAsync();
if (itemIds?.Count > 0)
{
var items = await _db.Queryable<ToolMoldMaintainItem>().Where(it => itemIds.Contains(it.id) && it.status.HasValue && it.status.Value == 1).ToListAsync();
if (items?.Count < itemIds.Count)
{
throw new AppFriendlyException("当前模具有未完成的保养项目", 500);
}
var row = await _db.Updateable<ToolMolds>().SetColumns(it => new ToolMolds { mold_status = MoldUseStatus.MOLDUSESTATUSZKID }).Where(it => it.id == input.mold_id).ExecuteCommandAsync();
if (row < 1) throw Oops.Oh(ErrorCode.COM1001);
var allMoldStatus = await _db.Queryable<ToolMoldMaintainPlanRelation>().InnerJoin<ToolMolds>((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.MOLDUSESTATUSZKID))
{
row = await _db.Updateable<ToolMoldMaintainPlan>().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);
}
}
}
}