|  |  | 
 |  |  | package com.moral.api.service.impl; | 
 |  |  |  | 
 |  |  | 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 java.util.Date; | 
 |  |  | import java.util.HashMap; | 
 |  |  | import java.util.Map; | 
 |  |  |  | 
 |  |  | import com.alibaba.fastjson.JSON; | 
 |  |  | import com.alibaba.fastjson.JSONObject; | 
 |  |  | import com.moral.api.mapper.HistoryMinutelyMapper; | 
 |  |  | import com.moral.api.service.DeviceService; | 
 |  |  | import com.moral.api.service.HistoryMinutelyService; | 
 |  |  | import com.moral.constant.Constants; | 
 |  |  | import com.moral.constant.RedisConstants; | 
 |  |  | import com.moral.util.DateUtils; | 
 |  |  |  | 
 |  |  | @Service | 
 |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private DeviceService deviceService; | 
 |  |  |  | 
 |  |  |     @Autowired | 
 |  |  |     private RedisTemplate redisTemplate; | 
 |  |  |  | 
 |  |  |     @Override | 
 |  |  |     public void insertHistoryMinutely(Map<String, Object> deviceData) { | 
 |  |  |     @Transactional | 
 |  |  |     public void insertHistoryMinutely(Map<String, Object> data) { | 
 |  |  |         Map<String, Object> result = new HashMap<>(); | 
 |  |  |         Object mac = deviceData.remove("mac"); | 
 |  |  |         Map<String, Object> dataAdjust = new HashMap<>(data); | 
 |  |  |         String mac = data.remove("mac").toString(); | 
 |  |  |         //根据mac获取型号 | 
 |  |  |         Map<String, Object> deviceInfo = (Map<String, Object>) redisTemplate.opsForHash().get(RedisConstants.DEVICE, mac); | 
 |  |  |         Map<String, Object> versionInfo = (Map<String, Object>) deviceInfo.get("version"); | 
 |  |  |         Integer version = (Integer) versionInfo.get("id"); | 
 |  |  |  | 
 |  |  |         result.put("mac", mac); | 
 |  |  |         result.put("version", deviceData.remove("ver")); | 
 |  |  |         Date time = new Date((Long) deviceData.remove("DataTime")); | 
 |  |  |         result.put("time", DateUtils.dateToDateString(time)); | 
 |  |  |         result.put("value", JSON.toJSONString(deviceData)); | 
 |  |  |         String timeUnits = DateUtils.dateToDateString(time, DateUtils.yyyyMM_EN); | 
 |  |  |         result.put("timeUnits", tableSuffix(timeUnits, Constants.UN_ADJUST)); | 
 |  |  |         //未校准 | 
 |  |  |         result.put("version", version); | 
 |  |  |  | 
 |  |  |         Date time = DateUtils.getDate((String) data.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN); | 
 |  |  |         result.put("time", DateUtils.getDateOfMin(time, -1)); | 
 |  |  |         result.put("value", JSONObject.toJSONString(data)); | 
 |  |  |         String timeUnits = DateUtils.dateToDateString(DateUtils.getDateOfMin(time, -1), DateUtils.yyyyMM_EN); | 
 |  |  |         result.put("timeUnits", timeUnits + "_" + Constants.UN_ADJUST); | 
 |  |  |  | 
 |  |  |         //原始数据(未校准) | 
 |  |  |         historyMinutelyMapper.insertHistoryMinutely(result); | 
 |  |  |  | 
 |  |  |         //设备信息 | 
 |  |  |         Map<String, Object> deviceInfo = deviceService.getDeviceByMac(mac.toString()); | 
 |  |  |  | 
 |  |  |         //设备数据校准,并存入数据库 | 
 |  |  |         //数据校准 | 
 |  |  |         dataAdjust = deviceService.adjustDeviceData(dataAdjust); | 
 |  |  |         result.put("timeUnits", timeUnits); | 
 |  |  |         deviceData = deviceService.adjustDeviceData(deviceData, deviceInfo); | 
 |  |  |         result.put("value", JSON.toJSONString(deviceData)); | 
 |  |  |         result.put("value", JSONObject.toJSONString(dataAdjust)); | 
 |  |  |         historyMinutelyMapper.insertHistoryMinutely(result); | 
 |  |  |  | 
 |  |  |         //判断设备状态 | 
 |  |  |         Map<String, Object> deviceState = deviceService.judgeDeviceState(deviceData, deviceInfo); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     //表后缀 | 
 |  |  |     private String tableSuffix(String... keys) { | 
 |  |  |         StringBuilder key = new StringBuilder(keys[0]); | 
 |  |  |         for (int i = 1; i < keys.length; i++) { | 
 |  |  |             key.append("_"); | 
 |  |  |             key.append(keys[i]); | 
 |  |  |         } | 
 |  |  |         return key.toString(); | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |