jiazx0107@163.com
2023-05-17 620eab6cca2bc9ef9ea6d3067a0a5ba1deadbd1c
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package com.ld.igds.common.impl;
 
import com.ld.igds.common.CoreSerService;
import com.ld.igds.common.mapper.DeviceSerMapper;
import com.ld.igds.constant.Constant;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.DeviceSer;
import com.ld.igds.util.ContextUtil;
import com.ld.igds.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
 
@Slf4j
@Component(CoreSerService.BEAN_ID)
public class CoreSerServiceImpl implements CoreSerService {
 
    @Autowired
    private DeviceSerMapper deviceSerMapper;
    @Autowired
    private RedisUtil redisUtil;
 
    @Override
    public void updateStatusByIp(String status, String ip, Integer port) {
        String companyId = ContextUtil.getDefaultCompanyId();
 
        deviceSerMapper.updateStatusByIp(companyId, status, ip, port);
        this.flushCacheByIp(companyId, status, ip);
    }
 
    @Override
    public void updateByData(DeviceSer data) {
        deviceSerMapper.updateByData(data);
        this.flushCacheByData(data);
    }
 
    @Override
    public void updateControlModel(String companyId, String serId,
                                   String controlModel) {
 
        deviceSerMapper.updateControlModel(companyId, serId, controlModel);
 
        updateControlModelCache(companyId, serId, controlModel);
    }
 
    @Override
    public void updateSerById(DeviceSer ser) {
 
        // 更新数据库
        deviceSerMapper.updateSerById(ser);
 
        // 更新缓存
        this.updateCacheSer(ser);
    }
 
    private void updateCacheSer(DeviceSer newSer) {
        String key = RedisConst.buildKey(newSer.getCompanyId(), RedisConst.KEY_DEVICE_SER_LIST, newSer.getId());
        redisUtil.set(key, newSer);
    }
 
    private void updateControlModelCache(String companyId, String serId,
                                         String controlModel) {
 
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST, serId);
 
 
        DeviceSer ser = (DeviceSer) redisUtil.get(key);
 
        if (null == ser) {
            log.error("----------------------");
            return;
        }
 
        ser.setStatus(Constant.YN_Y);
        ser.setControlModel(controlModel);
        ser.setUpdateTime(new Date());
 
        redisUtil.set(key, ser);
    }
 
    private void flushCacheByIp(String companyId, String status, String ip) {
        if (null == companyId) return;
 
        DeviceSer ser = this.getCacheSerByIp(companyId, ip);
 
        if (null == ser) return;
 
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST, ser.getId());
        ser.setStatus(status);
        ser.setIp(ip);
        ser.setUpdateTime(new Date());
        redisUtil.set(key, ser);
 
    }
 
    private void flushCacheByData(DeviceSer data) {
 
        if (null == data) return;
 
        DeviceSer ser = this.getCacheSer(data.getCompanyId(), data.getId());
 
        if (null == ser) return;
 
        ser.setStatus(Constant.YN_Y);
        ser.setIp(data.getIp());
        ser.setPort(data.getPort());
        ser.setUpdateTime(new Date());
        ser.setSn(data.getSn());
 
        String key = RedisConst.buildKey(data.getCompanyId(), RedisConst.KEY_DEVICE_SER_LIST, data.getId());
 
        redisUtil.set(key, ser);
    }
 
    @Override
    public void setCacheSer(List<DeviceSer> data, String companyId) {
        String key;
        for (DeviceSer ser : data) {
            ContextUtil.addSerCompany(ser.getSn(), ser.getCompanyId());
 
            key = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST, ser.getId());
            redisUtil.set(key, ser);
        }
    }
 
 
    @Override
    public List<DeviceSer> getCacheSerList(String companyId) {
 
        String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST);
 
        Set<String> keys = redisUtil.keys(pattern);
 
        List<DeviceSer> result = new ArrayList<>();
        if (null == keys) return result;
 
        for (String key : keys) {
            result.add((DeviceSer) redisUtil.get(key));
        }
        return result;
    }
 
    @Override
    public DeviceSer getCacheSer(String companyId, String serId) {
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST, serId);
 
        DeviceSer ser = (DeviceSer) redisUtil.get(key);
 
        if (null == ser) {
            log.error("---------缓存中没有获取到分机缓存----------");
            return null;
        }
 
        return ser;
    }
 
    @Override
    public DeviceSer getCacheSerByIp(String companyId, String ip) {
 
        if (null == companyId) return null;
 
        String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST);
 
        Set<String> keys = redisUtil.keys(pattern);
        if (null == keys) return null;
 
        DeviceSer ser;
        for (String key : keys) {
            ser = (DeviceSer) redisUtil.get(key);
            if (null == ser) continue;
 
            if (ser.getIp().equals(ip)) return ser;
        }
 
        return null;
    }
 
    @Override
    public DeviceSer getCacheSerByIp(String companyId, String ip, Integer port) {
        if (null == companyId) return null;
 
        String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST);
        Set<String> keys = redisUtil.keys(pattern);
        if (null == keys) return null;
 
        DeviceSer result = null;
        for (String key : keys) {
            result = (DeviceSer) redisUtil.get(key);
            if (result.getIp().equals(ip) && result.getPort() == port) return result;
        }
        return result;
    }
 
    @Override
    public DeviceSer getCacheSerBySn(String companyId, String sn) {
 
        if (null == companyId) return null;
 
        String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST);
 
        Set<String> keys = redisUtil.keys(pattern);
        if (null == keys) return null;
 
        DeviceSer ser;
        for (String key : keys) {
            ser = (DeviceSer) redisUtil.get(key);
            if (null == ser) continue;
            if (ser.getSn().equals(sn)) return ser;
        }
 
        return null;
    }
 
    @Override
    public void refreshCache(String companyId) {
        List<DeviceSer> list = deviceSerMapper.queryAll(companyId);
        this.setCacheSer(list, companyId);
    }
 
    @Override
    public void delCache(String companyId, String serId) {
        String key = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_SER_LIST, serId);
        redisUtil.del(key);
    }
 
    @Override
    public void allOffLine(String companyId) {
        if (StringUtils.isEmpty(companyId)) companyId = ContextUtil.getDefaultCompanyId();
        deviceSerMapper.updateStatusAll(companyId, Constant.YN_N);
    }
 
    @Override
    public void onlineBySn(String ip, Integer port, String sn, String status) {
        deviceSerMapper.updateBySn(ip, port, status, sn);
    }
 
}