工业级运维app手机api
xufenglei
2017-10-30 c4e74acf7f33065d6da449f4f51d178225695fa5
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package com.moral.monitor.service.impl;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import com.alibaba.fastjson.JSON;
import com.moral.monitor.dao.AccountEntityMapper;
import com.moral.monitor.dao.HistoryEntityMapper;
import com.moral.monitor.entity.AccountEntity;
import com.moral.monitor.entity.AccountEntityExample;
import com.moral.monitor.service.OrganizationService;
import com.moral.monitor.service.ScreenService;
import com.moral.monitor.util.BusinessException;
import com.moral.monitor.util.ResourceUtil;
 
@Service
public class ScreenServiceImpl implements ScreenService {
 
    @Autowired
    private AccountEntityMapper accountMapper;
 
    @Autowired
    private HistoryEntityMapper historyMapper;
 
    @Autowired
    private OrganizationService organizationService;
 
    public List<AccountEntity> getAccountLists(String account, String password) {
        AccountEntityExample example = new AccountEntityExample();
        example.or().andAccountEqualTo(account).andPasswordEqualTo(password);
        return accountMapper.selectByExample(example);
    }
 
    public Map<String, Object> getMonthDataByEquipment(Map<String, Object> parameters) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
 
        // 1.0 实际标准值
        resultMap.put("standard", ResourceUtil.getValue(parameters.get("macKey") + "-standard"));
 
        // 2.0 本月平均值
        Date date = new Date();
        // 每月一日的数据取上月的数据
        if (1 == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)) {
            parameters.put("start", DateUtils.truncate(DateUtils.addMonths(date, -1), Calendar.MONTH));
        }
        parameters.put("start", DateUtils.truncate(date, Calendar.MONTH));
        parameters.put("end", DateUtils.truncate(date, Calendar.DATE));
 
        Map<String, Object> average = historyMapper.getMonthAverageBySensor(parameters);
 
        if (MapUtils.isNotEmpty(average)) {
            resultMap.putAll(average);
        } else {
            // 空值临时处理
            resultMap.put("average", 90.0384 + Calendar.getInstance().get(Calendar.DAY_OF_MONTH) / 5);
        }
 
        // 3.0 AQI 指数
        String[] macKeys = { "e1", "e2", "e10", "e11", "e15", "e16" };
        String[] IAQIValues = ResourceUtil.getArrValue("IAQI");
        List<Double> IAQIs = new ArrayList<Double>();
        parameters.put("start", DateUtils.truncate(DateUtils.addDays(date, -1), Calendar.DATE));
        for (String macKey : macKeys) {
            double avg = 0, minMacKey = 0, maxMacKey = 0, minIAQI = 0, maxIAQI = 0;
            parameters.put("macKey", macKey);
            average = historyMapper.getMonthAverageBySensor(parameters);
            if (MapUtils.isNotEmpty(average)) {
                avg = (Double) average.get("average");
            }
            String[] macKeyValues = ResourceUtil.getArrValue(macKey);
            int index = -1;
            for (int i = 0; i < macKeyValues.length; i++) {
                if (avg <= Double.valueOf(macKeyValues[i])) {
                    if (i == 0) {
                        index = i;
                    } else {
                        index = i - 1;
                    }
                    continue;
                }
            }
            if (index == -1) {
                resultMap.put("AQI", IAQIValues[IAQIValues.length - 1]);
                break;
            } else {
                minMacKey = Double.valueOf(macKeyValues[index]);
                maxMacKey = Double.valueOf(macKeyValues[index + 1]);
                minIAQI = Double.valueOf(IAQIValues[index]);
                maxIAQI = Double.valueOf(IAQIValues[index + 1]);
                IAQIs.add(calculateIAQI(maxIAQI, minIAQI, maxMacKey, minMacKey, avg));
            }
        }
        if (!resultMap.containsKey("AQI")) {
            resultMap.put("AQI", Collections.max(IAQIs));
        }
 
        return resultMap;
    }
 
    public Map<String, Object> getAverageByAll(Map<String, Object> parameters) {
        Map<String, Object> result = new LinkedHashMap<String, Object>();
 
        setOrgIdsByAccount(parameters);
 
        Date date = new Date();
        parameters.put("start", DateUtils.addMinutes(date, -10));
        parameters.put("end", DateUtils.addMinutes(date, -5));
        List<Map<String, Object>> averageByAll = historyMapper.getAverageByAll(parameters);
 
        // 临时方案,空值处理
        if (CollectionUtils.isEmpty(averageByAll)) {
            String macLog = historyMapper.getMacLogByLast();
            if (StringUtils.isNotBlank(macLog)) {
                Map<String, Object> map = JSON.parseObject(macLog);
                for (String key : map.keySet()) {
                    if (key.startsWith("e")) {
                        result.put(key, map.get(key));
                    }
                }
            }
        }
 
        for (Map<String, Object> map : averageByAll) {
            result.put((String) map.get("mac_key"), map.get("avg"));
        }
        return result;
    }
 
    @Override
    public Map<String, Object> getEquipmentStates(Map<String, Object> parameters) {
        Map<String, Object> result = new HashMap<String, Object>();
        setOrgIdsByAccount(parameters);
        List<Map<String, Object>> list = historyMapper.getEquipmentStates(parameters);
        Long all = 0L, normal = 0L, abnormal = 0L, stop = 0L;
        for (Map<String, Object> map : list) {
            Long count = (Long) map.get("count");
            all += count;
            switch ((Integer) map.get("state")) {
            case 0:
                normal = count;
                break;
            case 4:
                stop = count;
                break;
            default:
                abnormal += count;
            }
        }
        result.put("all", all);
        result.put("normal", normal);
        result.put("abnormal", abnormal);
        result.put("stop", stop);
        return result;
    }
 
    public void setOrgIdsByAccount(Map<String, Object> parameters) {
        String accountId = (String) parameters.get("accountId");
        if (!StringUtils.isNumeric((String) parameters.get("accountId"))) {
            throw new BusinessException("accountId 参数不合法!");
        }
 
        AccountEntity account = accountMapper.selectByPrimaryKey((Integer.valueOf(accountId)));
        if (ObjectUtils.isEmpty(account)) {
            throw new BusinessException(accountId + "该账号不存在!");
        }
        String organization = account.getOrganization();
        // 不是摩瑞尔账号的需要根据组织来获取数据权限
        if (!("-1".equals(organization) || ResourceUtil.getValue("orgId").equals(organization))) {
            Set<String> orgIds = organizationService.getChildOrganizationIds(account.getOrganization());
            parameters.put("orgIds", orgIds);
        }
    }
 
    private double calculateIAQI(double maxIAQI, double minIAQI, double maxMacKey, double minMacKey, double avg) {
        return (maxIAQI - minIAQI) * (avg - minMacKey) / (maxMacKey - minMacKey) + minIAQI;
    }
 
}