242 lines
10 KiB
C#
242 lines
10 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Aop.Api.Domain;
|
|
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Enums;
|
|
using JNPF.Common.Extension;
|
|
using JNPF.Common.Security;
|
|
using JNPF.DependencyInjection;
|
|
using JNPF.DynamicApiController;
|
|
using JNPF.FriendlyException;
|
|
using JNPF.Logging;
|
|
using JNPF.Systems.Entitys.Permission;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using SqlSugar;
|
|
using Tnb.BasicData;
|
|
using Tnb.EquipMgr.Entities;
|
|
using Tnb.EquipMgr.Entities.Dto;
|
|
using Tnb.EquipMgr.Interfaces;
|
|
using Tnb.ProductionMgr.Entities;
|
|
using Tnb.ProductionMgr.Entities.Dto.PrdManage;
|
|
using Tnb.ProductionMgr.Interfaces;
|
|
|
|
namespace Tnb.ProductionMgr
|
|
{
|
|
/// <summary>
|
|
/// 业务实现:生产管理
|
|
/// </summary>
|
|
[ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)]
|
|
[Route("api/[area]/[controller]/[action]")]
|
|
|
|
public class PrdCancelCloseDownService : IPrdCancelCloseDownService, IDynamicApiController, ITransient
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
private readonly IToolMoldMaintainTaskService _maintainTaskService;
|
|
private readonly IToolMoldsService _moldService;
|
|
private readonly IPrdMoTaskService _prdMoTaskService;
|
|
private readonly IUserManager _userManager;
|
|
|
|
private static Dictionary<string, object> _dicWorkStationAndShopRelacion = new Dictionary<string, object>();
|
|
private static Dictionary<string, object> _dicWorkShop = new Dictionary<string, object>();
|
|
|
|
|
|
|
|
public PrdCancelCloseDownService(
|
|
ISqlSugarRepository<PrdCancelClosedown> repository,
|
|
IPrdMoTaskService prdMoTaskService,
|
|
IToolMoldsService moldsService,
|
|
IToolMoldMaintainTaskService maintainTaskService,
|
|
IUserManager userManager
|
|
)
|
|
{
|
|
_prdMoTaskService = prdMoTaskService;
|
|
_maintainTaskService = maintainTaskService;
|
|
_moldService = moldsService;
|
|
_userManager = userManager;
|
|
_db = repository.AsSugarClient();
|
|
}
|
|
/// <summary>
|
|
/// 根据设备ID获取异常关机信息
|
|
/// </summary>
|
|
/// <param name="eqpId"></param>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<dynamic> GetClosedownEndList([FromRoute] string eqpId)
|
|
{
|
|
if (_dicWorkShop.Count < 1)
|
|
{
|
|
var orgs = await _db.Queryable<OrganizeEntity>().Where(it => it.Category == "workstation").ToListAsync();
|
|
if (orgs?.Count > 0)
|
|
{
|
|
var shopIds = orgs.Select(x =>
|
|
{
|
|
var shopId = "";
|
|
var orgTree = x.OrganizeIdTree.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
|
if (orgTree?.Length > 1)
|
|
{
|
|
shopId = orgTree[orgTree.Length - 2];
|
|
_dicWorkStationAndShopRelacion[x.EnCode] = shopId;
|
|
}
|
|
return shopId;
|
|
}).Where(x => !x.IsNullOrEmpty()).ToList();
|
|
if (shopIds?.Count > 0)
|
|
{
|
|
_dicWorkShop = await _db.Queryable<OrganizeEntity>().Where(it => it.Category == "department" && shopIds.Contains(it.Id)).ToDictionaryAsync(x => x.Id, x => x.FullName);
|
|
}
|
|
}
|
|
}
|
|
var result = new List<ClosedownEndListOutput>();
|
|
var closeDown = await _db.Queryable<PrdCancelClosedown>().FirstAsync(it => it.eqp_id == eqpId);
|
|
if (closeDown != null)
|
|
{
|
|
if (!closeDown.reason.IsNullOrEmpty())
|
|
{
|
|
var eqp = await _db.Queryable<EqpEquipment>().FirstAsync(it => it.id == eqpId);
|
|
EqpEquipType eqpTypeInfo = null;
|
|
if (eqp != null)
|
|
eqpTypeInfo = await _db.Queryable<EqpEquipType>().FirstAsync(it => it.id == eqp.equip_type_id);
|
|
var reasonArr = closeDown.reason.Split(",");
|
|
foreach (var reason in reasonArr)
|
|
{
|
|
ClosedownEndListOutput ot = new();
|
|
if (eqp != null)
|
|
{
|
|
if (_dicWorkStationAndShopRelacion.ContainsKey(eqp.station_code))
|
|
{
|
|
var shopId = _dicWorkStationAndShopRelacion[eqp.station_code].ToString();
|
|
ot.workshop_name = _dicWorkShop.ContainsKey(shopId) ? _dicWorkShop[shopId].ToString()! : "";
|
|
}
|
|
}
|
|
ot.reason = reason;
|
|
ot.eqp_code = eqp?.code;
|
|
ot.eqp_type_code = eqpTypeInfo?.code;
|
|
ot.operator_name = _userManager.RealName;
|
|
ot.remark = closeDown.remark;
|
|
ot.closeddown_start_time = closeDown.closedown_start_time;
|
|
result.Add(ot);
|
|
}
|
|
}
|
|
}
|
|
if (result.Count < 1)
|
|
{
|
|
ClosedownEndListOutput output = new();
|
|
result.Add(output);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 异常停机-开始
|
|
/// </summary>
|
|
/// <param name="input"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task CloseDownStart(CloseDownStartInput input)
|
|
{
|
|
if (input == null) throw new ArgumentNullException("input");
|
|
if (input.eqp_id.IsNullOrWhiteSpace()) throw new ArgumentException($"parameter {nameof(input.eqp_id)} not be null or empty");
|
|
try
|
|
{
|
|
await _db.Ado.BeginTranAsync();
|
|
|
|
var moTaskList = await _prdMoTaskService.GetListByEqpId(input.eqp_id);
|
|
if (moTaskList?.Count > 1) throw new AppFriendlyException($"设备{input.eqp_id},目前有两条进行中的生产任务", 500);
|
|
if (moTaskList.Count > 0)
|
|
{
|
|
var cancelCloseDown = input.Adapt<PrdCancelClosedown>();
|
|
cancelCloseDown.id = SnowflakeIdHelper.NextId();
|
|
cancelCloseDown.eqp_id = input.eqp_id;
|
|
cancelCloseDown.create_id = _userManager.UserId;
|
|
cancelCloseDown.create_time = DateTime.Now;
|
|
cancelCloseDown.closedown_start_time = DateTime.Now;
|
|
|
|
await _db.Insertable(cancelCloseDown).ExecuteCommandAsync();
|
|
var record = cancelCloseDown.Adapt<PrdCancelClosedownRecord>();
|
|
await _db.Insertable(record).ExecuteCommandAsync();
|
|
|
|
var moldId = moTaskList.First().mold_id;
|
|
if (!moldId.IsNullOrEmpty())
|
|
{
|
|
var mold = await _moldService.GetListById(moldId);
|
|
var maintaindTask = new ToolMoldMaintainTask();
|
|
maintaindTask.mold_id = moldId;
|
|
maintaindTask.code = DictConst.MaintainStatusDWXCode;
|
|
maintaindTask.create_id = _userManager.UserId;
|
|
maintaindTask.create_time = DateTime.Now;
|
|
|
|
await _maintainTaskService.Create(maintaindTask);
|
|
}
|
|
}
|
|
else throw Oops.Oh(ErrorCode.COM1001);
|
|
|
|
await _db.Ado.CommitTranAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error("停机开始失败", ex);
|
|
await _db.Ado.RollbackTranAsync();
|
|
throw ex;
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 停机结束
|
|
/// </summary>
|
|
/// <param name="maintainTaskId"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task CloseDownEnd(CloseDownStartInput input)
|
|
{
|
|
if (input == null) throw new ArgumentNullException("input");
|
|
if (input.eqp_id.IsNullOrWhiteSpace()) throw new ArgumentException($"parameter {nameof(input.eqp_id)} not be null or empty");
|
|
try
|
|
{
|
|
await _db.Ado.BeginTranAsync();
|
|
|
|
var moTaskList = await _prdMoTaskService.GetListByEqpId(input.eqp_id);
|
|
if (moTaskList?.Count > 1) throw new AppFriendlyException($"设备{input.eqp_id},目前有两条进行中的生产任务", 500);
|
|
|
|
var cancelCloseDown = input.Adapt<PrdCancelClosedown>();
|
|
cancelCloseDown.id = SnowflakeIdHelper.NextId();
|
|
cancelCloseDown.create_id = _userManager.UserId;
|
|
cancelCloseDown.create_time = DateTime.Now;
|
|
cancelCloseDown.closedown_start_time = DateTime.Now;
|
|
|
|
var row = await _db.Insertable(cancelCloseDown).ExecuteCommandAsync();
|
|
|
|
var moldId = moTaskList.First().mold_id;
|
|
if (moldId.IsNullOrEmpty())
|
|
{
|
|
var mold = await _moldService.GetListById(moldId);
|
|
var maintaindTask = new ToolMoldMaintainTask();
|
|
maintaindTask.mold_id = moldId;
|
|
maintaindTask.code = DictConst.MaintainStatusYWCCode;
|
|
maintaindTask.create_id = _userManager.UserId;
|
|
maintaindTask.create_time = DateTime.Now;
|
|
|
|
|
|
await _maintainTaskService.Create(maintaindTask);
|
|
}
|
|
|
|
await _db.Ado.CommitTranAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error("停机开始失败", ex);
|
|
await _db.Ado.RollbackTranAsync();
|
|
}
|
|
//var row = await _db.Updateable<ToolMoldMaintainTask>().SetColumns(c => new ToolMoldMaintainTask
|
|
//{
|
|
// status = DictConst.MaintainStatusDWXCode,
|
|
// modify_id = _userManager.UserId,
|
|
// modify_end_time = DateTime.Now,
|
|
//})
|
|
// .ExecuteCommandAsync();
|
|
//if (row < 1) throw Oops.Oh(ErrorCode.COM1001);
|
|
}
|
|
}
|
|
}
|