ZhuDongming
2020-05-07 59ea47acd477c8681cd1a2a59be53e69eab9fba0
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
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;
 
/**
 * @Auther: fengxiang
 * @Date: 2018/8/24 10:32
 * @Description:国控api服务实现类
 */
@Service
public class HangzhouAqiServiceImpl implements HangzhouAqiService {
  @Resource
  private OrganizationMapper organizationMapper;
  @Resource
  private HangzhouAqiMapper hangzhouAqiMapper;
 
  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);
  }
}