Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

183 lines
8.7KB

  1. using HealthMonitor.Common;
  2. using HealthMonitor.Service.Biz.db;
  3. using HealthMonitor.Service.Cache;
  4. using HealthMonitor.Service.Resolver.Interface;
  5. using HealthMonitor.Service.Sub;
  6. using HealthMonitor.Service.Sub.Topic.Model;
  7. using Microsoft.Extensions.Logging;
  8. using Newtonsoft.Json;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Data.Common;
  12. using System.Linq;
  13. using System.Text;
  14. using System.Text.Json.Serialization;
  15. using System.Threading.Tasks;
  16. using TDengineTMQ;
  17. using TelpoDataService.Util.Entities.GpsLocationHistory;
  18. namespace HealthMonitor.Service.Resolver
  19. {
  20. public class BloodpressResolver: IResolver
  21. {
  22. private readonly ILogger<BloodpressResolver> _logger;
  23. private readonly PersonCacheManager _personCacheMgr;
  24. private readonly TDengineService _serviceTDengine;
  25. private readonly BloodPressReferenceValueCacheManager _bpRefValCacheManager;
  26. private AsyncLocal<string> _messageId = new();
  27. private AsyncLocal<HisGpsBloodPress> _msgData = new();
  28. public BloodpressResolver(
  29. TDengineService serviceDengine,
  30. BloodPressReferenceValueCacheManager bpRefValCacheManager,
  31. PersonCacheManager personCacheMgr,
  32. ILogger<BloodpressResolver> logger)
  33. {
  34. _serviceTDengine = serviceDengine;
  35. _bpRefValCacheManager = bpRefValCacheManager;
  36. _logger = logger;
  37. _personCacheMgr = personCacheMgr;
  38. }
  39. public void SetResolveInfo(PackageMsgModel msg)
  40. {
  41. var topicHmBloodPress = JsonConvert.DeserializeObject<TopicHmBloodPress>(msg.DetailData.ToString()!);
  42. _messageId.Value = msg.MessageId;
  43. _msgData.Value = new HisGpsBloodPress()
  44. {
  45. BloodPressId = topicHmBloodPress!.BloodPressId,
  46. MessageId = topicHmBloodPress!.MessageId,
  47. Serialno= topicHmBloodPress!.Serialno,
  48. SystolicValue = topicHmBloodPress!.SystolicValue,
  49. DiastolicValue= topicHmBloodPress!.DiastolicValue,
  50. LastUpdate= DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(topicHmBloodPress.LastUpdate) / 1000000),
  51. CreateTime= DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(SafeType.SafeInt64(topicHmBloodPress.CreateTime) / 1000000),
  52. Method= topicHmBloodPress!.Method,
  53. IsDisplay=topicHmBloodPress!.IsDisplay ? 1 : 0
  54. };
  55. // Console.WriteLine($"SetResolveInfo" + _messageId.Value);
  56. }
  57. public override string ToString()
  58. {
  59. return $"{nameof(BloodpressResolver)}[{_messageId.Value}]";
  60. }
  61. public async Task ExecuteMessageAsync()
  62. {
  63. var messageId = _messageId.Value;
  64. var bp = _msgData.Value!;
  65. #region 获取个人信息
  66. var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(bp.MessageId, bp.Serialno).ConfigureAwait(false);
  67. Console.WriteLine(person?.Person.PersonName);
  68. //验证这个信息是否存在
  69. if (person == null || person?.Person.BornDate == null)
  70. {
  71. Console.WriteLine("验证这个信息是否存在");
  72. return;
  73. }
  74. // 验证年龄是否在范围 (2 - 120)
  75. var age = SafeType.SafeInt(DateTime.Today.Year - person?.Person.BornDate!.Value.Year!);
  76. if (age < 1 || age > 120)
  77. {
  78. Console.WriteLine("验证年龄是否在范围 (2 - 120)");
  79. return;
  80. }
  81. #endregion
  82. var gender = person?.Person.Gender == true ? 1 : 2;
  83. var isHypertension = SafeType.SafeBool(person?.Person.Ishypertension!);
  84. var height = SafeType.SafeDouble(person?.Person.Height!);
  85. var weight = SafeType.SafeDouble(person?.Person.Weight!);
  86. #region 计算增量值
  87. var bpRef = await _bpRefValCacheManager.GetBloodPressReferenceValueAsync(age, gender, isHypertension);
  88. var systolicRefValue = bpRef?.Systolic;//?
  89. var diastolicRefValue = bpRef?.Diastolic;//?
  90. int duration = 30;
  91. // 获取历史数据
  92. //DateTime now = DateTime.Now;
  93. DateTime now = (DateTime)bp.LastUpdate!; //测试
  94. DateTime startTime = now.AddDays(-duration);
  95. DateTime endTime = now;
  96. //
  97. var systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'");
  98. var diastolicAggregate = await _serviceTDengine.GetAggregateValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'");
  99. //var systolicAggregate = _serviceTDengine.GetAggregateValue("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'");
  100. //var diastolicAggregate = _serviceTDengine.GetAggregateValue("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'");
  101. // 最大值
  102. var systolicMax = systolicAggregate.Max;
  103. var diastolicMax = diastolicAggregate.Max;
  104. // 最小值
  105. var systolicMin = systolicAggregate.Min;
  106. var diastolicMin = diastolicAggregate.Min;
  107. // 计算去除最大值和最小值和异常值的平均值
  108. var systolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} ");
  109. var diastolicAvg = await _serviceTDengine.GetAvgExceptMaxMinValueAsync("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and diastolic_value < {diastolicRefValue}");
  110. //var systolicAvg = _serviceTDengine.GetAvgExceptMaxMinValue("systolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and systolic_value < {systolicRefValue} ");
  111. //var diastolicAvg = _serviceTDengine.GetAvgExceptMaxMinValue("diastolic_value", "hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}' and diastolic_value < {diastolicRefValue}");
  112. // 偏移参数
  113. var avgOffset = 0.25M;
  114. var systolicAvgOffset = avgOffset;
  115. var diastolicAvgOffset = avgOffset;
  116. // 增量值=(标定值-平均值)* 0.25
  117. var systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
  118. var diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
  119. #endregion
  120. #region 插入BP增量值 hm_bloodpress_stats_inc
  121. var sql = $"INSERT INTO health_monitor.hm_bloodpress_stats_inc VALUES(" +
  122. $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  123. $"'{bp.BloodPressId}'," +
  124. $"'{bp.MessageId}'," +
  125. $"'{bp.Serialno}'," +
  126. $"{bp.SystolicValue}," +
  127. $"{systolicRefValue}," +
  128. $"{systolicAvg}," +
  129. $"{systolicMax}," +
  130. $"{systolicMin}," +
  131. $"{systolicAvgOffset}," +
  132. $"{systolicInc}," +
  133. $"{bp.DiastolicValue}," +
  134. $"{diastolicRefValue}," +
  135. $"{diastolicAvg}," +
  136. $"{diastolicMax}," +
  137. $"{diastolicMin}," +
  138. $"{diastolicAvgOffset}," +
  139. $"{diastolicInc}," +
  140. $"{gender}," +
  141. $"{age}," +
  142. $"{height}," +
  143. $"{weight}," +
  144. $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  145. $"{duration}," +
  146. $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  147. $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  148. $"'{string.Empty}')";
  149. //var res = await _serviceTDengine.GernalRestSql(sql);
  150. _serviceTDengine.ExecuteInsertSQL(sql);
  151. #endregion
  152. }
  153. }
  154. }