Files
tnb.server/EquipMgr/Tnb.EquipMgr/ToolMoldMaintainRunService.cs
2023-09-12 17:33:25 +08:00

413 lines
21 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.Reactive.Joins;
using System.Text;
using System.Threading.Tasks;
using Aop.Api.Domain;
using Aspose.Cells.Drawing;
using DingTalk.Api.Request;
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.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
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)
{
List<dynamic> result = new();
var planMoldRelations = 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,
})
.ToListAsync();
if (planMoldRelations?.Count > 0)
{
var mids = planMoldRelations.Select(x => x.mold_id).ToList();
var molds = await _db.Queryable<ToolMolds>().Where(it => mids.Contains(it.id)).ToListAsync();
if (molds?.Count > 0)
{
for (int i = 0, cnt = molds.Count; i < cnt; i++)
{
var mold = molds[i];
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.plan_start_time = planMoldRelations[i].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;
}
result.Add(info);
}
}
}
return result;
}
[HttpGet]
public async Task<dynamic> GetMaintainInfo()
{
List<dynamic> result = new();
var plans = await _db.Queryable<ToolMoldMaintainPlan>().ToListAsync();
var ToolMolds = await _db.Queryable<ToolMolds>().ToListAsync();
var ToolMoldsEquipments = await _db.Queryable<ToolMoldsEquipment>().ToListAsync();
var EqpEquipments = await _db.Queryable<EqpEquipment>().ToListAsync();
var dic = await _db.Queryable<DictionaryDataEntity>().Where(p => p.DictionaryTypeId == "26149299883285").ToListAsync();
var users = await _db.Queryable<UserEntity>().ToListAsync();
foreach (var plan in plans)
{
var planMoldRelations = 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 == plan.id)
.Select((a, b, c) => new
{
mold_id = a.mold_id,
plan_start_time = c.plan_start_time,
}).ToListAsync();
if (planMoldRelations?.Count > 0)
{
var mids = planMoldRelations.Select(x => x.mold_id).ToList();
var molds = ToolMolds.Where(it => mids.Contains(it.id)).ToList();
if (molds?.Count > 0)
{
List<dynamic> infos = new List<dynamic> { };
for (int i = 0, cnt = molds.Count; i < cnt; i++)
{
var mold = molds[i];
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 = planMoldRelations[i].plan_start_time == null ? "" : ((DateTime)planMoldRelations[i].plan_start_time).ToString("yyyy-MM-dd");
info.createtime = plan.create_time == null ? "" : ((DateTime)plan.create_time).ToString("yyyy-MM-dd");
info.status = 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;
var moldEqpRelation = ToolMoldsEquipments.Where(it => it.mold_id == mold.id).FirstOrDefault();
if (moldEqpRelation != null)
{
var 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);
}
}
}
}
return result;
}
/// <summary>
/// 根据计划Id、模具ID获取保养组及项目信息
/// </summary>
/// <param name="input">
/// 参数:
/// <br/>{
/// <br/> plan_id:计划Id
/// <br/> mold_id:模具Id
/// <br/>}
/// </param>
/// <remarks>
/// returns:
/// <br/>{
/// <br/> plan_id:计划ID
/// <br/> mold_id:模具ID
/// <br/> item_group_id:保养组ID
/// <br/> item_group_name:保养组名称
/// <br/> item_id:保养项ID
/// <br/> item_name:保养项名称
/// <br/>}
/// </remarks>
/// <exception cref="ArgumentNullException"></exception>
[HttpGet]
public async Task<dynamic> GetCheckItemAndGrpByMoldId([FromQuery] CheckItemQueryinput input)
{
if (input == null) throw new ArgumentNullException(nameof(input));
var items = await _db.Queryable<ToolMoldMaintainPlanRelation>()
.InnerJoin<ToolMoldMaintainGroupRelation>((a, b) => a.mold_id == b.mold_id)
.InnerJoin<ToolMoldMaintainGroupItem>((a, b, c) => b.item_group_id == c.item_group_id)
.InnerJoin<ToolMoldMaintainGroup>((a, b, c, d) => c.item_group_id == d.id)
.InnerJoin<ToolMoldMaintainItem>((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();
//新增功能
var ToolMoldMaintainPlanRelation= _db.Queryable<ToolMoldMaintainPlanRelation>().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<ToolMoldMaintainItemRecord>().Where(it => it.plan_id == input.plan_id && it.mold_id == input.mold_id).Select(it => new
{
plan_id = it.plan_id,
item_id = it.item_id,
item_group_id = it.item_group_id,
mold_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 (var item in items)
{
var 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;
}
/// <summary>
/// 模具保养计划执行-开始模具保养
/// </summary>
/// <param name="input">
/// {
/// plan_id:执行计划id
/// }
/// </param>
/// <returns></returns>
[HttpPost]
public async Task MaintainStart(MoldMaintainRunUpInput input)
{
if (input == null) throw new ArgumentNullException("input");
try
{
await _db.Ado.BeginTranAsync();
var dic = await _dictionaryDataService.GetDicByTypeId(DictConst.MaintainStatusTypeId);
var mold = await _db.Queryable<ToolMolds>().FirstAsync(it => it.id == input.mold_id);
if (mold != null)
{
mold.mold_status = MoldUseStatus.MOLD_USE_STATUS_MAINTAIN_ID;
var isOk = await _db.Updateable<ToolMolds>(mold).Where(it => it.id == input.mold_id).ExecuteCommandHasChangeAsync();
if (!isOk) throw Oops.Oh(ErrorCode.COM1001);
var plan = await _db.Queryable<ToolMoldMaintainPlanRelation>().LeftJoin<ToolMoldMaintainPlan>((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();
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;
record.mold_code = mold.mold_code;
record.mold_name = mold.mold_name;
record.plan_start_time = DateTime.Now;
var row = await _db.Insertable(record).ExecuteCommandAsync();
if (row < 1) throw Oops.Oh(ErrorCode.COM1001);
var groupids = _db.Queryable<ToolMoldMaintainPlanRelation>().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<ToolMoldMaintainGroupRelation>()
.LeftJoin<ToolMoldMaintainGroup>((a, b) => a.item_group_id == b.id)
.LeftJoin<ToolMoldMaintainGroupItem>((a, b, c) => b.id == c.item_group_id)
.LeftJoin<ToolMoldMaintainItem>((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<ToolMoldMaintainGroupRelation>()
.LeftJoin<ToolMoldMaintainGroup>((a, b) => a.item_group_id == b.id)
.LeftJoin<ToolMoldMaintainGroupItem>((a, b, c) => b.id == c.item_group_id)
.LeftJoin<ToolMoldMaintainItem>((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<ToolMoldMaintainRunRecordD> recordDs = new();
foreach (var info in maintainInfos)
{
ToolMoldMaintainRunRecordD record_d = new();
record_d.mainid = record.id;
record_d.group_id = info.group_id;
record_d.group_name = info.group_name;
record_d.check_item_id = info.check_item_id;
record_d.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;
}
}
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<ToolMoldMaintainItemRecord> records = new();
foreach (var item in input.items)
{
ToolMoldMaintainItemRecord record = new();
record.plan_id = input.plan_id;
record.mold_id = input.mold_id;
record.item_group_id = item.item_group_id;
record.item_id = item.item_id;
record.status = 1;
records.Add(record);
}
var row = await _db.Insertable(records).ExecuteCommandAsync();
if (row < 1) throw Oops.Oh(ErrorCode.COM1001);
}
/// <summary>
/// 模具保养计划执行-保养完成
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
public async Task MaintainFinish(MoldMaintainRunUpInput input)
{
var items = await _db.Queryable<ToolMoldMaintainPlanRelation>()
.InnerJoin<ToolMoldMaintainGroupRelation>((a, b) => a.mold_id == b.mold_id)
.InnerJoin<ToolMoldMaintainGroupItem>((a, b, c) => b.item_group_id == c.item_group_id)
.InnerJoin<ToolMoldMaintainGroup>((a, b, c, d) => c.item_group_id == d.id)
.InnerJoin<ToolMoldMaintainItem>((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();
//新增功能
var ToolMoldMaintainPlanRelation = _db.Queryable<ToolMoldMaintainPlanRelation>().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<ToolMoldMaintainItemRecord>().Where(it => it.plan_id == input.plan_id && it.mold_id == input.mold_id).Select(it => new
{
plan_id = it.plan_id,
item_id = it.item_id,
item_group_id = it.item_group_id,
mold_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());
var 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);
}
}
var row = await _db.Updateable<ToolMolds>().SetColumns(it => new ToolMolds { mold_status = MoldUseStatus.MOLD_USE_STATUS_ZK_ID }).Where(it => it.id == input.mold_id).ExecuteCommandAsync();
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.MOLD_USE_STATUS_ZK_ID))
{
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);
}
}
}