| | |
| | | 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(); |
| | |
| | | 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; |
| | | } |
| | | } |