package com.moral.service.impl;
|
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
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.RealTimeWeatherMapper;
|
import com.moral.service.RealTimeWeatherService;
|
import com.moral.util.Dom4jUtils;
|
|
@Service
|
public class RealTimeWeatherServiceImpl implements RealTimeWeatherService {
|
|
@Resource
|
private RealTimeWeatherMapper realTimeWeatherMapper;
|
|
@Resource
|
private MonitorPointMapper monitorPointMapper;
|
|
@Resource
|
private AreaMapper areaMapper;
|
|
@Resource
|
private CityMapper cityMapper;
|
|
@Resource
|
private ProvinceMapper provinceMapper;
|
|
@Override
|
public void insertRealTimeWeather() throws ParseException {
|
RestTemplate restTemplate = new RestTemplate();
|
List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList();
|
List<Map<String, Object>> list = new ArrayList<>();
|
Set<Map<String, Object>> hashSet = new HashSet<>();
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
for (MonitorPoint monitorPoint : monitorPointList) {
|
Map<String, Object> hashMap1 = new HashMap<>();
|
Map<String, Object> hashMap = new HashMap<>();
|
Integer areaCode = monitorPoint.getAreaCode();
|
Integer cityCode = monitorPoint.getCityCode();
|
Integer provinceCode = monitorPoint.getProvinceCode();
|
String parentName = "";
|
String name1 = "";
|
if (areaCode != null) {
|
Area area = areaMapper.getAreaByAreaCode(areaCode);
|
City city = cityMapper.getCityByCityCode(cityCode);
|
name1 = area.getAreaName();
|
parentName = city.getCityName();
|
if ("市辖区".equals(name1)) {
|
name1 = parentName;
|
parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
|
}
|
} else {
|
City city = cityMapper.getCityByCityCode(cityCode);
|
name1 = city.getCityName();
|
parentName = provinceMapper.getProvinceByProvinceCode(provinceCode).getProvinceName();
|
}
|
hashMap.put("name1", name1);
|
hashMap.put("parentName", parentName);
|
hashSet.add(hashMap);
|
hashMap1.put("monitorPointId", monitorPoint.getId());
|
hashMap1.put("name", name1);
|
resultList.add(hashMap1);
|
}
|
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 (name1.equals(name2)) {
|
cityID = element.element("Fid").getText();
|
break;
|
}
|
if (name2.endsWith(name1)) {
|
if (name2.startsWith(parentName)) {
|
cityID = element.element("Fid").getText();
|
break;
|
}
|
}
|
}
|
Map<String, Object> map1 = restTemplate.getForObject("http://sapi.7drlb.com/api/mj?cityID={1}&apiKey=condition", Map.class, cityID);
|
Map<String, Object> dateMap = (Map<String, Object>) map1.get("data");
|
Map<String, Object> conditionMap = (Map<String, Object>) dateMap.get("condition");
|
conditionMap.put("name", name1);
|
list.add(conditionMap);
|
}
|
List<Map<String, Object>> insertList = new ArrayList<>();
|
for (Map<String, Object> resultMap : resultList) {
|
Map<String, Object> map = new HashMap<>();
|
String monitorPointId = resultMap.get("monitorPointId").toString();
|
map.put("monitorPointId", Integer.valueOf(monitorPointId));
|
for (Map<String, Object> listMap : list) {
|
if (resultMap.get("name").equals(listMap.get("name"))) {
|
String json = JSONObject.toJSONString(listMap);
|
String[] strings = listMap.get("updatetime").toString().split(" ");
|
String time = strings[0] + " " + strings[1].split(":")[0] + ":00:00";
|
Date date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(time);
|
map.put("json", json);
|
map.put("time", date);
|
}
|
}
|
insertList.add(map);
|
}
|
|
for (Map<String, Object> map : insertList) {
|
String json = map.get("json").toString();
|
Map<String, Object> parse = (Map<String, Object>) JSONObject.parse(json);
|
parse.remove("name");
|
parse.remove("icon");
|
parse.remove("conditionId");
|
parse.remove("tips");
|
map.put("json", JSONObject.toJSONString(parse));
|
}
|
realTimeWeatherMapper.insertRealTimeWeather(insertList);
|
}
|
}
|