using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace IStation.RedisCache { /// /// 监测报警记录缓存辅助类 /// 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 报警记录 /// /// 设置最后一条记录 /// 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); } /// /// 设置最后一条记录 /// public void SetLastRecord(IEnumerable list) { if (list == null || list.Count() < 1) return; foreach (var item in list) { SetLastRecord(item); } } /// /// 获取最后一条记录列表 /// public List 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(); foreach (var redisKey in allKeys) { var vm_sub_list = _redisClient.HashGetJsonAll(redisKey); if (vm_sub_list != null&&vm_sub_list.Count>0) { vm_list.AddRange(vm_sub_list); } } return vm_list; } /// /// 获取最后一条记录 /// public Model.MonitorAlarmRecordPure GetLastRecord(long CorpID, long MonitorPointID, long ConfigureID) { var redisKey = CreateRedisKey(CorpID, MonitorPointID); var hashKey = CreateHashKey(ConfigureID); return _redisClient.HashGetJson(redisKey,hashKey); } /// /// 设置最后一条记录列表 /// public List GetLastRecordList(long CorpID, long MonitorPointID,long SignalID) { var redisKey = CreateRedisKey(CorpID, MonitorPointID, SignalID); return _redisClient.HashGetJsonAll(redisKey); } /// /// 获取最后一条记录 /// 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(redisKey, hashKey); } /// /// 删除最后一条记录 /// public void DeleteLastRecord(long CorpID, long MonitorPointID, long ConfigureID) { var redisKey = CreateRedisKey(CorpID,MonitorPointID); var hasKey = CreateHashKey(ConfigureID); _redisClient.HashDelete(redisKey, hasKey); } /// /// 删除最后一条记录 /// 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 } }