PushMsgService.cs 32 KB

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