Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

256 lines
12KB

  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 readonly AsyncLocal<string> _messageId = new();
  27. private readonly 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. }
  56. public override string ToString()
  57. {
  58. return $"{nameof(BloodpressResolver)}[{_messageId.Value}]";
  59. }
  60. public async Task ExecuteMessageAsync()
  61. {
  62. var messageId = _messageId.Value;
  63. var bp = _msgData.Value!;
  64. #region 获取个人信息
  65. var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(bp.MessageId, bp.Serialno).ConfigureAwait(false);
  66. //验证这个信息是否存在
  67. if (person == null || person?.Person.BornDate == null)
  68. {
  69. _logger.LogWarning("验证这个信息是否存在");
  70. return;
  71. }
  72. // 验证年龄是否在范围 (2 - 120)
  73. var age = SafeType.SafeInt(DateTime.Today.Year - person?.Person.BornDate!.Value.Year!);
  74. if (age < 1 || age > 120)
  75. {
  76. _logger.LogWarning("验证年龄是否在范围 (2 - 120)");
  77. return;
  78. }
  79. #endregion
  80. var gender = person?.Person.Gender == true ? 1 : 2;
  81. var isHypertension = SafeType.SafeBool(person?.Person.Ishypertension!);
  82. var height = SafeType.SafeDouble(person?.Person.Height!);
  83. var weight = SafeType.SafeDouble(person?.Person.Weight!);
  84. #region 计算增量值
  85. var bpRef = await _bpRefValCacheManager.GetBloodPressReferenceValueAsync(age, gender, isHypertension);
  86. var systolicRefValue = bpRef?.Systolic;//?
  87. var diastolicRefValue = bpRef?.Diastolic;//?
  88. int duration = 2;
  89. // 获取历史数据
  90. //DateTime now = DateTime.Now;
  91. DateTime now = (DateTime)bp.LastUpdate!; //测试
  92. DateTime startTime = now.AddDays(-duration);
  93. DateTime endTime = now;
  94. //
  95. var systolicAggregate = await _serviceTDengine.GetAggregateValueAsync("systolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'");
  96. var diastolicAggregate = await _serviceTDengine.GetAggregateValueAsync("diastolic_value", "stb_hm_bloodpress", $"ts>='{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and ts <='{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}' and serialno='{bp.Serialno}'");
  97. //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}'");
  98. //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}'");
  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.GetAvgExceptMaxMinValueAsync("systolic_value", "stb_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.GetAvgExceptMaxMinValueAsync("diastolic_value", "stb_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. if (systolicAvg.Equals(0)|| diastolicAvg.Equals(0))
  109. {
  110. _logger.LogWarning("平均值为0不保存");
  111. return;
  112. }
  113. //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} ");
  114. //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}");
  115. // 偏移参数
  116. var avgOffset = 0.25M;
  117. var systolicAvgOffset = avgOffset;
  118. var diastolicAvgOffset = avgOffset;
  119. // 增量值=(标定值-平均值)* 0.25
  120. var systolicInc = systolicAvg.Equals(0M) ? 0 : (int)((systolicRefValue - systolicAvg) * systolicAvgOffset)!;
  121. var diastolicInc = diastolicAvg.Equals(0M) ? 0 : (int)((diastolicRefValue - diastolicAvg) * diastolicAvgOffset)!;
  122. #endregion
  123. // _logger.LogInformation($"health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)}");
  124. //if (bp.MessageId.Equals("1674952622991276544"))
  125. //{
  126. // var aa = bp;
  127. // var cc = aa;
  128. //}
  129. #region 插入BP增量值 hm_bloodpress_stats_inc
  130. //var sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} VALUES(" +
  131. // $"{DateTimeUtil.ConvertToTimeStamp((DateTime)bp.LastUpdate)* 1000000}" +
  132. // $"'{bp.BloodPressId}'," +
  133. // $"'{bp.MessageId}'," +
  134. // $"'{bp.Serialno}'," +
  135. // $"{bp.SystolicValue}," +
  136. // $"{systolicRefValue}," +
  137. // $"{systolicAvg}," +
  138. // $"{systolicMax}," +
  139. // $"{systolicMin}," +
  140. // $"{systolicAvgOffset}," +
  141. // $"{systolicInc}," +
  142. // $"{bp.DiastolicValue}," +
  143. // $"{diastolicRefValue}," +
  144. // $"{diastolicAvg}," +
  145. // $"{diastolicMax}," +
  146. // $"{diastolicMin}," +
  147. // $"{diastolicAvgOffset}," +
  148. // $"{diastolicInc}," +
  149. // $"{gender}," +
  150. // $"{age}," +
  151. // $"{height}," +
  152. // $"{weight}," +
  153. // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  154. // $"{duration}," +
  155. // $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  156. // $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  157. // $"'{string.Empty}')";
  158. //var sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} VALUES(" +
  159. // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  160. // $"'{bp.BloodPressId}'," +
  161. // $"'{bp.MessageId}'," +
  162. // $"'{bp.Serialno}'," +
  163. // $"{bp.SystolicValue}," +
  164. // $"{systolicRefValue}," +
  165. // $"{systolicAvg}," +
  166. // $"{systolicMax}," +
  167. // $"{systolicMin}," +
  168. // $"{systolicAvgOffset}," +
  169. // $"{systolicInc}," +
  170. // $"{bp.DiastolicValue}," +
  171. // $"{diastolicRefValue}," +
  172. // $"{diastolicAvg}," +
  173. // $"{diastolicMax}," +
  174. // $"{diastolicMin}," +
  175. // $"{diastolicAvgOffset}," +
  176. // $"{diastolicInc}," +
  177. // $"{gender}," +
  178. // $"{age}," +
  179. // $"{height}," +
  180. // $"{weight}," +
  181. // $"'{bp.LastUpdate:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  182. // $"{duration}," +
  183. // $"'{startTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  184. // $"'{endTime:yyyy-MM-ddTHH:mm:ss.fffZ}'," +
  185. // $"'{string.Empty}')";
  186. //var res = await _serviceTDengine.GernalRestSql(sql);
  187. var sql = $"INSERT INTO health_monitor.hm_bp_stats_inc_{bp.Serialno.Substring(bp.Serialno.Length - 2)} VALUES(" +
  188. $"'{bp.LastUpdate:yyyy-MM-dd HH:mm:ss.fff}'," +
  189. $"'{bp.BloodPressId}'," +
  190. $"'{bp.MessageId}'," +
  191. $"'{bp.Serialno}'," +
  192. $"{bp.SystolicValue}," +
  193. $"{systolicRefValue}," +
  194. $"{systolicAvg}," +
  195. $"{systolicMax}," +
  196. $"{systolicMin}," +
  197. $"{systolicAvgOffset}," +
  198. $"{systolicInc}," +
  199. $"{bp.DiastolicValue}," +
  200. $"{diastolicRefValue}," +
  201. $"{diastolicAvg}," +
  202. $"{diastolicMax}," +
  203. $"{diastolicMin}," +
  204. $"{diastolicAvgOffset}," +
  205. $"{diastolicInc}," +
  206. $"{gender}," +
  207. $"{age}," +
  208. $"{height}," +
  209. $"{weight}," +
  210. $"'{bp.LastUpdate:yyyy-MM-dd HH:mm:ss.fff}'," +
  211. $"{duration}," +
  212. $"'{startTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  213. $"'{endTime:yyyy-MM-dd HH:mm:ss.fff}'," +
  214. $"'{string.Empty}')";
  215. _serviceTDengine.ExecuteInsertSQL(sql);
  216. #endregion
  217. }
  218. }
  219. }