|  |  |  | 
|---|
|  |  |  | import com.moral.api.entity.*; | 
|---|
|  |  |  | import com.moral.api.mapper.DeviceMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.SensorMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.SpecialDeviceMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.UnitConversionMapper; | 
|---|
|  |  |  | import com.moral.api.service.SensorService; | 
|---|
|  |  |  | import com.moral.api.service.SysDictDataService; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Component; | 
|---|
|  |  |  | import org.springframework.util.ObjectUtils; | 
|---|
|  |  |  | import org.springframework.util.StringUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | 
|---|
|  |  |  | private static RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static DeviceMapper deviceMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static SpecialDeviceMapper specialDeviceMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private static SysDictDataService sysDictDataService; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | 
|---|
|  |  |  | CacheUtils.unitConversionMapper = unitConversionMapper; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | public void setSpecialDeviceMapper(SpecialDeviceMapper specialDeviceMapper) { | 
|---|
|  |  |  | CacheUtils.specialDeviceMapper = specialDeviceMapper; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void refreshDeviceAlarmInfo() { | 
|---|
|  |  |  | //删除缓存 | 
|---|
|  |  |  | redisTemplate.delete(RedisConstants.DEVICE_INFO); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | refreshDeviceAlarmInfo(null); | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public static void refreshDeviceAlarmInfo(String mac){ | 
|---|
|  |  |  | if(StringUtils.isEmpty(mac)){ | 
|---|
|  |  |  | //删除缓存 | 
|---|
|  |  |  | redisTemplate.delete(RedisConstants.DEVICE_INFO); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | redisTemplate.opsForHash().delete(RedisConstants.DEVICE_INFO,mac); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //重新添加缓存 | 
|---|
|  |  |  | List<Device> devices = deviceMapper.queryDeviceUnitAlarmInfo(); | 
|---|
|  |  |  | List<Device> devices = deviceMapper.queryDeviceUnitAlarmInfo(mac); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | Map<String, SysDictData> unitMap = sysDictDataService.getDictDatasByType("unit"); | 
|---|
|  |  |  | //查询对应的单位名称以及转换公式 | 
|---|
|  |  |  | Map<String,Device> result = new HashMap<>(); | 
|---|
|  |  |  | int i = 0; | 
|---|
|  |  |  | for (Device device : devices) { | 
|---|
|  |  |  | System.out.println(">>>>>>>>>>>:"+(i++)); | 
|---|
|  |  |  | Version version = device.getVersion(); | 
|---|
|  |  |  | List<Sensor> sensors = version.getSensors(); | 
|---|
|  |  |  | for (Sensor sensor : sensors) { | 
|---|
|  |  |  | 
|---|
|  |  |  | redisTemplate.opsForHash().putAll(RedisConstants.DEVICE_INFO,result); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void refreshSpecialDeviceAlarmInfo() { | 
|---|
|  |  |  | //删除缓存 | 
|---|
|  |  |  | redisTemplate.delete(RedisConstants.SPECIAL_DEVICE_INFO); | 
|---|
|  |  |  | //重新添加缓存 | 
|---|
|  |  |  | List<SpecialDevice> specialDevices = specialDeviceMapper.querySpecialDeviceUnitAlarmInfo(); | 
|---|
|  |  |  | Map<String, SysDictData> unitMap = sysDictDataService.getDictDatasByType("unit"); | 
|---|
|  |  |  | //查询对应的单位名称以及转换公式 | 
|---|
|  |  |  | Map<String,SpecialDevice> result = new HashMap<>(); | 
|---|
|  |  |  | for (SpecialDevice specialDevice : specialDevices) { | 
|---|
|  |  |  | Version version = specialDevice.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(specialDevice.getMac(),specialDevice); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | //存入redis | 
|---|
|  |  |  | redisTemplate.opsForHash().putAll(RedisConstants.SPECIAL_DEVICE_INFO,result); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public static void refreshSensor(){ | 
|---|
|  |  |  | sensorService.refreshCache(); | 
|---|
|  |  |  | } | 
|---|