You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BloodpressResolver.cs 8.7KB

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