using JNPF.Common.Core.Manager; using JNPF.Common.Dtos.Message; using JNPF.Common.Enums; using JNPF.Common.Filter; using JNPF.Common.Security; using JNPF.DependencyInjection; using JNPF.DynamicApiController; using JNPF.FriendlyException; using JNPF.Message.Entitys.Entity; using JNPF.Message.Service; using JNPF.Systems.Entitys.Permission; using JNPF.Systems.Entitys.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.BasicData.Entities; using Tnb.BasicData.Interfaces; using Tnb.ProductionMgr.Entities; using Tnb.ProductionMgr.Entities.Dto; using Tnb.ProductionMgr.Entities.Entity; using Tnb.ProductionMgr.Interfaces; using MessageTemplateEntity = JNPF.Message.Entitys.Entity.MessageTemplateEntity; namespace Tnb.ProductionMgr { [ApiDescriptionSettings(Tag = ModuleConst.Tag, Area = ModuleConst.Area, Order = 700)] [Route("api/[area]/[controller]/[action]")] public class AndonRecordService : IAndonRecordService, IDynamicApiController, ITransient { private readonly ISqlSugarClient _db; private readonly IUserManager _userManager; private readonly SendMessageService _sendMessageService; private readonly IBasPushRuleLogService _basPushRuleLogService; private readonly TimeTaskService _timeTaskService; public AndonRecordService(ISqlSugarRepository repository, SendMessageService sendMessageService, TimeTaskService timeTaskService, IBasPushRuleLogService basPushRuleLogService, IUserManager userManager) { _userManager = userManager; _sendMessageService = sendMessageService; _timeTaskService = timeTaskService; _basPushRuleLogService = basPushRuleLogService; _db = repository.AsSugarClient(); } [HttpPost] public async Task GetAndonPadList(AndonPadListInput input) { SqlSugarPagedList result = await _db.Queryable() .LeftJoin((a, b) => a.andon_info_id == b.id) .LeftJoin((a, b, c) => a.repair_id == c.Id) .LeftJoin((a, b, c, d) => a.create_id == d.Id) .LeftJoin((a, b, c, d, e) => e.EnCode == DictConst.AndonStatus) .LeftJoin((a, b, c, d, e, f) => a.status == f.EnCode && e.Id == f.DictionaryTypeId) .Where((a, b, c, d) => a.andon_type_id == input.andon_type_id) .WhereIF(!string.IsNullOrEmpty(input.status), (a, b, c, d) => a.status == input.status) .Select((a, b, c, d, e, f) => new AndonPadListOutput { id = a.id, andon_info_name = b.name, promoter_name = d.RealName, repair_name = c.RealName, promoter_time = a.create_time == null ? "" : a.create_time.Value.ToString(DbTimeFormat.SS), response_time = a.response_time == null ? "" : a.response_time.Value.ToString(DbTimeFormat.SS), start_repair_time = a.start_repair_time == null ? "" : a.start_repair_time.Value.ToString(DbTimeFormat.SS), end_repair_time = a.end_repair_time == null ? "" : a.end_repair_time.Value.ToString(DbTimeFormat.SS), confirm_time = a.confirm_time == null ? "" : a.confirm_time.Value.ToString(DbTimeFormat.SS), status = f.FullName, }).ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } [HttpPost] public async Task GetAndonPdaList(AndonPdaListInput input) { Dictionary queryJson = string.IsNullOrEmpty(input.queryJson) ? new Dictionary() : input.queryJson.ToObject>(); string? status = queryJson.ContainsKey("status") ? queryJson["status"].ToString() : ""; List statusList = new(); if (!string.IsNullOrEmpty(status)) { switch (status) { case "1": statusList.Add(DictConst.AndonStatusHJZ); break; case "2": statusList.Add(DictConst.AndonStatusYXY); statusList.Add(DictConst.AndonStatusCLZ); break; case "3": statusList.Add(DictConst.AndonStatusDQR); statusList.Add(DictConst.AndonStatusYWC); break; } } if (string.IsNullOrEmpty(input.sidx)) { input.sidx = "promoter_time"; input.sort = "desc"; } SqlSugarPagedList result = await _db.Queryable() .LeftJoin((a, b) => a.andon_info_id == b.id) .LeftJoin((a, b, c) => a.repair_id == c.Id) .LeftJoin((a, b, c, d) => a.create_id == d.Id) .LeftJoin((a, b, c, d, e) => e.EnCode == DictConst.AndonStatus) .LeftJoin((a, b, c, d, e, f) => a.status == f.EnCode && e.Id == f.DictionaryTypeId) .LeftJoin((a,b,c,d,e,f,g)=>a.breakdown==g.id) .LeftJoin((a,b,c,d,e,f,g,h)=>a.andon_type_id==h.id) .WhereIF(statusList.Count > 0, a => statusList.Contains(a.status)) .Select((a, b, c, d, e, f,g,h) => new AndonPadListOutput { id = a.id, andon_info_name = b.name, promoter_name = d.RealName, repair_name = c.RealName, promoter_time = a.create_time == null ? "" : a.create_time.Value.ToString(DbTimeFormat.SS), response_time = a.response_time == null ? "" : a.response_time.Value.ToString(DbTimeFormat.SS), start_repair_time = a.start_repair_time == null ? "" : a.start_repair_time.Value.ToString(DbTimeFormat.SS), end_repair_time = a.end_repair_time == null ? "" : a.end_repair_time.Value.ToString(DbTimeFormat.SS), confirm_time = a.confirm_time == null ? "" : a.confirm_time.Value.ToString(DbTimeFormat.SS), status = f.FullName, andon_type_name = h.name, breakdown_name = g.name }) .MergeTable() .OrderBy($"{input.sidx} {input.sort}") .ToPagedListAsync(input.currentPage, input.pageSize); return PageResult.SqlSugarPageResult(result); } [HttpPost] public async Task AddAndon(AddAndonInput input) { AndonInfo andonInfo = await _db.Queryable().SingleAsync(x => x.id == input.andon_info_id); AndonBreakDown andonBreakDown = await _db.Queryable().SingleAsync(x => x.id == input.andon_breakdown_id); AndonRecords andonRecords = input.Adapt(); andonRecords.breakdown = input.andon_breakdown_id; andonRecords.breakdown_type = andonBreakDown?.type_id; DbResult result = await _db.Ado.UseTranAsync(async () => { andonRecords.create_time = DateTime.Now; andonRecords.create_id = _userManager.UserId; andonRecords.status = DictConst.AndonStatusHJZ; List toUsers = new(); if (!string.IsNullOrEmpty(andonInfo.personnel)) { toUsers.AddRange(JsonConvert.DeserializeObject>(andonInfo.personnel)); } if (!string.IsNullOrEmpty(andonInfo.position)) { toUsers.AddRange(_db.Queryable().Where(x => andonInfo.position.Contains(x.Id)).Select(s => s.UserId).ToList()); } if (!string.IsNullOrEmpty(andonInfo.role)) { toUsers.AddRange(_db.Queryable().Where(x => andonInfo.role.Contains(x.Id)).Select(s => s.UserId).ToList()); } if (toUsers.Count() > 0) { List list = await _sendMessageService.SendTest(andonInfo.send_config_id); foreach (MessageSendModel item in list) { item.toUser = toUsers; } foreach (MessageSendModel item in list) { _ = await _sendMessageService.SendMessage(item, new Dictionary()); } } _ = await _db.Insertable(andonRecords).ExecuteCommandAsync(); }); if (result.IsSuccess) { BasPushRuleH basPushRuleH = await _db.Queryable().SingleAsync(x => x.id == andonInfo.push_rule_id); BasPushRuleD basPushRuleD = await _db.Queryable().FirstAsync(x => x.push_rule_id == andonInfo.push_rule_id && x.ordinal == 1); if (basPushRuleH?.enabled == 1) { string sendConfigId = basPushRuleD?.send_config_id ?? ""; List sendModels = await GetSendParamJson(sendConfigId); if (sendModels != null && sendModels.Count > 0) { DateTime executeTime = DateTime.Now.AddMinutes(basPushRuleD.interval); // string cron = $"0 {executeTime.Minute} {executeTime.Hour} {executeTime.Day} {executeTime.Month} ? {executeTime.Year}"; string cron = $"0 {executeTime.Minute} {executeTime.Hour} {executeTime.Day} {executeTime.Month} ?"; ContentModel comtentModel = new() { // comtentModel.cron = (2 * 60).ToString(); cron = cron, interfaceId = "", interfaceName = "", parameter = new List(), localHostTaskId = "PushMsgTimeWorker/PushMsg", startTime = DateTimeOffset.Now.ToUnixTimeMilliseconds(), // comtentModel.endTime = DateTimeOffset.Now.AddSeconds(3).ToUnixTimeMilliseconds().ToString(); TenantId = _userManager?.TenantId, TenantDbName = _userManager?.TenantDbName, ConnectionConfig = _userManager?.ConnectionConfig, Token = _userManager?.ToKen }; // foreach (var item in sendModels) // { // } // TimeTaskCrInput timeTaskCrInput = new() { enCode = DateTime.Now.ToString("yyyyMMddHHmmss"), fullName = "andon推送消息" + DateTime.Now.ToString("yyyyMMddHHmmss"), executeType = "3", executeContent = comtentModel.ToJsonString(), description = JsonConvert.SerializeObject(sendModels[0].paramJson), sortCode = 9999, enabledMark = 1, }; await _timeTaskService.Create(timeTaskCrInput, false); TimeTaskEntity timeTaskEntity = await _db.Queryable().Where(x => x.EnCode == timeTaskCrInput.enCode).FirstAsync(); BasPushRuleLog basPushRuleLog = new() { push_rule_id = andonInfo.push_rule_id, timetask_id = timeTaskEntity?.Id, biz_id = andonRecords.id, ordinal = 1, is_push = 1, }; _ = await _db.Insertable(basPushRuleLog).ExecuteCommandAsync(); } } } return !result.IsSuccess ? throw Oops.Oh(ErrorCode.COM1008) : (dynamic)(result.IsSuccess ? "发起成功" : result.ErrorMessage); } [HttpPost] public async Task ResponseAndon(Dictionary dic) { string id = dic.ContainsKey("id") ? dic["id"] : ""; AndonRecords andonRecords = await _db.Queryable().SingleAsync(x => x.id == id); if (andonRecords != null) { if (!string.IsNullOrEmpty(andonRecords.repair_id)) { UserEntity user = await _db.Queryable().SingleAsync(x => x.Id == andonRecords.repair_id); throw Oops.Bah($"{user.RealName}已响应"); } else { if (andonRecords.status != DictConst.AndonStatusHJZ) { throw Oops.Bah($"状态错误"); } else { _ = await _db.Updateable() .SetColumns(x => x.repair_id == _userManager.UserId) .SetColumns(x=>x.status==DictConst.AndonStatusYXY) .SetColumns(x => x.response_time == DateTime.Now) .Where(x => x.id == id) .ExecuteCommandAsync(); Dictionary postData = new() { ["id"] = andonRecords.id }; await _basPushRuleLogService.Stop(postData); return true; } } } else { throw Oops.Bah($"无该andon记录"); } } [HttpPost] public async Task StartAndon(Dictionary dic) { string id = dic.ContainsKey("id") ? dic["id"] : ""; AndonRecords andonRecords = await _db.Queryable().SingleAsync(x => x.id == id); if (andonRecords != null) { if (andonRecords.repair_id!=_userManager.UserId) { UserEntity user = await _db.Queryable().SingleAsync(x => x.Id == andonRecords.repair_id); throw Oops.Bah($"您不是处理人"); } else { if (andonRecords.status != DictConst.AndonStatusYXY) { throw Oops.Bah($"状态错误"); } else { _ = await _db.Updateable() .SetColumns(x => x.start_repair_time == DateTime.Now) .SetColumns(x=>x.status==DictConst.AndonStatusCLZ) .Where(x => x.id == id) .ExecuteCommandAsync(); return true; } } } else { throw Oops.Bah($"无该andon记录"); } } [HttpPost] public async Task EndAndon(Dictionary dic) { string id = dic.ContainsKey("id") ? dic["id"] : ""; AndonRecords andonRecords = await _db.Queryable().SingleAsync(x => x.id == id); if (andonRecords != null) { if (andonRecords.repair_id!=_userManager.UserId) { UserEntity user = await _db.Queryable().SingleAsync(x => x.Id == andonRecords.repair_id); throw Oops.Bah($"您不是处理人"); } else { if (andonRecords.status != DictConst.AndonStatusCLZ) { throw Oops.Bah($"状态错误"); } else { _ = await _db.Updateable() .SetColumns(x => x.end_repair_time == DateTime.Now) .SetColumns(x=>x.status==DictConst.AndonStatusDQR) .Where(x => x.id == id) .ExecuteCommandAsync(); return true; } } } else { throw Oops.Bah($"无该andon记录"); } } [HttpPost] public async Task ConfirmAndon(Dictionary dic) { string id = dic.ContainsKey("id") ? dic["id"] : ""; AndonRecords andonRecords = await _db.Queryable().SingleAsync(x => x.id == id); if (andonRecords != null) { if (andonRecords.repair_id!=_userManager.UserId) { UserEntity user = await _db.Queryable().SingleAsync(x => x.Id == andonRecords.repair_id); throw Oops.Bah($"您不是处理人"); } else { if (andonRecords.status != DictConst.AndonStatusDQR) { throw Oops.Bah($"状态错误"); } else { _ = await _db.Updateable() .SetColumns(x => x.confirm_time == DateTime.Now) .SetColumns(x=>x.status==DictConst.AndonStatusYWC) .Where(x => x.id == id) .ExecuteCommandAsync(); return true; } } } else { throw Oops.Bah($"无该andon记录"); } } private async Task> GetSendParamJson(string id) { List list = await _db.Queryable((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().Where(u => u.Type == "1" && u.EnCode == a.MessageType).Select(u => u.FullName), msgTemplateName = b.FullName, sendConfigId = a.SendConfigId, templateId = a.TemplateId, }).ToListAsync(); foreach (MessageSendModel? item in list) { // 是否存在参数. bool flag = await _db.Queryable().AnyAsync(x => x.TemplateId == item.templateId && x.DeleteMark == null); item.paramJson = flag ? await _db.Queryable((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() : await _db.Queryable((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; } } }