666 lines
35 KiB
C#
666 lines
35 KiB
C#
using System.Dynamic;
|
||
using JNPF.Common.Core.Manager;
|
||
using JNPF.Common.Enums;
|
||
using JNPF.Common.Filter;
|
||
using JNPF.Common.Security;
|
||
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 JNPF.VisualDev;
|
||
using JNPF.VisualDev.Entitys.Dto.VisualDevModelData;
|
||
using JNPF.VisualDev.Interfaces;
|
||
using Microsoft.AspNetCore.Mvc;
|
||
using Newtonsoft.Json.Linq;
|
||
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]")]
|
||
[OverideVisualDev(ModuleId)]
|
||
public class ToolMoldMaintainPlanRunService : IOverideVisualDevService,IToolMoldMaintainPlanRunService, IDynamicApiController, ITransient
|
||
{
|
||
private const string ModuleId = "26186915586085";
|
||
private readonly IRunService _runService;
|
||
private readonly IVisualDevService _visualDevService;
|
||
private readonly ISqlSugarClient _db;
|
||
private readonly IUserManager _userManager;
|
||
private readonly IDictionaryDataService _dictionaryDataService;
|
||
|
||
public OverideVisualDevFunc OverideFuncs { get; } = new OverideVisualDevFunc();
|
||
|
||
public ToolMoldMaintainPlanRunService(ISqlSugarRepository<ToolMoldMaintainRule> repository,
|
||
IUserManager userManager,
|
||
IRunService runService,
|
||
IVisualDevService visualDevService,
|
||
IDictionaryDataService dictionaryDataService)
|
||
{
|
||
_db = repository.AsSugarClient();
|
||
_runService = runService;
|
||
_visualDevService = visualDevService;
|
||
_userManager = userManager;
|
||
_dictionaryDataService = dictionaryDataService;
|
||
OverideFuncs.GetListAsync = GetList;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 生产bom列表
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
public async Task<dynamic> GetList(VisualDevModelListQueryInput input)
|
||
{
|
||
Dictionary<string, object> queryJson = string.IsNullOrEmpty(input.queryJson) ? new Dictionary<string, object>() : input.queryJson.ToObject<Dictionary<string, object>>();
|
||
string planCode = queryJson.GetValueOrDefault("plan_code", "").ToString();
|
||
string status = queryJson.GetValueOrDefault("status", "").ToString();
|
||
DateTime[] planStartDateArr = queryJson.ContainsKey("plan_start_date") ? queryJson["plan_start_date"].ToObject<long[]>().Select(x => DateTimeOffset.FromUnixTimeSeconds(x / 1000).ToLocalTime().DateTime).ToArray() : null;
|
||
DateTime[] planEndDateArr = queryJson.ContainsKey("plan_end_date") ? queryJson["plan_end_date"].ToObject<long[]>().Select(x => DateTimeOffset.FromUnixTimeSeconds(x / 1000).ToLocalTime().DateTime).ToArray() : null;
|
||
SqlSugarPagedList<ToolMoldMaintainPlanRunListOutput> list = await _db.Queryable<ToolMoldMaintainPlan, DictionaryDataEntity, UserEntity,DictionaryDataEntity,UserEntity>((a, b, c, d,e) => new object[]
|
||
{
|
||
JoinType.Left, a.mode == b.Id,
|
||
JoinType.Left, a.create_id == c.Id,
|
||
JoinType.Left,a.status==d.EnCode && d.DictionaryTypeId==DictConst.MaintainStatusTypeId,
|
||
JoinType.Left, a.starter_id == e.Id,
|
||
})
|
||
.WhereIF(!string.IsNullOrEmpty(planCode), (a, b, c, d) => a.plan_code.Contains(planCode))
|
||
.WhereIF(!string.IsNullOrEmpty(status), (a, b, c, d) => a.status==status)
|
||
.WhereIF(planStartDateArr!=null,(a, b, c, d) =>a.plan_start_date>=planStartDateArr[0] && a.plan_start_date<=planStartDateArr[1])
|
||
.WhereIF(planEndDateArr!=null,(a, b, c, d) =>a.plan_end_date>=planEndDateArr[0] && a.plan_end_date<=planEndDateArr[1])
|
||
.Select((a, b, c, d,e) => new ToolMoldMaintainPlanRunListOutput
|
||
{
|
||
id = a.id,
|
||
plan_code = a.plan_code,
|
||
mode = b.FullName,
|
||
status = d.FullName,
|
||
create_id = c.RealName,
|
||
create_time = a.create_time == null ? "" : a.create_time.Value.ToString(DbTimeFormat.SS),
|
||
plan_start_date = a.plan_start_date == null ? "" : a.plan_start_date.Value.ToString(DbTimeFormat.SS),
|
||
plan_end_date = a.plan_end_date == null ? "" : a.plan_end_date.Value.ToString(DbTimeFormat.SS),
|
||
remark = a.remark,
|
||
act_start_date = a.act_start_date == null ? "" : a.act_start_date.Value.ToString(DbTimeFormat.SS),
|
||
starter_id = e.RealName,
|
||
}).ToPagedListAsync(input.currentPage, input.pageSize);
|
||
|
||
return PageResult<ToolMoldMaintainPlanRunListOutput>.SqlSugarPageResult(list);
|
||
}
|
||
|
||
/// <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<ToolMolds>((a, b, c) => a.mold_id == c.id)
|
||
.LeftJoin<ToolMoldMaintainRunRecord>((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<string> moldids = planMoldRelations.Select(x => x.mold_id).ToList();
|
||
List<ToolMolds> molds = await _db.Queryable<ToolMolds>().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<ToolMoldsEquipment>().FirstAsync(it => it.mold_id == mold.id);
|
||
if (moldEqpRelation != null)
|
||
{
|
||
EqpEquipment 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([FromQuery] MaintainInfoQueryinput input)
|
||
{
|
||
Dictionary<string, string> dicstatus = new()
|
||
{
|
||
{ "UnMaintain", "待保养" },
|
||
{ "Completed", "已完成" }
|
||
};
|
||
List<dynamic> result = new();
|
||
List<ToolMoldMaintainPlan> plans = await _db.Queryable<ToolMoldMaintainPlan>().ToListAsync();
|
||
List<ToolMolds> ToolMolds = await _db.Queryable<ToolMolds>().ToListAsync();
|
||
List<ToolMoldsEquipment> ToolMoldsEquipments = await _db.Queryable<ToolMoldsEquipment>().ToListAsync();
|
||
List<EqpEquipment> EqpEquipments = await _db.Queryable<EqpEquipment>().ToListAsync();
|
||
List<DictionaryDataEntity> dic = await _db.Queryable<DictionaryDataEntity>().Where(p => p.DictionaryTypeId == "26149299883285").ToListAsync();
|
||
List<UserEntity> users = await _db.Queryable<UserEntity>().ToListAsync();
|
||
List<ToolMoldMaintainItemRecord> records = _db.Queryable<ToolMoldMaintainItemRecord>().ToList();
|
||
List<ToolMoldMaintainRunRecord> runrecords = _db.Queryable<ToolMoldMaintainRunRecord>().ToList();
|
||
foreach (ToolMoldMaintainPlan plan in plans)
|
||
{
|
||
var planMoldRelations = await _db.Queryable<ToolMoldMaintainPlanRelation>()
|
||
.LeftJoin<ToolMoldMaintainPlan>((a, b) => a.maintain_plan_id == b.id)
|
||
.LeftJoin<ToolMoldMaintainRunRecord>((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<string> mids = planMoldRelations.Select(x => x.mold_id).ToList();
|
||
List<ToolMolds> 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<dynamic> 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<string> records = await _db.Queryable<ToolMoldMaintainItemRecord>().Select(p => p.plan_id + p.mold_id).ToListAsync();
|
||
SqlSugarPagedList<PadMainListOutput> result = await _db.Queryable<ToolMoldMaintainPlanRelation>()
|
||
.LeftJoin<ToolMoldMaintainPlan>((a, b) => a.maintain_plan_id == b.id)
|
||
.LeftJoin<ToolMolds>((a, b, c) => a.mold_id == c.id)
|
||
.LeftJoin<ToolMoldMaintainRunRecord>((a, b, c, d) => b.plan_code == d.plan_code && c.mold_code == d.mold_code)
|
||
// .LeftJoin<ToolMoldMaintainItemRecord>((a, b, c, d, e) => e.plan_id == b.id && e.mold_id == c.id)
|
||
.LeftJoin<UserEntity>((a, b, c, d, e) => b.create_id == e.Id)
|
||
.LeftJoin<DictionaryDataEntity>((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<PadMainListOutput>.SqlSugarPageResult(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));
|
||
}
|
||
|
||
List<CheckItemOutput>? 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();
|
||
//新增功能
|
||
ToolMoldMaintainPlanRelation 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
|
||
{
|
||
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<dynamic> GetMaintainItem(MoldMaintainRunUpInput input)
|
||
{
|
||
var data = await _db.Queryable<ToolMoldMaintainItemRecord>()
|
||
.LeftJoin<ToolMoldMaintainItem>((a, b) => a.item_id == b.id)
|
||
.LeftJoin<ToolMoldMaintainGroup>((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;
|
||
}
|
||
/// <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");
|
||
}
|
||
|
||
bool flag = _db.Queryable<ToolMoldMaintainRunRecord>()
|
||
.LeftJoin<ToolMoldMaintainPlan>((a, b) => a.plan_code == b.plan_code)
|
||
.LeftJoin<ToolMolds>((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<string, object> dic = await _dictionaryDataService.GetDicByTypeId(DictConst.MaintainStatusTypeId);
|
||
ToolMolds mold = await _db.Queryable<ToolMolds>().FirstAsync(it => it.id == input.mold_id);
|
||
if (mold != null)
|
||
{
|
||
mold.mold_status = MoldUseStatus.MOLD_USE_STATUS_MAINTAIN_ID;
|
||
bool isOk = await _db.Updateable<ToolMolds>(mold).Where(it => it.id == input.mold_id).ExecuteCommandHasChangeAsync();
|
||
if (!isOk)
|
||
{
|
||
throw Oops.Oh(ErrorCode.COM1001);
|
||
}
|
||
|
||
ToolMoldMaintainPlan? 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)
|
||
{
|
||
|
||
await _db.Updateable<ToolMoldMaintainPlan>()
|
||
.SetColumns(x => x.act_start_date == DateTime.Now)
|
||
.SetColumns(x => x.starter_id == _userManager.UserId)
|
||
.Where(x => x.id == plan.id)
|
||
.ExecuteCommandAsync();
|
||
|
||
//插入保养计划记录
|
||
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<string> 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()
|
||
{
|
||
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;
|
||
}
|
||
|
||
}
|
||
|
||
/// <summary>
|
||
/// 模具保养完成
|
||
/// </summary>
|
||
/// <returns></returns>
|
||
[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<ToolMoldMaintainPlan>().Where(p => p.id == input.plan_id).Select(p=>p.plan_code).First();
|
||
var mold_code = _db.Queryable<ToolMolds>().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<ToolMoldMaintainRunRecord>().SetColumns(it => new ToolMoldMaintainRunRecord { plan_end_time = DateTime.Now }).Where(it => it.plan_code == plan_code && it.mold_code== mold_code).ExecuteCommandAsync();
|
||
}
|
||
|
||
List<ToolMoldMaintainItemRecord> 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,
|
||
reason = item.reason,
|
||
};
|
||
records.Add(record);
|
||
}
|
||
_ = await _db.Insertable(records).ExecuteCommandAsync();
|
||
_ = await _db.Updateable<ToolMolds>().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<ToolMoldMaintainPlanRelation>().Where(p => p.maintain_plan_id == input.plan_id).Select(p => p.mold_id).Distinct().CountAsync();
|
||
int finish = await _db.Queryable<ToolMoldMaintainItemRecord>().Where(p => p.plan_id == input.plan_id).Select(p => p.mold_id).Distinct().CountAsync();
|
||
if (count == finish)
|
||
{
|
||
_ = await _db.Updateable<ToolMoldMaintainPlan>().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<ToolMoldMaintainItemRecord> 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);
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
|
||
/// <summary>
|
||
/// 模具保养计划执行-保养完成
|
||
/// </summary>
|
||
/// <param name="input"></param>
|
||
/// <returns></returns>
|
||
[HttpPost]
|
||
public async Task MaintainFinish(MoldMaintainRunUpInput input)
|
||
{
|
||
List<CheckItemOutput>? 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();
|
||
//新增功能
|
||
ToolMoldMaintainPlanRelation 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
|
||
{
|
||
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<CheckItemOutput>? 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<ToolMolds>().SetColumns(it => new ToolMolds { mold_status = MoldUseStatus.MOLD_USE_STATUS_ZK_ID }).Where(it => it.id == input.mold_id).ExecuteCommandAsync();
|
||
List<string?> 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);
|
||
}
|
||
}
|
||
|
||
}
|
||
}
|