kaiyu
2020-11-24 1d7e1b771160dd8b7f25765e402ce25cac676641
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package com.moral.service.impl;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import com.moral.common.util.ParameterUtils;
import com.moral.common.util.ResourceUtil;
import com.moral.entity.Device;
import com.moral.entity.DeviceProperty;
import com.moral.mapper.HistoryDailyMapper;
import com.moral.service.DeviceService;
import com.moral.service.HistoryDailyService;
import com.moral.service.OrganizationService;
 
@Service
public class HistoryDailyServiceImpl implements HistoryDailyService {
    @Resource
    private HistoryDailyMapper historyDailyMapper;
 
    @Resource
    private OrganizationService organizationService;
 
    @Resource
    private DeviceService deviceService;
 
    @Override
    public List<Map<String, Object>> getEmissionsData(Map<String, Object> parameters) throws Exception {
        String sensorKey = parameters.get("sensorKey").toString();
        getAllchildrenOrg(parameters);
        ParameterUtils.getTimeType4Time(parameters);
        ParameterUtils.getElementByType(parameters);
        ParameterUtils.getStartAndEndByTime(parameters);
        ParameterUtils.getRegionType4RegionCode(parameters);
        List<Map<String, Object>> list = historyDailyMapper.getEmissionsData(parameters);
 
        Map<String, Float> result = new HashMap<String, Float>();
 
        for (Map<String, Object> map : list) {
            String mac = map.get("mac").toString();
            String name = map.get("name").toString();
            if (!ObjectUtils.isEmpty(map.get(sensorKey))) {
                Float thickness = Float.valueOf(map.get(sensorKey).toString());
                Device device = deviceService.getDeviceByMac(mac);
                DeviceProperty deviceProperty = device.getDeviceProperty();
                if (!ObjectUtils.isEmpty(deviceProperty)) {
                    String extC = deviceProperty.getExtC();
                    String extD = deviceProperty.getExtD();
                    if (!(ObjectUtils.isEmpty(extC) || ObjectUtils.isEmpty(extD))) {
                        Float acreage = Float.valueOf(extC);
                        Float windSpeed = Float.valueOf(extD);
                        Float emissions = thickness * acreage * windSpeed * 24 * 60 * 60 / 1000000;
                        if (result.containsKey(name)) {
                            result.put(name, emissions + result.get(name));
                        } else {
                            result.put(name, emissions);
                        }
                    }
                }
 
            }
        }
        List<Map<String, Object>> resultMap = new ArrayList<Map<String, Object>>();
 
        for (Entry<String, Float> entry : result.entrySet()) {
            resultMap.add(new HashMap<String, Object>() {
                {
                    put("name", entry.getKey());
                    put("emissions", entry.getValue());
                }
            });
        }
        return resultMap;
    }
 
    private void getAllchildrenOrg(Map<String, Object> parameters) {
        if (parameters.containsKey("orgId")) {
            Integer orgId = Integer.valueOf(parameters.remove("orgId").toString());
            if (!(-1 == orgId || ResourceUtil.getValue("orgId").equals(orgId + ""))) {
                Set<Integer> orgIds = organizationService.getChildOrganizationIds(orgId);
                parameters.put("orgIds", orgIds);
            }
        }
    }
 
    @Override
    public Map getOverproofData(Map<String, Object> parameters) throws Exception {
        getAllchildrenOrg(parameters);
        ParameterUtils.getTimeType4Time(parameters);
        ParameterUtils.getElementByType(parameters);
        ParameterUtils.getStartAndEndByTime(parameters);
        ParameterUtils.getRegionType4RegionCode(parameters);
        List<Map<String, Object>> list = historyDailyMapper.getOverproofData(parameters);
        Map<String, Map<String, Object>[]> resultMap = new HashMap<String, Map<String, Object>[]>();
        for (Map<String, Object> map : list) {
            String name = map.get("name").toString();
            Integer deviceTech = Integer.valueOf(map.get("device_tech").toString());
            Map<String, Object>[] resultlist;
            if (resultMap.containsKey(name)) {
                resultlist = resultMap.get(name);
            } else {
                resultlist = new HashMap[3];
            }
            resultlist[deviceTech - 1] = new HashMap<String, Object>() {
                private static final long serialVersionUID = 1L;
                {
                    put("avg", map.get("avg"));
                    put("limit", map.get("limit"));
                }
            };
            resultMap.put(name, resultlist);
        }
        return resultMap;
    }
 
    // 通过选择的参数(设备mac,监测因子类型,查询时间)查询监测因子的数值
    @Override
    public Map<String, Double> getTraceabilityData(Map<String, Object> parameters) throws Exception {
        ParameterUtils.getElementByType(parameters);
        Map<String, Double> resultMap = historyDailyMapper.getTraceabilityData(parameters);
        return resultMap;
    }
 
    @Override
    public List<Map<String, Object>> getDataByTimeSlot(String mac, String startTime, String endTime) throws Exception {
        List<Map<String, Object>> resultMap = historyDailyMapper.getDataByTimeSlot(mac,startTime,endTime);
        //System.out.println(resultMap);
        return resultMap;
    }
 
}