lixiaojun
2023-03-20 14801a2e40bc79833c41151a37fe4cb0acbc5c7f
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace IStation.RedisCache
{
    public class Smi_Expert_StoreVibrationRecord
    {
        private const string Flag = "smi-expert";//标志
 
        //最后一条记录
        private const string _lastRecord = "LastRecord";
 
 
        //Redis客户端辅助类对象
        private readonly RedisClientHelper _redisClient = new RedisClientHelper();
 
        //创建RedisKey
        private static string CreateRedisKey(string objectId)
        {
            return $"{RedisKeyHelper.CreateKey(Flag)}:{objectId}";
        }
 
 
        /// <summary>
        /// 设置最后一条记录
        /// </summary>
        public void SetLastRecord(Model.Smi_Expert_VibrationRecord model)
        {
            if (model == null)
                return;
            var redisKey = CreateRedisKey(model.ObjectId);
            _redisClient.HashSetJosn(redisKey, _lastRecord,model);
        }
 
        /// <summary>
        /// 设置最后一条记录(批量)
        /// </summary>
        public void SetLastRecord(IEnumerable<Model.Smi_Expert_VibrationRecord> list)
        {
            if (list == null || list.Count() < 1)
                return;
            foreach (var item in list)
            {
                SetLastRecord(item);
            }
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public Model.Smi_Expert_VibrationRecord GetLastRecord(string ObjectId)
        {
            var redisKey = CreateRedisKey(ObjectId);
            return _redisClient.HashGetJson<Model.Smi_Expert_VibrationRecord>(redisKey,_lastRecord);
        }
 
        /// <summary>
        /// 获取最后一条记录
        /// </summary>
        public List<Model.Smi_Expert_VibrationRecord> GetLastRecord(IEnumerable<string> ObjectIds)
        {
            if (ObjectIds == null || ObjectIds.Count() < 1)
                return default;
            return ObjectIds.Select(x => GetLastRecord(x)).Where(x => x != null).ToList();
        }
 
        /// <summary>
        /// 获取最后一条记录StartWith 
        /// </summary>
        public List<Model.Smi_Expert_VibrationRecord> GetLastRecordStartWith(string objectId)
        {
            var key = CreateRedisKey(objectId);
            var allKeys = _redisClient.AllKeyStartWidth(key);
            if (allKeys == null || allKeys.Count < 1)
                return default;
            var vm_list = new List<Model.Smi_Expert_VibrationRecord>();
            foreach (var redisKey in allKeys)
            {
                var vm = _redisClient.HashGetJson<Model.Smi_Expert_VibrationRecord>(redisKey, _lastRecord);
                if (vm != null)
                {
                    vm_list.Add(vm);
                }
            }
            return vm_list;
        }
      
 
       
 
 
 
    }
}