PushMsgService.cs 35 KB

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