lixiaojun
2022-08-22 cd280a9457eda95433896e3e9e3aa5164bdd64a0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
    }
}