PushMsgService.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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. string miniProgramAppId = "";//跳转小程序的Appid
  306. //获取小程序页面
  307. string miniProgramPagePath = DataDictionaryHelper.GetXCXPage(pushModel.C_MsgTypeCode, message.C_ID, paramId, pushModel.C_DevStoreCode, StoreCode, QRCode, status, title);
  308. if (!string.IsNullOrEmpty(miniProgramPagePath))
  309. {
  310. miniProgramAppId = WXConstModel.XCXAppId;
  311. }
  312. if (sendUserWechatIDList.Any())
  313. {
  314. try
  315. {
  316. string typeName = tBDMCodeDetailList.Where(i => i.C_Code == pushModel.C_MsgTypeCode).Select(x => x.C_Name).FirstOrDefault();
  317. #region 长期订阅 【2025-2-11】
  318. //var content = new
  319. //{
  320. // character_string1 = new { value = devStore.C_NumberCode ?? "" },
  321. // time2 = new { value = DateTime.Now.ToString("yyyy-MM-dd hh:mm") },
  322. // short_thing3 = new { value = typeName ?? "" }
  323. //};
  324. //new WeChatHelper(_httpClientFactory).PushMessageToUser(sendUserWechatIDList, content, WXConstModel.WatchHealthAlarm_TemplateId);
  325. #endregion
  326. #region 公众号模板【2025-3-5】
  327. string msg = pushModel.Msg;
  328. if (string.IsNullOrEmpty(msg))
  329. {
  330. msg = DataDictionaryHelper.GetValNameByCode(pushModel.C_MsgTypeCode);
  331. }
  332. if (msg.Length > 20)
  333. {
  334. msg = msg.Substring(0, 17) + "...";
  335. }
  336. string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm");
  337. if (!string.IsNullOrEmpty(pushModel.CreateOn))
  338. {
  339. time = Convert.ToDateTime(pushModel.CreateOn).ToString("yyyy-MM-dd HH:mm");
  340. }
  341. #region //_wP22WQxAlfYGfS8zlnHTl_-KgKS8j_FP5HDeqfsPtU
  342. //设备编号{ { character_string2.DATA} }
  343. //设备名称{ { thing1.DATA} }
  344. //关闭状态{ { thing3.DATA} }
  345. //关闭时间{ { time4.DATA} }
  346. //var data = new
  347. //{
  348. // thing1 = new { value = devStore.C_Name }, //异常位置
  349. // character_string2 = new { value = devStore.C_NumberCode },//异常点位
  350. // thing3 = new { value = typeName }, //上报人
  351. // time4 = new { value = pushModel.CreateOn}//上报时间
  352. //};
  353. #endregion
  354. #region 7eGjzIBAyysTOLfcn-oqkpOCEL_My5ItXHkd_DdniGw
  355. //设备名称{ { thing2.DATA} }
  356. //设备编号{ { character_string20.DATA} }
  357. //报警时间{ { time4.DATA} }
  358. //报警内容{ { thing5.DATA} }
  359. var data = new
  360. {
  361. thing2 = new { value = devStore.C_Name }, //设备名称
  362. character_string20 = new { value = devStore.C_NumberCode },//设备编号
  363. thing5 = new { value = msg }, //报警内容
  364. time4 = new { value = time }//报警时间
  365. };
  366. #endregion
  367. var row = new WeChatHelper(_httpClientFactory).PushGZHMessageToUser(sendUserWechatIDList, WXConstModel.GZHDevAlarmTemplateId, "", data, miniProgramAppId, miniProgramPagePath);
  368. #endregion
  369. }
  370. catch (Exception ex)
  371. {
  372. log.Info($" 发送消息-WeChatHelper-发送异常【C_MsgTypeCode={pushModel.C_MsgTypeCode}】 异常信息:{ex.Message}");
  373. throw;
  374. }
  375. }
  376. if (pushMsgResultList.Any())
  377. {
  378. //await _tmtnPushMsgResultRepository.CreateRangeAsync(pushMsgResultList);
  379. foreach (var content in pushMsgResultList)
  380. {
  381. try
  382. {
  383. content.C_MessageCode = message.C_ID;
  384. _tmtnPushMsgResultRepository.Create(content);
  385. bool bols = await _tmtnPushMsgResultRepository.SaveAsync();
  386. }
  387. catch (Exception ex)
  388. {
  389. log.Info($" 发送消息-pushMsgResultList-TMTN_PushMsgResult-保存异常【数据:{JsonConvert.SerializeObject(content)}】 异常信息:{ex.Message}");
  390. Console.WriteLine(ex.Message);
  391. }
  392. }
  393. //
  394. if (pushModel.C_MsgTypeCode == "MSG_TYPE_012" && rabbitMQModel != null)
  395. {
  396. rabbitMQModel.msgStr = JsonConvert.SerializeObject(message);
  397. bool bol = await RabbitMQHelper.SnedRabbitMQ_ExchangeDirect(rabbitMQModel);
  398. if (!bol)
  399. {
  400. log.Info($" 发送消息-[MSG_TYPE_012]发送RabbitMQ消息失败【TMTN_PushMsgResult:{rabbitMQModel.msgStr}】");
  401. }
  402. }
  403. }
  404. }
  405. else {
  406. log.Info($"发送消息-TSYS_Message 数据添加失败【{JsonConvert.SerializeObject(message)}】");
  407. }
  408. log.Info($"发送消息-END");
  409. return await Task.FromResult(true);
  410. }
  411. catch (Exception ex)
  412. {
  413. log.Info($"发送消息-Error【{ex.Message}】");
  414. return await Task.FromResult(false);
  415. }
  416. }
  417. /// <summary>
  418. /// 点检、维保发送消息
  419. /// </summary>
  420. /// <param name="type">消息类型</param>
  421. /// <param name="viewModel">点检数据</param>
  422. /// <param name="SpotCode">巡检点编号</param>
  423. /// <param name="paramId">传参ID</param>
  424. /// <param name="paramStatus">数据状态</param>
  425. /// <returns></returns>
  426. public async Task PushRecordMsgAsync(string type,TispRecordItemAlarmDetailViewModel viewModel,TpushMsgModel pushModel = null, string SpotCode=null,string paramId = null,string paramStatus = null,string title=null)
  427. {
  428. try
  429. {
  430. log.Info($"点检、维保发送消息BEGIN-【type={type}】【viewModel={JsonConvert.SerializeObject(viewModel)}】【pushModel={JsonConvert.SerializeObject(pushModel)}】【SpotCode={SpotCode};paramId={paramId};paramStatus={paramStatus}】");
  431. bool IsRun=true;
  432. string statusStr = "未知";
  433. if (viewModel!=null)
  434. {
  435. //1 = 正常; 0 = 异常; 2 = 恢复; 3 = 确认异常; 4 = 取消异常
  436. switch (viewModel.SecurityStatus)
  437. {
  438. case "0": statusStr = "异常"; break;
  439. case "1": statusStr = "正常"; break;
  440. case "2": statusStr = "恢复"; break;
  441. case "3": statusStr = "确认异常"; break;
  442. case "4": statusStr = "取消异常"; break;
  443. }
  444. if (string.IsNullOrEmpty(paramId))
  445. {
  446. paramId = viewModel.C_ID.ToString();
  447. if (type == "MSG_TYPE_005") {
  448. paramId = viewModel.C_RecordCode.ToString();
  449. }
  450. }
  451. if (type == "MSG_TYPE_003"|| type == "MSG_TYPE_004" || type == "MSG_TYPE_005")
  452. {
  453. title = viewModel.SpotName;
  454. }
  455. }
  456. string Subject = DataDictionaryHelper.GetValNameByCode(type);
  457. if (pushModel == null)
  458. {
  459. pushModel = new TpushMsgModel
  460. {
  461. C_MsgTypeCode = type,
  462. Msg = Subject,
  463. Subject = Subject,
  464. CreateOn = DateTime.Now.ToString(),
  465. GenerationType = 2,
  466. msgStatus = 0,
  467. };
  468. if (type == "MSG_TYPE_003" || type == "MSG_TYPE_004" || type == "MSG_TYPE_005")
  469. {
  470. pushModel.Msg = viewModel?.SpotName + "-" + statusStr;
  471. }
  472. }
  473. if (!string.IsNullOrEmpty(SpotCode))
  474. {
  475. pushModel.C_SpotCode= SpotCode;
  476. var DevSpotContent =await _tdevDevSpotRepository.GetByConditionAsync(t => t.C_SpotCode == SpotCode);
  477. var DevSpotList = DevSpotContent.Select(d => d.C_DevStoreCode).ToList();
  478. if (DevSpotList!=null&& DevSpotList.Count>0)
  479. {
  480. foreach (var item in DevSpotList)
  481. {
  482. if (item!=null)
  483. {
  484. var devStore = await _tdevDevStoreRepository.GetByIdAsync(item);
  485. pushModel.C_DevStoreCode = item;
  486. pushModel.DevNumber = devStore.C_NumberCode;
  487. pushModel.DevName = devStore.C_Name;
  488. await PushAlarmMsgAsync(pushModel, pushModel.Subject, null, paramId,paramStatus,title);
  489. IsRun = false;
  490. }
  491. }
  492. }
  493. }
  494. //是点检消息类型并且没有找匹配的设备给组织下的所有人发送消息
  495. if (IsRun&&(type == "MSG_TYPE_003" || type == "MSG_TYPE_004" || type == "MSG_TYPE_005"))
  496. {
  497. IEnumerable<TSYS_User> users = await _tsysUserRepository.GetByRecordItemCodeAsync(viewModel.C_ID.ToString());
  498. if (!users.Any())
  499. {
  500. return;
  501. }
  502. List<string> openIds = users.Where(u => u.C_WxopenID != null).Select(i => i.C_WxopenID).ToList();
  503. ////设备编号{character_string2.DATA};设备名称{ thing1.DATA};关闭状态{ thing3.DATA};关闭时间{time4.DATA}
  504. //var data = new
  505. //{
  506. // thing1 = new { value = viewModel.SpotName }, //设备名称
  507. // character_string2 = new { value = viewModel.SpotNumber },//设备编号
  508. // thing3 = new { value = statusStr }, //关闭状态
  509. // time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm") }//关闭时间
  510. //};
  511. string msg = pushModel.Msg;
  512. if (string.IsNullOrEmpty(msg))
  513. {
  514. msg = DataDictionaryHelper.GetValNameByCode(pushModel.C_MsgTypeCode);
  515. }
  516. if (msg.Length > 20)
  517. {
  518. msg = msg.Substring(0, 17) + "...";
  519. }
  520. //设备名称{ { thing2.DATA} };设备编号{ { character_string20.DATA} };报警时间{ { time4.DATA} };报警内容{ { thing5.DATA} }
  521. var data = new
  522. {
  523. thing2 = new { value = viewModel.SpotName }, //设备名称
  524. character_string20 = new { value = viewModel.SpotNumber },//设备编号
  525. thing5 = new { value = msg }, //报警内容
  526. time4 = new { value = DateTime.Now.ToString("yyyy-MM-dd HH:mm") }//报警时间
  527. };
  528. string miniProgramAppId = "";//跳转小程序的Appid
  529. //获取小程序页面
  530. string miniProgramPagePath = DataDictionaryHelper.GetXCXPage(type,"", paramId,"", viewModel.C_StoreCode, viewModel.C_QRCode, viewModel.SecurityStatus, title);
  531. if (!string.IsNullOrEmpty(miniProgramPagePath))
  532. {
  533. miniProgramAppId = WXConstModel.XCXAppId;
  534. }
  535. TSYS_Message message = new TSYS_Message
  536. {
  537. C_ID = Guid.NewGuid().ToString(),
  538. C_LicenseCode = "SYSTEM",
  539. C_Content = JsonConvert.SerializeObject(pushModel),
  540. C_MsgTypeCode = type,
  541. C_Subject = Subject,
  542. D_MsgCreateOn = Convert.ToDateTime(pushModel.CreateOn),
  543. C_GenerationCode = _claims.ApiUserId.ToString(),
  544. I_GenerationType = 2,
  545. I_MsgStatus = 0,
  546. C_DevStoreCode = "",
  547. C_Remark = "",
  548. C_CreateBy = _claims.ApiUserId.ToString(),
  549. C_Creator = "点检",
  550. D_CreateOn = DateTime.Now,
  551. C_Status = "1"
  552. };
  553. _messageRepository.Create(message);
  554. bool messResult = await _messageRepository.SaveAsync();
  555. var row = new WeChatHelper(_httpClientFactory).PushGZHMessageToUser(openIds, WXConstModel.GZHDevAlarmTemplateId, "", data, miniProgramAppId, miniProgramPagePath);
  556. }
  557. }
  558. catch (Exception ex)
  559. {
  560. log.Info($"点检、维保发送消息-Error【{ex.Message}】");
  561. }
  562. }
  563. }
  564. }