PushMsgService.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. using AutoMapper;
  2. using LinqKit;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.Extensions.Configuration;
  5. using Newtonsoft.Json;
  6. using Renci.SshNet.Messages;
  7. using Ropin.Inspection.Common;
  8. using Ropin.Inspection.Common.Accessor.Interface;
  9. using Ropin.Inspection.Common.Helper;
  10. using Ropin.Inspection.Model;
  11. using Ropin.Inspection.Model.Entities;
  12. using Ropin.Inspection.Model.ViewModel;
  13. using Ropin.Inspection.Repository;
  14. using Ropin.Inspection.Repository.Interface;
  15. using Ropin.Inspection.Repository.SYS.Interface;
  16. using Ropin.Inspection.Service.Interface;
  17. using System;
  18. using System.Collections.Generic;
  19. using System.Linq;
  20. using System.Net.Http;
  21. using System.Net.NetworkInformation;
  22. using System.Security.Claims;
  23. using System.Threading.Tasks;
  24. namespace Ropin.Inspection.Service
  25. {
  26. public class PushMsgService : IPushMsgService
  27. {
  28. private readonly IClaimsAccessor _claims;
  29. private readonly IMapper _mapper;
  30. private readonly IHttpContextAccessor _httpContextAccessor;
  31. private readonly IHttpClientFactory _httpClientFactory;
  32. private readonly ITsysUserRepository _tsysUserRepository;
  33. private readonly ITmtnPushMsgToRepository _tmtnPushMsgToRepository;
  34. private readonly ITmtnPushMsgResultRepository _tmtnPushMsgResultRepository;
  35. private readonly ITbdmCodeDetailRepository _tbdmCodeDetailRepository;
  36. private readonly ITdevDevStoreRepository _tdevDevStoreRepository;
  37. private readonly ITsysUserService _tsysUserService;
  38. private readonly ITsysMessageRepository _messageRepository;
  39. private readonly ITsysMessageFileRepository _tsysMessageFileRepository;
  40. private readonly ITdevDevSpotRepository _tdevDevSpotRepository;
  41. private readonly ITispSpotRepository _tispSpotRepository;
  42. private static readonly log4net.ILog log = log4net.LogManager.GetLogger(typeof(PushMsgService));
  43. private string IsSendEmail = "true";
  44. public PushMsgService(IClaimsAccessor claims,
  45. ITmtnPushMsgToRepository tmtnPushMsgToRepository,
  46. ITbdmCodeDetailRepository tbdmCodeDetailRepository,
  47. ITmtnPushMsgResultRepository tmtnPushMsgResultRepository,
  48. ITdevDevStoreRepository tdevDevStoreRepository,
  49. IMapper mapper,
  50. ITsysUserService tsysUserService,
  51. IHttpClientFactory httpClientFactory,
  52. ITsysUserRepository tsysUserRepository
  53. , IConfiguration configuration, ITsysMessageRepository messageRepository, ITsysMessageFileRepository tsysMessageFileRepository,
  54. ITdevDevSpotRepository tdevDevSpotRepository, ITispSpotRepository tispSpotRepository)
  55. {
  56. _mapper = mapper;
  57. _claims = claims;
  58. _httpClientFactory = httpClientFactory;
  59. _tsysUserRepository = tsysUserRepository;
  60. _tmtnPushMsgToRepository = tmtnPushMsgToRepository;
  61. _tbdmCodeDetailRepository = tbdmCodeDetailRepository;
  62. _tmtnPushMsgResultRepository = tmtnPushMsgResultRepository;
  63. _tdevDevStoreRepository = tdevDevStoreRepository;
  64. _tsysUserService = tsysUserService;
  65. _messageRepository = messageRepository;
  66. _tsysMessageFileRepository= tsysMessageFileRepository;
  67. _tdevDevSpotRepository = tdevDevSpotRepository;
  68. _tispSpotRepository = tispSpotRepository;
  69. string IsEmail = configuration.GetSection("IsSendEmail")?.Value;
  70. IsSendEmail = !string.IsNullOrEmpty(IsEmail)? IsEmail :"true";
  71. }
  72. /// <summary>
  73. ///
  74. /// </summary>
  75. /// <param name="pushModel"></param>
  76. /// <param name="Subject"></param>
  77. /// <param name="rabbitMQModel"></param>
  78. /// <param name="paramId">跳转小程序URLID</param>
  79. /// <param name="status">状态</param>
  80. /// <returns></returns>
  81. public async Task<bool> PushAlarmMsgAsync(TpushMsgModel pushModel,string Subject=null,RabbitMQModel rabbitMQModel=null, string paramId = null,string status=null, string title = null)
  82. {
  83. try
  84. {
  85. if (string.IsNullOrEmpty(pushModel.CreateOn))
  86. {
  87. pushModel.CreateOn = DateTime.Now.ToString();
  88. }
  89. string StoreCode = "", QRCode = "";
  90. List<MessageFile> messageFiles = pushModel.FileList;
  91. pushModel.FileList = null;
  92. log.Info($"发送消息-Begin-接收数据【{JsonConvert.SerializeObject(pushModel)}】");
  93. int msgStatus = DataDictionaryHelper.GetMsgStatus(pushModel.C_MsgTypeCode);
  94. pushModel.msgStatus = msgStatus;
  95. string createBy = "6e864cbc-5252-11ec-8681-fa163e02b3e4";
  96. var devStore = await _tdevDevStoreRepository.GetByIdAsync(pushModel.C_DevStoreCode);
  97. if(devStore == null) return await Task.FromResult(false);
  98. pushModel.DevName = devStore.C_Name;
  99. pushModel.DevNumber = devStore.C_NumberCode;
  100. var predicate = PredicateBuilder.New<TMTN_PushMsgTo>(true);//查询条件,推荐后台使用这种方式灵活筛选
  101. if (!string.IsNullOrEmpty(pushModel.C_DevStoreCode))
  102. {
  103. predicate = predicate.And(i => i.C_DevStoreCode.Equals(pushModel.C_DevStoreCode));
  104. }
  105. if (!string.IsNullOrEmpty(pushModel.C_MsgTypeCode))
  106. {
  107. predicate = predicate.And(i => i.C_MsgTypeCode.Equals(pushModel.C_MsgTypeCode));
  108. }
  109. IEnumerable<TMTN_PushMsgTo> pushMsgTo = await _tmtnPushMsgToRepository.GetByConditionAsync(predicate);
  110. IList<TMTN_PushMsgTo> pushMsgTolist = pushMsgTo.ToList();
  111. //IEnumerable<TSYS_User> users = await _tsysUserRepository.GetByConditionAsync(x=>x.C_Status == "1");
  112. IList<TSYS_User> userList = _tsysUserRepository.GetUserList();//users.ToList();
  113. IEnumerable<TBDM_CodeDetail> tBDMCodeDetail = await _tbdmCodeDetailRepository.GetAllAsync();
  114. IList<TBDM_CodeDetail> tBDMCodeDetailList = tBDMCodeDetail.ToList();
  115. if (!string.IsNullOrEmpty(pushModel.C_SpotCode))
  116. {
  117. Guid spotId = Guid.Parse(pushModel.C_SpotCode);
  118. var spotCond = await _tispSpotRepository.GetByConditionAsync(s => s.C_Code == spotId);
  119. TISP_Spot tISP_Spot = spotCond.FirstOrDefault();
  120. if (tISP_Spot != null)
  121. {
  122. StoreCode = tISP_Spot.C_StoreCode;
  123. QRCode = tISP_Spot.C_QRCode;
  124. if ((pushModel.C_MsgTypeCode == "MSG_TYPE_009" && (status == "5" || status == "7")) || (pushModel.C_MsgTypeCode == "MSG_TYPE_003" || pushModel.C_MsgTypeCode == "MSG_TYPE_004" || pushModel.C_MsgTypeCode == "MSG_TYPE_005"))
  125. {
  126. title = tISP_Spot.C_Name;
  127. }
  128. }
  129. }
  130. List<string> sendUserWechatIDList = new List<string>();
  131. List<TMTN_PushMsgResult> pushMsgResultList = new List<TMTN_PushMsgResult>();
  132. log.Info($"发送消息-pushMsgTolist【{JsonConvert.SerializeObject(pushMsgTolist)}】");
  133. foreach (var pushMsgToItem in pushMsgTolist)
  134. {
  135. var user = userList.Where(x => x.C_UserID.ToString() == pushMsgToItem.C_PushPersonCode)?.FirstOrDefault();
  136. if (user is null)
  137. continue;
  138. if (string.IsNullOrWhiteSpace(pushModel.UserName))
  139. {
  140. var creadUser = await _tsysUserService.GetByIdAsync(_claims.ApiUserId);
  141. createBy = _claims.ApiUserId.ToString();
  142. pushModel.UserName = creadUser?.C_Name;
  143. pushModel.UserMobile = creadUser?.C_Mobile;
  144. }
  145. if (pushMsgToItem.C_PushTypeCode == "PUSH_TYPE_001")//Email
  146. {
  147. if (!string.IsNullOrEmpty(user.C_Email))
  148. {
  149. if (!string.IsNullOrEmpty(user.C_Email))
  150. {
  151. switch (pushModel.C_MsgTypeCode)
  152. {
  153. case "MSG_TYPE_001"://通知维修消息
  154. break;
  155. case "MSG_TYPE_002"://通知运维消息
  156. break;
  157. case "MSG_TYPE_003"://通知巡检消息
  158. break;
  159. case "MSG_TYPE_004"://巡检异常消息
  160. break;
  161. case "MSG_TYPE_005"://异常消除消息
  162. break;
  163. case "MSG_TYPE_006"://维修确认消息
  164. break;
  165. case "MSG_TYPE_007"://维修取消消息
  166. break;
  167. case "MSG_TYPE_008"://维修完成消息
  168. break;
  169. case "MSG_TYPE_009"://运维取消消息
  170. break;
  171. case "MSG_TYPE_010"://异常确认消息
  172. break;
  173. case "MSG_TYPE_011"://设备启停消息
  174. break;
  175. case "MSG_TYPE_012"://设备点报警
  176. break;
  177. case "MSG_TYPE_013"://设备点报警取消
  178. break;
  179. case "MSG_TYPE_014"://正在维修消息
  180. break;
  181. case "MSG_TYPE_015"://维修返工消息
  182. break;
  183. case "MSG_TYPE_016"://维修完成确认
  184. break;
  185. default:
  186. break;
  187. }
  188. string msg =
  189. "<p><td>设备编号:</td>" + pushModel.DevNumber + "</p>" +
  190. "<p><td>设备名称:</td>" + pushModel.DevName + "</p>" +
  191. "<p><td>消息内容:</td>" + pushModel.Msg + "</p>" +
  192. "<p><td>设备地址:</td>" + pushModel.DevAddress + "</p>" +
  193. "<p><td> 报警时间:</td> " + pushModel.CreateOn + "</p>" +
  194. "<p><td> 上报人:</td> " + pushModel.UserName + "</p>"
  195. //+"<p><td> 上报人手机号:</td> " + pushModel.UserMobile + "</p>"
  196. ;
  197. if (IsSendEmail== "true")
  198. {
  199. var bols= EmailHelper.SendEmail(user.C_Email, tBDMCodeDetailList.Where(i => i.C_Code == pushMsgToItem.C_MsgTypeCode).Select(x => x.C_Name).FirstOrDefault(), pushModel.Subject, msg);
  200. if (!bols)
  201. {
  202. log.Info($"发送邮件失败【C_Email={user.C_Email},C_MsgTypeCode={pushMsgToItem.C_MsgTypeCode} ,C_DevStoreCode={pushModel.C_DevStoreCode}】");
  203. }
  204. }
  205. }
  206. }
  207. pushMsgResultList.Add(new TMTN_PushMsgResult
  208. {
  209. C_ID = Guid.NewGuid().ToString(),
  210. C_PushMsgToCode = pushMsgToItem.C_PushPersonCode,
  211. //C_Content = "消息主题:" + pushModel.Subject + " 设备编号:" + pushModel.DevNumber + " 设备名称:" + pushModel.DevName + " 设备地址:" + pushModel.DevAddress + " 消息内容:" + pushModel.Msg,
  212. C_Content = JsonConvert.SerializeObject(pushModel),
  213. C_PushTypeCode = pushMsgToItem.C_PushTypeCode,
  214. C_DevStoreCode = pushMsgToItem.C_DevStoreCode,
  215. C_MsgTypeCode = pushModel.C_MsgTypeCode,
  216. C_Subject = Subject,
  217. C_CreateBy = new Guid(createBy),
  218. D_CreateOn = DateTime.Now,
  219. C_Status = "1"
  220. }) ;
  221. }
  222. if (pushMsgToItem.C_PushTypeCode == "PUSH_TYPE_002")//wx
  223. {
  224. //if (!string.IsNullOrEmpty(user.C_WechatID)&& IsSendEmail == "true")
  225. //{
  226. // if (!sendUserWechatIDList.Contains(user.C_WechatID))
  227. // sendUserWechatIDList.Add(user.C_WechatID);
  228. //}
  229. log.Info($"发送消息-获取微信C_WxopenID【{user.C_WxopenID}】用户名字【{user.C_Name}】IsSendEmail【{IsSendEmail}】");
  230. if (!string.IsNullOrEmpty(user.C_WxopenID) && IsSendEmail == "true")
  231. {
  232. if (!sendUserWechatIDList.Contains(user.C_WxopenID))
  233. sendUserWechatIDList.Add(user.C_WxopenID);
  234. }
  235. pushMsgResultList.Add(new TMTN_PushMsgResult
  236. {
  237. C_ID = Guid.NewGuid().ToString(),
  238. C_PushMsgToCode = pushMsgToItem.C_PushPersonCode,
  239. //C_Content = "消息主题:" + pushModel.Subject + " 设备编号:" + pushModel.DevNumber + " 设备名称:" + pushModel.DevName + " 设备地址:" + pushModel.DevAddress + " 消息内容:" + pushModel.Msg,
  240. C_Content = JsonConvert.SerializeObject(pushModel),
  241. C_PushTypeCode = pushMsgToItem.C_PushTypeCode,
  242. C_DevStoreCode = pushMsgToItem.C_DevStoreCode,
  243. C_MsgTypeCode = pushModel.C_MsgTypeCode,
  244. C_Subject = Subject,
  245. C_CreateBy = new Guid(createBy),
  246. D_CreateOn = DateTime.Now,
  247. C_Status = "1"
  248. });
  249. }
  250. }
  251. log.Info($"发送消息-sendUserWechatIDList【{JsonConvert.SerializeObject(sendUserWechatIDList)}】pushMsgResultList=【{JsonConvert.SerializeObject(pushMsgResultList)}】");
  252. string GenerationCode = "";
  253. switch (pushModel.GenerationType)
  254. {
  255. case 1: GenerationCode = pushModel.C_DevStoreCode; break;
  256. case 2: GenerationCode = _claims.ApiUserId.ToString(); break;
  257. case 3: GenerationCode = "6e864cbc-5252-11ec-8681-fa163e02b3e4"; break;
  258. }
  259. TSYS_Message message = new TSYS_Message
  260. {
  261. C_ID = Guid.NewGuid().ToString(),
  262. C_LicenseCode = "SYSTEM",
  263. C_Content = JsonConvert.SerializeObject(pushModel),
  264. C_MsgTypeCode = pushModel.C_MsgTypeCode,
  265. C_Subject = Subject,
  266. D_MsgCreateOn = Convert.ToDateTime(pushModel.CreateOn),
  267. C_GenerationCode = GenerationCode,
  268. I_GenerationType = pushModel.GenerationType,
  269. I_MsgStatus = pushModel.msgStatus,
  270. C_DevStoreCode = pushModel.C_DevStoreCode,
  271. C_Remark = "",
  272. C_CreateBy = createBy,
  273. C_Creator = "Api",
  274. D_CreateOn = DateTime.Now,
  275. C_Status = "1"
  276. };
  277. List<TSYS_MessageFile> FileList = null;
  278. if (messageFiles != null)
  279. {
  280. FileList=new List<TSYS_MessageFile>();
  281. foreach (var item in messageFiles)
  282. {
  283. TSYS_MessageFile mesFile = new TSYS_MessageFile
  284. {
  285. C_ID = Guid.NewGuid().ToString(),
  286. C_MessageCode= message.C_ID,
  287. C_Url=item.Url,
  288. C_Text = item.Text,
  289. C_Type = item.Type,
  290. C_CreateBy = createBy,
  291. D_CreateOn = DateTime.Now,
  292. C_Status = "1"
  293. };
  294. FileList.Add(mesFile);
  295. }
  296. }
  297. _messageRepository.Create(message);
  298. bool messResult = await _messageRepository.SaveAsync();
  299. if (messResult)
  300. {
  301. if (FileList != null && FileList.Count > 0)
  302. {
  303. int fileSaveResult = await _tsysMessageFileRepository.CreateRangeAsync(FileList);
  304. }
  305. //获取小程序页面
  306. var (miniProgramPagePath, miniProgramAppId) = DataDictionaryHelper.GetXCXPage(pushModel.C_MsgTypeCode, message.C_ID, paramId, pushModel.C_DevStoreCode, StoreCode, QRCode, status, title);
  307. if (sendUserWechatIDList.Any())
  308. {
  309. try
  310. {
  311. string typeName = tBDMCodeDetailList.Where(i => i.C_Code == pushModel.C_MsgTypeCode).Select(x => x.C_Name).FirstOrDefault();
  312. #region 长期订阅 【2025-2-11】
  313. //var content = new
  314. //{
  315. // character_string1 = new { value = devStore.C_NumberCode ?? "" },
  316. // time2 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm") },
  317. // short_thing3 = new { value = typeName ?? "" }
  318. //};
  319. //new WeChatHelper(_httpClientFactory).PushMessageToUser(sendUserWechatIDList, content, WXConstModel.WatchHealthAlarm_TemplateId);
  320. #endregion
  321. #region 公众号模板【2025-3-5】
  322. string msg = pushModel.Msg;
  323. if (string.IsNullOrEmpty(msg))
  324. {
  325. msg = DataDictionaryHelper.GetValNameByCode(pushModel.C_MsgTypeCode);
  326. }
  327. if (msg.Length > 20)
  328. {
  329. msg = msg.Substring(0, 17) + "...";
  330. }
  331. string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
  332. if (!string.IsNullOrEmpty(pushModel.CreateOn))
  333. {
  334. time = Convert.ToDateTime(pushModel.CreateOn).ToString("yyyy-MM-dd HH:mm");
  335. }
  336. #region //_wP22WQxAlfYGfS8zlnHTl_-KgKS8j_FP5HDeqfsPtU
  337. //设备编号{ { character_string2.DATA} }
  338. //设备名称{ { thing1.DATA} }
  339. //关闭状态{ { thing3.DATA} }
  340. //关闭时间{ { time4.DATA} }
  341. //var data = new
  342. //{
  343. // thing1 = new { value = devStore.C_Name }, //异常位置
  344. // character_string2 = new { value = devStore.C_NumberCode },//异常点位
  345. // thing3 = new { value = typeName }, //上报人
  346. // time4 = new { value = pushModel.CreateOn}//上报时间
  347. //};
  348. #endregion
  349. #region 7eGjzIBAyysTOLfcn-oqkpOCEL_My5ItXHkd_DdniGw
  350. //设备名称{ { thing2.DATA} }
  351. //设备编号{ { character_string20.DATA} }
  352. //报警时间{ { time4.DATA} }
  353. //报警内容{ { thing5.DATA} }
  354. var data = new
  355. {
  356. thing2 = new { value = devStore.C_Name }, //设备名称
  357. character_string20 = new { value = devStore.C_NumberCode },//设备编号
  358. thing5 = new { value = msg }, //报警内容
  359. time4 = new { value = time }//报警时间
  360. };
  361. #endregion
  362. var row = new WeChatHelper(_httpClientFactory).PushGZHMessageToUser(sendUserWechatIDList, WXConstModel.GZHDevAlarmTemplateId, "", data, miniProgramAppId, miniProgramPagePath);
  363. #endregion
  364. }
  365. catch (Exception ex)
  366. {
  367. log.Info($" 发送消息-WeChatHelper-发送异常【C_MsgTypeCode={pushModel.C_MsgTypeCode}】 异常信息:{ex.Message}");
  368. throw;
  369. }
  370. }
  371. if (pushMsgResultList.Any())
  372. {
  373. //await _tmtnPushMsgResultRepository.CreateRangeAsync(pushMsgResultList);
  374. foreach (var content in pushMsgResultList)
  375. {
  376. try
  377. {
  378. content.C_MessageCode = message.C_ID;
  379. _tmtnPushMsgResultRepository.Create(content);
  380. bool bols = await _tmtnPushMsgResultRepository.SaveAsync();
  381. }
  382. catch (Exception ex)
  383. {
  384. log.Info($" 发送消息-pushMsgResultList-TMTN_PushMsgResult-保存异常【数据:{JsonConvert.SerializeObject(content)}】 异常信息:{ex.Message}");
  385. Console.WriteLine(ex.Message);
  386. }
  387. }
  388. //
  389. if (pushModel.C_MsgTypeCode == "MSG_TYPE_012" && rabbitMQModel != null)
  390. {
  391. rabbitMQModel.msgStr = JsonConvert.SerializeObject(message);
  392. bool bol = await RabbitMQHelper.SnedRabbitMQ_ExchangeDirect(rabbitMQModel);
  393. if (!bol)
  394. {
  395. log.Info($" 发送消息-[MSG_TYPE_012]发送RabbitMQ消息失败【TMTN_PushMsgResult:{rabbitMQModel.msgStr}】");
  396. }
  397. }
  398. }
  399. }
  400. else {
  401. log.Info($"发送消息-TSYS_Message 数据添加失败【{JsonConvert.SerializeObject(message)}】");
  402. }
  403. log.Info($"发送消息-END");
  404. return await Task.FromResult(true);
  405. }
  406. catch (Exception ex)
  407. {
  408. log.Info($"发送消息-Error【{ex.Message}】");
  409. return await Task.FromResult(false);
  410. }
  411. }
  412. /// <summary>
  413. /// 点检、维保发送消息
  414. /// </summary>
  415. /// <param name="type">消息类型</param>
  416. /// <param name="viewModel">点检数据</param>
  417. /// <param name="SpotCode">巡检点编号</param>
  418. /// <param name="paramId">传参ID</param>
  419. /// <param name="paramStatus">数据状态</param>
  420. /// <returns></returns>
  421. public async Task PushRecordMsgAsync(string type,TispRecordItemAlarmDetailViewModel viewModel,TpushMsgModel pushModel = null, string SpotCode=null,string paramId = null,string paramStatus = null,string title=null)
  422. {
  423. try
  424. {
  425. log.Info($"点检、维保发送消息BEGIN-【type={type}】【viewModel={JsonConvert.SerializeObject(viewModel)}】【pushModel={JsonConvert.SerializeObject(pushModel)}】【SpotCode={SpotCode};paramId={paramId};paramStatus={paramStatus}】");
  426. bool IsRun=true;
  427. string statusStr = "未知";
  428. if (viewModel!=null)
  429. {
  430. //1 = 正常; 0 = 异常; 2 = 恢复; 3 = 确认异常; 4 = 取消异常
  431. switch (viewModel.SecurityStatus)
  432. {
  433. case "0": statusStr = "异常"; break;
  434. case "1": statusStr = "正常"; break;
  435. case "2": statusStr = "恢复"; break;
  436. case "3": statusStr = "确认异常"; break;
  437. case "4": statusStr = "取消异常"; break;
  438. }
  439. if (string.IsNullOrEmpty(paramId))
  440. {
  441. paramId = viewModel.C_ID.ToString();
  442. if (type == "MSG_TYPE_005") {
  443. paramId = viewModel.C_RecordCode.ToString();
  444. }
  445. }
  446. if (type == "MSG_TYPE_003"|| type == "MSG_TYPE_004" || type == "MSG_TYPE_005")
  447. {
  448. title = viewModel.SpotName;
  449. }
  450. }
  451. string Subject = DataDictionaryHelper.GetValNameByCode(type);
  452. if (pushModel == null)
  453. {
  454. pushModel = new TpushMsgModel
  455. {
  456. C_MsgTypeCode = type,
  457. Msg = Subject,
  458. Subject = Subject,
  459. CreateOn = DateTime.Now.ToString(),
  460. GenerationType = 2,
  461. msgStatus = 0,
  462. };
  463. if (type == "MSG_TYPE_003" || type == "MSG_TYPE_004" || type == "MSG_TYPE_005")
  464. {
  465. pushModel.Msg = viewModel?.SpotName + "-" + statusStr;
  466. }
  467. }
  468. if (!string.IsNullOrEmpty(SpotCode))
  469. {
  470. pushModel.C_SpotCode= SpotCode;
  471. var DevSpotContent =await _tdevDevSpotRepository.GetByConditionAsync(t => t.C_SpotCode == SpotCode);
  472. var DevSpotList = DevSpotContent.Select(d => d.C_DevStoreCode).ToList();
  473. if (DevSpotList!=null&& DevSpotList.Count>0)
  474. {
  475. foreach (var item in DevSpotList)
  476. {
  477. if (item!=null)
  478. {
  479. var devStore = await _tdevDevStoreRepository.GetByIdAsync(item);
  480. pushModel.C_DevStoreCode = item;
  481. pushModel.DevNumber = devStore.C_NumberCode;
  482. pushModel.DevName = devStore.C_Name;
  483. await PushAlarmMsgAsync(pushModel, pushModel.Subject, null, paramId,paramStatus,title);
  484. IsRun = false;
  485. }
  486. }
  487. }
  488. }
  489. //是点检消息类型并且没有找匹配的设备给组织下的所有人发送消息
  490. if (IsRun&&(type == "MSG_TYPE_003" || type == "MSG_TYPE_004" || type == "MSG_TYPE_005"))
  491. {
  492. IEnumerable<TSYS_User> users = await _tsysUserRepository.GetByRecordItemCodeAsync(viewModel.C_ID.ToString());
  493. if (!users.Any())
  494. {
  495. return;
  496. }
  497. List<string> openIds = users.Where(u => u.C_WxopenID != null).Select(i => i.C_WxopenID).ToList();
  498. ////设备编号{character_string2.DATA};设备名称{ thing1.DATA};关闭状态{ thing3.DATA};关闭时间{time4.DATA}
  499. //var data = new
  500. //{
  501. // thing1 = new { value = viewModel.SpotName }, //设备名称
  502. // character_string2 = new { value = viewModel.SpotNumber },//设备编号
  503. // thing3 = new { value = statusStr }, //关闭状态
  504. // time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm") }//关闭时间
  505. //};
  506. string msg = pushModel.Msg;
  507. if (string.IsNullOrEmpty(msg))
  508. {
  509. msg = DataDictionaryHelper.GetValNameByCode(pushModel.C_MsgTypeCode);
  510. }
  511. if (msg.Length > 20)
  512. {
  513. msg = msg.Substring(0, 17) + "...";
  514. }
  515. //设备名称{ { thing2.DATA} };设备编号{ { character_string20.DATA} };报警时间{ { time4.DATA} };报警内容{ { thing5.DATA} }
  516. var data = new
  517. {
  518. thing2 = new { value = viewModel.SpotName }, //设备名称
  519. character_string20 = new { value = viewModel.SpotNumber },//设备编号
  520. thing5 = new { value = msg }, //报警内容
  521. time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm") }//报警时间
  522. };
  523. //获取小程序页面
  524. var (miniProgramPagePath, miniProgramAppId) = DataDictionaryHelper.GetXCXPage(type,"", paramId,"", viewModel.C_StoreCode, viewModel.C_QRCode, viewModel.SecurityStatus, title);
  525. TSYS_Message message = new TSYS_Message
  526. {
  527. C_ID = Guid.NewGuid().ToString(),
  528. C_LicenseCode = "SYSTEM",
  529. C_Content = JsonConvert.SerializeObject(pushModel),
  530. C_MsgTypeCode = type,
  531. C_Subject = Subject,
  532. D_MsgCreateOn = Convert.ToDateTime(pushModel.CreateOn),
  533. C_GenerationCode = _claims.ApiUserId.ToString(),
  534. I_GenerationType = 2,
  535. I_MsgStatus = 0,
  536. C_DevStoreCode = "",
  537. C_Remark = "",
  538. C_CreateBy = _claims.ApiUserId.ToString(),
  539. C_Creator = "点检",
  540. D_CreateOn = DateTime.Now,
  541. C_Status = "1"
  542. };
  543. _messageRepository.Create(message);
  544. bool messResult = await _messageRepository.SaveAsync();
  545. var row = new WeChatHelper(_httpClientFactory).PushGZHMessageToUser(openIds, WXConstModel.GZHDevAlarmTemplateId, "", data, miniProgramAppId, miniProgramPagePath);
  546. }
  547. }
  548. catch (Exception ex)
  549. {
  550. log.Info($"点检、维保发送消息-Error【{ex.Message}】");
  551. }
  552. }
  553. }
  554. }