No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

175 líneas
7.6KB

  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.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}'");
  98. var diastolicAggregate = await _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}'");
  99. // 最大值
  100. var systolicMax = systolicAggregate.Max;
  101. var diastolicMax = diastolicAggregate.Max;
  102. // 最小值
  103. var systolicMin = systolicAggregate.Min;
  104. var diastolicMin = diastolicAggregate.Min;
  105. // 计算去除最大值和最小值和异常值的平均值
  106. var systolicAvg = await _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} ");
  107. var diastolicAvg = await _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}");
  108. // 偏移参数
  109. var avgOffset = 0.25M;
  110. var systolicAvgOffset = avgOffset;
  111. var diastolicAvgOffset = avgOffset;
  112. // 增量值=(标定值-平均值)* 0.25
  113. var systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
  114. var diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
  115. #endregion
  116. #region 插入BP增量值 hm_bloodpress_stats_inc
  117. var sql = $"INSERT INTO health_monitor.hm_bloodpress_stats_inc VALUES(" +
  118. $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  119. $"'{bp.BloodPressId}'," +
  120. $"'{bp.MessageId}'," +
  121. $"'{bp.Serialno}'," +
  122. $"{bp.SystolicValue}," +
  123. $"{systolicRefValue}," +
  124. $"{systolicAvg}," +
  125. $"{systolicMax}," +
  126. $"{systolicMin}," +
  127. $"{systolicAvgOffset}," +
  128. $"{systolicInc}," +
  129. $"{bp.DiastolicValue}," +
  130. $"{diastolicRefValue}," +
  131. $"{diastolicAvg}," +
  132. $"{diastolicMax}," +
  133. $"{diastolicMin}," +
  134. $"{diastolicAvgOffset}," +
  135. $"{diastolicInc}," +
  136. $"{gender}," +
  137. $"{age}," +
  138. $"{height}," +
  139. $"{weight}," +
  140. $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  141. $"{duration}," +
  142. $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  143. $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  144. $"'{string.Empty}')";
  145. var res = await _serviceTDengine.GernalRestSql(sql);
  146. #endregion
  147. }
  148. }
  149. }