jinpengyong
2021-02-20 1bc01b6d0c904c80dc002285654ebb542f044474
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
package com.moral.service.impl;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.moral.mapper.AQIMapper;
import com.moral.service.AQIService;
import com.moral.util.HttpUtils;
 
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
 
import javax.annotation.Resource;
 
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
public class AQIServiceImpl implements AQIService {
    @Resource
    private AQIMapper aqiMapper;
 
    @Override
    public List<Map<String, Object>> getCityAqiConfig() {
        return aqiMapper.getCityAqiConfig();
    }
 
    @Override
    public int insertAQIData(Map<String, Object> parameters) {
        return aqiMapper.insertAQIData(parameters);
    }
 
    @Override
    public int insertStationData() {
        int state = 500;
        List<Map<String, Object>> stationCodes = aqiMapper.getStationConfig();
        String host = "https://api.epmap.org";
        String path = "/api/v1/air/station";
        String method = "GET";
        String appcode = "31b6ea8f804a4472be3b633cfee44849";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH");
        String pubtime = df.format(date) + ":00:00";
        querys.put("pubtime", pubtime);
        List<Map<String, Object>> insertList = new ArrayList<>();
        for (Map<String, Object> stationMap : stationCodes) {
            String stationCode = stationMap.get("station_code").toString();
            String cityCode = stationMap.get("city_code").toString();
            Map<String, Object> map = new HashMap<>();
            querys.put("station_code", stationCode);
            try {
                HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
                JSONObject json = JSON.parseObject(EntityUtils.toString(response.getEntity()));
                Map<String, Object> data = (Map<String, Object>) json.get("data");
                if (!ObjectUtils.isEmpty(data)) {
                    pubtime = data.get("pubtime").toString();
                    map.put("time", pubtime);
                    map.put("json", JSON.toJSONString(data));
                    map.put("stationCode", stationCode);
                    map.put("cityCode", cityCode);
                    insertList.add(map);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (!insertList.isEmpty()) {
            aqiMapper.insertStationData(insertList);
            state = 200;
        }
        return state;
    }
}