using FreeRedis; using FreeSql; using JiShe.CollectBus.Common.Consts; using JiShe.CollectBus.Common.Enums; using JiShe.CollectBus.FreeRedisProvider; using JiShe.CollectBus.FreeSql; using JiShe.CollectBus.Localization; using JiShe.CollectBus.Serializer; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Threading.Tasks; using Volo.Abp.Application.Services; namespace JiShe.CollectBus; [ApiExplorerSettings(GroupName = CollectBusDomainSharedConsts.Business)] public abstract class CollectBusAppService : ApplicationService { public IFreeSqlProvider SqlProvider => LazyServiceProvider.LazyGetRequiredService(); protected IFreeRedisProvider FreeRedisProvider => LazyServiceProvider.LazyGetService()!; protected CollectBusAppService() { LocalizationResource = typeof(CollectBusResource); ObjectMapperContext = typeof(CollectBusApplicationModule); } /// /// Lua脚本批量获取缓存的表计信息 /// /// 表信息数据对象 /// 采集频率对应的缓存Key集合 /// 系统类型 /// 服务器标识 /// 采集频率,1分钟、5分钟、15分钟 /// 表计类型 /// protected async Task>> GetMeterRedisCacheDictionaryData(string[] redisKeys, string systemType, string serverTagName, string timeDensity, MeterTypeEnum meterType) where T : class { if (redisKeys == null || redisKeys.Length <=0 || string.IsNullOrWhiteSpace(systemType) || string.IsNullOrWhiteSpace(serverTagName) || string.IsNullOrWhiteSpace(timeDensity)) { throw new Exception($"{nameof(GetMeterRedisCacheDictionaryData)} 获取缓存的表计信息失败,参数异常,-101"); } //通过lua脚本一次性获取所有缓存内容 var luaScript = @" local results = {} for i, key in ipairs(KEYS) do local data = redis.call('HGETALL', key) results[i] = {key, data} end return results"; var merterResult = await FreeRedisProvider.Instance.EvalAsync(luaScript, redisKeys); //传递 KEYS if (merterResult == null) { throw new Exception($"{nameof(GetMeterRedisCacheDictionaryData)} 获取缓存的表计信息失败,没有获取到数据,-102"); } // 解析结果(结果为嵌套数组) var meterInfos = new Dictionary>(); ; if (merterResult is object[] arr) { foreach (object[] item in arr) { string key = (string)item[0];//集中器地址对应的Redis缓存Key object[] fieldsAndValues = (object[])item[1];//缓存Key对应的Hash表数据集合 var redisCacheKey = $"{string.Format(RedisConst.CacheMeterInfoKey, systemType, serverTagName, meterType, timeDensity)}"; string focusAddress = key.Replace(redisCacheKey, "");//集中器地址 var meterHashs = new Dictionary(); for (int i = 0; i < fieldsAndValues.Length; i += 2) { string meterld = (string)fieldsAndValues[i];//表ID string meterStr = (string)fieldsAndValues[i + 1];//表详情数据 T meterInfo = default!; if (!string.IsNullOrWhiteSpace(meterStr)) { meterInfo = meterStr.Deserialize()!; } if (meterInfo != null) { meterHashs[meterld] = meterInfo; } else { throw new Exception($"{nameof(GetMeterRedisCacheDictionaryData)} 获取缓存的表计信息集中器缓存{key}数据的{meterld}处理异常,-102"); } } meterInfos[focusAddress] = meterHashs; } } return meterInfos; } /// /// Lua脚本批量获取缓存的表计信息 /// /// 表信息数据对象 /// 采集频率对应的缓存Key集合 /// 系统类型 /// 服务器标识 /// 采集频率,1分钟、5分钟、15分钟 /// 表计类型 /// protected async Task> GetMeterRedisCacheListData(string[] redisKeys,string systemType,string serverTagName, string timeDensity, MeterTypeEnum meterType) where T : class { if (redisKeys == null || redisKeys.Length <= 0 || string.IsNullOrWhiteSpace(systemType) || string.IsNullOrWhiteSpace(serverTagName) || string.IsNullOrWhiteSpace(timeDensity)) { throw new Exception($"{nameof(GetMeterRedisCacheListData)} 获取缓存的表计信息失败,参数异常,-101"); } //通过lua脚本一次性获取所有缓存内容 var luaScript = @" local results = {} for i, key in ipairs(KEYS) do local data = redis.call('HGETALL', key) results[i] = {key, data} end return results"; var merterResult = await FreeRedisProvider.Instance.EvalAsync(luaScript, redisKeys); //传递 KEYS if (merterResult == null) { throw new Exception($"{nameof(GetMeterRedisCacheListData)} 获取缓存的表计信息失败,没有获取到数据,-102"); } // 解析结果(结果为嵌套数组) var meterInfos = new List(); ; if (merterResult is object[] arr) { foreach (object[] item in arr) { string key = (string)item[0];//集中器地址对应的Redis缓存Key object[] fieldsAndValues = (object[])item[1];//缓存Key对应的Hash表数据集合 var redisCacheKey = $"{string.Format(RedisConst.CacheMeterInfoKey, systemType, serverTagName, meterType, timeDensity)}"; string focusAddress = key.Replace(redisCacheKey, "");//集中器地址 for (int i = 0; i < fieldsAndValues.Length; i += 2) { string meterld = (string)fieldsAndValues[i];//表ID string meterStr = (string)fieldsAndValues[i + 1];//表详情数据 T meterInfo = default!; if (!string.IsNullOrWhiteSpace(meterStr)) { meterInfo = meterStr.Deserialize()!; } if (meterInfo != null) { meterInfos.Add(meterInfo); } else { throw new Exception($"{nameof(GetMeterRedisCacheListData)} 获取缓存的表计信息集中器缓存{key}数据的{meterld}处理异常,-103"); } } } } return meterInfos; } }