wangrong
2021-11-24 130b72d6c5734f8acaac0214b6dfe5c8bfa319c3
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
83
84
85
86
87
88
89
package com.example.demo.service;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo.dao.WeatherRepository;
import com.example.demo.model.Weather;
import com.example.demo.utils.RestTemplateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import xyz.erupt.jpa.dao.EruptDao;
 
import javax.annotation.Resource;
import javax.transaction.Transactional;
 
@Service
//public class WeatherService implements CommandLineRunner {
public class WeatherService {
 
    @Resource
    private EruptDao eruptDao;
 
    @Resource
    private WeatherRepository weatherRepository;
 
    @Autowired
    private RestTemplate restTemplate;
 
    @Value("${gis.weather_0}")
    private String gisWeather_0;
 
    @Transactional
    public void run(String... args) {
//        new ProjectUtil().projectStartLoaded("monitor", bool -> {
//            if (bool) {
//                String code = "code";
//                EruptMenu eruptMenu = eruptDao.persistIfNotExist(EruptMenu.class, new EruptMenu("monitor", "系统监控", null, null,
//                        MenuStatus.OPEN.getValue(), 10, "fa fa-bullseye", null), "code", "monitor");
//                eruptDao.persistIfNotExist(EruptMenu.class, new EruptMenu("server.html", "服务监控", EruptTplService.TPL,
//                        "server.html", MenuStatus.OPEN.getValue(), 10, null, eruptMenu), code, "server.html");
//                eruptDao.persistIfNotExist(EruptMenu.class, new EruptMenu("redis.html", "缓存监控", EruptTplService.TPL,
//                        "redis.html", MenuStatus.OPEN.getValue(), 20, null, eruptMenu), code, "redis.html");
//                eruptDao.persistIfNotExist(EruptMenu.class, new EruptMenu(EruptOnline.class.getSimpleName(), "在线用户", MenuTypeEnum.TABLE.getCode(),
//                        EruptOnline.class.getSimpleName(), MenuStatus.OPEN.getValue(), 30, null, eruptMenu), code, EruptOnline.class.getSimpleName());
//            }
//        });
    }
 
 
    /**
     * 获取天气
     * @return
     */
    public Weather getWeather() {
//        String weather_url ="https://restapi.amap.com/v3/weather/weatherInfo?key=8a3f6bae17eb0b3d948b9fbff613daad&city=310100&extensions=base&output=JSON";
        JSONObject retJson3 = RestTemplateUtil.getDataByRestTemplate(gisWeather_0, restTemplate);
        JSONArray array = retJson3.getJSONArray("lives");
        JSONObject json = JSONObject.parseObject(array.get(0).toString()) ;
        Weather weather = new Weather();
        weather.setAdcode(json.get("adcode").toString());
        weather.setProvince(json.get("province").toString());
        weather.setCity(json.get("city").toString());
        weather.setWinddirection(json.get("winddirection").toString());
        weather.setWindpower(json.get("windpower").toString());
        weather.setReporttime(json.get("reporttime").toString());
        weather.setTemperature(json.get("temperature").toString());
        weather.setHumidity(json.get("humidity").toString());
        weather.setWeather(json.get("weather").toString());
        weather.setWdate(json.get("reporttime").toString().substring(0,10));
        weather.setWhour(json.get("reporttime").toString().substring(11,13));
        weather.setWeatherjson(json.toJSONString());
 
//        String js=JSONObject.toJSONString(retJson3.getJSONArray("lives"), SerializerFeature.WriteClassName);//将array数组转换成字符串
//        List<Weather> collection = JSONObject.parseArray(js, Weather.class);
        weatherRepository.save(weather);
        return weather;
    }
 
 
    private JSONObject postRemoteDataByRestTemplate(String url, Object params, RestTemplate restTemplate) {
        return RestTemplateUtil.postDataByRestTemplate(url, params, restTemplate);
    }
 
 
 
 
 
}