using HealthMonitor.Common; using HealthMonitor.Model.Service.Mapper; using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.Extensions.Logging; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using TelpoDataService.Util.Clients; using TelpoDataService.Util.Entities.GpsLocationHistory; using TelpoDataService.Util.Models; using TelpoDataService.Util.QueryObjects; namespace HealthMonitor.Service.Cache { public class FetalMovementCacheManager { private readonly ILogger _logger; private readonly GpsLocationHistoryAccessorClient _hisFetalMovementApiClient; private const string CACHE_KEY_FETALMOVEMENT = "FM_"; public FetalMovementCacheManager(ILogger logger, GpsLocationHistoryAccessorClient hisFetalMovementApiClient) { _logger = logger; _hisFetalMovementApiClient = hisFetalMovementApiClient; } public async Task SetFetalMovementAsync(string serialNo, string sampleTime, HisGpsFetalMovement fm) { try { var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}"; var time = long.Parse(sampleTime.Length < 13 ? sampleTime.PadRight(13, '0') : sampleTime); var value = DateTimeUtil.GetDateTimeFromUnixTimeMilliseconds(time).ToString("yyyy-MM-dd HH:mm:ss"); fm.SampleTime = value; await RedisHelper.SetAsync(key, JsonConvert.SerializeObject(fm), 7200).ConfigureAwait(false); } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } } public async Task FetalMovementIsExistedAsync(string serialNo, string sampleTime) { try { var key = $"{CACHE_KEY_FETALMOVEMENT}_{serialNo}_{sampleTime}"; var res = await RedisHelper.GetAsync(key).ConfigureAwait(false); if (string.IsNullOrEmpty(res)) { GeneralParam param = new() { Filters = new List { new () { Key=nameof(HisGpsFetalMovement.Serialno), Value=serialNo, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal }, new () { Key=nameof(HisGpsFetalMovement.SampleTime), Value=sampleTime, ValueType=QueryValueTypeEnum.String, Operator=QueryOperatorEnum.Equal }, } }; var isFetalMovementExist = await _hisFetalMovementApiClient.GetFirstAsync(param, serialNo[^2..], null, new RequestHeader { RequestId = Guid.NewGuid().ToString("D") }); if (isFetalMovementExist != null) { await SetFetalMovementAsync(serialNo, sampleTime, isFetalMovementExist); return true; } } else { return true; } } catch (Exception ex) { _logger.LogWarning($"Redis 发生异常:{ex.Message}, {ex.StackTrace}"); } return false; } } }