您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

97 行
3.7KB

  1. using HealthMonitor.Common;
  2. using HealthMonitor.Model.Service.Mapper;
  3. using Microsoft.EntityFrameworkCore.Metadata.Internal;
  4. using Microsoft.Extensions.Logging;
  5. using Newtonsoft.Json;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using TelpoDataService.Util.Clients;
  12. using TelpoDataService.Util.Entities.GpsLocationHistory;
  13. using TelpoDataService.Util.Models;
  14. using TelpoDataService.Util.QueryObjects;
  15. namespace HealthMonitor.Service.Cache
  16. {
  17. public class FetalMovementCacheManager
  18. {
  19. private readonly ILogger<FetalMovementCacheManager> _logger;
  20. private readonly GpsLocationHistoryAccessorClient<HisGpsFetalMovement> _hisFetalMovementApiClient;
  21. private const string CACHE_KEY_FETALMOVEMENT = "FM_";
  22. public FetalMovementCacheManager(ILogger<FetalMovementCacheManager> logger, GpsLocationHistoryAccessorClient<HisGpsFetalMovement> hisFetalMovementApiClient)
  23. {
  24. _logger = logger;
  25. _hisFetalMovementApiClient = hisFetalMovementApiClient;
  26. }
  27. public async Task SetFetalMovementAsync(string serialNo, string sampleTime)
  28. {
  29. try
  30. {
  31. var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}";
  32. var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime);
  33. var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss");
  34. await RedisHelper.SetAsync(key, value, 2 * 3600).ConfigureAwait(false);
  35. }
  36. catch (Exception ex)
  37. {
  38. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  39. }
  40. }
  41. public async Task<bool> FetalMovementIsExistedAsync(string serialNo, string sampleTime)
  42. {
  43. try
  44. {
  45. var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}";
  46. var res = await RedisHelper.GetAsync(key).ConfigureAwait(false);
  47. if (string.IsNullOrEmpty(res))
  48. {
  49. GeneralParam param = new()
  50. {
  51. Filters = new List<QueryFilterCondition>
  52. {
  53. new ()
  54. {
  55. Key=nameof(HisGpsFetalHeartRate.Serialno),
  56. Value=serialNo,
  57. ValueType=QueryValueTypeEnum.String,
  58. Operator=QueryOperatorEnum.Equal
  59. },
  60. new ()
  61. {
  62. Key=nameof(HisGpsFetalHeartRate.SampleTime),
  63. Value=sampleTime,
  64. ValueType=QueryValueTypeEnum.String,
  65. Operator=QueryOperatorEnum.Equal
  66. },
  67. }
  68. };
  69. var isFetalMovementExist = await _hisFetalMovementApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") });
  70. if (isFetalMovementExist != null)
  71. {
  72. await SetFetalMovementAsync(serialNo,sampleTime);
  73. return true;
  74. }
  75. }
  76. else
  77. {
  78. return true;
  79. }
  80. }
  81. catch (Exception ex)
  82. {
  83. _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}");
  84. }
  85. return false;
  86. }
  87. }
  88. }