kaiyu
2020-10-21 5e9129373399e9cc3bdecf9b8365e1c6b6b0ecdd
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
package com.moral.util;
 
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.dom4j.Element;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
 
import com.moral.entity.AreaNames;
import com.moral.entity.Organization;
import com.moral.service.OrganizationService;
 
@Component
public class WeatherUtil {
 
    @Resource
    private OrganizationService organizationService;
 
    public List<Map<String, Object>> getWeatherByMac(Map<String, Object> parameters) {
        String areaName = "昆山市";
        String parentName = "";
        Organization organization = organizationService.getOrganizationByMac(parameters);
        if (organization.getAreaNames() != null) {
            AreaNames areaNames = organization.getAreaNames();
            if (ObjectUtils.isEmpty(areaNames.getAreaName())) {
                if (ObjectUtils.isEmpty(areaNames.getCityName())) {
                    areaName = areaNames.getProvinceName();
                } else {
                    areaName = areaNames.getCityName();
                }
            } else {
                areaName = areaNames.getAreaName();
                parentName = areaNames.getCityName();
                if ("市辖区".equals(areaName)) {
                    areaName = parentName;
                }
            }
        }
        List<Element> elements = Dom4jUtils.readDocument();
        String cityID = "1102";
        for (Element element : elements) {
            String name = element.element("name").getText();
            if (areaName.equals(name)) {
                cityID = element.element("Fid").getText();
                break;
            }
            if (name.endsWith(areaName)) {
                if (name.startsWith(parentName)) {
                    cityID = element.element("Fid").getText();
                    break;
                }
            }
        }
        RestTemplate restTemplate = new RestTemplate();
        Map<String, Object> map = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=forecast24hours", Map.class, cityID);
        List<Map<String, Object>> mapList = new ArrayList<>();
        if (!map.isEmpty()) {
            if ((Collection<? extends Map<String, Object>>) ((Map) map.get("data")).get("hourly") != null) {
                mapList.addAll((Collection<? extends Map<String, Object>>) ((Map) map.get("data")).get("hourly"));
            }
        }
        return mapList;
    }
 
}