using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using System.Threading.Tasks;
|
|
namespace IStation.RedisCache
|
{
|
/// <summary>
|
/// 监测报警记录缓存辅助类
|
/// </summary>
|
public class MonitorAlarmRecordCacheHelper
|
{
|
//监测记录
|
private const string _flag = "monitor-alarm-record";
|
//最后一条报警记录
|
private const string _lastRecord = "last-record";
|
//Redis客户端辅助类对象
|
private readonly RedisClientHelper _redisClient = new RedisClientHelper();
|
|
//获取 RedisKey
|
private static string CreateRedisKey(long CorpID, long MonitorPointID)
|
{
|
return $"{RedisKeyHelper.CreateKey(_flag)}:{CorpID}-{MonitorPointID}-";
|
}
|
|
//获取 RedisKey
|
private static string CreateRedisKey(long CorpID, long MonitorPointID, long SignalID)
|
{
|
return $"{CreateRedisKey(CorpID,MonitorPointID)}{SignalID}";
|
}
|
|
//创建 报警HashKey
|
private static string CreateHashKey(long ConfigureID)
|
{
|
return $"{_lastRecord}_{ConfigureID}";
|
}
|
|
#region 报警记录
|
|
/// <summary>
|
/// 设置最后一条记录
|
/// </summary>
|
public void SetLastRecord(Model.MonitorAlarmRecordPure model)
|
{
|
if (model == null)
|
return;
|
string redisKey;
|
if (model.SignalID.HasValue)
|
{
|
redisKey= CreateRedisKey(model.CorpID, model.MonitorPointID,model.SignalID.Value);
|
}
|
else
|
{
|
redisKey = CreateRedisKey(model.CorpID, model.MonitorPointID);
|
}
|
var hashKey = CreateHashKey(model.ConfigureID);
|
_redisClient.HashSetJosn(redisKey, hashKey, model);
|
}
|
|
/// <summary>
|
/// 设置最后一条记录
|
/// </summary>
|
public void SetLastRecord(IEnumerable<Model.MonitorAlarmRecordPure> list)
|
{
|
if (list == null || list.Count() < 1)
|
return;
|
foreach (var item in list)
|
{
|
SetLastRecord(item);
|
}
|
}
|
|
/// <summary>
|
/// 获取最后一条记录列表
|
/// </summary>
|
public List<Model.MonitorAlarmRecordPure> GetLastRecordList(long CorpID, long MonitorPointID)
|
{
|
var key = CreateRedisKey(CorpID,MonitorPointID);
|
var allKeys = _redisClient.AllKeyStartWidth(key);
|
if (allKeys == null || allKeys.Count < 1)
|
return default;
|
var vm_list = new List<Model.MonitorAlarmRecordPure>();
|
foreach (var redisKey in allKeys)
|
{
|
var vm_sub_list = _redisClient.HashGetJsonAll<Model.MonitorAlarmRecordPure>(redisKey);
|
if (vm_sub_list != null&&vm_sub_list.Count>0)
|
{
|
vm_list.AddRange(vm_sub_list);
|
}
|
}
|
return vm_list;
|
}
|
|
/// <summary>
|
/// 获取最后一条记录
|
/// </summary>
|
public Model.MonitorAlarmRecordPure GetLastRecord(long CorpID, long MonitorPointID, long ConfigureID)
|
{
|
var redisKey = CreateRedisKey(CorpID, MonitorPointID);
|
var hashKey = CreateHashKey(ConfigureID);
|
return _redisClient.HashGetJson<Model.MonitorAlarmRecordPure>(redisKey,hashKey);
|
}
|
|
/// <summary>
|
/// 设置最后一条记录列表
|
/// </summary>
|
public List<Model.MonitorAlarmRecordPure> GetLastRecordList(long CorpID, long MonitorPointID,long SignalID)
|
{
|
var redisKey = CreateRedisKey(CorpID, MonitorPointID, SignalID);
|
return _redisClient.HashGetJsonAll<Model.MonitorAlarmRecordPure>(redisKey);
|
}
|
|
/// <summary>
|
/// 获取最后一条记录
|
/// </summary>
|
public Model.MonitorAlarmRecordPure GetLastRecord(long CorpID, long MonitorPointID,long SignalID, long ConfigureID)
|
{
|
var redisKey = CreateRedisKey(CorpID, MonitorPointID, SignalID);
|
var hashKey = CreateHashKey(ConfigureID);
|
return _redisClient.HashGetJson<Model.MonitorAlarmRecordPure>(redisKey, hashKey);
|
}
|
|
/// <summary>
|
/// 删除最后一条记录
|
/// </summary>
|
public void DeleteLastRecord(long CorpID, long MonitorPointID, long ConfigureID)
|
{
|
var redisKey = CreateRedisKey(CorpID,MonitorPointID);
|
var hasKey = CreateHashKey(ConfigureID);
|
_redisClient.HashDelete(redisKey, hasKey);
|
}
|
|
/// <summary>
|
/// 删除最后一条记录
|
/// </summary>
|
public void DeleteLastRecord(long CorpID, long MonitorPointID, long SignalID, long ConfigureID)
|
{
|
var redisKey = CreateRedisKey(CorpID, MonitorPointID,SignalID);
|
var hasKey = CreateHashKey(ConfigureID);
|
_redisClient.HashDelete(redisKey, hasKey);
|
}
|
|
|
|
#endregion
|
}
|
}
|