推送规则,异常停机推送

This commit is contained in:
2023-07-06 11:18:02 +08:00
parent c6e46b4ad9
commit 902c0eb301
16 changed files with 712 additions and 60 deletions

View File

@@ -6,6 +6,7 @@ using System.Text;
using System.Threading.Tasks;
using Aop.Api.Domain;
using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.Message;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Security;
@@ -13,10 +14,17 @@ using JNPF.DependencyInjection;
using JNPF.DynamicApiController;
using JNPF.FriendlyException;
using JNPF.Logging;
using JNPF.Message.Entitys.Entity;
using JNPF.Message.Service;
using JNPF.Systems.Entitys.Permission;
using JNPF.Systems.Entitys.System;
using JNPF.Systems.Interfaces.System;
using JNPF.TaskScheduler;
using JNPF.TaskScheduler.Entitys.Dto.TaskScheduler;
using JNPF.TaskScheduler.Entitys.Model;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using SqlSugar;
using Tnb.BasicData;
using Tnb.EquipMgr.Entities;
@@ -25,6 +33,8 @@ using Tnb.EquipMgr.Interfaces;
using Tnb.ProductionMgr.Entities;
using Tnb.ProductionMgr.Entities.Dto.PrdManage;
using Tnb.ProductionMgr.Interfaces;
using Tnb.BasicData.Entities;
using MessageTemplateEntity = JNPF.Message.Entitys.Entity.MessageTemplateEntity;
namespace Tnb.ProductionMgr
{
@@ -42,9 +52,12 @@ namespace Tnb.ProductionMgr
private readonly IPrdMoTaskService _prdMoTaskService;
private readonly IUserManager _userManager;
private readonly IBillRullService _billRullService;
private readonly TimeTaskService _timeTaskService;
private SendMessageService _sendMessageService;
private static Dictionary<string, object> _dicWorkStationAndShopRelacion = new Dictionary<string, object>();
private static Dictionary<string, object> _dicWorkShop = new Dictionary<string, object>();
private const string PUSHRULEID = "27121606262805";//异常停机的推送规则id
@@ -54,6 +67,8 @@ namespace Tnb.ProductionMgr
IToolMoldsService moldsService,
IToolMoldMaintainTaskService maintainTaskService,
IBillRullService billRullService,
TimeTaskService timeTaskService,
SendMessageService sendMessageService,
IUserManager userManager
)
{
@@ -62,6 +77,8 @@ namespace Tnb.ProductionMgr
_moldService = moldsService;
_userManager = userManager;
_billRullService = billRullService;
_timeTaskService = timeTaskService;
_sendMessageService = sendMessageService;
_db = repository.AsSugarClient();
}
/// <summary>
@@ -194,77 +211,175 @@ namespace Tnb.ProductionMgr
{
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");
var eqp = await _db.Queryable<EqpEquipment>().FirstAsync(it => it.id == input.eqp_id);
var cancelCloseDown = input.Adapt<PrdCancelClosedown>();
EqpRepairApply eqpRepairApply = null;
BasPushRuleD basPushRuleD = await _db.Queryable<BasPushRuleD>().FirstAsync(x => x.push_rule_id == PUSHRULEID && x.ordinal == 1);
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)
DbResult<bool> result = await _db.Ado.UseTranAsync(async () =>
{
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 eqp = await _db.Queryable<EqpEquipment>().FirstAsync(it => it.id == input.eqp_id);
var record = cancelCloseDown.Adapt<PrdCancelClosedownRecord>();
record.eqp_code = eqp?.code;
record.eqp_name = eqp?.name;
await _db.Insertable(record).ExecuteCommandAsync();
var moldId = moTaskList.First().mold_id;
if (!moldId.IsNullOrEmpty())
var moTaskList = await _prdMoTaskService.GetListByEqpId(input.eqp_id);
if (moTaskList?.Count > 1) throw new AppFriendlyException($"设备{input.eqp_id},目前有两条进行中的生产任务", 500);
if (moTaskList?.Count > 0)
{
var mold = await _moldService.GetListById(moldId);
var maintaindTask = new ToolMoldMaintainTask();
maintaindTask.mold_id = moldId;
maintaindTask.code = mold.mold_code;
maintaindTask.create_id = _userManager.UserId;
maintaindTask.status = DictConst.UnMaintainStatusCode;
maintaindTask.create_time = DateTime.Now;
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 _maintainTaskService.Create(maintaindTask);
}
await _db.Insertable(cancelCloseDown).ExecuteCommandAsync();
var record = cancelCloseDown.Adapt<PrdCancelClosedownRecord>();
record.eqp_code = eqp?.code;
record.eqp_name = eqp?.name;
await _db.Insertable(record).ExecuteCommandAsync();
string reason = "";
if (!string.IsNullOrEmpty(input.reason))
{
List<PrdCancelClosedownReason> prdCancelClosedownReasons = await _db.Queryable<PrdCancelClosedownReason>().ToListAsync();
string[] reasonArr = input.reason.Split(",");
var reasonList = prdCancelClosedownReasons.Where(x => reasonArr.Contains(x.id)).Select(x => x.reason).ToList();
reason = string.Join(',', reasonList);
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 = mold?.mold_code ?? "";
maintaindTask.create_id = _userManager.UserId;
maintaindTask.status = DictConst.UnMaintainStatusCode;
maintaindTask.create_time = DateTime.Now;
await _maintainTaskService.Create(maintaindTask);
}
string reason = "";
if (!string.IsNullOrEmpty(input.reason))
{
List<PrdCancelClosedownReason> prdCancelClosedownReasons = await _db.Queryable<PrdCancelClosedownReason>().ToListAsync();
string[] reasonArr = input.reason.Split(",");
var reasonList = prdCancelClosedownReasons.Where(x => reasonArr.Contains(x.id)).Select(x => x.reason).ToList();
reason = string.Join(',', reasonList);
}
string code = await _billRullService.GetBillNumber("EqpRepair");
eqpRepairApply = new EqpRepairApply()
{
code = code,
name = eqp.name + "停机",
equip_id = input.eqp_id,
status = Tnb.EquipMgr.RepairApplyStatus.TOBEEXECUTED,
description = reason,
create_id = _userManager.UserId,
create_time = DateTime.Now,
remark = input.remark,
};
await _db.Insertable(eqpRepairApply).ExecuteCommandAsync();
if (!string.IsNullOrEmpty(eqp.responsibler_id))
{
List<MessageSendModel> list = await _sendMessageService.SendTest(basPushRuleD.send_config_id);
List<string> toUsers = JsonConvert.DeserializeObject<List<string>>(eqp.responsibler_id);
foreach (var item in list)
{
foreach (var param in item.paramJson)
{
if (param.field=="equip_code")
{
param.value = eqp.code;
}
}
item.toUser = toUsers;
}
foreach (var item in list)
{
await _sendMessageService.SendMessage(item, new Dictionary<string, object>());
}
}
}
string code = await _billRullService.GetBillNumber("EqpRepair");
EqpRepairApply eqpRepairApply = new EqpRepairApply()
else throw Oops.Oh(ErrorCode.COM1001);
});
if (result.IsSuccess)
{
BasPushRuleH basPushRuleH = await _db.Queryable<BasPushRuleH>().SingleAsync(x=>x.id==PUSHRULEID);
if (basPushRuleH?.enabled == 1)
{
code = code,
name = eqp.name + "停机",
equip_id = input.eqp_id,
status = Tnb.EquipMgr.RepairApplyStatus.TOBEEXECUTED,
description = reason,
create_id = _userManager.UserId,
create_time = DateTime.Now,
remark = input.remark,
};
string sendConfigId = basPushRuleD?.send_config_id ?? "";
List<MessageSendModel> sendModels = await GetSendParamJson(sendConfigId);
if (sendModels != null && sendModels.Count > 0)
{
DateTime executeTime = DateTime.Now.AddMinutes(2);
// string cron = $"0 {executeTime.Minute} {executeTime.Hour} {executeTime.Day} {executeTime.Month} ? {executeTime.Year}";
string cron = $"0 {executeTime.Minute} {executeTime.Hour} {executeTime.Day} {executeTime.Month} ?";
var comtentModel = new ContentModel();
// comtentModel.cron = (2 * 60).ToString();
comtentModel.cron = cron;
comtentModel.interfaceId = "";
comtentModel.interfaceName = "";
comtentModel.parameter = new List<InterfaceParameter>();
comtentModel.localHostTaskId = "PushMsgTimeWorker/PushMsg";
comtentModel.startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds();
// comtentModel.endTime = DateTimeOffset.Now.AddSeconds(3).ToUnixTimeMilliseconds().ToString();
comtentModel.TenantId = _userManager?.TenantId;
comtentModel.TenantDbName = _userManager?.TenantDbName;
comtentModel.ConnectionConfig = _userManager?.ConnectionConfig;
comtentModel.Token = _userManager?.ToKen;
foreach (var item in sendModels)
{
foreach (var param in item.paramJson)
{
if (param.field == "equip_code")
{
param.value = eqp?.code;
}
}
}
TimeTaskCrInput timeTaskCrInput = new TimeTaskCrInput()
{
enCode = DateTime.Now.ToString("yyyyMMddHHmmss"),
fullName = "临时推送消息" + DateTime.Now.ToString("yyyyMMddHHmmss"),
executeType = "3",
executeContent = comtentModel.ToJsonString(),
description = JsonConvert.SerializeObject(sendModels[0].paramJson),
sortCode = 9999,
enabledMark = 1,
};
await _db.Insertable(eqpRepairApply).ExecuteCommandAsync();
await _timeTaskService.Create(timeTaskCrInput,false);
TimeTaskEntity timeTaskEntity = await _db.Queryable<TimeTaskEntity>().Where(x => x.EnCode == timeTaskCrInput.enCode).FirstAsync();
BasPushRuleLog basPushRuleLog = new BasPushRuleLog()
{
push_rule_id = PUSHRULEID,
timetask_id = timeTaskEntity?.Id,
biz_id = eqpRepairApply.id,
ordinal = 1,
is_push = 1,
};
await _db.Insertable<BasPushRuleLog>(basPushRuleLog).ExecuteCommandAsync();
}
}
}
else throw Oops.Oh(ErrorCode.COM1001);
await _db.Ado.CommitTranAsync();
}
catch (Exception ex)
{
Log.Error("停机开始失败", ex);
await _db.Ado.RollbackTranAsync();
// await _db.Ado.RollbackTranAsync();
throw;
}
}
/// <summary>
/// 停机结束
@@ -304,5 +419,57 @@ namespace Tnb.ProductionMgr
throw;
}
}
private async Task<List<MessageSendModel>> GetSendParamJson(string id)
{
var list = await _db.Queryable<MessageSendTemplateEntity, MessageTemplateEntity>((a, b) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id))
.Where((a, b) => a.SendConfigId == id && a.DeleteMark == null && b.DeleteMark == null)
.Select((a, b) => new MessageSendModel
{
accountConfigId = a.AccountConfigId,
id = a.Id,
messageType = SqlFunc.Subqueryable<MessageDataTypeEntity>().Where(u => u.Type == "1" && u.EnCode == a.MessageType).Select(u => u.FullName),
msgTemplateName = b.FullName,
sendConfigId = a.SendConfigId,
templateId = a.TemplateId,
}).ToListAsync();
foreach (var item in list)
{
// 是否存在参数.
var flag = await _db.Queryable<MessageSmsFieldEntity>().AnyAsync(x => x.TemplateId == item.templateId && x.DeleteMark == null);
if (flag)
{
item.paramJson = await _db.Queryable<MessageTemplateParamEntity, MessageTemplateEntity, MessageSmsFieldEntity>((a, b, c) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id, JoinType.Left, a.TemplateId == c.TemplateId))
.Where((a, b, c) => a.TemplateId == item.templateId && a.DeleteMark == null && b.DeleteMark == null && a.Field == c.Field && a.Field != "@flowLink")
.Select((a, b) => new MessageSendParam
{
field = a.Field,
fieldName = a.FieldName,
id = a.Id,
templateCode = b.TemplateCode,
templateId = a.TemplateId,
templateName = b.FullName,
templateType = b.TemplateType
}).ToListAsync();
}
else
{
item.paramJson = await _db.Queryable<MessageTemplateParamEntity, MessageTemplateEntity>((a, b) => new JoinQueryInfos(JoinType.Left, a.TemplateId == b.Id))
.Where((a, b) => a.TemplateId == item.templateId && a.DeleteMark == null && b.DeleteMark == null && a.Field != "@flowLink")
.Where((a, b) => b.Title.Contains(a.Field) || b.Content.Contains(a.Field))
.Select((a, b) => new MessageSendParam
{
field = a.Field,
fieldName = a.FieldName,
id = a.Id,
templateCode = b.TemplateCode,
templateId = a.TemplateId,
templateName = b.FullName,
templateType = b.TemplateType
}).ToListAsync();
}
}
return list;
}
}
}

View File

@@ -11,6 +11,7 @@
<ItemGroup>
<ProjectReference Include="..\..\BasicData\Tnb.BasicData.Interfaces\Tnb.BasicData.Interfaces.csproj" />
<ProjectReference Include="..\..\EquipMgr\Tnb.EquipMgr.Interfaces\Tnb.EquipMgr.Interfaces.csproj" />
<ProjectReference Include="..\..\taskschedule\Tnb.TaskScheduler\Tnb.TaskScheduler.csproj" />
<ProjectReference Include="..\..\visualdev\Tnb.VisualDev.Engine\Tnb.VisualDev.Engine.csproj" />
<ProjectReference Include="..\..\WarehouseMgr\Tnb.WarehouseMgr.Entities\Tnb.WarehouseMgr.Entities.csproj" />
<ProjectReference Include="..\Tnb.ProductionMgr.Interfaces\Tnb.ProductionMgr.Interfaces.csproj" />