jinpengyong
2021-08-03 bf8cb657cd550ce3061401bdf2842723d3c190a2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.moral.api.service.impl;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.api.entity.HistoryHourly;
import com.moral.api.mapper.HistoryHourlyMapper;
import com.moral.api.service.DeviceService;
import com.moral.api.service.HistoryHourlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.DateUtils;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
/**
 * <p>
 * 小时表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-06-04
 */
@Service
public class HistoryHourlyServiceImpl extends ServiceImpl<HistoryHourlyMapper, HistoryHourly> implements HistoryHourlyService {
 
    @Autowired
    private HistoryHourlyMapper historyHourlyMapper;
 
    @Autowired
    private DeviceService deviceService;
 
    @Override
    @Transactional
    public void insertHistoryHourly(Map<String, Object> data) {
        Map<String, Object> dataAdjust = new HashMap<>(data);
        String mac = data.remove("mac").toString();
 
        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);
 
        //数据校准
        dataAdjust = deviceService.adjustDeviceData(dataAdjust);
        dataAdjust.remove("mac");
        dataAdjust.remove("DataTime");
        dataAdjust.remove("ver");
 
        int count = historyHourlyMapper.getCountByMacAndTime(mac, DateUtils.dateToDateString(time));
 
        //判断中间表有没有该mac,该小时的数据,有就更新,没有就新增
        if (count == 0) {
            //小时数据校准后存入小时中间表
            result.put("value", JSONObject.toJSONString(dataAdjust));
            //新增
            historyHourlyMapper.insertHistoryHourlyTransition(result);
        } else {
            //更新
            historyHourlyMapper.updateHistoryTransition(mac, DateUtils.dateToDateString(time));
        }
    }
}