|
|
@@ -0,0 +1,112 @@
|
|
|
+package org.example.tqyb.service;
|
|
|
+
|
|
|
+import com.fasterxml.jackson.core.type.TypeReference;
|
|
|
+import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
+import org.example.tqyb.mapper.CityMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.net.URL;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.List;
|
|
|
+import java.util.Map;
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class WeatherService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private CityMapper cityMapper;
|
|
|
+
|
|
|
+ public List<Integer> getWeather(String city_name, String date) throws Exception {
|
|
|
+ String apiKey = "32da14fc72a9aaca0a1b0ab73eaf10d9";
|
|
|
+ String apiUrl = "http://v.juhe.cn/hisWeather/weather";
|
|
|
+ SearchHttpAK snCal = new SearchHttpAK();
|
|
|
+ Map<String, Object> tq = new HashMap<>();
|
|
|
+
|
|
|
+ // 处理多个城市输入(用逗号或中文逗号分隔)
|
|
|
+ if (city_name.contains(",") || city_name.contains(",")) {
|
|
|
+ String str1 = city_name.replace(",", ",");
|
|
|
+ String[] str = str1.split(",");
|
|
|
+ for (String s : str) {
|
|
|
+ handleCityWeather(snCal, s.trim(), date, apiKey, apiUrl, tq);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ handleCityWeather(snCal, city_name.trim(), date, apiKey, apiUrl, tq);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 解析 night_temp 的数字并返回 List<Integer>
|
|
|
+ List<Integer> nightTemps = new ArrayList<>();
|
|
|
+ Pattern pattern = Pattern.compile("night_temp=([0-9]+)");
|
|
|
+ for (Object value : tq.values()) {
|
|
|
+ String weatherStr = value.toString();
|
|
|
+ Matcher matcher = pattern.matcher(weatherStr);
|
|
|
+ if (matcher.find()) {
|
|
|
+ nightTemps.add(Integer.parseInt(matcher.group(1)));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nightTemps;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 查询单个城市的天气并存入 tq
|
|
|
+ */
|
|
|
+ private void handleCityWeather(SearchHttpAK snCal, String cityName, String date,
|
|
|
+ String apiKey, String apiUrl, Map<String, Object> tq) throws Exception {
|
|
|
+ String city_name1 = snCal.getStationCityMap(cityName).replace("市", "");
|
|
|
+
|
|
|
+ if (!city_name1.isEmpty()) {
|
|
|
+
|
|
|
+
|
|
|
+ String city_id = cityMapper.selectcity(city_name1).get(0).getId();
|
|
|
+
|
|
|
+ HashMap<String, String> map = new HashMap<>();
|
|
|
+ map.put("key", apiKey);
|
|
|
+ map.put("city_id", city_id);
|
|
|
+ map.put("weather_date", date);
|
|
|
+
|
|
|
+ URL url = new URL(String.format("%s?%s", apiUrl, params(map)));
|
|
|
+ BufferedReader in = new BufferedReader(new InputStreamReader((url.openConnection()).getInputStream(), StandardCharsets.UTF_8));
|
|
|
+ String inputLine;
|
|
|
+ StringBuilder response = new StringBuilder();
|
|
|
+ while ((inputLine = in.readLine()) != null) {
|
|
|
+ response.append(inputLine);
|
|
|
+ }
|
|
|
+ in.close();
|
|
|
+
|
|
|
+ Map<String, Object> map1 = parseJson(response);
|
|
|
+ if ("查询成功".equals(map1.get("reason"))) {
|
|
|
+ tq.put(city_name1, map1.get("result").toString());
|
|
|
+ } else {
|
|
|
+ tq.put(city_name1, map1.get("reason").toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public static String params(Map<String, String> map) {
|
|
|
+ return map.entrySet().stream()
|
|
|
+ .map(entry -> {
|
|
|
+ try {
|
|
|
+ return entry.getKey() + "=" + URLEncoder.encode(entry.getValue(), StandardCharsets.UTF_8.toString());
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return entry.getKey() + "=" + entry.getValue();
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .collect(Collectors.joining("&"));
|
|
|
+ }
|
|
|
+
|
|
|
+ public Map<String, Object> parseJson(StringBuilder buffer) throws IOException {
|
|
|
+ ObjectMapper mapper = new ObjectMapper();
|
|
|
+ return mapper.readValue(buffer.toString(), new TypeReference<Map<String, Object>>() {
|
|
|
+ });
|
|
|
+ }
|
|
|
+}
|