jinpengyong
2020-08-07 0ef88815f1893b069d5e59a4161d2a9a4a2f3a10
src/main/java/com/moral/service/impl/ForecastWeatherServiceImpl.java
@@ -50,7 +50,7 @@
    private ProvinceMapper provinceMapper;
    @Override
    public void insertForecastWeather() throws ParseException {
    public int insertForecastWeather() throws ParseException {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@@ -64,39 +64,40 @@
        RestTemplate restTemplate = new RestTemplate();
        List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList();
        Set<Map<String, Object>> hashSet = new HashSet<>();
        List<Map<String, Object>> resultList = new ArrayList<>();
        for (MonitorPoint monitorPoint : monitorPointList) {
            Map<String, Object> hashMap1 = new HashMap<>();
            Map<String, Object> hashMap = new HashMap<>();
            Integer areaCode = monitorPoint.getAreaCode();
            Integer cityCode = monitorPoint.getCityCode();
            Integer provinceCode = monitorPoint.getProvinceCode();
            String parentName = "";
            String name1 = "";
            Integer code=0;
            if (areaCode != null) {
                Area area = areaMapper.getAreaByAreaCode(areaCode);
                City city = cityMapper.getCityByCityCode(cityCode);
                name1 = area.getAreaName();
                parentName = city.getCityName();
                code=areaCode;
                if ("市辖区".equals(name1)) {
                    name1 = parentName;
                    parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
                    code=cityCode;
                }
            } else {
                City city = cityMapper.getCityByCityCode(cityCode);
                name1 = city.getCityName();
                parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
                code=cityCode;
                if ("市辖区".equals(name1)) {
                    name1 = parentName;
                    parentName = "";
                    code=provinceCode;
                }
            }
            hashMap.put("name1", name1);
            hashMap.put("parentName", parentName);
            hashMap.put("cityCode",code);
            hashSet.add(hashMap);
            hashMap1.put("monitorPointId", monitorPoint.getId());
            hashMap1.put("name", name1);
            resultList.add(hashMap1);
        }
        List<Element> elements = Dom4jUtils.readDocument();
        String cityID = "101190404";
@@ -123,11 +124,12 @@
            }
        }
        int count=0;
        for (Map<String, Object> map : hashSet) {
            String id = map.get("cityID").toString();
            Map<String, Object> dataMap = restTemplate.getForObject("https://api.heweather.net/v7/weather/72h?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id);
            if (dataMap == null) {
                dataMap = new HashMap<>();
                return count;
            }
            String json = JSONObject.toJSONString(dataMap);
            dataMap = (Map<String, Object>) JSONObject.parse(json);
@@ -147,37 +149,31 @@
            }
        }
        for (Map<String, Object> resultMap : resultList) {
            ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
            for (Map<String, Object> hashMap : hashSet) {
                if (resultMap.get("name").equals(hashMap.get("name1"))) {
                    ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) hashMap.get("data");
                    for (Map<String, Object> hourMap : list) {
                        Map<String, Object> map = new HashMap<>();
                        map.put("monitorPointId", resultMap.get("monitorPointId").toString());
                        Map<String, Object> jsonMap = new HashMap<>();
                        String time = hourMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00";
                        map.put("time", sdf1.parse(time));
                        hourMap.remove("icon");
                        jsonMap.putAll(hourMap);
                        map.put("json", JSONObject.toJSONString(jsonMap));
                        arrayList.add(map);
                    }
                }
        for (Map<String, Object> map : hashSet) {
            List<Map<String,Object>> resultList=new ArrayList<>();
            ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map.get("data");
            for (Map<String, Object> dataMap : dataList) {
                Map<String,Object> hashMap=new HashMap<>();
                hashMap.put("cityCode",map.get("cityCode").toString());
                String time = dataMap.get("fxTime").toString().split("\\+")[0].replace("T", " ") + ":00";
                hashMap.put("time",sdf1.parse(time));
                hashMap.put("json",JSONObject.toJSONString(dataMap));
                resultList.add(hashMap);
            }
            arrayList.remove(0);
            resultList.remove(0);
            for (Map<String, Object> resultMap : resultList) {
                Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(resultMap.get("json").toString());
                jsonMap.remove("fxTime");
                jsonMap.remove("icon");
                resultMap.put("json", JSONObject.toJSONString(jsonMap));
            }
            Map<String, Object> parameters = new HashMap<>();
            parameters.put("start", start);
            parameters.put("end", end);
            parameters.put("monitorPointId", resultMap.get("monitorPointId").toString());
            parameters.put("cityCode", map.get("cityCode").toString());
            forecastWeatherMapper.deleteByTime(parameters);
            for (Map<String, Object> map : arrayList) {
                Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(map.get("json").toString());
                jsonMap.remove("fxTime");
                map.put("json", JSONObject.toJSONString(jsonMap));
            }
            forecastWeatherMapper.insertForecastWeather(arrayList);
            count = count+forecastWeatherMapper.insertForecastWeather(resultList);
        }
        return count;
    }
}