| | |
| | | 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=1102&apiKey=aqi", Map.class);
|
| | | 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=1102&apiKey=condition", Map.class);
|
| | | 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;
|
| | | }
|