kaiyu
2021-07-28 11d10fc9ca90ec29159738c1eac5e2d9203d8bb6
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.moral.api.util;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.*;
import com.moral.api.mapper.DeviceMapper;
import com.moral.api.mapper.SensorMapper;
import com.moral.api.mapper.UnitConversionMapper;
import com.moral.api.service.SensorService;
import com.moral.api.service.SysDictDataService;
import com.moral.api.service.UnitConversionService;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.TokenUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * @ClassName CacheUtils
 * @Description 缓存工具类
 * @Author 陈凯裕
 * @Date 2021/4/16 8:57
 * @Version TODO
 **/
@Component
@Slf4j
public class CacheUtils {
 
    private static RedisTemplate redisTemplate;
 
    private static DeviceMapper deviceMapper;
 
    private static SysDictDataService sysDictDataService;
 
    private static UnitConversionMapper unitConversionMapper;
 
    private static SensorService sensorService;
 
    private static UnitConversionService unitConversionService;
 
    @Autowired
    public void setUnitConversionService(UnitConversionService unitConversionService){CacheUtils.unitConversionService = unitConversionService;}
 
    @Autowired
    public void setSensorService(SensorService sensorService) {
        CacheUtils.sensorService = sensorService;
    }
 
    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        CacheUtils.redisTemplate = redisTemplate;
    }
 
    @Autowired
    public void setDeviceMapper(DeviceMapper deviceMapper) {
        CacheUtils.deviceMapper = deviceMapper;
    }
 
    @Autowired
    public void setSysDictDataService(SysDictDataService sysDictDataService) {
        CacheUtils.sysDictDataService = sysDictDataService;
    }
 
    @Autowired
    public void setUnitConversionMapper(UnitConversionMapper unitConversionMapper) {
        CacheUtils.unitConversionMapper = unitConversionMapper;
    }
 
 
    public static void refreshDeviceAlarmInfo() {
        //删除缓存
        redisTemplate.delete(RedisConstants.DEVICE_INFO);
        //重新添加缓存
        List<Device> devices = deviceMapper.queryDeviceUnitAlarmInfo();
        Map<String, SysDictData> unitMap = sysDictDataService.getDictDatasByType("unit");
        //查询对应的单位名称以及转换公式
        Map<String,Device> result = new HashMap<>();
        for (Device device : devices) {
            Version version = device.getVersion();
            List<Sensor> sensors = version.getSensors();
            for (Sensor sensor : sensors) {
                //封装单位名称
                String unitKey = sensor.getUnitKey();
                String showUnitKey = sensor.getShowUnitKey();
                SysDictData unitData = unitMap.get(unitKey);
                SysDictData showUnitData = unitMap.get(showUnitKey);
                sensor.setUnit(unitData.getDataValue());
                sensor.setShowUnit(showUnitData.getDataValue());
                //封装因子名称
                Map<String, Sensor> allSensors = sensorService.getAllSensorFromCache();
                String sensorName = allSensors.get(sensor.getCode()).getName();
                sensor.setName(sensorName);
                //读取转换公式
                if (!unitKey.equals(showUnitKey)) {
                    QueryWrapper<UnitConversion> queryWrapper = new QueryWrapper<>();
                    queryWrapper.eq("is_delete", Constants.NOT_DELETE);
                    queryWrapper.eq("original_unit_key", unitKey);
                    queryWrapper.eq("target_unit_key", showUnitKey);
                    List<UnitConversion> unitConversions = unitConversionMapper.selectList(queryWrapper);
                    if (unitConversions.size() == 1) {
                        UnitConversion unitConversion = unitConversions.get(0);
                        if (ObjectUtils.isEmpty(unitConversion.getSensorCode()))
                            sensor.setFormula(unitConversion.getFormula());
 
                    } else {
                        for (UnitConversion unitConversion : unitConversions) {
                            if (sensor.getCode().equals(unitConversion.getSensorCode()))
                                sensor.setFormula(unitConversion.getFormula());
                        }
                    }
                }
            }
            result.put(device.getMac(),device);
        }
        //存入redis
        redisTemplate.opsForHash().putAll(RedisConstants.DEVICE_INFO,result);
    }
 
    public static void refreshSensor(){
        sensorService.refreshCache();
    }
 
    public static void refreshDicTypeAndData(){
 
    }
 
    public static void refreshUnitConversion(){
        unitConversionService.refreshCache();
    }
}