lizijie
2020-12-03 a66d53c3cbfb0024804045f4d795be06089d4f9d
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
package com.moral.service.impl;
 
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import com.moral.common.exception.BusinessException;
import com.moral.entity.Organization;
import com.moral.entity.charts.TimePeriod;
import com.moral.mapper.HangzhouAqiMapper;
import com.moral.mapper.OrganizationMapper;
import com.moral.service.HangzhouAqiService;
import com.moral.service.HistoryMinutelyService;
 
/**
 * @Auther: fengxiang
 * @Date: 2018/8/24 10:32
 * @Description:国控api服务实现类
 */
@Service
public class HangzhouAqiServiceImpl implements HangzhouAqiService {
  @Resource
  private OrganizationMapper organizationMapper;
  @Resource
  private HangzhouAqiMapper hangzhouAqiMapper;
 
  @Resource
  private HistoryMinutelyService historyMinutelyService;
 
  public List<Map> queryAqiOfTimePeriod(String code, TimePeriod timePeriod) {
    return hangzhouAqiMapper.selectAqisByCodeAndTimePeriod(code, timePeriod);
  }
 
  /**
   * 根据组织id获取最近24小时国控aqi数据
   *
   * @param orgId
   * @return 返回长度为24的数组,查不到的数据设置为0
   */
  @SuppressWarnings("serial")
  @Override
  public List<Map<String, Object>> queryAqi24Hours(Integer orgId) {
    List<Map<String, Object>> aqi24HoursValues = new ArrayList<>(24);
/*
        Date now = ReportTimeFormat.getFormatDate(ReportTimeFormat.HOUR_FORMAT);
        Date start = ReportTimeFormat.dateCalc(now, TimeUnits.HOUR,-24);
        Date end = ReportTimeFormat.dateCalc(now, TimeUnits.MILLISECOND,-1);;
        TimePeriod timePeriod = new TimePeriod(start,end,TimeUnits.HOUR);
        Integer code = getCode(orgId,false);
        List<Map> aqis = queryAqiOfTimePeriod(code.toString(),timePeriod);
        List<String> timeList = ReportTimeFormat.makeTimeList(timePeriod);
        if(CollectionUtils.isEmpty(aqis)) {
            code = getCode(orgId,true);
            aqis = queryAqiOfTimePeriod(code.toString(),timePeriod);
        }
        int mTemp = 0;
        for(int n =0;n<timeList.size();n++) {
             String time = timeList.get(n);
             Object aqiValue = null;
             for(int m =mTemp;m<aqis.size();m++) {
                 Map<String,Object> rowData = aqis.get(m);
                 Object dbTime = rowData.get("time");
                 if(time.equals(dbTime)){
                     mTemp = m+1;
                     aqiValue = rowData.get("aqi");
                 }
             }
            Map<String,Object> aqiItem = new HashMap<>();
            aqiItem.put("time",time);
            aqiValue = aqiValue!=null?Float.valueOf(aqiValue.toString().replace("\"","")):null;
            aqiItem.put("aqi",aqiValue);
            aqi24HoursValues.add(aqiItem);
        }
*/
    Organization organization = organizationMapper.selectByPrimaryKey(orgId);
 
    Map<String, Object> parameters = new HashMap<String, Object>() {{
      put("end", LocalDateTime.now());
      put("cityCode", organization.getAreaCode());
    }};
    aqi24HoursValues = hangzhouAqiMapper.getAqisByOrganizationId(parameters);
    if (ObjectUtils.isEmpty(aqi24HoursValues)) {
      parameters.put("cityCode", organization.getCityCode());
      aqi24HoursValues = hangzhouAqiMapper.getAqisByOrganizationId(parameters);
    }
    Collections.reverse(aqi24HoursValues);
    return aqi24HoursValues;
  }
 
  private Integer getCode(Integer orgId, boolean isGetCityCode) {
    Organization org = organizationMapper.selectByPrimaryKey(orgId);
    Integer code = null;
    if (isGetCityCode) {
      code = org.getCityCode();
    } else {
      code = org.getAreaCode();
    }
    if (code == null) {
      throw new BusinessException("citeCode or areaCode is null;");
    }
    return code;
  }
 
  @Override
  public Integer queryCityCode(Integer areaCode) {
    return  hangzhouAqiMapper.queryCityCode(areaCode);
  }
 
  @Override
  public List<Map<String, Object>> getAreaAvgDataByAreaCode(Map<String, Object> parameters) throws Exception {
    historyMinutelyService.convertQueryParam(parameters);
    if (!ObjectUtils.isEmpty(parameters.get("compensate"))) {
      parameters.put("timeUnits", "10min");
    }
 
    String sensors2 = parameters.get("sensors2").toString();
 
    String sensors1 = sensors2;
    String sensors="";
    switch (sensors2) {
      case "e1":
        sensors1 = "PM2_5";
        sensors="PM25C";
        break;
      case "e2":
        sensors1 = "PM10";
        sensors="PM10C";
        break;
      case "e10":
        sensors1 = "CO";
        sensors="COC";
        break;
      case "e11":
        sensors1 = "SO2";
        sensors="SO2C";
        break;
      case "e15":
        sensors1 = "O3";
        sensors="O3C";
        break;
      case "e16":
        sensors1 = "NO2";
        sensors="NO2C";
        break;
      default:
        break;
    }
 
    parameters.put("sensors1", sensors1);
    parameters.put("sensors",sensors);
    List<Map<String, Object>> areaList = hangzhouAqiMapper.getAreaAvgDataByAreaCode(parameters);
    if (areaList.isEmpty()) {
      Integer areaCode = Integer.valueOf(parameters.get("areaCode").toString());
      areaCode = hangzhouAqiMapper.queryCityCode(areaCode);
      parameters.put("areaCode", String.valueOf(areaCode));
      areaList = hangzhouAqiMapper.getAreaAvgDataByAreaCode(parameters);
    }
    return areaList;
 
  }
}