2025-03-12 09:58:37 +08:00
|
|
|
|
using FreeRedis;
|
|
|
|
|
|
using FreeSql;
|
2025-04-14 16:41:41 +08:00
|
|
|
|
using JiShe.CollectBus.Common.Consts;
|
|
|
|
|
|
using JiShe.CollectBus.Common.Enums;
|
2025-03-12 09:58:37 +08:00
|
|
|
|
using JiShe.CollectBus.FreeRedisProvider;
|
|
|
|
|
|
using JiShe.CollectBus.FreeSql;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using JiShe.CollectBus.Localization;
|
2025-04-14 16:41:41 +08:00
|
|
|
|
using JiShe.CollectBus.Serializer;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2025-04-14 16:41:41 +08:00
|
|
|
|
using System;
|
2025-03-12 09:58:37 +08:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Threading.Tasks;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
using Volo.Abp.Application.Services;
|
|
|
|
|
|
|
|
|
|
|
|
namespace JiShe.CollectBus;
|
|
|
|
|
|
|
|
|
|
|
|
[ApiExplorerSettings(GroupName = CollectBusDomainSharedConsts.Business)]
|
|
|
|
|
|
public abstract class CollectBusAppService : ApplicationService
|
|
|
|
|
|
{
|
|
|
|
|
|
public IFreeSqlProvider SqlProvider => LazyServiceProvider.LazyGetRequiredService<IFreeSqlProvider>();
|
2025-03-17 08:35:19 +08:00
|
|
|
|
protected IFreeRedisProvider FreeRedisProvider => LazyServiceProvider.LazyGetService<IFreeRedisProvider>()!;
|
2024-12-19 16:07:07 +08:00
|
|
|
|
|
|
|
|
|
|
protected CollectBusAppService()
|
|
|
|
|
|
{
|
|
|
|
|
|
LocalizationResource = typeof(CollectBusResource);
|
|
|
|
|
|
ObjectMapperContext = typeof(CollectBusApplicationModule);
|
2025-04-14 16:41:41 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Lua脚本批量获取缓存的表计信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">表信息数据对象</typeparam>
|
|
|
|
|
|
/// <param name="redisKeys">采集频率对应的缓存Key集合</param>
|
|
|
|
|
|
/// <param name="systemType"><see cref="SystemTypeConst"/> 系统类型</param>
|
|
|
|
|
|
/// <param name="serverTagName">服务器标识</param>
|
|
|
|
|
|
/// <param name="timeDensity">采集频率,1分钟、5分钟、15分钟</param>
|
|
|
|
|
|
/// <param name="meterType"><see cref="MeterTypeEnum"/> 表计类型</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected async Task<Dictionary<string, Dictionary<string, T>>> GetMeterRedisCacheDictionaryData<T>(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<string, Dictionary<string, T>>(); ;
|
|
|
|
|
|
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<string, T>();
|
|
|
|
|
|
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<T>()!;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (meterInfo != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
meterHashs[meterld] = meterInfo;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"{nameof(GetMeterRedisCacheDictionaryData)} 获取缓存的表计信息集中器缓存{key}数据的{meterld}处理异常,-102");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
meterInfos[focusAddress] = meterHashs;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return meterInfos;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Lua脚本批量获取缓存的表计信息
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T">表信息数据对象</typeparam>
|
|
|
|
|
|
/// <param name="redisKeys">采集频率对应的缓存Key集合</param>
|
|
|
|
|
|
/// <param name="systemType"><see cref="SystemTypeConst"/> 系统类型</param>
|
|
|
|
|
|
/// <param name="serverTagName">服务器标识</param>
|
|
|
|
|
|
/// <param name="timeDensity">采集频率,1分钟、5分钟、15分钟</param>
|
|
|
|
|
|
/// <param name="meterType"><see cref="MeterTypeEnum"/> 表计类型</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
protected async Task<List<T>> GetMeterRedisCacheListData<T>(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<T>(); ;
|
|
|
|
|
|
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<T>()!;
|
|
|
|
|
|
}
|
|
|
|
|
|
if (meterInfo != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
meterInfos.Add(meterInfo);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new Exception($"{nameof(GetMeterRedisCacheListData)} 获取缓存的表计信息集中器缓存{key}数据的{meterld}处理异常,-103");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return meterInfos;
|
|
|
|
|
|
}
|
2024-12-19 16:07:07 +08:00
|
|
|
|
}
|