tangxu
2024-01-09 ddc2780231ea76be74fadb7486401a3d0d17b101
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
namespace Yw.RedisCache
{
    /// <summary>
    /// 指标健康评价记录缓存辅助类
    /// </summary>
    public class HealthMultiItemEvaluationRecordCacheHelper
    {
        //健康结果记录
        private const string _flag = "health-multi-item-evaluation-record";
 
        //最后一条记录
        private const string _lastRecord = "last-record";
 
        //Redis客户端辅助类对象
        private readonly RedisClientHelper _redisClient = new();
 
        //获取 RedisKey
        private static string CreateRedisKey(long BindingID, long ItemID)
        {
            return $"{RedisKeyHelper.CreateKey(_flag)}:{BindingID}-{ItemID}";
        }
 
 
        #region 记录
 
        /// <summary>
        /// 设置最后一条记录
        /// </summary>
        public bool SetLastRecord(Model.HealthMultiItemEvaluationRecord model)
        {
            if (model == null)
                return false;
            string redisKey = CreateRedisKey(model.BindingID, model.ItemID);
            var hashKey = _lastRecord;
            _redisClient.HashSetJosn(redisKey, hashKey, model);
            return true;
        }
 
        /// <summary>
        /// 设置最后一条记录
        /// </summary>
        public bool SetLastRecord(IEnumerable<Model.HealthMultiItemEvaluationRecord> list)
        {
            if (list == null || !list.Any())
                return false;
            foreach (var item in list)
            {
                var bol = SetLastRecord(item);
                if (!bol)
                    return false;
            }
            return true;
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public Model.HealthMultiItemEvaluationRecord GetLastRecord(long BindingID, long ItemID)
        {
            var redisKey = CreateRedisKey(BindingID, ItemID);
            var hashKey = _lastRecord;
            return _redisClient.HashGetJson<Model.HealthMultiItemEvaluationRecord>(redisKey, hashKey);
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public List<Model.HealthMultiItemEvaluationRecord> GetLastRecord(long BindingID, IEnumerable<long> ItemIds)
        {
            if (ItemIds == null || !ItemIds.Any())
                return default;
            return ItemIds.ToList().Select(x => GetLastRecord(BindingID, x)).Where(x => x != null).ToList();
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public List<Model.HealthMultiItemEvaluationRecord> GetLastRecord(IEnumerable<KeyValuePair<long, long>> list)
        {
            if (list == null || !list.Any())
                return default;
            return list.ToList().Select(x => GetLastRecord(x.Key, x.Value)).Where(x => x != null).ToList();
        }
 
        /// <summary>
        /// 删除最后一条记录
        /// </summary>
        public void DeleteLastRecord(long BindingID, long ItemID)
        {
            var redisKey = CreateRedisKey(BindingID, ItemID);
            var hasKey = _lastRecord;
            _redisClient.HashDelete(redisKey, hasKey);
        }
 
        #endregion
    }
}