jinpengyong
2020-09-21 5a292756180242c83dcb4603a7c425b52be69f6d
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package com.moral.service.impl;
 
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
 
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import javax.annotation.Resource;
 
import org.dom4j.Element;
 
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
 
import com.alibaba.fastjson.JSONObject;
import com.moral.entity.Area;
import com.moral.entity.City;
import com.moral.entity.MonitorPoint;
import com.moral.mapper.AreaMapper;
import com.moral.mapper.CityMapper;
import com.moral.mapper.MonitorPointMapper;
import com.moral.mapper.ProvinceMapper;
import com.moral.mapper.RealWeatherMapper;
import com.moral.service.RealWeatherService;
import com.moral.util.Dom4jUtils;
 
@Service
public class RealWeatherServiceImpl implements RealWeatherService {
 
    @Resource
    private RealWeatherMapper realWeatherMapper;
 
    @Resource
    private MonitorPointMapper monitorPointMapper;
 
    @Resource
    private AreaMapper areaMapper;
 
    @Resource
    private CityMapper cityMapper;
 
    @Resource
    private ProvinceMapper provinceMapper;
 
    @Override
    public int insertRealWeather() throws ParseException {
        DecimalFormat df = new DecimalFormat("0.0");
        Calendar c = Calendar.getInstance();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date now = sdf1.parse(sdf.format(c.getTime()) + ":00:00");
        RestTemplate restTemplate = new RestTemplate();
        List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList();
        Set<Map<String, Object>> hashSet = new HashSet<>();
        for (MonitorPoint monitorPoint : monitorPointList) {
            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);
        }
 
        Map<String, Object> smap = new HashMap<>();
        smap.put("name1", "苏州市");
        smap.put("parentName", "江苏省");
        smap.put("cityCode", 320500);
        smap.put("parentCode", 320000);
        hashSet.add(smap);
 
        Map<String, Object> bmap = new HashMap<>();
        bmap.put("name1", "北京市");
        bmap.put("parentName", "北京市");
        bmap.put("cityCode", 110000);
        bmap.put("parentCode", 110000);
        hashSet.add(bmap);
 
        List<Element> elements = Dom4jUtils.readDocument();
        String cityID = "101190404";
        for (Map<String, Object> map : hashSet) {
            String name1 = map.get("name1").toString();
            for (Element element : elements) {
                String name2 = element.element("name").getText();
                String parentName = map.get("parentName").toString();
                if (name2.equals("大丰市")) {
                    name2 = "大丰区";
                }
                if (name1.equals(name2)) {
                    cityID = element.element("Fweathercn").getText();
                    map.put("cityID", cityID);
                    break;
                }
                if (name2.endsWith(name1)) {
                    if (name2.startsWith(parentName)) {
                        cityID = element.element("Fweathercn").getText();
                        map.put("cityID", cityID);
                        break;
                    }
                }
            }
        }
        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/now?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}", Map.class, id);
            if (dataMap == null) {
                return count;
            }
            String json = JSONObject.toJSONString(dataMap);
            dataMap = (Map<String, Object>) JSONObject.parse(json);
            Map<String, Object> nowMap = (Map<String, Object>) dataMap.get("now");
            map.put("data", nowMap);
        }
 
        List<Map<String,Object>> resultList=new ArrayList<>();
        for (Map<String, Object> map : hashSet) {
            Map<String,Object> resultMap=new HashMap<>();
            resultMap.put("cityCode",map.get("cityCode").toString());
            resultMap.put("time",now);
            Map<String, Object> jsonMap = (Map<String, Object>) map.get("data");
 
            //风速km/h->m/s
            Double windSpeed = Double.valueOf(jsonMap.get("windSpeed").toString());
            windSpeed = windSpeed * 1000 / 3600;
            jsonMap.put("windSpeed",df.format(windSpeed));
            jsonMap.remove("obsTime");
            jsonMap.remove("icon");
            String text = jsonMap.get("text").toString();
            if(text.equals("晴")){
                jsonMap.put("condition","100");
            }else if(text.equals("多云")){
                jsonMap.put("condition","80");
            }else if(text.equals("阴")){
                jsonMap.put("condition","60");
            }else if(text.equals("雷阵雨")||text.equals("阵雨")){
                jsonMap.put("condition","40");
            }else if(text.equals("小雨")){
                jsonMap.put("condition","20");
            }else if(text.equals("雨")){
                jsonMap.put("condition","0");
            }
            resultMap.put("json",JSONObject.toJSONString(jsonMap));
            resultList.add(resultMap);
        }
        realWeatherMapper.deleteRealWeather(sdf1.format(now));
        return realWeatherMapper.insertRealWeather(resultList);
    }
}