package com.moral.service.impl;
|
|
import java.math.BigDecimal;
|
import java.text.ParseException;
|
import java.text.SimpleDateFormat;
|
import java.util.ArrayList;
|
import java.util.Calendar;
|
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.entity.Province;
|
import com.moral.mapper.AreaMapper;
|
import com.moral.mapper.CityMapper;
|
import com.moral.mapper.MonitorPointMapper;
|
import com.moral.mapper.ProvinceMapper;
|
import com.moral.mapper.RealWeatherDailyMapper;
|
import com.moral.mapper.WeatherMapper;
|
import com.moral.service.RealWeatherDailyService;
|
import com.moral.util.Dom4jUtils;
|
|
@Service
|
public class RealWeatherDailyImpl implements RealWeatherDailyService {
|
|
@Resource
|
private MonitorPointMapper monitorPointMapper;
|
|
@Resource
|
private AreaMapper areaMapper;
|
|
@Resource
|
private CityMapper cityMapper;
|
|
@Resource
|
private ProvinceMapper provinceMapper;
|
|
@Resource
|
private WeatherMapper weatherMapper;
|
|
@Resource
|
private RealWeatherDailyMapper realWeatherDailyMapper;
|
|
@Override
|
public int insertRealWeatherDaily() throws ParseException {
|
Calendar c = Calendar.getInstance();
|
Date today = c.getTime();
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
today = sdf.parse(sdf.format(today));
|
c.add(Calendar.DAY_OF_MONTH, -1);
|
Date yesterday = c.getTime();
|
c.add(Calendar.DAY_OF_MONTH, -1);
|
Date beforeDay = c.getTime();
|
//明天日期
|
String yesterdayString = sdf.format(yesterday);
|
yesterday = sdf.parse(yesterdayString);
|
RestTemplate restTemplate = new RestTemplate();
|
List<MonitorPoint> monitorPointList = monitorPointMapper.getMonitorPointList();
|
Set<Map<String, Object>> hashSet = new HashSet<>();
|
for (MonitorPoint monitorPoint : monitorPointList) {
|
Map<String, Object> hashMap = new HashMap<>();
|
Integer areaCode = monitorPoint.getAreaCode();
|
Integer cityCode = monitorPoint.getCityCode();
|
Integer provinceCode = monitorPoint.getProvinceCode();
|
String parentName = "";
|
String name1 = "";
|
Integer code = 0;
|
Integer parentCode = 0;
|
if (areaCode != null) {
|
Area area = areaMapper.getAreaByAreaCode(areaCode);
|
City city = cityMapper.getCityByCityCode(cityCode);
|
name1 = area.getAreaName();
|
parentName = city.getCityName();
|
parentCode = city.getCityCode();
|
code = areaCode;
|
if ("市辖区".equals(name1)) {
|
name1 = parentName;
|
Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
|
parentName = province.getProvinceName();
|
parentCode = province.getProvinceCode();
|
code = cityCode;
|
}
|
} else {
|
City city = cityMapper.getCityByCityCode(cityCode);
|
name1 = city.getCityName();
|
Province province = provinceMapper.getProvinceByProvinceCode(provinceCode);
|
parentName = province.getProvinceName();
|
code = cityCode;
|
parentCode = province.getProvinceCode();
|
if ("市辖区".equals(name1)) {
|
name1 = parentName;
|
parentName = "";
|
code = provinceCode;
|
}
|
}
|
hashMap.put("name1", name1);
|
hashMap.put("parentName", parentName);
|
hashMap.put("cityCode", code);
|
hashMap.put("parentCode", parentCode);
|
hashSet.add(hashMap);
|
}
|
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 (name2.equals("大丰市")) {
|
name2 = "大丰区";
|
}
|
if (name1.equals(name2)) {
|
cityID = element.element("Fweathercn").getText();
|
map.put("cityID", cityID);
|
break;
|
}
|
if (name2.endsWith(name1)) {
|
if (name2.startsWith(parentName)) {
|
cityID = element.element("Fweathercn").getText();
|
map.put("cityID", cityID);
|
break;
|
}
|
}
|
}
|
}
|
|
int count = 0;
|
List<Map<String, Object>> resultList = new ArrayList<>();
|
String date = yesterdayString.replace("-", "");
|
for (Map<String, Object> map : hashSet) {
|
System.out.println(map);
|
map.put("start", yesterday);
|
map.put("end", today);
|
map.put("typeFormat", "%Y-%m-%d %H:%i:%s");
|
Map<String, Object> resultMap = new HashMap<>();
|
resultMap.put("cityCode", map.get("cityCode").toString());
|
resultMap.put("time", yesterday);
|
String id = map.get("cityID").toString();
|
Map<String, Object> dataMap = restTemplate.getForObject("https://datasetapi.heweather.net/v7/historical/weather?key=da05c6c4852d4f7aa3364a9236ee9e26&gzip=n&location={1}&date={2}", Map.class, id, date);
|
if (!dataMap.get("code").equals("200")) {
|
return count;
|
}
|
String json = JSONObject.toJSONString(dataMap);
|
dataMap = (Map<String, Object>) JSONObject.parse(json);
|
String string = dataMap.get("weatherDaily").toString();
|
Map<String, Object> dailyMap = (Map<String, Object>) JSONObject.parse(string);
|
//温度平均值
|
dailyMap.remove("date");
|
Map<String, Object> dayData = weatherMapper.getDayData(map);
|
for (String key : dayData.keySet()) {
|
if (!key.equals("precip")) {
|
dayData.put(key, String.valueOf(Math.round(Double.valueOf(dayData.get(key).toString()))));
|
}
|
}
|
System.out.println(dayData);
|
Double windSpeed = Double.valueOf(dayData.get("windSpeed").toString());
|
windSpeed = new BigDecimal(windSpeed).setScale(1, BigDecimal.ROUND_HALF_UP).doubleValue();
|
//风速km/h转m/s,进行风级判断
|
windSpeed = windSpeed * 1000 / 3600;
|
int windScale = 0;
|
if (windSpeed >= 0.0 && windSpeed <= 0.2) {
|
windScale = 0;
|
} else if (windSpeed >= 0.3 && windSpeed <= 1.5) {
|
windScale = 1;
|
} else if (windSpeed >= 1.6 && windSpeed <= 3.3) {
|
windScale = 2;
|
} else if (windSpeed >= 3.4 && windSpeed <= 5.4) {
|
windScale = 3;
|
} else if (windSpeed >= 5.5 && windSpeed <= 7.9) {
|
windScale = 4;
|
} else if (windSpeed >= 8.0 && windSpeed <= 10.7) {
|
windScale = 5;
|
} else if (windSpeed >= 10.8 && windSpeed <= 13.8) {
|
windScale = 6;
|
} else if (windSpeed >= 13.9 && windSpeed <= 17.1) {
|
windScale = 7;
|
} else if (windSpeed >= 17.2 && windSpeed <= 20.7) {
|
windScale = 8;
|
}
|
dayData.put("windScale", windScale);
|
//相比昨日气温
|
map.put("time", beforeDay);
|
Integer yesterdayTemp = Integer.valueOf(dayData.get("tempAvg").toString());
|
System.out.println(map);
|
Map<String, Object> temp = realWeatherDailyMapper.getTemp(map);
|
System.out.println(temp);
|
if (temp != null) {
|
Integer beforeYesterdayTemp = Integer.valueOf(temp.get("beforeYesterdayTemp").toString());
|
dayData.put("tempChange", String.valueOf(yesterdayTemp - beforeYesterdayTemp));
|
}
|
|
//最低温,最高温时间点
|
Map<String, Object> timeMaxTemp = weatherMapper.getTimeMaxTemp(map);
|
String hour1 = timeMaxTemp.get("time").toString().substring(11, 13);
|
System.out.println(hour1);
|
dayData.put("maxTempTime", hour1);
|
|
Map<String, Object> timeMinTemp = weatherMapper.getTimeMinTemp(map);
|
String hour2 = timeMinTemp.get("time").toString().substring(11, 13);
|
dayData.put("minTempTime", hour2);
|
System.out.println(hour2);
|
|
dailyMap.putAll(dayData);
|
resultMap.put("json", JSONObject.toJSONString(dailyMap));
|
resultList.add(resultMap);
|
}
|
return realWeatherDailyMapper.insertRealWeatherDaily(null);
|
}
|
}
|