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> getCityAqiConfig() { return aqiMapper.getCityAqiConfig(); } @Override public int insertAQIData(Map parameters) { return aqiMapper.insertAQIData(parameters); } @Override public int insertStationData() { int state = 500; List> stationCodes = aqiMapper.getStationConfig(); String host = "https://api.epmap.org"; String path = "/api/v1/air/station"; String method = "GET"; String appcode = "31b6ea8f804a4472be3b633cfee44849"; Map headers = new HashMap(); headers.put("Authorization", "APPCODE " + appcode); Map querys = new HashMap(); Date date = new Date(); DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH"); String pubtime = df.format(date) + ":00:00"; querys.put("pubtime", pubtime); List> insertList = new ArrayList<>(); for (Map stationMap : stationCodes) { String stationCode = stationMap.get("station_code").toString(); String cityCode = stationMap.get("city_code").toString(); Map 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 data = (Map) 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; } }