|  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.alibaba.fastjson.JSON; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.core.toolkit.ObjectUtils; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 
|---|
|  |  |  | import com.moral.api.entity.Device; | 
|---|
|  |  |  | import com.moral.api.entity.DeviceAdjustValue; | 
|---|
|  |  |  | import com.moral.api.entity.Sensor; | 
|---|
|  |  |  | import com.moral.api.entity.SpecialDevice; | 
|---|
|  |  |  | import com.moral.api.mapper.DeviceAdjustValueMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.DeviceMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.SensorMapper; | 
|---|
|  |  |  | import com.moral.api.mapper.SpecialDeviceMapper; | 
|---|
|  |  |  | import com.moral.api.pojo.dto.adjust.AdjustDTO; | 
|---|
|  |  |  | import com.moral.api.pojo.form.adjust.AdjustForm; | 
|---|
|  |  |  | import com.moral.api.service.DeviceAdjustValueService; | 
|---|
|  |  |  | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; | 
|---|
|  |  |  | import com.moral.api.util.LogUtils; | 
|---|
|  |  |  | 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.jdbc.object.UpdatableSqlQuery; | 
|---|
|  |  |  | import org.springframework.stereotype.Service; | 
|---|
|  |  |  | import org.springframework.transaction.annotation.Transactional; | 
|---|
|  |  |  | import org.springframework.web.context.request.RequestContextHolder; | 
|---|
|  |  |  | 
|---|
|  |  |  | import javax.servlet.http.HttpServletRequest; | 
|---|
|  |  |  | import java.sql.Time; | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.HashMap; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  | import java.util.Map; | 
|---|
|  |  |  | 
|---|
|  |  |  | private DeviceMapper deviceMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired(required = false) | 
|---|
|  |  |  | private SpecialDeviceMapper specialDeviceMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired(required = false) | 
|---|
|  |  |  | private SensorMapper sensorMapper; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Autowired | 
|---|
|  |  |  | LogUtils logUtils; | 
|---|
|  |  |  | RedisTemplate redisTemplate; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | 
|---|
|  |  |  | QueryWrapper<Device> wrapper_device = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper_device.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | wrapper_device.eq("mac",deviceAdjustValue.getMac()); | 
|---|
|  |  |  | if (deviceMapper.selectCount(wrapper_device)==0){ | 
|---|
|  |  |  | QueryWrapper<SpecialDevice> wrapper_specialDevice = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper_specialDevice.eq("is_delete",Constants.NOT_DELETE); | 
|---|
|  |  |  | wrapper_specialDevice.eq("mac",deviceAdjustValue.getMac()); | 
|---|
|  |  |  | if (deviceMapper.selectCount(wrapper_device)==0 && specialDeviceMapper.selectCount(wrapper_specialDevice)==0){ | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.DEVICE_IS_NULL.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.DEVICE_IS_NULL.getMsg()); | 
|---|
|  |  |  | return resultMap; | 
|---|
|  |  |  | 
|---|
|  |  |  | //操作插入日志 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | String content = "添加了"+deviceAdjustValue.getMac()+"设备"+deviceAdjustValue.getSensorCode()+"传感器"+deviceAdjustValue.getStartTime()+"到"+deviceAdjustValue.getEndTime()+"的校准信息:"+";"; | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.INSERT_OPERATE_TYPE); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | content = content+"校准公式:"+deviceAdjustValueOld.getValue()+"->"+map.get(key)+";"; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | logUtils.saveOperationForManage(request,content,Constants.UPDATE_OPERATE_TYPE); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | 
|---|
|  |  |  | 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; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 批量修改校准值 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param parameters | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | public List<Map<String, Object>> getAllAdjust(Map<String, Object> parameters) { | 
|---|
|  |  |  | ArrayList<Map<String, Object>> rsList = new ArrayList<>(); | 
|---|
|  |  |  | String code = parameters.get("code").toString(); | 
|---|
|  |  |  | List<AdjustDTO> adjustDTOS = deviceAdjustValueMapper.selectAllAdjust(parameters); | 
|---|
|  |  |  | for (AdjustDTO adjustDTO : adjustDTOS) { | 
|---|
|  |  |  | HashMap<String, Object> map = new HashMap<>(); | 
|---|
|  |  |  | String value = adjustDTO.getValue(); | 
|---|
|  |  |  | if (ObjectUtils.isEmpty(value)){ | 
|---|
|  |  |  | if (code.equals("a99054")){ | 
|---|
|  |  |  | value ="cel*1"; | 
|---|
|  |  |  | map.put("cel",1); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | value ="aqi*0+cel*1"; | 
|---|
|  |  |  | map.put("aqi",0); | 
|---|
|  |  |  | map.put("cel",1); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | if (code.equals("a99054")&& !value.contains("+")){ | 
|---|
|  |  |  | String[] split = value.split("\\*"); | 
|---|
|  |  |  | map.put("cel",Double.parseDouble(split[1])); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | String[] split = value.split("\\+"); | 
|---|
|  |  |  | String[] split1 = split[0].toString().split("\\*"); | 
|---|
|  |  |  | map.put("aqi",Double.parseDouble(split1[1])); | 
|---|
|  |  |  | String[] split2 = split[1].toString().split("\\*"); | 
|---|
|  |  |  | map.put("cel",Double.parseDouble(split2[1])); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | map.put("value",value); | 
|---|
|  |  |  | map.put("name",adjustDTO.getName()); | 
|---|
|  |  |  | map.put("id",adjustDTO.getId()); | 
|---|
|  |  |  | map.put("mac",adjustDTO.getMac()); | 
|---|
|  |  |  | map.put("code",code); | 
|---|
|  |  |  | rsList.add(map); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return rsList; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 批量修改校准值 | 
|---|
|  |  |  | * | 
|---|
|  |  |  | * @param paras | 
|---|
|  |  |  | * @return | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Override | 
|---|
|  |  |  | @Transactional | 
|---|
|  |  |  | public Map<String, Object> updateAllAdjust(List<AdjustForm> paras) { | 
|---|
|  |  |  | Map<String,Object> resultMap = new HashMap<>(); | 
|---|
|  |  |  | StringBuffer buffer = new StringBuffer(); | 
|---|
|  |  |  | for (AdjustForm adjustForm : paras) { | 
|---|
|  |  |  | DeviceAdjustValue deviceAdjustValue = new DeviceAdjustValue(); | 
|---|
|  |  |  | QueryWrapper<DeviceAdjustValue> wrapper1 = new QueryWrapper<>(); | 
|---|
|  |  |  | wrapper1.eq("mac",adjustForm.getMac()); | 
|---|
|  |  |  | wrapper1.eq ("sensor_code",adjustForm.getCode()); | 
|---|
|  |  |  | wrapper1.eq("is_delete",0); | 
|---|
|  |  |  | Integer integer = deviceAdjustValueMapper.selectCount(wrapper1); | 
|---|
|  |  |  | if (integer==0){ | 
|---|
|  |  |  | deviceAdjustValue.setValue(adjustForm.getAgehou()); | 
|---|
|  |  |  | deviceAdjustValue.setCreateTime(new Date()); | 
|---|
|  |  |  | deviceAdjustValue.setIsDelete("0"); | 
|---|
|  |  |  | deviceAdjustValue.setUpdateTime(new Date()); | 
|---|
|  |  |  | deviceAdjustValue.setSensorCode(adjustForm.getCode()); | 
|---|
|  |  |  | deviceAdjustValue.setMac(adjustForm.getMac()); | 
|---|
|  |  |  | deviceAdjustValue.setEndTime(Time.valueOf("23:59:00")); | 
|---|
|  |  |  | deviceAdjustValue.setStartTime(Time.valueOf("00:00:00")); | 
|---|
|  |  |  | deviceAdjustValueMapper.insert(deviceAdjustValue); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | deviceAdjustValue.setValue(adjustForm.getAgehou()); | 
|---|
|  |  |  | deviceAdjustValue.setUpdateTime(new Date()); | 
|---|
|  |  |  | deviceAdjustValue.setId(adjustForm.getId()); | 
|---|
|  |  |  | deviceAdjustValueMapper.updateById(deviceAdjustValue); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | buffer.append("mac: "+adjustForm.getMac()).append(", value: "+adjustForm.getAgehou()+"; "); | 
|---|
|  |  |  | //刷新缓存 | 
|---|
|  |  |  | refreshCache(adjustForm.getMac()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | resultMap.put("code",ResponseCodeEnum.SUCCESS.getCode()); | 
|---|
|  |  |  | resultMap.put("msg",ResponseCodeEnum.SUCCESS.getMsg()); | 
|---|
|  |  |  | //插入日志 | 
|---|
|  |  |  | HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); | 
|---|
|  |  |  | LogUtils.saveOperationForManage(request,"批量修改效准值: "+buffer.toString(),Constants.UPDATE_OPERATE_TYPE); | 
|---|
|  |  |  | 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); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | } | 
|---|