|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337 |
- using GpsCardGatewayPosition.Model.Config;
- using GpsCardGatewayPosition.Model.Enum;
- using GpsCardGatewayPosition.Service.Cache;
- using GpsCardGatewayPosition.Service.MqProducer;
- using GpsCardGatewayPosition.Service.Resolver.Property.Dto;
- using Microsoft.Extensions.Logging;
- using Microsoft.Extensions.Options;
- 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;
-
- namespace GpsCardGatewayPosition.Service.Biz.Health
- {
- public class HealthLogic
- {
- public enum TemperatureStatusType : int
- {
- /// <summary>
- /// 不存在
- /// </summary>
- NotExisted = 0,
- /// <summary>
- /// 不完整,已填写内容
- /// </summary>
- FilledContent = 1,
- /// <summary>
- /// 不完整,已填写地址
- /// </summary>
- FilledAddress = 2,
- /// <summary>
- /// 信息已完整
- /// </summary>
- Complete = 3,
- /// <summary>
- ///
- /// </summary>
- Reserved
- }
-
- private const string KEY_CACHE_TEMPERATURE_STATUS = "Temperature_Status";
- private const int KEY_CACHE_SECONDS = 600;
-
- private readonly ServiceConfig _configService;
- private readonly MqProcessLogic _serviceMqProcess;
- private readonly PersonCacheManager _personCacheMgr;
- private readonly DeviceCacheManager _deviceCacheMgr;
- //private readonly StepCacheManager _stepCacheMgr;
- private readonly GpsLocationHistoryAccessorClient<HisGpsTemperature> _hisTemperatureApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsStep> _hisStepApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsHeartRate> _hisHeartApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsSpo2> _hisSpo2ApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsBloodPress> _hisBloodPressApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsDrownReport> _hisDrownReportApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsWearStatus> _hisWearStatusApiClient;
- private readonly GpsLocationHistoryAccessorClient<HisGpsSportResult> _hisSportResultApiClient;
- private readonly ILogger<HealthLogic> _logger;
-
-
-
- public HealthLogic(IOptions<ServiceConfig> optConfigService, MqProcessLogic serviceMqProcess,
- // StepCacheManager stepCacheMgr,
- PersonCacheManager personCacheMgr,
- DeviceCacheManager deviceCacheMgr,
- GpsLocationHistoryAccessorClient<HisGpsTemperature> temperatureApiClient,
- GpsLocationHistoryAccessorClient<HisGpsStep> stepApiClient,
- GpsLocationHistoryAccessorClient<HisGpsHeartRate> hisHeartApiClient,
- GpsLocationHistoryAccessorClient<HisGpsSpo2> hisSpo2ApiClient,
- GpsLocationHistoryAccessorClient<HisGpsBloodPress> hisBloodPressApiClient,
- GpsLocationHistoryAccessorClient<HisGpsDrownReport> hisDrownReportApiClient,
- GpsLocationHistoryAccessorClient<HisGpsWearStatus> hisWearStatusApiClient,
- GpsLocationHistoryAccessorClient<HisGpsSportResult> hisSportResultApiClient,
- ILogger<HealthLogic> logger)
- {
- _configService = optConfigService.Value;
- _serviceMqProcess = serviceMqProcess;
-
- _deviceCacheMgr = deviceCacheMgr;
- _personCacheMgr = personCacheMgr;
- //_stepCacheMgr = stepCacheMgr;
- _hisTemperatureApiClient = temperatureApiClient;
- _hisStepApiClient = stepApiClient;
- _logger = logger;
- _hisHeartApiClient = hisHeartApiClient;
- _hisSpo2ApiClient = hisSpo2ApiClient;
- _hisDrownReportApiClient = hisDrownReportApiClient;
- _hisWearStatusApiClient = hisWearStatusApiClient;
- _hisBloodPressApiClient = hisBloodPressApiClient;
- _hisSportResultApiClient = hisSportResultApiClient;
- }
-
- #region 体温相关
- /// <summary>
- /// 处理测温事件消息(内容部分)
- /// </summary>
- /// <param name="messageId"></param>
- /// <param name="tempId">和serialno组合的tempId</param>
- /// <param name="factoryToAdd">返回测温对象用于插入数据库</param>
- /// <param name="factoryToUpdate">返回测温对象用于更新数据库</param>
- /// <param name="handleFollowup">后续处理</param>
- /// <param name="type">定位类型</param> // 2022/11/10 增加 type 参数判断定位类型
- /// <returns></returns>
- public async Task<bool> HandleTemperatureContent(string messageId, string tempId,
- Func<HisGpsTemperature> factoryToAdd, Func<HisGpsTemperature, HisGpsTemperature> factoryToUpdate,
- Action handleFollowup = null, LocationType type = default)
- {
- if (factoryToAdd == null || factoryToUpdate == null) throw new ArgumentNullException();
-
- HisGpsTemperature temperature = null;
-
- var status = await GetTemperatureStatusAsync(tempId).ConfigureAwait(false);
- if (status == TemperatureStatusType.Complete || status == TemperatureStatusType.FilledContent)
- {
- _logger.LogWarning($"TEMPERATURE事件[{tempId}]已处理(内容部分)|事件");
- return false;
- }
-
- if (status == TemperatureStatusType.NotExisted)
- {
- temperature = factoryToAdd.Invoke();
- await AddTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.FilledContent);
- // 对应无地址也要推送测温情况,创建一个线程,并让该线程休眠 60 秒
- Thread thread = new Thread(async () =>
- {
- _logger.LogInformation($"messageId:[{messageId}],TEMPERATURE事件[{tempId}] 无地址测温推送延时60s,判断入库后gps_temperature表中address是否为空,若为空也推送");
- Task.Delay(TimeSpan.FromSeconds(60)).Wait();
- var temperatureLate = await GetTemperatureAsync(messageId, tempId).ConfigureAwait(false);
- if (string.IsNullOrEmpty(temperatureLate.Address))
- {
- PushWxTemperature(messageId, temperatureLate);
- _logger.LogInformation($"messageId:[{messageId}],TEMPERATURE事件[{tempId}]延时60s,无地址已推送");
- }
- _logger.LogInformation($"messageId:[{messageId}],TEMPERATURE事件[{tempId}]延时60s,在HandleTemperatureAddressAsync地址已推送");
- });
- thread.Start();
-
- }
- else
- {
- temperature = await GetTemperatureAsync(messageId, tempId).ConfigureAwait(false);
- if (temperature != null)
- {
- temperature = factoryToUpdate.Invoke(temperature);
- await UpdateTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.Complete);
-
- //推送微信公众号
- await PushWxTemperature(messageId, temperature, type);
- }
- }
-
- handleFollowup?.Invoke();
- return true;
- }
-
-
- /// <summary>
- /// 处理测温事件消息(地址信息部分)
- /// </summary>
- /// <param name="messageId"></param>
- /// <param name="tempId">和serialno组合的tempId</param>
- /// <param name="factoryToAdd">返回测温对象用于插入数据库</param>
- /// <param name="factoryToUpdate">返回测温对象用于更新数据库</param>
- /// <param name="handleFollowup">后续处理</param>
- /// <param name="type">定位类型</param> // 2022/11/10 增加 type 参数判断定位类型
- /// <returns></returns>
- public async Task<bool> HandleTemperatureAddressAsync(string messageId, string tempId,
- Func<HisGpsTemperature> factoryToAdd, Func<HisGpsTemperature, HisGpsTemperature> factoryToUpdate,
- Action handleFollowup = null, LocationType type = default)
- {
- if (factoryToAdd == null || factoryToUpdate == null) throw new ArgumentNullException();
- HisGpsTemperature temperature = null;
-
- var status = await GetTemperatureStatusAsync(tempId).ConfigureAwait(false);
- if (status == TemperatureStatusType.Complete || status == TemperatureStatusType.FilledAddress)
- {
- _logger.LogWarning($"TEMPERATURE事件[{tempId}]已处理(地址信息内容部分)");
- return false;
- }
-
- if (status == TemperatureStatusType.NotExisted)
- {
- temperature = factoryToAdd.Invoke();
- await AddTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.FilledAddress);
- // 2023/02/02 新增测温数据 没有匹配定位信息,也推送给第三方平台。
- // if (temperature.Temperature != null)
- // {
- // PushWxTemperature(messageId, temperature);
- // }
- }
- else
- {
- temperature = await GetTemperatureAsync(messageId, tempId).ConfigureAwait(false);
- if (temperature != null)
- {
- temperature = factoryToUpdate.Invoke(temperature);
- await UpdateTemperatureAsync(messageId, temperature).ConfigureAwait(false);
-
- SetTemperatureStatus(tempId, TemperatureStatusType.Complete);
-
- //推送微信公众号
- PushWxTemperature(messageId, temperature, type);
- }
- }
-
- handleFollowup?.Invoke();
- return true;
- }
- // 2022/11/10 增加 type 参数判断定位类型
- private async Task PushWxTemperature(string messageId, HisGpsTemperature temperature, LocationType type = default)
- {
- var device = await _deviceCacheMgr.GetDeviceBySerialNoAsync(messageId, temperature.Serialno);
- if (device == null) return;
-
- string deviceName = temperature.Serialno;
- var person = await _personCacheMgr.GetPersonBySerialNoAsync(messageId, temperature.Serialno).ConfigureAwait(false);
- if (person != null) deviceName = person.NickName;
- MethodType method = MethodType.Manual;
-
- //if (temperature.Method == 0) method = MethodType.Manual;
- if (temperature.Method == 1) method = MethodType.Period;
- await _serviceMqProcess.ProcessWxTemperatureAsync(messageId, new TemperatureInfoModel
- {
- DeviceId = device.DeviceId,
- DeviceName = deviceName,
- TempId = temperature.TempId,
- TempTime = temperature.LastUpdate,
- Imei = temperature.Serialno,
- Temperature = temperature.Temperature,
- Province = temperature.Province,
- City = temperature.City,
- District = temperature.District,
- Address = temperature.Address
- }, type, method);
- }
-
- private async Task<TemperatureStatusType> GetTemperatureStatusAsync(string tempId)
- {
- var key = $"{KEY_CACHE_TEMPERATURE_STATUS}_{tempId}";
- var result = await RedisHelper.GetAsync<TemperatureStatusType?>(key);
- if (result == null) return TemperatureStatusType.NotExisted;
- if (result > TemperatureStatusType.Reserved || result < TemperatureStatusType.NotExisted) return TemperatureStatusType.NotExisted;
-
- return result.Value;
- }
-
- private void SetTemperatureStatus(string sosId, TemperatureStatusType status)
- {
- var key = $"{KEY_CACHE_TEMPERATURE_STATUS}_{sosId}";
- RedisHelper.SetAsync(key, (int)status, KEY_CACHE_SECONDS);
- }
-
- public async Task<HisGpsTemperature> GetTemperatureAsync(string messageId, string temperatureId)
- {
- try
- {
- //var param = new GeneralParam
- //{
- // Filters = new List<QueryFilterCondition>
- // {
- // new QueryFilterCondition
- // {
- // Key=nameof(HisGpsTemperature.TempId),
- // Value=tempId,
- // ValueType=QueryValueTypeEnum.String,
- // Operator=QueryOperatorEnum.Equal
- // },
- // new QueryFilterCondition
- // {
- // Key=nameof(HisGpsTemperature.Serialno),
- // Value=serialno,
- // ValueType=QueryValueTypeEnum.String,
- // Operator=QueryOperatorEnum.Equal
- // }
- // }
- //};
- //var temperature = await _hisTemperatureApiClient.GetFirstAsync(param, header: new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- //return temperature;
-
- var temperature = await _hisTemperatureApiClient.GetByIdAsync(temperatureId, header: new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- return temperature;
- }
- catch (Exception ex)
- {
- _logger.LogError($"根据{temperatureId}获取指定测温信息失败, {ex.Message}, {ex.StackTrace}");
- return null;
- }
- }
-
- public async Task<bool> AddTemperatureAsync(string messageId, HisGpsTemperature temperature)
- {
- try
- {
- // 体温gps_temperature,步数gps_step,血压gps_bloodpress,心率gps_heart_rate,血氧gps_spo2 五个表新增 person_id 字段
- var person = await _personCacheMgr.GetDeviceGpsPersonCacheBySerialNoAsync(messageId, temperature.Serialno).ConfigureAwait(false);
- var persnId = string.Empty;
- if (person != null && person.Person != null) persnId = person.Person.PersonId;
- temperature.PersonId = persnId;
-
- await _hisTemperatureApiClient.AddAsync(temperature, header: new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- return true;
- }
- catch (Exception ex)
- {
- _logger.LogError($"新增体温信息发生异常:{ex.Message}, {ex.StackTrace}");
- }
-
- return false;
- }
-
- public async Task<bool> UpdateTemperatureAsync(string messageId, HisGpsTemperature temperature)
- {
- try
- {
- await _hisTemperatureApiClient.UpdateAsync(temperature, header: new RequestHeader { RequestId = messageId }).ConfigureAwait(false);
- return true;
- }
- catch (Exception ex)
- {
- _logger.LogError($"更新体温信息发生异常:{ex.Message}, {ex.StackTrace}");
- }
-
- return false;
- }
-
- #endregion
-
- }
- }
|