jinpengyong
2021-06-30 e6c6e6225bdbaaa27bcde320a79acde8239416c2
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
package com.moral.api.service.impl;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
 
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.util.DateUtils;
 
@Service
public class HistoryMinutelyServiceImpl implements HistoryMinutelyService {
 
    @Autowired
    private HistoryMinutelyMapper historyMinutelyMapper;
 
    @Autowired
    private DeviceService deviceService;
 
    @Override
    public void insertHistoryMinutely(Map<String, Object> data) {
        Map<String, Object> result = new HashMap<>();
        Map<String, Object> dataAdjust = new HashMap<>(data);
        Object mac = data.remove("mac");
        result.put("mac", mac);
        result.put("version", data.remove("ver"));
        Date time = DateUtils.getDate((String) data.remove("DataTime"), DateUtils.yyyyMMddHHmmss_EN);
        result.put("time", time);
        result.put("value", JSONObject.toJSONString(data));
        String timeUnits = DateUtils.dateToDateString(time, DateUtils.yyyyMM_EN);
        result.put("timeUnits", tableSuffix(timeUnits, Constants.UN_ADJUST));
 
        //原始数据(未校准)
        historyMinutelyMapper.insertHistoryMinutely(result);
 
        //数据校准
        dataAdjust = deviceService.adjustDeviceData(dataAdjust);
        dataAdjust.remove("mac");
        dataAdjust.remove("DataTime");
        dataAdjust.remove("ver");
        result.put("timeUnits", timeUnits);
        result.put("value", JSONObject.toJSONString(dataAdjust));
        historyMinutelyMapper.insertHistoryMinutely(result);
 
 
    }
 
    //表后缀
    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();
    }
}