OfilmLanxinNoticeService.cs 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. using Cksoft.Data;
  2. using Cksoft.Data.Repository;
  3. using Cksoft.Unity;
  4. using Cksoft.Unity.Log4NetConfig;
  5. using DllEapDal.OFILM;
  6. using DllEapEntity;
  7. using DllEapEntity.OFILM;
  8. using Microsoft.Extensions.Configuration;
  9. using Newtonsoft.Json;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Text;
  14. namespace WebStatusToolkit.Notice
  15. {
  16. public class OfilmLanxinNoticeService
  17. {
  18. public static async void NoticeDisconn(Machine mac, MacStatus entity, string errorinfo)
  19. {
  20. var lanxinConfig = AppConfigurtaionServices.Configuration.GetSection("LanXinConfig").Get<LanxinConfig>();
  21. if (lanxinConfig == null)
  22. {
  23. errorinfo = "配置文件中LanXin节点未找到或配置不正确";
  24. LogHelper<OfilmLanxinNoticeService>.LogError(errorinfo, "断线提醒", string.Empty);
  25. return;
  26. }
  27. try
  28. {
  29. using (IDatabase db = DbFactory.Base("eap"))
  30. {
  31. var configDal = new NoticeConfigDal(db);
  32. var config = configDal.GetByFactory(mac.FactoryId);
  33. if (config == null || config.NoticeConfigDetails == null || config.NoticeConfigDetails.Count() <= 0)
  34. {
  35. errorinfo = "获取该园区配置失败";
  36. LogHelper<OfilmLanxinNoticeService>.LogError($"推送机台[{entity.MacCode + " " + entity.STime.ToString("yyyy-MM-dd HH:mm:ss")}]断线信息失败:{errorinfo ?? "获取配置信息失败"}", "断线提醒", string.Empty);
  37. return;
  38. }
  39. var toUsers = string.Join(",", config.NoticeConfigDetails.Select(c => c.FCode));
  40. var sql = $"SELECT COUNT(1) FROM NoticeHistory where maccode='{mac.FCode}' and rectime>='{DateTime.Now.ToString("yyyy-MM-dd 00:00:00")}'";
  41. var count = Convert.ToInt32(db.FindList<string>(sql).FirstOrDefault() ?? "0");
  42. var machineInfo = $"{mac.FactoryName}的机台[{mac.FCode}]";
  43. string content = $"{machineInfo}已于[{entity.STime.ToString("yyyy-MM-dd HH:mm:ss")}]断开连接,本日已断线【{count + 1}】次," +
  44. $"请及时处理!";
  45. var requestData = new LanxinRequestDto
  46. {
  47. Appid = lanxinConfig.Appid,
  48. Secret = lanxinConfig.Secret,
  49. Tousers = toUsers,
  50. Content = content
  51. };
  52. var json = JsonConvert.SerializeObject(requestData);
  53. var res = HttpRequestHelper<OfilmLanxinReponseDto>.Post(lanxinConfig.Url, json, ref errorinfo, "application/json");
  54. if (res == null || res.Code != 1)
  55. {
  56. LogHelper<OfilmLanxinNoticeService>.LogFatal($"推送机台[{content}]断线信息失败:{res.Message ?? "请求蓝信接口异常"}", "断线提醒", string.Empty);
  57. return;
  58. }
  59. var history = new NoticeHistory
  60. {
  61. MacCode = entity.MacCode,
  62. RecTime = DateTime.Now
  63. };
  64. if (db.InsertFor(history, string.Empty) < 0)
  65. {
  66. LogHelper<OfilmLanxinNoticeService>.LogError($"推送机台[{content}]断线信息失败", "断线提醒", string.Empty);
  67. return;
  68. }
  69. LogHelper<OfilmLanxinNoticeService>.LogFatal($"推送机台[{content}]断线信息成功", "断线提醒", string.Empty);
  70. }
  71. }
  72. catch (Exception ex)
  73. {
  74. LogHelper<OfilmLanxinNoticeService>.LogError($"推送机台[{mac.FCode} {entity.STime.ToString("yyyy-MM-dd HH:mm:ss")}]断线信息失败:{ex.Message}", "断线提醒", string.Empty);
  75. }
  76. }
  77. }
  78. }