package com.whyc.service;
|
|
import com.whyc.dto.Response;
|
import com.whyc.dto.weather.Weather;
|
import com.whyc.dto.weather.WeatherInfo;
|
import com.whyc.dto.weather.WeatherInfo2;
|
import com.whyc.mapper.WeatherCityMapper;
|
import com.whyc.pojo.WeatherCity;
|
import com.whyc.util.ActionUtil;
|
import com.whyc.util.HttpUtil;
|
import net.sf.ezmorph.object.DateMorpher;
|
import net.sf.json.JSONArray;
|
import net.sf.json.JSONObject;
|
import net.sf.json.util.JSONUtils;
|
|
import org.springframework.stereotype.Service;
|
|
import javax.annotation.Resource;
|
import javax.servlet.ServletContext;
|
import java.util.*;
|
|
import static com.whyc.util.HttpUtil.urlencode;
|
|
@Service
|
public class WeatherService {
|
|
/**属于第三方-聚合的天气接口*/
|
// 天气情况查询接口地址
|
private static String API_URL = "http://apis.juhe.cn/simpleWeather/query";
|
// 接口请求Key
|
private static String API_KEY = "d88bb2c73f4fa5d1c0fee1df0b5d108a";
|
|
/**属于第三方-百度的天气接口*/
|
private static String API_URL_BAIDU = "https://api.map.baidu.com/weather/v1/";
|
private static String API_KEY_BAIDU = "tcnlFtekhszWP65H14Mzvhd10uwZVW6G";
|
|
@Resource
|
private WeatherCityMapper weatherCityMapper;
|
|
/**百度天气查询使用*/
|
public Response get2(WeatherCity city) {
|
Response model = new Response();
|
|
//调试用
|
//city.setId(1);
|
List<WeatherInfo2> futureList = new LinkedList<>();
|
WeatherInfo realtime = new WeatherInfo();
|
|
//根据传过来的省市区,定位到区码cityId
|
Integer cityId = getId2(city);
|
if(cityId==0){
|
model.setII(0,"暂不支持当前城市天气查询");
|
}else {
|
//首先从ApplicationContext中获取,如果不存在,则请求第三方接口,同时将值存入
|
ServletContext application = ActionUtil.getApplication();
|
List<Weather> weatherList = (List<Weather>) application.getAttribute("weather");
|
|
|
if (weatherList == null) {
|
weatherList = new ArrayList<>();
|
/*//调试
|
Weather weather = new Weather();
|
weather.setId(2);
|
weather.setRealtime(new WeatherInfo());
|
weatherList.add(weather);*/
|
}
|
//List<Weather> weatherList = new ArrayList<>();
|
//weatherList.addAll(weatherList2);
|
|
if (weatherList.size() == 0) {
|
Map<String, Object> params = new HashMap<>();//组合参数
|
params.put("district_id", cityId);
|
params.put("data_type", "all");
|
params.put("ak", API_KEY_BAIDU);
|
String queryParams = urlencode(params);
|
String response = HttpUtil.doGet(API_URL_BAIDU, queryParams);
|
try {
|
JSONObject jsonObject = JSONObject.fromObject(response);
|
int error_code = jsonObject.getInt("status");
|
if (error_code == 0) {
|
|
JSONObject result = jsonObject.getJSONObject("result");
|
JSONObject realtimeJson = result.getJSONObject("now");
|
JSONArray futureJsonArr = result.getJSONArray("forecasts");
|
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd", "yyyy-MM-dd"}));
|
realtime.setTemperature(String.valueOf(realtimeJson.getInt("temp")));
|
realtime.setPower(realtimeJson.getString("wind_class"));
|
realtime.setDirect(realtimeJson.getString("wind_dir"));
|
realtime.setInfo(realtimeJson.getString("text"));
|
for (int i = 0; i < futureJsonArr.size(); i++) {
|
|
WeatherInfo2 futureInfo = new WeatherInfo2();
|
|
JSONObject futureJson = futureJsonArr.getJSONObject(i);
|
futureInfo.setTemperatureHigh(futureJson.getInt("high"));
|
futureInfo.setTemperatureLow(futureJson.getInt("low"));
|
futureInfo.setPowerDay(futureJson.getString("wc_day"));
|
futureInfo.setPowerNight(futureJson.getString("wc_night"));
|
futureInfo.setDirectDay(futureJson.getString("wd_day"));
|
futureInfo.setDirectNight(futureJson.getString("wd_night"));
|
futureInfo.setInfoDay(futureJson.getString("text_day"));
|
futureInfo.setInfoNight(futureJson.getString("text_night"));
|
futureInfo.setWeek(futureJson.getString("week"));
|
futureInfo.setDate(futureJson.getString("date"));
|
|
futureList.add(futureInfo);
|
}
|
model.setCode(1);
|
model.setData(realtime);
|
model.setData2(futureList);
|
System.out.println("调用第三方接口取,并存入内存");
|
//将值存入到application中
|
Weather weatherTemp = new Weather();
|
weatherTemp.setId(cityId);
|
weatherTemp.setRealtime(realtime);
|
weatherTemp.setFuture2(futureList);
|
weatherList.add(weatherTemp);
|
application.setAttribute("weather", weatherList);
|
} else {
|
model.setMsg("天气查询接口失败:" + jsonObject.getString("message"));
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
model.setMsg("接口异常");
|
}
|
} else {
|
for (int i = 0; i < weatherList.size(); i++) {
|
if(weatherList.get(i).getId().intValue()==cityId){
|
model.setCode(1);
|
model.setData(weatherList.get(i).getRealtime());
|
model.setData2(weatherList.get(i).getFuture2());
|
System.out.println("从内存中取");
|
return model;
|
}
|
|
}
|
|
Map<String, Object> params = new HashMap<>();//组合参数
|
params.put("district_id", cityId);
|
params.put("data_type", "all");
|
params.put("ak", API_KEY_BAIDU);
|
String queryParams = urlencode(params);
|
String response = HttpUtil.doGet(API_URL_BAIDU, queryParams);
|
try {
|
JSONObject jsonObject = JSONObject.fromObject(response);
|
int error_code = jsonObject.getInt("status");
|
if (error_code == 0) {
|
JSONObject result = jsonObject.getJSONObject("result");
|
JSONObject realtimeJson = result.getJSONObject("now");
|
JSONArray futureJsonArr = result.getJSONArray("forecasts");
|
JSONUtils.getMorpherRegistry().registerMorpher(new DateMorpher(new String[]{"yyyy-MM-dd", "yyyy-MM-dd"}));
|
realtime.setTemperature(String.valueOf(realtimeJson.getInt("temp")));
|
realtime.setPower(realtimeJson.getString("wind_class"));
|
realtime.setDirect(realtimeJson.getString("wind_dir"));
|
realtime.setInfo(realtimeJson.getString("text"));
|
for (int i = 0; i < futureJsonArr.size(); i++) {
|
|
WeatherInfo2 futureInfo = new WeatherInfo2();
|
|
JSONObject futureJson = futureJsonArr.getJSONObject(i);
|
futureInfo.setTemperatureHigh(futureJson.getInt("high"));
|
futureInfo.setTemperatureLow(futureJson.getInt("low"));
|
futureInfo.setPowerDay(futureJson.getString("wc_day"));
|
futureInfo.setPowerNight(futureJson.getString("wc_night"));
|
futureInfo.setDirectDay(futureJson.getString("wd_day"));
|
futureInfo.setDirectNight(futureJson.getString("wd_night"));
|
futureInfo.setInfoDay(futureJson.getString("text_day"));
|
futureInfo.setInfoNight(futureJson.getString("text_night"));
|
futureInfo.setWeek(futureJson.getString("week"));
|
futureInfo.setDate(futureJson.getString("date"));
|
|
futureList.add(futureInfo);
|
}
|
model.setCode(1);
|
model.setData(realtime);
|
model.setData2(futureList);
|
System.out.println("调用第三方接口取,并存入内存");
|
//将值存入到application中
|
Weather weatherTemp = new Weather();
|
weatherTemp.setId(cityId);
|
weatherTemp.setRealtime(realtime);
|
weatherTemp.setFuture2(futureList);
|
weatherList.add(weatherTemp);
|
application.setAttribute("weather", weatherList);
|
} else {
|
model.setMsg("天气查询接口失败:" + jsonObject.getString("message"));
|
}
|
} catch (Exception e) {
|
e.printStackTrace();
|
model.setMsg("接口异常");
|
}
|
}
|
}
|
|
return model;
|
}
|
|
|
public Integer getId2(WeatherCity city){
|
WeatherCity weatherCity = weatherCityMapper.getWeatherCity2(city);
|
//当前区不存在,则查询上一级市的
|
if (weatherCity==null){
|
city.setDistrict(city.getCity().replace("市",""));
|
WeatherCity weatherCity1 = weatherCityMapper.getWeatherCity2(city);
|
if (weatherCity1==null){
|
return 0;
|
}else {
|
return Integer.valueOf(weatherCity1.getDistrictCode());
|
}
|
}else{
|
return Integer.valueOf(weatherCity.getDistrictCode());
|
}
|
}
|
|
}
|