| | |
| | | package com.moral.service.impl;
|
| | |
|
| | | import java.text.ParseException;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Calendar;
|
| | | import java.util.Collection;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
|
| | |
| | |
|
| | | import com.moral.common.util.Dom4jUtils;
|
| | | import com.moral.common.util.ValidateUtil;
|
| | | import com.moral.entity.Area;
|
| | | import com.moral.entity.AreaNames;
|
| | | import com.moral.entity.City;
|
| | | import com.moral.entity.MonitorPoint;
|
| | | import com.moral.entity.Organization;
|
| | | import com.moral.mapper.AreaMapper;
|
| | | import com.moral.mapper.CityMapper;
|
| | | import com.moral.mapper.HangzhouAqiMapper;
|
| | | import com.moral.mapper.MonitorPointMapper;
|
| | | import com.moral.mapper.ProvinceMapper;
|
| | | 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;
|
| | | }
|
| | | |
| | | @Override
|
| | | public Map<String, Object> get15DayWeatherDataByRegion(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);
|
| | | List<Map<String, Object>> result = new ArrayList<>();
|
| | | map = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=forecast15days", Map.class,cityID);
|
| | | result.addAll((Collection<? extends Map<String, Object>>) ((Map)map.get("data")).get("forecast"));
|
| | | for (Map<String, Object> map2 : result) {
|
| | | Map<String, Object> returnMap = new HashMap<>();
|
| | | String predictDate = map2.get("predictDate").toString();
|
| | | String time = parameters.get("time").toString();
|
| | | if (predictDate.equals(time)) {
|
| | | returnMap = map2;
|
| | | return returnMap;
|
| | | }
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | @Resource
|
| | | private OrganizationService organizationService;
|
| | |
|
| | | @Resource
|
| | | private HangzhouAqiMapper hangzhouAqiMapper;
|
| | |
|
| | | @Resource
|
| | | private MonitorPointMapper monitorPointMapper;
|
| | |
|
| | | @Resource
|
| | | private AreaMapper areaMapper;
|
| | |
|
| | | @Resource
|
| | | private CityMapper cityMapper;
|
| | |
|
| | | @Resource
|
| | | private ProvinceMapper provinceMapper;
|
| | |
|
| | | @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;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public Map<String, Object> get15DayWeatherDataByRegion(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);
|
| | | List<Map<String, Object>> result = new ArrayList<>();
|
| | | map = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=forecast15days", Map.class, cityID);
|
| | | result.addAll((Collection<? extends Map<String, Object>>) ((Map) map.get("data")).get("forecast"));
|
| | | for (Map<String, Object> map2 : result) {
|
| | | Map<String, Object> returnMap = new HashMap<>();
|
| | | String predictDate = map2.get("predictDate").toString();
|
| | | String time = parameters.get("time").toString();
|
| | | if (predictDate.equals(time)) {
|
| | | returnMap = map2;
|
| | | return returnMap;
|
| | | }
|
| | | }
|
| | | return null;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getNextDayWeatherDataByRegion(Map<String, Object> parameters) throws ParseException {
|
| | | String monitorPointId = parameters.get("monitorPointId").toString();
|
| | | MonitorPoint monitorPoint = monitorPointMapper.selectByPrimaryKey(Integer.valueOf(monitorPointId));
|
| | | Integer cityCode = monitorPoint.getCityCode();
|
| | | Integer areaCode = monitorPoint.getAreaCode();
|
| | | String name1 = "";
|
| | | String parentName = "";
|
| | | if (areaCode != null) {
|
| | | Area area = areaMapper.selectByPrimaryKey(areaCode);
|
| | | name1 = area.getAreaName();
|
| | | City city = cityMapper.selectByPrimaryKey(area.getCityCode());
|
| | | parentName = city.getCityName();
|
| | | if ("市辖区".equals(area.getAreaName())) {
|
| | | name1 = parentName;
|
| | | parentName = provinceMapper.selectByPrimaryKey(city.getProvinceCode()).getProvinceName();
|
| | | }
|
| | | } else {
|
| | | City city = cityMapper.selectByPrimaryKey(cityCode);
|
| | | name1 = city.getCityName();
|
| | | parentName = provinceMapper.selectByPrimaryKey(city.getProvinceCode()).getProvinceName();
|
| | | }
|
| | | List<Element> elements = Dom4jUtils.readDocument();
|
| | | String cityID = "101190404";
|
| | | for (Element element : elements) {
|
| | | String name2 = element.element("name").getText();
|
| | | if (name1.equals(name2)) {
|
| | | cityID = element.element("Fweathercn").getText();
|
| | | break;
|
| | | }
|
| | | if (name2.endsWith(name1)) {
|
| | | if (name2.startsWith(parentName)) {
|
| | | cityID = element.element("Fweathercn").getText();
|
| | | break;
|
| | | }
|
| | | }
|
| | | }
|
| | | RestTemplate restTemplate = new RestTemplate();
|
| | | Calendar c = Calendar.getInstance();
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
| | | SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH");
|
| | | String now = sdf.format(c.getTime());
|
| | | String selectTime = parameters.get("time").toString();
|
| | | Date t1 = sdf.parse(selectTime);
|
| | | Date t2 = sdf.parse(now);
|
| | | List<Map<String, Object>> resultList = new ArrayList<>();
|
| | | if (t1.getTime() > t2.getTime()) {
|
| | | parameters.put("now", now);
|
| | | Map<String, Object> map = restTemplate.getForObject("https://yiketianqi.com/api?version=v9&appid=68884517&appsecret=St9sFnvi&cityid={1}", Map.class, cityID);
|
| | | ArrayList<Map<String, Object>> dataList = (ArrayList<Map<String, Object>>) map.get("data");
|
| | | ArrayList<Map<String, Object>> arrayList = new ArrayList<>();
|
| | | String start = parameters.get("now").toString();
|
| | | String end = parameters.get("time").toString();
|
| | | for (Map<String, Object> dataMap : dataList) {
|
| | | String date = dataMap.get("date").toString();
|
| | | ArrayList<Map<String, Object>> list = (ArrayList<Map<String, Object>>) dataMap.get("hours");
|
| | | if (date.equals(start)) {
|
| | | for (int i = list.size() - 7; i < list.size(); i++) {
|
| | | Map<String, Object> resultMap = list.get(i);
|
| | | arrayList.add(resultMap);
|
| | | }
|
| | | }
|
| | | if (date.equals(end)) {
|
| | | for (int i = 0; i < 17; i++) {
|
| | | Map<String, Object> resultMap = list.get(i);
|
| | | arrayList.add(resultMap);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | for (Map<String, Object> resultMap : arrayList) {
|
| | | Map<String, Object> hashMap = new LinkedHashMap<>();
|
| | | List<String> values = new ArrayList<>();
|
| | | String hour = resultMap.get("hours").toString().replaceAll("[\u4e00-\u9fa5]+", "");
|
| | | values.add(resultMap.get("tem").toString());
|
| | | values.add("");
|
| | | hashMap.put("values", values);
|
| | | hashMap.put("time", Integer.valueOf(hour));
|
| | | if (Integer.valueOf(hour) == 0) {
|
| | | hashMap.put("time", 24);
|
| | | }
|
| | | resultList.add(hashMap);
|
| | | }
|
| | | } else {
|
| | | parameters.put("start", selectTime);
|
| | | c.setTime(sdf.parse(selectTime));
|
| | | c.add(Calendar.DAY_OF_MONTH, 1);
|
| | | c.add(Calendar.HOUR_OF_DAY, 1);
|
| | | String end = sdf1.format(c.getTime());
|
| | | parameters.put("end", end);
|
| | | parameters.put("sensors", "O3C");
|
| | | parameters.put("sensors1", "O3");
|
| | | parameters.put("sensors2", parameters.get("sensors1").toString());
|
| | | List<Map<String, Object>> areaList = new ArrayList<>();
|
| | | if (areaCode == null) {
|
| | | parameters.put("areaCode", cityCode);
|
| | | areaList = hangzhouAqiMapper.getAreaAvgDataByAreaCode(parameters);
|
| | | } else {
|
| | | parameters.put("areaCode", areaCode);
|
| | | areaList = hangzhouAqiMapper.getAreaAvgDataByAreaCode(parameters);
|
| | | if (areaList.size() == 0) {
|
| | | parameters.put("areaCode", cityCode);
|
| | | areaList = hangzhouAqiMapper.getAreaAvgDataByAreaCode(parameters);
|
| | | }
|
| | | }
|
| | | areaList.remove(0);
|
| | | for (Map<String, Object> areaMap : areaList) {
|
| | | Map<String, Object> hashMap = new LinkedHashMap<>();
|
| | | List<String> values = new ArrayList<>();
|
| | | values.add("");
|
| | | values.add(areaMap.get("O3").toString());
|
| | | hashMap.put("values", values);
|
| | | String hour = areaMap.get("time").toString().split(" ")[1];
|
| | | hashMap.put("time", Integer.valueOf(hour));
|
| | | if (Integer.valueOf(hour) == 0) {
|
| | | hashMap.put("time", 24);
|
| | | }
|
| | | resultList.add(hashMap);
|
| | | }
|
| | | }
|
| | | return resultList;
|
| | | }
|
| | | }
|