jinpengyong
2021-08-26 3be7bd55ff0a4ab2ed25b46cdfd1dede92300ea3
screen-manage/src/main/java/com/moral/api/service/impl/HistoryHourlyServiceImpl.java
@@ -7,9 +7,11 @@
import com.moral.api.service.HistoryHourlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.constant.RedisConstants;
import com.moral.util.DateUtils;
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;
@@ -34,40 +36,40 @@
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    @Transactional
    public void insertHistoryHourly(Map<String, Object> data) {
        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");
        Map<String, Object> dataAdjust = new HashMap<>(data);
        Date time = DateUtils.getDate((String) data.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN);
        Integer version = (Integer) data.remove("ver");
        Map<String, Object> result = new HashMap<>();
        result.put("mac", mac);
        result.put("time", time);
        result.put("version", version);
        result.put("timeUnits", Constants.UN_ADJUST);
        result.put("value", JSONObject.toJSONString(data));
        //原始数据(未校准)
        historyHourlyMapper.insertHistoryHourlyUnAdjust(result);
        //未校准小时表insert
        historyHourlyMapper.insertHistoryHourly(result);
        //数据校准
        dataAdjust = deviceService.adjustDeviceData(dataAdjust);
        dataAdjust.remove("mac");
        dataAdjust.remove("DataTime");
        dataAdjust.remove("ver");
        int count = historyHourlyMapper.getCountByMacAndTime(mac, DateUtils.dateToDateString(time));
        HistoryHourly historyHourly = new HistoryHourly();
        historyHourly.setMac(mac);
        historyHourly.setTime(time);
        historyHourly.setVersion(version);
        historyHourly.setValue(JSONObject.toJSONString(dataAdjust));
        //判断中间表有没有该mac,该小时的数据,有就更新,没有就新增
        if (count == 0) {
            //小时数据校准后存入小时中间表
            result.put("value", JSONObject.toJSONString(dataAdjust));
            //新增
            historyHourlyMapper.insertHistoryHourlyTransition(result);
        } else {
            //更新
            historyHourlyMapper.updateHistoryTransition(mac, DateUtils.dateToDateString(time));
        }
        //校准后小时表insert
        historyHourlyMapper.insert(historyHourly);
    }
}