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
package com.ld.igds.common.impl;
 
import com.ld.igds.common.CoreDeviceIotService;
import com.ld.igds.common.dto.DepotSerData;
import com.ld.igds.common.mapper.DeviceIotMapper;
import com.ld.igds.constant.RedisConst;
import com.ld.igds.models.DeviceIot;
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.*;
import java.util.stream.Collectors;
 
@Slf4j
@Component
public class CoreDeviceIotServiceImpl implements CoreDeviceIotService {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private DeviceIotMapper deviceMapper;
 
 
    @Override
    public void setCacheAllDeviceIot(List<DeviceIot> listAll, String companyId) {
        // 首先按照分机分组,然后存更新缓存。
        Map<String, List<DeviceIot>> map = listAll.stream().collect(
                Collectors.groupingBy(DeviceIot::getSerId));
 
        if (null == map || map.isEmpty()) {
            log.error("字典信息:所有设备按照分机分组保存缓存失败,没有分组成功=={}", companyId);
            return;
        }
 
        for (String serId : map.keySet()) {
            updateCacheDeviceIotBySerId(map.get(serId), companyId, serId);
        }
 
        this.setCacheDepotDeviceIotSer(listAll, companyId);
    }
 
    @SuppressWarnings({ "unchecked", "rawtypes" })
    private void setCacheDepotDeviceIotSer(List<DeviceIot> listAll, String companyId) {
        // 处理仓库与分机的关系缓存
        Map<String, DepotSerData> tempMap = new HashMap<>();
        String key;
        for (DeviceIot item : listAll) {
            key = companyId + "-" + item.getDepotId() + "-" + item.getSerId();
            if (tempMap.get(key) == null) {
                tempMap.put(key, new DepotSerData(companyId, item.getDepotId(), item.getSerId()));
            }
        }
 
        redisUtil.set(RedisConst.buildKey(companyId, RedisConst.KEY_DEPOT_SER_LIST),
                new ArrayList(tempMap.values()));
    }
 
    @Override
    public void updateCacheDeviceIotBySerId(List<DeviceIot> listBySer,
                                         String companyId, String serId) {
 
        String key = RedisConst.buildDeviceKey(companyId,
                RedisConst.KEY_DEVICE_IOT_LIST, serId);
 
        log.debug("分机-设备-KEY={}", key);
 
        redisUtil.set(key, listBySer);
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public List<DeviceIot> getCacheDeviceIotBySerId(String companyId, String serId) {
        String key = RedisConst.buildDeviceKey(companyId,
                RedisConst.KEY_DEVICE_IOT_LIST, serId);
        List<DeviceIot> list = (List<DeviceIot>) redisUtil.get(key);
        if (null == list || list.isEmpty()) {
            log.error("字典信息:没有获取到缓存信息,KEY={}", key);
            return null;
        }
        return list;
    }
 
    @SuppressWarnings("unchecked")
    @Override
    public List<DeviceIot> getCacheDeviceIotByDepotId(String companyId, String depotId) {
 
        if(StringUtils.isEmpty(depotId)){
            log.error("仓库编码为空,获取Iot设备失败,depotId={}", depotId);
            return null;
        }
        if(StringUtils.isEmpty(companyId)){
            companyId = ContextUtil.getCompanyId();
        }
 
        String pattern = RedisConst.buildKey(companyId, RedisConst.KEY_DEVICE_IOT_LIST);
        Set<String> keys = redisUtil.keys(pattern);
        if (null == keys) {
            log.error("没有获取到Iot缓存key信息");
            return null;
        }
 
        List<DeviceIot> list = new ArrayList<>();
        List<DeviceIot> result;
 
        for (String key : keys) {
            result = (List<DeviceIot>) redisUtil.get(key);
            if(null == result || result.isEmpty()){
                continue;
            }
            for (DeviceIot iot : result) {
                if(depotId.equals(iot.getDepotId())){
                    list.add(iot);
                }
            }
        }
 
        return list;
    }
 
    @Override
    public void refreshCache(String companyId) {
        // 获取所有的设备信息
        List<DeviceIot> listAll = deviceMapper.queryAll(companyId);
        if (null != listAll) {
            this.setCacheAllDeviceIot(listAll, companyId);
        }
    }
 
 
}