PushMsgService.cs 39 KB

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