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.

1300 lines
56KB

  1. using HealthMonitor.Common;
  2. using HealthMonitor.Common.helper;
  3. using HealthMonitor.Model.Config;
  4. using HealthMonitor.Model.Service.Mapper;
  5. using HealthMonitor.Service.Biz.db.Dto;
  6. using HealthMonitor.Util.Models;
  7. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  8. using Microsoft.Extensions.Logging;
  9. using Microsoft.Extensions.Options;
  10. using Newtonsoft.Json;
  11. using Newtonsoft.Json.Linq;
  12. using SqlSugar;
  13. using SqlSugar.DbConvert;
  14. using SqlSugar.TDengine;
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Data;
  18. using System.Linq;
  19. using System.Linq.Expressions;
  20. using System.Reflection;
  21. using System.Text;
  22. using System.Threading.Tasks;
  23. using System.Xml.Linq;
  24. using TDengineDriver;
  25. using TDengineDriver.Impl;
  26. using TDengineTMQ;
  27. using HealthMonitor.Service.Cache;
  28. using System.Text.RegularExpressions;
  29. using Etcdserverpb;
  30. using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
  31. namespace HealthMonitor.Service.Biz.db
  32. {
  33. public class TDengineService
  34. {
  35. private readonly ILogger<TDengineService> _logger;
  36. private readonly HttpHelper _httpHelper=default!;
  37. private readonly TDengineServiceConfig _configTDengineService;
  38. private readonly SqlSugarClient _clientSqlSugar;
  39. private readonly FhrPhrMapCacheManager _mgrFhrPhrMapCache;
  40. private readonly DeviceCacheManager _deviceCacheMgr;
  41. public TDengineService(ILogger<TDengineService> logger,
  42. IOptions<TDengineServiceConfig> configTDengineService,
  43. HttpHelper httpHelper,
  44. FhrPhrMapCacheManager fhrPhrMapCacheManager, DeviceCacheManager deviceCacheMgr
  45. )
  46. {
  47. _logger = logger;
  48. _configTDengineService = configTDengineService.Value;
  49. _httpHelper = httpHelper;
  50. _mgrFhrPhrMapCache = fhrPhrMapCacheManager;
  51. _deviceCacheMgr = deviceCacheMgr;
  52. _clientSqlSugar = new SqlSugarClient(new ConnectionConfig()
  53. {
  54. DbType = SqlSugar.DbType.TDengine,
  55. ConnectionString = $"Host={_configTDengineService.Host};Port={_configTDengineService.Port};Username={_configTDengineService.UserName};Password={_configTDengineService.Password};Database={_configTDengineService.DB};TsType=config_ns",
  56. IsAutoCloseConnection = true,
  57. AopEvents = new AopEvents
  58. {
  59. OnLogExecuting = (sql, p) =>
  60. {
  61. Console.WriteLine(SqlSugar.UtilMethods.GetNativeSql(sql, p));
  62. }
  63. },
  64. ConfigureExternalServices = new ConfigureExternalServices()
  65. {
  66. EntityService = (property, column) =>
  67. {
  68. if (column.SqlParameterDbType == null)
  69. {
  70. column.SqlParameterDbType = typeof(CommonPropertyConvert);
  71. }
  72. }
  73. },
  74. MoreSettings=new ConnMoreSettings()
  75. {
  76. PgSqlIsAutoToLower = false,
  77. PgSqlIsAutoToLowerCodeFirst = false,
  78. }
  79. });
  80. }
  81. public IntPtr Connection()
  82. {
  83. string host = _configTDengineService.Host;
  84. string user = _configTDengineService.UserName;
  85. string db = _configTDengineService.DB;
  86. short port = _configTDengineService.Port;
  87. string password = _configTDengineService.Password;
  88. //#if DEBUG
  89. // //string configDir = "C:/TDengine/cfg";
  90. // //TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR, configDir);
  91. // TDengine.Options((int)TDengineInitOption.TSDB_OPTION_TIMEZONE, "Asia/Beijing");
  92. //#endif
  93. IntPtr conn = TDengine.Connect(host, user, password, db, port);
  94. if (conn == IntPtr.Zero)
  95. {
  96. _logger.LogError($"连接 TDengine 失败....");
  97. }
  98. else
  99. {
  100. _logger.LogInformation($"连接 TDengine 成功....");
  101. }
  102. return conn;
  103. }
  104. public void ExecuteSQL(IntPtr conn, string sql)
  105. {
  106. IntPtr res = TDengine.Query(conn, sql);
  107. // Check if query success
  108. if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
  109. {
  110. Console.Write(sql + " failure, ");
  111. // Get error message while Res is a not null pointer.
  112. if (res != IntPtr.Zero)
  113. {
  114. Console.Write("reason:" + TDengine.Error(res));
  115. }
  116. }
  117. else
  118. {
  119. Console.Write(sql + " success, {0} rows affected", TDengine.AffectRows(res));
  120. //... do something with res ...
  121. // Important: need to free result to avoid memory leak.
  122. TDengine.FreeResult(res);
  123. }
  124. }
  125. public void ExecuteQuerySQL(IntPtr conn, string sql)
  126. {
  127. IntPtr res = TDengine.Query(conn, sql);
  128. // Check if query success
  129. if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
  130. {
  131. Console.Write(sql + " failure, ");
  132. // Get error message while Res is a not null pointer.
  133. if (res != IntPtr.Zero)
  134. {
  135. Console.Write("reason:" + TDengine.Error(res));
  136. }
  137. }
  138. else
  139. {
  140. Console.Write(sql + " success, {0} rows affected", TDengine.AffectRows(res));
  141. //... do something with res ...
  142. List<TDengineDriver.TDengineMeta> resMeta = LibTaos.GetMeta(res);
  143. List<object> resData = LibTaos.GetData(res);
  144. foreach (var meta in resMeta)
  145. {
  146. _logger.LogInformation("\t|{meta.name} {meta.TypeName()} ({meta.size})\t|", meta.name, meta.TypeName(), meta.size);
  147. }
  148. for (int i = 0; i < resData.Count; i++)
  149. {
  150. _logger.LogInformation($"|{resData[i].ToString()} \t");
  151. if (((i + 1) % resMeta.Count == 0))
  152. {
  153. _logger.LogInformation("");
  154. }
  155. }
  156. // Important: need to free result to avoid memory leak.
  157. TDengine.FreeResult(res);
  158. }
  159. }
  160. public void CheckRes(IntPtr conn, IntPtr res, String errorMsg)
  161. {
  162. if (TDengine.ErrorNo(res) != 0)
  163. {
  164. throw new Exception($"{errorMsg} since: {TDengine.Error(res)}");
  165. }
  166. }
  167. public void ExecuteInsertSQL(string sql)
  168. {
  169. var conn = Connection();
  170. try
  171. {
  172. //sql = "INSERT INTO d1001 USING meters TAGS('California.SanFrancisco', 2) VALUES ('2018-10-03 14:38:05.000', 10.30000, 219, 0.31000) ('2018-10-03 14:38:15.000', 12.60000, 218, 0.33000) ('2018-10-03 14:38:16.800', 12.30000, 221, 0.31000) " +
  173. // "d1002 USING power.meters TAGS('California.SanFrancisco', 3) VALUES('2018-10-03 14:38:16.650', 10.30000, 218, 0.25000) " +
  174. // "d1003 USING power.meters TAGS('California.LosAngeles', 2) VALUES('2018-10-03 14:38:05.500', 11.80000, 221, 0.28000)('2018-10-03 14:38:16.600', 13.40000, 223, 0.29000) " +
  175. // "d1004 USING power.meters TAGS('California.LosAngeles', 3) VALUES('2018-10-03 14:38:05.000', 10.80000, 223, 0.29000)('2018-10-03 14:38:06.500', 11.50000, 221, 0.35000)";
  176. //#if DEBUG
  177. // //string configDir = "C:/TDengine/cfg";
  178. // //TDengine.Options((int)TDengineInitOption.TSDB_OPTION_CONFIGDIR, configDir);
  179. // TDengine.Options((int)TDengineInitOption.TSDB_OPTION_TIMEZONE, "Asia/Beijing");
  180. //#endif
  181. _logger.LogInformation($"Insert SQL: {sql}");
  182. IntPtr res = TDengine.Query(conn, sql);
  183. CheckRes(conn, res, "failed to insert data");
  184. int affectedRows = TDengine.AffectRows(res);
  185. _logger.LogInformation("affectedRows {affectedRows}" , affectedRows);
  186. TDengine.FreeResult(res);
  187. }
  188. finally
  189. {
  190. TDengine.Close(conn);
  191. }
  192. }
  193. #region TDengine.Connector async query
  194. public void QueryCallback(IntPtr param, IntPtr taosRes, int code)
  195. {
  196. if (code == 0 && taosRes != IntPtr.Zero)
  197. {
  198. FetchRawBlockAsyncCallback fetchRowAsyncCallback = new FetchRawBlockAsyncCallback(FetchRawBlockCallback);
  199. TDengine.FetchRawBlockAsync(taosRes, fetchRowAsyncCallback, param);
  200. }
  201. else
  202. {
  203. _logger.LogInformation("async query data failed, failed code {code}",code);
  204. }
  205. }
  206. // Iteratively call this interface until "numOfRows" is no greater than 0.
  207. public void FetchRawBlockCallback(IntPtr param, IntPtr taosRes, int numOfRows)
  208. {
  209. if (numOfRows > 0)
  210. {
  211. _logger.LogInformation("{numOfRows} rows async retrieved", numOfRows);
  212. IntPtr pdata = TDengine.GetRawBlock(taosRes);
  213. List<TDengineMeta> metaList = TDengine.FetchFields(taosRes);
  214. List<object> dataList = LibTaos.ReadRawBlock(pdata, metaList, numOfRows);
  215. for (int i = 0; i < metaList.Count; i++)
  216. {
  217. _logger.LogInformation("{0} {1}({2}) \t|", metaList[i].name, metaList[i].type, metaList[i].size);
  218. }
  219. _logger.LogInformation("");
  220. for (int i = 0; i < dataList.Count; i++)
  221. {
  222. if (i != 0 && i % metaList.Count == 0)
  223. {
  224. _logger.LogInformation("{dataList[i]}\t|", dataList[i]);
  225. }
  226. _logger.LogInformation("{dataList[i]}\t|", dataList[i]);
  227. }
  228. TDengine.FetchRawBlockAsync(taosRes, FetchRawBlockCallback, param);
  229. }
  230. else
  231. {
  232. if (numOfRows == 0)
  233. {
  234. _logger.LogInformation("async retrieve complete.");
  235. }
  236. else
  237. {
  238. _logger.LogInformation("FetchRawBlockCallback callback error, error code {numOfRows}", numOfRows);
  239. }
  240. TDengine.FreeResult(taosRes);
  241. }
  242. }
  243. public void ExecuteQueryAsync(string sql)
  244. {
  245. var conn = Connection();
  246. QueryAsyncCallback queryAsyncCallback = new QueryAsyncCallback(QueryCallback);
  247. TDengine.QueryAsync(conn, sql, queryAsyncCallback, IntPtr.Zero);
  248. }
  249. //public void ExecuteQuery(string sql)
  250. //{
  251. // var conn = Connection();
  252. // QueryAsyncCallback queryAsyncCallback = new QueryAsyncCallback(QueryCallback);
  253. // TDengine.QueryAsync(conn, sql, queryAsyncCallback, IntPtr.Zero);
  254. //}
  255. public Aggregate GetAggregateValue(string field, string tbName, string? condition)
  256. {
  257. List<int> data = new();
  258. var sql = $"SELECT MAX({field}), MIN({field}) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}";
  259. var conn = Connection();
  260. try
  261. {
  262. IntPtr res = TDengine.Query(conn, sql);
  263. // Check if query success
  264. if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
  265. {
  266. Console.Write(sql + " failure, ");
  267. // Get error message while Res is a not null pointer.
  268. if (res != IntPtr.Zero)
  269. {
  270. Console.Write("reason:" + TDengine.Error(res));
  271. }
  272. }
  273. else
  274. {
  275. Console.Write(sql + " success, {0} rows affected", TDengine.AffectRows(res));
  276. //... do something with res ...
  277. List<TDengineMeta> resMeta = LibTaos.GetMeta(res);
  278. List<object> resData = LibTaos.GetData(res);
  279. foreach (var meta in resMeta)
  280. {
  281. Console.Write($"\t|{meta.name} {meta.TypeName()} ({meta.size})\t|");
  282. }
  283. resData.ForEach(x => data.Add(SafeType.SafeInt(x)));
  284. // Important: need to free result to avoid memory leak.
  285. TDengine.FreeResult(res);
  286. }
  287. }
  288. finally
  289. {
  290. TDengine.Close(conn);
  291. }
  292. return new Aggregate
  293. {
  294. Max = data.Count.Equals(0) ? 0 : data[0],
  295. Min = data.Count.Equals(0) ? 0 : data[1],
  296. };
  297. }
  298. public int GetAvgExceptMaxMinValue(string field, string tbName, string? condition)
  299. {
  300. List<int> data = new();
  301. var sql = $"SELECT MAX({field}), MIN({field}) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}";
  302. var aggregate= GetAggregateValue(field, tbName, condition);
  303. var sqlAvg = $"SELECT AVG({field}) FROM {_configTDengineService.DB}.{tbName} WHERE {condition} AND {field} < {aggregate.Max} and {field} > {aggregate.Min}";
  304. var conn = Connection();
  305. try
  306. {
  307. IntPtr res = TDengine.Query(conn, sqlAvg);
  308. // Check if query success
  309. if ((res == IntPtr.Zero) || (TDengine.ErrorNo(res) != 0))
  310. {
  311. Console.Write(sqlAvg + " failure, ");
  312. // Get error message while Res is a not null pointer.
  313. if (res != IntPtr.Zero)
  314. {
  315. Console.Write("reason:" + TDengine.Error(res));
  316. }
  317. }
  318. else
  319. {
  320. Console.Write(sqlAvg + " success, {0} rows affected", TDengine.AffectRows(res));
  321. //... do something with res ...
  322. List<TDengineMeta> resMeta = LibTaos.GetMeta(res);
  323. List<object> resData = LibTaos.GetData(res);
  324. foreach (var meta in resMeta)
  325. {
  326. Console.Write($"\t|{meta.name} {meta.TypeName()} ({meta.size})\t|");
  327. }
  328. resData.ForEach(x => data.Add(SafeType.SafeInt(x)));
  329. // Important: need to free result to avoid memory leak.
  330. TDengine.FreeResult(res);
  331. }
  332. }
  333. finally
  334. {
  335. TDengine.Close(conn);
  336. }
  337. return data.Count.Equals(0) ? 0 : data[0];
  338. }
  339. #endregion
  340. #region RestAPI
  341. public async Task<string?> ExecuteQuerySQLRestResponse(string sql)
  342. {
  343. var url = $"http://{_configTDengineService.Host}:{_configTDengineService.RestPort}/rest/sql/{_configTDengineService.DB}";
  344. List<KeyValuePair<string, string>> headers = new()
  345. {
  346. new KeyValuePair<string, string>("Authorization", "Basic " + _configTDengineService.Token)
  347. };
  348. var result = await _httpHelper.HttpToPostAsync(url, sql, headers).ConfigureAwait(false);
  349. return result;
  350. }
  351. public async Task<string?> ExecuteSelectRestResponseAsync( string tbName, string condition="1", string field = "*")
  352. {
  353. var url = $"http://{_configTDengineService.Host}:{_configTDengineService.RestPort}/rest/sql/{_configTDengineService.DB}";
  354. var sql = $"SELECT {field} FROM {_configTDengineService.DB}.{tbName} WHERE {condition}";
  355. List<KeyValuePair<string, string>> headers = new()
  356. {
  357. new KeyValuePair<string, string>("Authorization", "Basic " + _configTDengineService.Token)
  358. };
  359. _logger.LogInformation($"{nameof(ExecuteSelectRestResponseAsync)} --- SQL 语句执行 {sql}");
  360. var result = await _httpHelper.HttpToPostAsync(url, sql, headers).ConfigureAwait(false);
  361. return result;
  362. }
  363. public async Task<bool> GernalRestSql(string sql)
  364. {
  365. //"http://{server}:{port}/rest/sql/{db}"
  366. var url = $"http://{_configTDengineService.Host}:{_configTDengineService.RestPort}/rest/sql/{_configTDengineService.DB}";
  367. List<KeyValuePair<string, string>> headers = new()
  368. {
  369. new KeyValuePair<string, string>("Authorization", "Basic " + _configTDengineService.Token)
  370. };
  371. var result = await _httpHelper.HttpToPostAsync(url, sql, headers).ConfigureAwait(false);
  372. var res = JsonConvert.DeserializeObject<TDengineRestResBase>(result!);
  373. if (result != null)
  374. {
  375. if (res?.Code == 0)
  376. {
  377. _logger.LogInformation($"{nameof(GernalRestSql)},SQL 语句执行成功|{sql}");
  378. return true;
  379. }
  380. else
  381. {
  382. _logger.LogWarning($"{nameof(GernalRestSql)},SQL 语句执行失败||{sql}");
  383. return false;
  384. }
  385. }
  386. else
  387. {
  388. _logger.LogError($"{nameof(GernalRestSql)},TDengine 服务器IP:{_configTDengineService.Host} 错误,请联系运维人员");
  389. return false;
  390. }
  391. //return res.Code==0;
  392. }
  393. public async Task<string?> GernalRestSqlResTextAsync(string sql)
  394. {
  395. _logger.LogInformation($"执行 SQL: {nameof(GernalRestSqlResTextAsync)}--{sql}");
  396. var url = $"http://{_configTDengineService.Host}:{_configTDengineService.RestPort}/rest/sql/{_configTDengineService.DB}";
  397. List<KeyValuePair<string, string>> headers = new()
  398. {
  399. new KeyValuePair<string, string>("Authorization", "Basic " + _configTDengineService.Token)
  400. };
  401. var result = await _httpHelper.HttpToPostAsync(url, sql, headers).ConfigureAwait(false);
  402. return result;
  403. }
  404. /** 取消
  405. /// <summary>
  406. /// 最大值,最小值聚合
  407. /// </summary>
  408. /// <param name="field"></param>
  409. /// <param name="tbName"></param>
  410. /// <param name="condition"></param>
  411. /// <returns></returns>
  412. public async Task<Aggregate> GetAggregateValueAsync(string field,string tbName,string? condition)
  413. {
  414. var sql = $"SELECT MAX({field}), MIN({field}) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}";
  415. var result = await GernalRestSqlResTextAsync(sql);
  416. var res = JsonConvert.DeserializeObject<Aggregate>(result!);
  417. List<dynamic> data = res?.Data!;
  418. return new Aggregate
  419. {
  420. Max = data.Count.Equals(0) ? 0 : data[0][0],
  421. Min = data.Count.Equals(0) ? 0 : data[0][1],
  422. };
  423. }
  424. /// <summary>
  425. /// 去除最大值和最小值后的平均值
  426. /// </summary>
  427. /// <param name="field"></param>
  428. /// <param name="tbName"></param>
  429. /// <param name="condition"></param>
  430. /// <returns></returns>
  431. public async Task<int> GetAvgExceptMaxMinValueAsync(string field, string tbName, string? condition)
  432. {
  433. var sql = $"SELECT MAX({field}), MIN({field}) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}";
  434. var result = await GernalRestSqlResTextAsync(sql);
  435. var res = JsonConvert.DeserializeObject<TDengineRestResBase>(result!);
  436. _logger.LogInformation($"最大小值:{sql}");
  437. List<dynamic> data = res?.Data!;
  438. var sqlAvg = $"SELECT AVG({field}) FROM {_configTDengineService.DB}.{tbName} WHERE {condition} AND {field} < { (data.Count.Equals(0)? 0: data[0][0]) } and {field} > {(data.Count.Equals(0) ? 0 : data[0][1])}";
  439. result = await GernalRestSqlResTextAsync(sqlAvg);
  440. _logger.LogInformation($"sqlAvg:{sqlAvg}");
  441. res = JsonConvert.DeserializeObject<TDengineRestResBase>(result!);
  442. data = res?.Data!;
  443. return data.Count.Equals(0)?0:(int)data[0][0];
  444. }
  445. /// <summary>
  446. /// 获取最后的记录
  447. /// </summary>
  448. /// <param name="tbName"></param>
  449. /// <param name="condition"></param>
  450. /// <returns></returns>
  451. public async Task<JArray?> GetLastAsync(string tbName, string? condition)
  452. {
  453. var sql = $"SELECT last_row(*) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}";
  454. var result = await GernalRestSqlResTextAsync(sql);
  455. var res = JsonConvert.DeserializeObject<TDengineRestResBase>(result!);
  456. if(res?.Data?.Count==0) return null;
  457. List<dynamic> data = res?.Data!;
  458. return data[0] as JArray;
  459. }
  460. public async Task<int> GetCount(string tbName, string? condition)
  461. {
  462. var sql = $"SELECT count(ts) FROM {_configTDengineService.DB}.{tbName} WHERE {condition}";
  463. var result = await GernalRestSqlResTextAsync(sql);
  464. var res = JsonConvert.DeserializeObject<TDengineRestResBase>(result!);
  465. List<dynamic> data = res?.Data!;
  466. return SafeType.SafeInt(data[0][0]);
  467. }
  468. */
  469. #endregion
  470. /// <summary>
  471. /// 平均值算法(去除最大值,最小值和大于标定值的平均值)
  472. /// </summary>
  473. /// <param name="numToRemove"></param>
  474. /// <param name="collection"></param>
  475. /// <param name="max"></param>
  476. /// <param name="min"></param>
  477. /// <returns></returns>
  478. public static decimal AverageAfterRemovingOneMinMaxRef(List<int> collection, int max, int min,int refValue)
  479. {
  480. collection.Remove(max);
  481. collection.Remove(min);
  482. collection.RemoveAll(_ => _ > refValue);
  483. if (collection.Count < 2)
  484. {
  485. throw new ArgumentException($"数据集{collection.ToArray()},去掉一个最大值 {max}和一个最小值{min},异常值(大于标定值{refValue}),后数据值不足");
  486. }
  487. return (decimal)collection.Average(x => x);
  488. //var values = ParseData.Select(valueSelector).ToList();
  489. //collection = values.Select(i => (int)i).ToArray();
  490. //if (values.Count <= 2)
  491. //{
  492. // throw new ArgumentException("Not enough elements to remove.");
  493. //}
  494. //// Remove the specified number of minimum and maximum values
  495. ////values.RemoveAll(_ => _ == values.Min());
  496. ////values.RemoveAll(_ => _ == values.Max());
  497. //max = (int)values.Max();
  498. //min = (int)values.Min();
  499. //values.Remove(max);
  500. //values.Remove(min);
  501. //// Remove values less than the specified threshold
  502. //values.RemoveAll(_ => _ > numToRemove);
  503. //// Calculate and return the average of the remaining values
  504. //return values.Average();
  505. }
  506. /// <summary>
  507. /// 去除最大值和最小值各一个(列表的头和尾),再去除异常值
  508. /// </summary>
  509. /// <param name="systolicRefValue"></param>
  510. /// <param name="hmBpParser"></param>
  511. /// <returns></returns>
  512. public decimal[] AverageAfterRemovingOneMinMaxRef(int systolicRefValue, ParseTDengineRestResponse<BloodPressureModel>? hmBpParser)
  513. {
  514. var sortedList = hmBpParser?.Select(i => i)
  515. .Where(i => i.IsDisplay.Equals(true))
  516. .OrderByDescending(i => i.SystolicValue)
  517. .ThenByDescending(i => i.DiastolicValue)
  518. .ToList();
  519. _logger.LogInformation($"计算时间段排列数据集:{JsonConvert.SerializeObject(sortedList)}");
  520. // 去除最大值和最小值各一个(列表的头和尾)
  521. var trimmedList = sortedList?
  522. .Skip(1)
  523. .Take(sortedList.Count - 2)
  524. .ToList();
  525. _logger.LogInformation($"计算去除最大值和最小值各一个数据集:{JsonConvert.SerializeObject(trimmedList)}");
  526. // 去除异常值
  527. var filteredList = trimmedList?.Where(bp => bp.SystolicValue < SafeType.SafeInt(systolicRefValue!)).ToList();
  528. _logger.LogInformation($"计算除异常值个数据集:{JsonConvert.SerializeObject(filteredList)}");
  529. if (filteredList?.Count < 2)
  530. {
  531. // throw new ArgumentException("数据不够不能计算");
  532. // 平均值为0,说明数据不足,不能计算增量值
  533. return new decimal[] { 0M, 0M };
  534. }
  535. var systolicAvg = filteredList?.Select(bp => bp.SystolicValue).Average();
  536. var diastolicAvg = filteredList?.Select(bp => bp.DiastolicValue).Average();
  537. return new decimal[] { (decimal)systolicAvg!, (decimal)diastolicAvg! };
  538. }
  539. /// <summary>
  540. /// 去除最大值和最小值各一个(列表的头和尾)
  541. /// </summary>
  542. /// <param name="systolicRefValue"></param>
  543. /// <param name="hmBpParser"></param>
  544. /// <returns></returns>
  545. public decimal[] AverageAfterRemovingOneMinMaxRef(ParseTDengineRestResponse<BloodPressureModel>? hmBpParser)
  546. {
  547. var sortedList = hmBpParser?.Select(i => i)
  548. .Where(i => i.IsDisplay.Equals(true))
  549. .OrderByDescending(i => i.SystolicValue)
  550. .ThenByDescending(i => i.DiastolicValue)
  551. .ToList();
  552. _logger.LogInformation($"计算时间段排列数据集:{JsonConvert.SerializeObject(sortedList)}");
  553. // 去除最大值和最小值各一个(列表的头和尾)
  554. var trimmedList = sortedList?
  555. .Skip(1)
  556. .Take(sortedList.Count - 2)
  557. .ToList();
  558. _logger.LogInformation($"计算去除最大值和最小值各一个数据集:{JsonConvert.SerializeObject(trimmedList)}");
  559. var filteredList = trimmedList?.ToList();
  560. _logger.LogInformation($"计算除异常值个数据集:{JsonConvert.SerializeObject(filteredList)}");
  561. if (filteredList?.Count < 2)
  562. {
  563. // throw new ArgumentException("数据不够不能计算");
  564. // 平均值为0,说明数据不足,不能计算增量值
  565. return new decimal[] { 0M, 0M };
  566. }
  567. var systolicAvg = filteredList?.Select(bp => bp.SystolicValue).Average();
  568. var diastolicAvg = filteredList?.Select(bp => bp.DiastolicValue).Average();
  569. return new decimal[] { (decimal)systolicAvg!, (decimal)diastolicAvg! };
  570. }
  571. #region SqlSugarClient
  572. /**
  573. public async Task InsertFetalHeartRateAsync()
  574. {
  575. var tableName = typeof(FetalHeartRateModel)
  576. .GetCustomAttribute<STableAttribute>()?
  577. .STableName;
  578. if (tableName == null)
  579. {
  580. throw new InvalidOperationException("STableAttribute not found on FetalHeartRateModel class.");
  581. }
  582. await _clientSqlSugar.Ado.ExecuteCommandAsync($"create table IF NOT EXISTS hm_fhr_00 using {tableName} tags('00')");
  583. _clientSqlSugar.Insertable(new FetalHeartRateModel()
  584. {
  585. Timestamp = DateTime.Now,
  586. CreateTime = DateTime.Now,
  587. FetalHeartRate = 90,
  588. FetalHeartRateId = Guid.NewGuid().ToString("D"),
  589. IsDisplay = false,
  590. Method = 1,
  591. PersonId = Guid.NewGuid().ToString("D"),
  592. MessageId = Guid.NewGuid().ToString("D"),
  593. SerialNumber = Guid.NewGuid().ToString("D"),
  594. DeviceKey = Guid.NewGuid().ToString("D"),
  595. SerialTailNumber = "00",
  596. LastUpdate = DateTime.Now,
  597. }).AS("hm_fhr_00").ExecuteCommand();
  598. }
  599. public Task<FetalHeartRateModel> GetFirst()
  600. {
  601. var tableName = typeof(FetalHeartRateModel)
  602. .GetCustomAttribute<STableAttribute>()?
  603. .STableName;
  604. var first = _clientSqlSugar
  605. .Queryable<FetalHeartRateModel>()
  606. .AS(tableName)
  607. .OrderByDescending(x => x.Timestamp).FirstAsync();
  608. return first;
  609. }
  610. */
  611. /// <summary>
  612. /// 插入记录
  613. /// </summary>
  614. /// <typeparam name="T"></typeparam>
  615. /// <param name="model"></param>
  616. /// <param name="tbName">子表名称</param>
  617. /// <returns></returns>
  618. /// <exception cref="InvalidOperationException"></exception>
  619. public async Task InsertAsync<T>(string tbName, T model)
  620. {
  621. var stbName = typeof(T)
  622. .GetCustomAttribute<STableAttribute>()?
  623. .STableName;
  624. if (stbName == null)
  625. {
  626. throw new InvalidOperationException($"STableAttribute not found on {nameof(T)} class.");
  627. }
  628. var tailNo = typeof(T).GetProperty("SerialTailNumber")?.GetValue(model)?.ToString();
  629. if (string.IsNullOrEmpty(tailNo))
  630. {
  631. throw new InvalidOperationException($"SerialNumberAttribute not found on {nameof(T)} class.");
  632. }
  633. var tbFullName = $"{tbName}_{tailNo}";
  634. await _clientSqlSugar.Ado.ExecuteCommandAsync($"create table IF NOT EXISTS {tbFullName} using {stbName} tags('{tailNo}')");
  635. //_clientSqlSugar.InsertableByDynamic(model).AS(tbFullName).ExecuteCommand();
  636. _clientSqlSugar.InsertableByObject(model).AS(tbFullName).ExecuteCommand();
  637. }
  638. public Task<T> GetLastAsync<T>() where T : class
  639. {
  640. var tableName = typeof(T)
  641. .GetCustomAttribute<STableAttribute>()?
  642. .STableName;
  643. // 创建一个表示 Timestamp 属性的表达式
  644. var parameter = Expression.Parameter(typeof(T), "x");
  645. var property = Expression.Property(parameter, "Timestamp");
  646. var lambda = Expression.Lambda<Func<T, object>>(Expression.Convert(property, typeof(object)), parameter);
  647. var first = _clientSqlSugar
  648. .Queryable<T>()
  649. .AS(tableName)
  650. .OrderByDescending(lambda).FirstAsync();
  651. return first;
  652. }
  653. public Task<T> GetLastAsync<T>(string serialNo) where T : class
  654. {
  655. var tableName = typeof(T)
  656. .GetCustomAttribute<STableAttribute>()?
  657. .STableName;
  658. // 创建表示 Timestamp 属性的表达式
  659. var parameter = Expression.Parameter(typeof(T), "x");
  660. var timestampProperty = Expression.Property(parameter, "Timestamp");
  661. var timestampLambda = Expression.Lambda<Func<T, object>>(Expression.Convert(timestampProperty, typeof(object)), parameter);
  662. // 创建表示 SerialNo 属性的表达式
  663. var serialNoProperty = Expression.Property(parameter, "SerialNumber");
  664. var serialNoConstant = Expression.Constant(serialNo);
  665. var equalExpression = Expression.Equal(serialNoProperty, serialNoConstant);
  666. var serialNoLambda = Expression.Lambda<Func<T, bool>>(equalExpression, parameter);
  667. var first = _clientSqlSugar
  668. .Queryable<T>()
  669. .AS(tableName)
  670. .Where(serialNoLambda)
  671. .OrderByDescending(timestampLambda).FirstAsync();
  672. return first;
  673. }
  674. public async Task<List<T>> GetBySerialNoAsync<T>(string serialNo) where T : class
  675. {
  676. var tableName = typeof(T)
  677. .GetCustomAttribute<STableAttribute>()?
  678. .STableName;
  679. // 创建表示 Timestamp 属性的表达式
  680. var parameter = Expression.Parameter(typeof(T), "x");
  681. var timestampProperty = Expression.Property(parameter, "Timestamp");
  682. var timestampLambda = Expression.Lambda<Func<T, object>>(Expression.Convert(timestampProperty, typeof(object)), parameter);
  683. // 创建表示 SerialNo 属性的表达式
  684. var serialNoProperty = Expression.Property(parameter, "SerialNumber");
  685. var serialNoConstant = Expression.Constant(serialNo);
  686. var equalExpression = Expression.Equal(serialNoProperty, serialNoConstant);
  687. var serialNoLambda = Expression.Lambda<Func<T, bool>>(equalExpression, parameter);
  688. var res = await _clientSqlSugar
  689. .Queryable<T>()
  690. .AS(tableName)
  691. .Where(serialNoLambda)
  692. .OrderByDescending(timestampLambda).ToListAsync();
  693. return res;
  694. }
  695. public async Task<List<T>> GetBySerialNoAsync<T>(string serialNo, int days) where T : class
  696. {
  697. var tableName = typeof(T)
  698. .GetCustomAttribute<STableAttribute>()?
  699. .STableName;
  700. // 创建表示 Timestamp 属性的表达式
  701. var parameter = Expression.Parameter(typeof(T), "x");
  702. var timestampProperty = Expression.Property(parameter, "Timestamp");
  703. var timestampLambda = Expression.Lambda<Func<T, object>>(Expression.Convert(timestampProperty, typeof(object)), parameter);
  704. // 创建表示 SerialNo 属性的表达式
  705. var serialNoProperty = Expression.Property(parameter, "SerialNumber");
  706. var serialNoConstant = Expression.Constant(serialNo);
  707. var equalExpression = Expression.Equal(serialNoProperty, serialNoConstant);
  708. // 创建表示 Timestamp 大于指定天数的表达式
  709. var daysAgo = DateTime.Now.AddDays(-days);
  710. var daysAgoConstant = Expression.Constant(daysAgo, typeof(DateTime));
  711. var greaterThanExpression = Expression.GreaterThan(timestampProperty, daysAgoConstant);
  712. // 合并 SerialNo 和 Timestamp 条件
  713. var combinedExpression = Expression.AndAlso(equalExpression, greaterThanExpression);
  714. var combinedLambda = Expression.Lambda<Func<T, bool>>(combinedExpression, parameter);
  715. var res = await _clientSqlSugar
  716. .Queryable<T>()
  717. .AS(tableName)
  718. .Where(combinedLambda)
  719. .OrderByDescending(timestampLambda).ToListAsync();
  720. return res;
  721. }
  722. public async Task DeleteAllBySerialNoCMDAsync<T>(string serialNo) where T : class
  723. {
  724. var records = await GetBySerialNoAsync<T>(serialNo, 365);
  725. var stbName = typeof(T)
  726. .GetCustomAttribute<STableAttribute>()?
  727. .STableName;
  728. var tasks = records.Select(async r =>
  729. {
  730. Type modelType = typeof(T);
  731. PropertyInfo timestampProperty = typeof(T).GetProperty("Timestamp")!;
  732. object timestampValue = timestampProperty.GetValue(r)!;
  733. var ts = ((DateTime)timestampValue);
  734. var startTimestamp = ts.ToString("yyyy-MM-dd HH:mm:ss.fff");
  735. var endTimestamp = ts.AddMilliseconds(1).ToString("yyyy-MM-dd HH:mm:ss.fff");
  736. var sql = $"DELETE FROM {stbName} WHERE ts >= '{startTimestamp}' AND ts < '{endTimestamp}'";
  737. var res= await _clientSqlSugar.Ado.ExecuteCommandAsync(sql);
  738. Console.WriteLine(res);
  739. });
  740. await Task.WhenAll(tasks);
  741. }
  742. #endregion
  743. #region 胎心算法
  744. ///// <summary>
  745. ///// 计算个人一般心率(最大值,最大值,最小值)
  746. ///// </summary>
  747. ///// <param name="serialNo"></param>
  748. ///// <param name="days"></param>
  749. ///// <param name="percentage"></param>
  750. ///// <returns></returns>
  751. //public async Task<PregnancyCommonHeartRateModel?> InitPregnancyCommonHeartRateModeAsync(string serialNo, int days = 7, int percentage = 90)
  752. //{
  753. // var tableName = typeof(PregnancyHeartRateModel)
  754. // .GetCustomAttribute<STableAttribute>()?
  755. // .STableName;
  756. // var daysAgo = DateTime.Now.AddDays(-days);
  757. // var collection = await _clientSqlSugar
  758. // .Queryable<PregnancyHeartRateModel>()
  759. // .AS(tableName)
  760. // .Where(i => i.SerialNumber.Equals(serialNo))
  761. // .Where(i => i.Timestamp > daysAgo)
  762. // .OrderByDescending(i => i.Timestamp)
  763. // .ToArrayAsync();
  764. // var res = collection
  765. // .Select(i => i.PregnancyHeartRate).ToList();
  766. // // 心率数据量必须30个以上才进行计算
  767. // if (res.Count < 30)
  768. // {
  769. // _logger.LogInformation($"{serialNo} 心率数据不足,无法计算其众数");
  770. // return null;
  771. // }
  772. // #region 计算众数
  773. // var mode = res.GroupBy(n => n)
  774. // .OrderByDescending(g => g.Count())
  775. // .First()
  776. // .Key;
  777. // Console.WriteLine("众数是: " + mode);
  778. // // 如果有多个众数的情况
  779. // var maxCount = res.GroupBy(n => n)
  780. // .Max(g => g.Count());
  781. // var modes = res.GroupBy(n => n)
  782. // .Where(g => g.Count() == maxCount)
  783. // .Select(g => g.Key)
  784. // .ToList();
  785. // // 多个众数,选择最接近平均数或中位数的众数
  786. // if (modes.Count > 1)
  787. // {
  788. // // 计算平均值
  789. // double average = res.Average();
  790. // Console.WriteLine("平均值是: " + average);
  791. // // 计算中位数
  792. // double median;
  793. // int count = res.Count;
  794. // var sortedRes = res.OrderBy(n => n).ToList();
  795. // if (count % 2 == 0)
  796. // {
  797. // // 偶数个元素,取中间两个数的平均值
  798. // median = (sortedRes[count / 2 - 1] + sortedRes[count / 2]) / 2.0;
  799. // }
  800. // else
  801. // {
  802. // // 奇数个元素,取中间的数
  803. // median = sortedRes[count / 2];
  804. // }
  805. // //Console.WriteLine("中位数是: " + median);
  806. // _logger.LogInformation($"{serialNo} 中位数是: " + median);
  807. // // 找出最接近平均值的众数
  808. // //var closestToAverage = modes.OrderBy(m => Math.Abs(m - average)).First();
  809. // //Console.WriteLine("最接近平均值的众数是: " + closestToAverage);
  810. // // 找出最接近中位数的众数
  811. // var closestToMedian = modes.OrderBy(m => Math.Abs(m - median)).First();
  812. // _logger.LogInformation($"{serialNo} 最接近中位数的众数是: " + closestToMedian);
  813. // mode = closestToMedian;
  814. // }
  815. // #endregion
  816. // // 计算需要的数量
  817. // int requiredCount = (int)(res.Count * 0.9);
  818. // // 从原始数据集中获取最接近众数的元素
  819. // var closestToModeData = res.OrderBy(n => Math.Abs(n - mode))
  820. // .Take(requiredCount)
  821. // .ToList();
  822. // // 输出新数据集
  823. // _logger.LogInformation($"{serialNo} 新数据集: " + string.Join(", ", closestToModeData));
  824. // _logger.LogInformation($"{serialNo} 新数据集的数量: " + closestToModeData.Count);
  825. // var fhrMap = _mgrFhrPhrMapCache.GetHeartRatesMap();
  826. // var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(serialNo, "0067");
  827. // if (watchConfig == null)
  828. // {
  829. // return null;
  830. // }
  831. // // long.TryParse(watchConfig["EDOC"]!.ToString(), out long edoc);
  832. // // "EDOC": "1720860180652",当前时间 - (EDOC - 280) days =怀孕时间
  833. // //edoc = edoc.ToString().Length == 10 ? edoc * 1000 : edoc;
  834. // var edoc = DateTimeUtil.ToDateTime(watchConfig["EDOC"]!.ToString());
  835. // int pregnancyWeek = (DateTime.Now - edoc.AddDays(-280)).Days / 7;
  836. // _logger.LogInformation($"IMEI {serialNo},EDOC:{edoc},NOW:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},SinceNOW:{edoc.AddDays(-280).ToString("yyyy-MM-dd HH:mm:ss")},怀孕周数 {pregnancyWeek}");
  837. // float statMaxValueFprCoefficient = 0f;
  838. // float statMinValueFprCoefficient = 0f;
  839. // float StatModeAvgFprCoefficient = 0f;
  840. // // 20-45周之间
  841. // if (pregnancyWeek >= 12 && pregnancyWeek <= 45)
  842. // {
  843. // var map = fhrMap
  844. // .Where(i =>
  845. // i.PregnancyPeriod![0] <= pregnancyWeek &&
  846. // i.PregnancyPeriod[1] >= pregnancyWeek &&
  847. // i.PregnancyHeartRateRange![0] <= mode &&
  848. // i.PregnancyHeartRateRange[1] >= mode)
  849. // .FirstOrDefault();
  850. // if (map != null)
  851. // {
  852. // statMaxValueFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateRange![1] / res.Max(), 3);
  853. // statMinValueFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateRange[0] / res.Min(), 3);
  854. // StatModeAvgFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateAverage / mode, 3);
  855. // }
  856. // }
  857. // return new PregnancyCommonHeartRateModel()
  858. // {
  859. // Timestamp = DateTime.Now,
  860. // PersonId = collection.First().DeviceKey,
  861. // DeviceKey = collection.First().DeviceKey,
  862. // SerialNumber = collection.First().SerialNumber,
  863. // Mode = mode,
  864. // Percentage = percentage,
  865. // MaxValue = closestToModeData.Max(),
  866. // MinValue = closestToModeData.Min(),
  867. // OriginalMaxValue = res.Max(),
  868. // OriginalMinValue = res.Min(),
  869. // CreateTime = DateTime.Now,
  870. // StatStartTime = collection.OrderBy(i => i.Timestamp).Select(i => i.Timestamp).First(),
  871. // StatEndTime = collection.OrderBy(i => i.Timestamp).Select(i => i.Timestamp).Last(),
  872. // StatMaxValueFprCoefficient = statMaxValueFprCoefficient,
  873. // StatMinValueFprCoefficient = statMinValueFprCoefficient,
  874. // StatModeAvgFprCoefficient = StatModeAvgFprCoefficient,
  875. // Remark = string.Empty,
  876. // SerialTailNumber = serialNo.Substring(serialNo.Length - 2)
  877. // };
  878. //}
  879. public async Task<PregnancyCommonHeartRateModel?> InitPregnancyCommonHeartRateModeAsync(string serialNo, int days = 7, int percentage = 90, int highFreqSampleInterval=0)
  880. {
  881. var tableName = typeof(PregnancyHeartRateModel)
  882. .GetCustomAttribute<STableAttribute>()?
  883. .STableName;
  884. var daysAgo = DateTime.Now.AddDays(-days);
  885. var collection = await _clientSqlSugar
  886. .Queryable<PregnancyHeartRateModel>()
  887. .AS(tableName)
  888. .Where(i => i.SerialNumber.Equals(serialNo))
  889. .Where(i => i.LastUpdate > daysAgo)
  890. .OrderByDescending(i => i.LastUpdate)
  891. .ToArrayAsync();
  892. // 去除高频数据
  893. var filteredCollection = new List<PregnancyHeartRateModel>();
  894. PregnancyHeartRateModel? previousItem = null;
  895. //foreach (var item in collection)
  896. //{
  897. // if (previousItem == null || Math.Abs((previousItem.LastUpdate - item.LastUpdate).TotalSeconds) >= highFreqSampleInterval)
  898. // {
  899. // filteredCollection.Add(item);
  900. // previousItem = item;
  901. // }
  902. //}
  903. foreach (var item in collection)
  904. {
  905. if (previousItem!=null)
  906. {
  907. var timeDiff = (previousItem.LastUpdate - item.LastUpdate).TotalSeconds;
  908. Console.WriteLine($"previousItem:{previousItem.PregnancyHeartRate} -- item:{previousItem.PregnancyHeartRate} timeDiff:{timeDiff}--highFreqSampleInterval:{highFreqSampleInterval}");
  909. if (timeDiff >= highFreqSampleInterval)
  910. {
  911. filteredCollection.Add(item);
  912. }
  913. }
  914. previousItem = item;
  915. }
  916. // 心率数据量必须30个以上才进行计算
  917. if (filteredCollection.Count < 30)
  918. {
  919. _logger.LogInformation($"{serialNo} 心率数据不足,无法计算其众数");
  920. return null;
  921. }
  922. var res = filteredCollection
  923. .Select(i => i.PregnancyHeartRate).ToList();
  924. //// 心率数据量必须30个以上才进行计算
  925. //if (res.Count < 30)
  926. //{
  927. // _logger.LogInformation($"{serialNo} 心率数据不足,无法计算其众数");
  928. // return null;
  929. //}
  930. var listRes = filteredCollection.Select(i => new { last_update=i.LastUpdate.ToString("yyyy-MM-dd HH:mm:ss"),heart_rate=i.PregnancyHeartRate }).ToList();
  931. //_logger.LogInformation($"highFreqSampleInterval:{highFreqSampleInterval},{serialNo} 去除高频数据后列表: {JsonConvert.SerializeObject(listRes)}");
  932. _logger.LogInformation($"{serialNo} 去除高频数据后的数据集: " + string.Join(", ", res));
  933. #region 计算众数
  934. var mode = res.GroupBy(n => n)
  935. .OrderByDescending(g => g.Count())
  936. .First()
  937. .Key;
  938. Console.WriteLine("众数是: " + mode);
  939. // 如果有多个众数的情况
  940. var maxCount = res.GroupBy(n => n)
  941. .Max(g => g.Count());
  942. var modes = res.GroupBy(n => n)
  943. .Where(g => g.Count() == maxCount)
  944. .Select(g => g.Key)
  945. .ToList();
  946. // 多个众数,选择最接近平均数或中位数的众数
  947. if (modes.Count > 1)
  948. {
  949. // 计算平均值
  950. double average = res.Average();
  951. Console.WriteLine("平均值是: " + average);
  952. // 计算中位数
  953. double median;
  954. int count = res.Count;
  955. var sortedRes = res.OrderBy(n => n).ToList();
  956. if (count % 2 == 0)
  957. {
  958. // 偶数个元素,取中间两个数的平均值
  959. median = (sortedRes[count / 2 - 1] + sortedRes[count / 2]) / 2.0;
  960. }
  961. else
  962. {
  963. // 奇数个元素,取中间的数
  964. median = sortedRes[count / 2];
  965. }
  966. //Console.WriteLine("中位数是: " + median);
  967. _logger.LogInformation($"{serialNo} 中位数是: " + median);
  968. // 找出最接近平均值的众数
  969. //var closestToAverage = modes.OrderBy(m => Math.Abs(m - average)).First();
  970. //Console.WriteLine("最接近平均值的众数是: " + closestToAverage);
  971. // 找出最接近中位数的众数
  972. var closestToMedian = modes.OrderBy(m => Math.Abs(m - median)).First();
  973. _logger.LogInformation($"{serialNo} 最接近中位数的众数是: " + closestToMedian);
  974. mode = closestToMedian;
  975. }
  976. #endregion
  977. // 计算需要的数量
  978. int requiredCount = (int)(res.Count * 0.9);
  979. // 从原始数据集中获取最接近众数的元素
  980. var closestToModeData = res.OrderBy(n => Math.Abs(n - mode))
  981. .Take(requiredCount)
  982. .ToList();
  983. // 输出新数据集
  984. _logger.LogInformation($"{serialNo} 新数据集: " + string.Join(", ", closestToModeData));
  985. _logger.LogInformation($"{serialNo} 新数据集的数量: {closestToModeData.Count},最大值: {closestToModeData.Max()},最小值 {closestToModeData.Min()},原始最大值: {res.Max()},原始最小值 {res.Min()}" );
  986. var fhrMap = _mgrFhrPhrMapCache.GetHeartRatesMap();
  987. var watchConfig = await _deviceCacheMgr.GetGpsDeviceWatchConfigCacheObjectBySerialNoAsync(serialNo, "0067");
  988. if (watchConfig == null)
  989. {
  990. return null;
  991. }
  992. // long.TryParse(watchConfig["EDOC"]!.ToString(), out long edoc);
  993. // "EDOC": "1720860180652",当前时间 - (EDOC - 280) days =怀孕时间
  994. //edoc = edoc.ToString().Length == 10 ? edoc * 1000 : edoc;
  995. var edoc = DateTimeUtil.ToDateTime(watchConfig["EDOC"]!.ToString());
  996. int pregnancyWeek = (DateTime.Now - edoc.AddDays(-280)).Days / 7;
  997. _logger.LogInformation($"IMEI {serialNo},EDOC:{edoc},NOW:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")},SinceNOW:{edoc.AddDays(-280).ToString("yyyy-MM-dd HH:mm:ss")},怀孕周数 {pregnancyWeek}");
  998. float statMaxValueFprCoefficient = 0f;
  999. float statMinValueFprCoefficient = 0f;
  1000. float StatModeAvgFprCoefficient = 0f;
  1001. // 如果最大值与最小值在60~100范围内,都按个固定值下发高频阈值。
  1002. var maxValue = closestToModeData.Max();
  1003. var minValue = closestToModeData.Min();
  1004. //if ((maxValue >= 60 && maxValue <= 100) && (minValue >= 60 && minValue <= 100))
  1005. //{
  1006. // minValue = 60;
  1007. // maxValue = 100;
  1008. //}
  1009. // 最大值最小值两边扩散
  1010. // 最小值不能大于60
  1011. minValue = minValue > 60 ? 60 : minValue;
  1012. // 最大值不能少于100
  1013. maxValue= maxValue < 100 ? 100 : maxValue;
  1014. // 20-45周之间
  1015. if (pregnancyWeek >= 12 && pregnancyWeek <= 45)
  1016. {
  1017. var map = fhrMap
  1018. .Where(i =>
  1019. i.PregnancyPeriod![0] <= pregnancyWeek &&
  1020. i.PregnancyPeriod[1] >= pregnancyWeek &&
  1021. i.PregnancyHeartRateRange![0] <= mode &&
  1022. i.PregnancyHeartRateRange[1] >= mode)
  1023. .FirstOrDefault();
  1024. if (map != null)
  1025. {
  1026. //statMaxValueFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateRange![1] / closestToModeData.Max(), 3);
  1027. //statMinValueFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateRange[0] / closestToModeData.Min(), 3);
  1028. statMaxValueFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateRange![1] / maxValue, 3);
  1029. statMinValueFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateRange[0] / minValue, 3);
  1030. StatModeAvgFprCoefficient = (float)Math.Round((decimal)map.FetalHeartRateAverage / mode, 3);
  1031. }
  1032. }
  1033. return new PregnancyCommonHeartRateModel()
  1034. {
  1035. Timestamp = DateTime.Now,
  1036. PersonId = collection.First().DeviceKey,
  1037. DeviceKey = collection.First().DeviceKey,
  1038. SerialNumber = collection.First().SerialNumber,
  1039. Mode = mode,
  1040. Percentage = percentage,
  1041. MaxValue = maxValue,
  1042. MinValue = minValue,
  1043. OriginalMaxValue = res.Max(),
  1044. OriginalMinValue = res.Min(),
  1045. CreateTime = DateTime.Now,
  1046. StatStartTime = collection.OrderBy(i => i.Timestamp).Select(i => i.Timestamp).First(),
  1047. StatEndTime = collection.OrderBy(i => i.Timestamp).Select(i => i.Timestamp).Last(),
  1048. StatMaxValueFprCoefficient = statMaxValueFprCoefficient,
  1049. StatMinValueFprCoefficient = statMinValueFprCoefficient,
  1050. StatModeAvgFprCoefficient = StatModeAvgFprCoefficient,
  1051. Remark = string.Empty,
  1052. SerialTailNumber = serialNo.Substring(serialNo.Length - 2)
  1053. };
  1054. }
  1055. /// <summary>
  1056. /// 获取孕妇心率众数
  1057. /// </summary>
  1058. /// <param name="serialNo"></param>
  1059. /// <param name="days"></param>
  1060. /// <returns></returns>
  1061. //public async Task<int> GetPregnancyHeartRateModeAsync(string serialNo,int days=7)
  1062. //{
  1063. // var tableName = typeof(PregnancyHeartRateModel)
  1064. // .GetCustomAttribute<STableAttribute>()?
  1065. // .STableName;
  1066. // var daysAgo = DateTime.Now.AddDays(-days);
  1067. // var res = await _clientSqlSugar
  1068. // .Queryable<PregnancyHeartRateModel>()
  1069. // .AS(tableName)
  1070. // .Where(i=>i.SerialNumber.Equals(serialNo))
  1071. // .Where(i => i.Timestamp > daysAgo)
  1072. // //.OrderByDescending(i => i.PregnancyHeartRate)
  1073. // .Select(i =>i.PregnancyHeartRate)
  1074. // .ToListAsync();
  1075. // // 心率数据量必须30个以上才进行计算
  1076. // if (res.Count < 30) return 0;
  1077. // // 计算众数
  1078. // var mode = res.GroupBy(n => n)
  1079. // .OrderByDescending(g => g.Count())
  1080. // .First()
  1081. // .Key;
  1082. // Console.WriteLine("众数是: " + mode);
  1083. // // 如果有多个众数的情况
  1084. // var maxCount = res.GroupBy(n => n)
  1085. // .Max(g => g.Count());
  1086. // var modes = res.GroupBy(n => n)
  1087. // .Where(g => g.Count() == maxCount)
  1088. // .Select(g => g.Key)
  1089. // .ToList();
  1090. // // 多个众数,选择最接近平均数或中位数的众数
  1091. // if (modes.Count>1)
  1092. // {
  1093. // // 计算平均值
  1094. // double average = res.Average();
  1095. // Console.WriteLine("平均值是: " + average);
  1096. // // 计算中位数
  1097. // double median;
  1098. // int count = res.Count;
  1099. // var sortedRes = res.OrderBy(n => n).ToList();
  1100. // if (count % 2 == 0)
  1101. // {
  1102. // // 偶数个元素,取中间两个数的平均值
  1103. // median = (sortedRes[count / 2 - 1] + sortedRes[count / 2]) / 2.0;
  1104. // }
  1105. // else
  1106. // {
  1107. // // 奇数个元素,取中间的数
  1108. // median = sortedRes[count / 2];
  1109. // }
  1110. // Console.WriteLine("中位数是: " + median);
  1111. // // 找出最接近平均值的众数
  1112. // //var closestToAverage = modes.OrderBy(m => Math.Abs(m - average)).First();
  1113. // //Console.WriteLine("最接近平均值的众数是: " + closestToAverage);
  1114. // // 找出最接近中位数的众数
  1115. // var closestToMedian = modes.OrderBy(m => Math.Abs(m - median)).First();
  1116. // Console.WriteLine("最接近中位数的众数是: " + closestToMedian);
  1117. // mode = closestToMedian;
  1118. // }
  1119. // return mode;
  1120. //}
  1121. #endregion
  1122. }
  1123. }