|  |  |  | 
|---|
|  |  |  | import com.moral.constant.Constants; | 
|---|
|  |  |  | import com.moral.constant.ResponseCodeEnum; | 
|---|
|  |  |  | import org.springframework.beans.factory.annotation.Autowired; | 
|---|
|  |  |  | import org.springframework.data.redis.core.RedisTemplate; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
|---|
|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | LogUtils logUtils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | String content = "添加了"+deviceAdjustValue.getMac()+"设备"+deviceAdjustValue.getSensorCode()+"传感器"+deviceAdjustValue.getStartTime()+"到"+deviceAdjustValue.getEndTime()+"的校准信息:"+";"; | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.INSERT_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(deviceAdjustValue.getMac()); | 
|---|
|  |  |  | resultMap.put("code", ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg", ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.UPDATE_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(deviceAdjustValueOld.getMac()); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | String content = "删除了"+deviceAdjustValue.getMac()+"设备"+deviceAdjustValue.getSensorCode()+"传感器"+deviceAdjustValue.getStartTime()+"到"+deviceAdjustValue.getEndTime()+"的校准信息:"+";"; | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.DELETE_OPERATE_TYPE); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(deviceAdjustValue.getMac()); | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | for (DeviceAdjustValue deviceAdjustValue:deviceAdjustValues) { | 
|---|
|  |  |  | Map timeSlotMap = new HashMap(); | 
|---|
|  |  |  | timeSlotMap.put("id",deviceAdjustValue.getId()); | 
|---|
|  |  |  | timeSlotMap.put("startTime",deviceAdjustValue.getStartTime()); | 
|---|
|  |  |  | timeSlotMap.put("endTime",deviceAdjustValue.getEndTime()); | 
|---|
|  |  |  | timeSlotList.add(timeSlotMap); | 
|---|
|  |  |  | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public Map<String, Object> refreshRedis() { | 
|---|
|  |  |  | Map<String,Object> resultMap = new HashMap<>(); | 
|---|
|  |  |  | QueryWrapper<DeviceAdjustValue> wrapper_mac = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper_mac.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | wrapper_mac.select("DISTINCT mac"); | 
|---|
|  |  |  | List<DeviceAdjustValue> deviceAdjustValues = deviceAdjustValueMapper.selectList(wrapper_mac); | 
|---|
|  |  |  | for (DeviceAdjustValue deviceAdjustValue:deviceAdjustValues) { | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(deviceAdjustValue.getMac()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public void refreshRedisAll(){ | 
|---|
|  |  |  | refreshRedis(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private Map<String,Object> getDeviceAdjustValueFromDB(String mac){ | 
|---|
|  |  |  | QueryWrapper<DeviceAdjustValue> wapper_redis = new QueryWrapper<>(); | 
|---|
|  |  |  | wapper_redis.eq("mac",mac); | 
|---|
|  |  |  | wapper_redis.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | List<DeviceAdjustValue> deviceAdjustValueList = deviceAdjustValueMapper.selectList(wapper_redis); | 
|---|
|  |  |  | if (deviceAdjustValueList.size()==0){ | 
|---|
|  |  |  | return null; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Map<String, Object> deviceAdjustValueMap = new HashMap<>(); | 
|---|
|  |  |  | for (DeviceAdjustValue deviceAdjustValue:deviceAdjustValueList) { | 
|---|
|  |  |  | String sensor_code = deviceAdjustValue.getSensorCode(); | 
|---|
|  |  |  | List<DeviceAdjustValue> timeSlotValueList = new ArrayList<>(); | 
|---|
|  |  |  | for (DeviceAdjustValue deviceAdjustValueIn:deviceAdjustValueList) { | 
|---|
|  |  |  | if (deviceAdjustValueIn.getSensorCode().equals(sensor_code)){ | 
|---|
|  |  |  | Map<String, Object> timeSlot_value = new HashMap<>(); | 
|---|
|  |  |  | timeSlot_value.put("start_time",deviceAdjustValue.getStartTime()); | 
|---|
|  |  |  | timeSlot_value.put("end_time",deviceAdjustValue.getEndTime()); | 
|---|
|  |  |  | timeSlot_value.put("value",deviceAdjustValue.getValue()); | 
|---|
|  |  |  | timeSlotValueList.add(deviceAdjustValueIn); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | deviceAdjustValueMap.put(sensor_code,timeSlotValueList); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return deviceAdjustValueMap; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void refreshCache(String mac){ | 
|---|
|  |  |  | Map<String, Object> deviceAdjustValueMap = getDeviceAdjustValueFromDB(mac); | 
|---|
|  |  |  | refreshCache(deviceAdjustValueMap,mac); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | private void refreshCache(Map<String,Object> deviceAdjustValueMap,String mac){ | 
|---|
|  |  |  | //删除缓存 | 
|---|
|  |  |  | redisTemplate.delete("adjust_"+mac); | 
|---|
|  |  |  | //添加缓存 | 
|---|
|  |  |  | if (!ObjectUtils.isEmpty(deviceAdjustValueMap)){ | 
|---|
|  |  |  | redisTemplate.opsForHash().putAll("adjust_"+mac,deviceAdjustValueMap); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|