xufenglei
2018-08-28 866520f60e83c8632a712de22d5c40df0af2c2fa
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
package com.moral.service.impl;
 
import java.util.List;
import java.util.Map;
 
import javax.annotation.Resource;
 
import org.dom4j.Element;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.web.client.RestTemplate;
 
import com.moral.common.util.Dom4jUtils;
import com.moral.common.util.ValidateUtil;
import com.moral.entity.AreaNames;
import com.moral.entity.Organization;
import com.moral.service.OrganizationService;
import com.moral.service.WeatherService;
 
@Service
public class WeatherServiceImpl implements WeatherService {
    
    @Resource
    private  OrganizationService organizationService;
    
    @Override
    public Map<String, Object> getWeatherDataByRegion(Map<String, Object> parameters) {
        Object organizationId = parameters.get("organizationId");
        ValidateUtil.notNull(organizationId, "param.is.null");
        String areaName = "昆山市";
        String parentName = "";
        Organization organization = organizationService.getOrganizationById(Integer.valueOf(parameters.get("organizationId").toString()));
        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=aqi", Map.class,cityID);
        Map<String, Object> result = (Map<String, Object>) ((Map)map.get("data")).get("aqi");
        map =  restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=condition", Map.class,cityID);
        result.putAll((Map<? extends String, ? extends Object>) ((Map)map.get("data")).get("condition"));
        return result;
    }
 
}