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