| | |
| | | package com.moral.service.impl;
|
| | |
|
| | | import java.math.BigDecimal;
|
| | | import java.text.DecimalFormat;
|
| | | import java.text.ParseException;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.ArrayList;
|
| | | import java.util.Calendar;
|
| | | import java.util.Collection;
|
| | | import java.util.Collections;
|
| | | import java.util.Date;
|
| | | import java.util.HashMap;
|
| | | import java.util.LinkedHashMap;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | |
|
| | | import javax.annotation.Resource;
|
| | |
|
| | | import org.apache.jasper.compiler.JspUtil;
|
| | | import org.dom4j.Element;
|
| | | import org.springframework.stereotype.Service;
|
| | | import org.springframework.util.ObjectUtils;
|
| | | import org.springframework.web.client.RestTemplate;
|
| | |
|
| | | import com.alibaba.fastjson.JSONObject;
|
| | | 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.Device;
|
| | | import com.moral.entity.MonitorPoint;
|
| | | import com.moral.entity.Organization;
|
| | | import com.moral.entity.Province;
|
| | | import com.moral.mapper.AreaMapper;
|
| | | import com.moral.mapper.CityMapper;
|
| | | import com.moral.mapper.DeviceMapper;
|
| | | import com.moral.mapper.ForecastWeatherMapper;
|
| | | import com.moral.mapper.HangzhouAqiMapper;
|
| | | import com.moral.mapper.HistoryHourlyMapper;
|
| | | import com.moral.mapper.MonitorPointMapper;
|
| | | import com.moral.mapper.ProvinceMapper;
|
| | | import com.moral.mapper.RealWeatherMapper;
|
| | | import com.moral.mapper.ShAreaMapper;
|
| | | import com.moral.service.OrganizationService;
|
| | | import com.moral.service.WeatherService;
|
| | |
|
| | | @Service
|
| | | public class WeatherServiceImpl implements WeatherService {
|
| | | |
| | | @Override
|
| | | public Map<String, Object> getWeatherDataByRegion(Map<String, Object> parameters) {
|
| | | 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> 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);
|
| | | result.putAll((Map<? extends String, ? extends Object>) ((Map)map.get("data")).get("condition"));
|
| | | return result;
|
| | | }
|
| | |
|
| | | @Resource
|
| | | private OrganizationService organizationService;
|
| | |
|
| | | @Resource
|
| | | private HangzhouAqiMapper hangzhouAqiMapper;
|
| | |
|
| | | @Resource
|
| | | private MonitorPointMapper monitorPointMapper;
|
| | |
|
| | | @Resource
|
| | | private ForecastWeatherMapper forecastWeatherMapper;
|
| | |
|
| | | @Resource
|
| | | private AreaMapper areaMapper;
|
| | |
|
| | | @Resource
|
| | | private CityMapper cityMapper;
|
| | |
|
| | | @Resource
|
| | | private ProvinceMapper provinceMapper;
|
| | |
|
| | | @Resource
|
| | | private RealWeatherMapper realWeatherMapper;
|
| | |
|
| | | @Resource
|
| | | private DeviceMapper deviceMapper;
|
| | |
|
| | | @Resource
|
| | | private HistoryHourlyMapper historyHourlyMapper;
|
| | |
|
| | | @Resource
|
| | | private ShAreaMapper shAreaMapper;
|
| | |
|
| | | @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>> getForecastAndReal(Map<String, Object> parameters) throws ParseException {
|
| | | ArrayList<String> sensorKeys = (ArrayList<String>) parameters.get("sensorKeys");
|
| | | List<Map<String, Object>> resultList = new ArrayList<>();
|
| | | for (int i = 0; i < 24; i++) {
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | List<String> arrayList = new ArrayList<>();
|
| | | map.put("values", arrayList);
|
| | | map.put("time", i + 1);
|
| | | resultList.add(map);
|
| | | }
|
| | |
|
| | | for (String sensorKey : sensorKeys) {
|
| | | parameters.put("sensorKey", sensorKey);
|
| | | List<Map<String, Object>> list = forecastAndReal(parameters);
|
| | | for (Map<String, Object> map : list) {
|
| | | Object time = map.get("time");
|
| | | ArrayList<String> values = (ArrayList<String>) map.get("values");
|
| | | for (Map<String, Object> resultMap : resultList) {
|
| | | if (time.equals(resultMap.get("time"))) {
|
| | | ArrayList<String> resultValues = (ArrayList<String>) resultMap.get("values");
|
| | | resultValues.addAll(values);
|
| | | resultMap.put("values", resultValues);
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | return resultList;
|
| | | }
|
| | |
|
| | | public List<Map<String, Object>> forecastAndReal(Map<String, Object> parameters) throws ParseException {
|
| | | Object sensorKey = parameters.remove("sensorKey");
|
| | | String sensor1;
|
| | | String sensor2;
|
| | | if ("e1".equals(sensorKey)) {
|
| | | sensor1 = "PM25C";
|
| | | sensor2 = "PM2_5";
|
| | | } else if ("e2".equals(sensorKey)) {
|
| | | sensor1 = "PM10C";
|
| | | sensor2 = "PM10";
|
| | | } else {
|
| | | sensor1 = "O3C";
|
| | | sensor2 = "O3";
|
| | | }
|
| | | parameters.put("sensor1", sensor1);
|
| | | parameters.put("sensor2", sensor2);
|
| | | parameters.put("typeFormat", "%Y-%m-%d %H:%i:%s");
|
| | | String monitorPointId = parameters.get("monitorPointId").toString();
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | String time = parameters.get("time").toString() + " 00:00:00";
|
| | | Calendar c = Calendar.getInstance();
|
| | | c.setTime(sdf.parse(time));
|
| | | c.add(Calendar.HOUR_OF_DAY, 1);
|
| | | time = sdf.format(c.getTime());
|
| | | c.add(Calendar.DAY_OF_MONTH, 1);
|
| | | String nextDay = sdf.format(c.getTime());
|
| | | parameters.put("start", sdf.parse(time));
|
| | | parameters.put("end", sdf.parse(nextDay));
|
| | | MonitorPoint monitorPoint = monitorPointMapper.selectByPrimaryKey(Integer.valueOf(monitorPointId));
|
| | | int cityCode;
|
| | | int parentCode;
|
| | | if (monitorPoint.getAreaCode() == null) {
|
| | | cityCode = monitorPoint.getCityCode();
|
| | | parentCode = monitorPoint.getProvinceCode();
|
| | | } else {
|
| | | cityCode = monitorPoint.getAreaCode();
|
| | | parentCode = monitorPoint.getCityCode();
|
| | | }
|
| | | parameters.put("cityCode", cityCode);
|
| | | //实测O3
|
| | | List<Map<String, Object>> aqiList = hangzhouAqiMapper.getAqiDataByAreaCode(parameters);
|
| | | String timeUnits = time.substring(0, 4);
|
| | | if (aqiList.size() == 0) {
|
| | | parameters.put("timeUnits", timeUnits);
|
| | | aqiList = hangzhouAqiMapper.getHistoryO3(parameters);
|
| | | if (aqiList.size() == 0) {
|
| | | parameters.put("cityCode", parentCode);
|
| | | aqiList = hangzhouAqiMapper.getAqiDataByAreaCode(parameters);
|
| | | if (aqiList.size() == 0) {
|
| | | aqiList = hangzhouAqiMapper.getHistoryO3(parameters);
|
| | | }
|
| | | }
|
| | | }
|
| | | //预测O3
|
| | | parameters.put("cityCode", cityCode);
|
| | | List<Map<String, Object>> forecastList = forecastWeatherMapper.getForecastHour(parameters);
|
| | | if (forecastList.size() == 0) {
|
| | | parameters.put("cityCode", parentCode);
|
| | | forecastList = forecastWeatherMapper.getForecastHour(parameters);
|
| | | }
|
| | | for (Map<String, Object> map : aqiList) {
|
| | | int s = (int) Double.parseDouble(map.get(sensor2).toString().replace("\"", ""));
|
| | | map.put(sensor2, String.valueOf(s));
|
| | | }
|
| | |
|
| | | List<Map<String, Object>> resultList = new ArrayList<>();
|
| | | if (forecastList.size() == 0 && aqiList.size() == 0) {
|
| | | return new ArrayList<>();
|
| | | } else if (forecastList.size() == 0) {
|
| | | for (Map<String, Object> aqiMap : aqiList) {
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | List<String> values = new ArrayList<>();
|
| | | String hour = aqiMap.get("time").toString().split(" ")[1].split(":")[0];
|
| | | if (hour.equals("00")) {
|
| | | hour = "24";
|
| | | }
|
| | | map.put("time", Integer.valueOf(hour));
|
| | | values.add("");
|
| | | values.add(aqiMap.get(sensor2).toString());
|
| | | map.put("values", values);
|
| | | resultList.add(map);
|
| | | }
|
| | |
|
| | | } else if (aqiList.size() == 0) {
|
| | | for (Map<String, Object> forecastMap : forecastList) {
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | List<String> values = new ArrayList<>();
|
| | | String hour = forecastMap.get("time").toString().split(" ")[1].split(":")[0];
|
| | | if (hour.equals("00")) {
|
| | | hour = "24";
|
| | | }
|
| | | map.put("time", Integer.valueOf(hour));
|
| | | if (forecastMap.get(sensor2) != null) {
|
| | | values.add(forecastMap.get(sensor2).toString().replace("\"", ""));
|
| | | } else {
|
| | | values.add("");
|
| | | }
|
| | | values.add("");
|
| | | map.put("values", values);
|
| | | resultList.add(map);
|
| | | }
|
| | | } else {
|
| | | for (Map<String, Object> forecastMap : forecastList) {
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | List<String> values = new ArrayList<>();
|
| | | String hour = forecastMap.get("time").toString().split(" ")[1].split(":")[0];
|
| | | if (hour.equals("00")) {
|
| | | hour = "24";
|
| | | }
|
| | | map.put("time", Integer.valueOf(hour));
|
| | | if (forecastMap.get(sensor2) != null) {
|
| | | values.add(forecastMap.get(sensor2).toString().replace("\"", ""));
|
| | | } else {
|
| | | values.add("");
|
| | | }
|
| | | for (Map<String, Object> aqiMap : aqiList) {
|
| | | if (forecastMap.get("time").equals(aqiMap.get("time"))) {
|
| | | values.add(aqiMap.get(sensor2).toString());
|
| | | }
|
| | | }
|
| | | map.put("values", values);
|
| | | resultList.add(map);
|
| | | }
|
| | | }
|
| | | for (Map<String, Object> resultMap : resultList) {
|
| | | ArrayList<String> values = (ArrayList<String>) resultMap.get("values");
|
| | | if (values.size() == 1) {
|
| | | values.add("");
|
| | | }
|
| | | }
|
| | | return resultList;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public void updateForecastWeather(Map<String, Object> parameters) {
|
| | | List<Integer> list = new ArrayList<>();
|
| | | List<Map<String, Object>> resultList = new ArrayList<>();
|
| | | for (int i = 0; i < 48; i++) {
|
| | | list.add(i);
|
| | | resultList.add(null);
|
| | | }
|
| | | for (Integer s : list) {
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | for (String key : parameters.keySet()) {
|
| | | String replace = key.split("\\[")[0];
|
| | | if (replace.equals(s + "")) {
|
| | | String ss = key.split("\\[")[1].replace("]", "");
|
| | | map.put(ss, parameters.get(key).toString());
|
| | | }
|
| | | }
|
| | | resultList.set(s, map);
|
| | | }
|
| | | for (Map<String, Object> map : resultList) {
|
| | | if (map.get("type").equals("预测")) {
|
| | | String time = map.get("time").toString() + ":00";
|
| | | Map<String, Object> hashMap = new HashMap<>();
|
| | | hashMap.put("time", time);
|
| | | hashMap.put("cityCode", Integer.valueOf(map.get("code").toString()));
|
| | | Map<String, Object> jsonMap = new HashMap<>();
|
| | | for (String key : map.keySet()) {
|
| | | if (!key.equals("city") && !key.equals("time") && !key.equals("type") && !key.equals("fxTime") && !key.equals("code")) {
|
| | | String value = map.get(key).toString();
|
| | | jsonMap.put(key, value);
|
| | | }
|
| | | }
|
| | | hashMap.put("json", JSONObject.toJSONString(jsonMap));
|
| | | forecastWeatherMapper.updateForecastWeather(hashMap);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getWeatherDataByDay(Map<String, Object> parameters) throws ParseException {
|
| | | String selectTime = parameters.get("time").toString();
|
| | | String cityName = parameters.get("city").toString();
|
| | | Area area = areaMapper.getAreaByName(cityName);
|
| | | Integer code;
|
| | | String name;
|
| | | if (area == null) {
|
| | | City city = cityMapper.getCityByName(cityName);
|
| | | if (city == null) {
|
| | | Province province = provinceMapper.getProvinceByName(cityName);
|
| | | code = province.getProvinceCode();
|
| | | name = province.getProvinceName();
|
| | | } else {
|
| | | code = city.getCityCode();
|
| | | name = city.getCityName();
|
| | | }
|
| | | } else {
|
| | | code = area.getAreaCode();
|
| | | name = area.getAreaName();
|
| | | }
|
| | |
|
| | | parameters.put("cityCode", code);
|
| | | parameters.put("name", name);
|
| | | return null;
|
| | | }
|
| | |
|
| | | @Override
|
| | | public List<Map<String, Object>> getWeatherData(Map<String, Object> parameters) throws ParseException {
|
| | | parameters.put("typeFormat", "%Y-%m-%d %H:%i:%s");
|
| | | if (parameters.get("city") == null) {
|
| | | return new ArrayList<Map<String, Object>>();
|
| | | }
|
| | | String cityName = parameters.get("city").toString();
|
| | | Area area = areaMapper.getAreaByName(cityName);
|
| | | Integer code;
|
| | | String name;
|
| | | Integer parentCode;
|
| | | if (area == null) {
|
| | | City city = cityMapper.getCityByName(cityName);
|
| | | if (city == null) {
|
| | | Province province = provinceMapper.getProvinceByName(cityName);
|
| | | if (province == null) {
|
| | | return new ArrayList<>();
|
| | | }
|
| | | code = province.getProvinceCode();
|
| | | name = province.getProvinceName();
|
| | | parentCode = 0;
|
| | | } else {
|
| | | code = city.getCityCode();
|
| | | name = city.getCityName();
|
| | | parentCode = city.getProvinceCode();
|
| | | }
|
| | | } else {
|
| | | code = area.getAreaCode();
|
| | | name = area.getAreaName();
|
| | | parentCode = area.getCityCode();
|
| | | }
|
| | | parameters.put("cityCode", code);
|
| | | parameters.put("name", name);
|
| | | String string = parameters.get("time").toString();
|
| | | String year = string.substring(0, 4);
|
| | | String[] split = string.substring(5).split("-");
|
| | | String month = split[0];
|
| | | String day = split[1];
|
| | | if (split[0].length() < 2) {
|
| | | month = 0 + split[0];
|
| | | }
|
| | | if (split[1].length() < 2) {
|
| | | day = 0 + split[1];
|
| | | }
|
| | | String time = year + "-" + month + "-" + day;
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
| | | SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | Date now = new Date();
|
| | | Date selectTime = sdf1.parse(time + " 01:00:00");
|
| | | Calendar cal = Calendar.getInstance();
|
| | | cal.setTime(selectTime);
|
| | | cal.add(Calendar.DAY_OF_MONTH, 1);
|
| | | Date end = cal.getTime();
|
| | | cal.setTime(sdf.parse(time));
|
| | | cal.add(Calendar.DAY_OF_MONTH, -1);
|
| | | //获取前天时间,作为预测时的时间,提前一天预测
|
| | | String beforeDay = sdf.format(cal.getTime());
|
| | | parameters.put("start", selectTime);
|
| | | parameters.put("end", end);
|
| | | List<Map<String, Object>> forecastList = forecastWeatherMapper.getForecast(parameters);
|
| | |
|
| | | if (forecastList.size() == 0) {
|
| | | if (!String.valueOf(code).endsWith("0000")) {
|
| | | parameters.put("cityCode", parentCode);
|
| | | forecastList = forecastWeatherMapper.getForecast(parameters);
|
| | | } else {
|
| | | return new ArrayList<>();
|
| | | }
|
| | | }
|
| | | //添加预测时间,每天07,15点各预测一次
|
| | | for (Map<String, Object> forecastMap : forecastList) {
|
| | | forecastMap.put("type", "预测");
|
| | | forecastMap.put("city", name);
|
| | | if (sdf.parse(time).getTime() <= now.getTime()) {
|
| | | forecastMap.put("fxTime", (beforeDay + " 15:00").substring(5, 16));
|
| | | } else {
|
| | | String format = sdf1.format(now).split(" ")[0];
|
| | | Date date1 = sdf1.parse(format + " 07:00:00");
|
| | | Date date2 = sdf1.parse(format + " 15:00:00");
|
| | | if (now.getTime() > date2.getTime()) {
|
| | | forecastMap.put("fxTime", (beforeDay + " 15:00").substring(5, 16));
|
| | | } else if (now.getTime() > date1.getTime()) {
|
| | | forecastMap.put("fxTime", (beforeDay + " 07:00").substring(5, 16));
|
| | | }
|
| | | }
|
| | | Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(forecastMap.remove("json").toString());
|
| | | forecastMap.putAll(jsonMap);
|
| | | //预测中的过去6小时降水总量
|
| | | Map<String, Object> hashMap = new HashMap<>();
|
| | | hashMap.put("cityCode", parameters.get("cityCode").toString());
|
| | | Date endTime = sdf1.parse(forecastMap.get("time").toString());
|
| | | hashMap.put("end", endTime);
|
| | | cal.setTime(endTime);
|
| | | cal.add(Calendar.HOUR, -6);
|
| | | Date startTime = cal.getTime();
|
| | | hashMap.put("start", startTime);
|
| | | Map<String, Object> precip6HourMap = forecastWeatherMapper.getPrecip6Hour(hashMap);
|
| | | if (precip6HourMap != null) {
|
| | | forecastMap.put("precip6", precip6HourMap.get("precip6").toString());
|
| | | }
|
| | | //过去8小时O3平均值(预测)
|
| | | cal.setTime(endTime);
|
| | | cal.add(Calendar.HOUR, -8);
|
| | | startTime = cal.getTime();
|
| | | hashMap.put("start", startTime);
|
| | | Map<String, Object> O3Map = forecastWeatherMapper.getO38Hours(hashMap);
|
| | | if (O3Map != null) {
|
| | | forecastMap.put("O3C_8H", O3Map.get("O3_8H").toString());
|
| | | }
|
| | |
|
| | | }
|
| | |
|
| | | //获取实测aqi
|
| | | parameters.put("cityCode", code);
|
| | | //从hangzhou_aqi获取,没有再从history_aqi年分表获取
|
| | | int p = 0;
|
| | | String timeUnits1 = time.substring(0, 4);
|
| | | String timeUnits2 = sdf1.format(end).substring(0, 4);
|
| | | List<Map<String, Object>> aqiList = hangzhouAqiMapper.getAqi(parameters);
|
| | | if (aqiList.size() == 0) {
|
| | | p = 1;
|
| | | //1-23点数据必然是同年,00点可能是明年
|
| | | parameters.put("timeUnits", timeUnits1);
|
| | | List<Map<String, Object>> aqiList1 = hangzhouAqiMapper.getHistoryAqi(parameters);
|
| | | parameters.put("timeUnits", timeUnits2);
|
| | | List<Map<String, Object>> aqiList2 = hangzhouAqiMapper.getHistoryAqi(parameters);
|
| | | aqiList.addAll(aqiList1);
|
| | | aqiList.addAll(aqiList2);
|
| | | //获取上一级城市数据
|
| | | if (aqiList.size() == 0) {
|
| | | p = 0;
|
| | | parameters.put("cityCode", parentCode);
|
| | | aqiList = hangzhouAqiMapper.getAqi(parameters);
|
| | | if (aqiList.size() == 0) {
|
| | | p = 1;
|
| | | parameters.put("timeUnits", timeUnits1);
|
| | | aqiList1 = hangzhouAqiMapper.getHistoryAqi(parameters);
|
| | | parameters.put("timeUnits", timeUnits2);
|
| | | aqiList2 = hangzhouAqiMapper.getHistoryAqi(parameters);
|
| | | aqiList.addAll(aqiList1);
|
| | | aqiList.addAll(aqiList2);
|
| | | }
|
| | | }
|
| | | }
|
| | | DecimalFormat df = new DecimalFormat(".####");
|
| | | DecimalFormat df1 = new DecimalFormat("#.#");
|
| | | for (Map<String, Object> aqiMap : aqiList) {
|
| | | aqiMap.put("type", "实测");
|
| | | aqiMap.put("city", name);
|
| | | Map<String, Object> jsonMap = (Map<String, Object>) JSONObject.parse(aqiMap.remove("json").toString());
|
| | | for (String key : jsonMap.keySet()) {
|
| | | if ("NO2".equals(key) || "NO2C".equals(key) || "O3".equals(key) || "O3C".equals(key) ||
|
| | | "SO2".equals(key) || "SO2C".equals(key) || "PM2_5".equals(key) || "PM25C".equals(key) || "PM10".equals(key) || "PM10C".equals(key)) {
|
| | | long v = Math.round(Double.valueOf(jsonMap.get(key).toString()));
|
| | | jsonMap.put(key, String.valueOf(v));
|
| | | } else if ("COC".equals(key) || "CO".equals(key)) {
|
| | | String CO = df1.format(Double.valueOf(jsonMap.get(key).toString()));
|
| | | jsonMap.put(key, CO);
|
| | | } else if ("AQI".equals(key)) {
|
| | | long AQI = Math.round(Double.valueOf(jsonMap.get(key).toString()));
|
| | | jsonMap.put(key, String.valueOf(AQI));
|
| | | }
|
| | | }
|
| | | aqiMap.putAll(jsonMap);
|
| | |
|
| | | //计算O3_8H(实测)
|
| | | String s = aqiMap.get("time").toString();
|
| | | Date endTime = sdf1.parse(s);
|
| | | cal.setTime(endTime);
|
| | | cal.add(Calendar.HOUR, -8);
|
| | | Date startTime = cal.getTime();
|
| | | Map<String, Object> hashMap = new HashMap<>();
|
| | | hashMap.put("cityCode", parameters.get("cityCode").toString());
|
| | | hashMap.put("start", startTime);
|
| | | hashMap.put("end", endTime);
|
| | | Map<String, Object> O3Map;
|
| | | if (p == 0) {
|
| | | O3Map = hangzhouAqiMapper.getAvgO3EightHours(hashMap);
|
| | | } else {
|
| | | hashMap.put("time", endTime);
|
| | | hashMap.put("timeUnits", s.substring(0, 4));
|
| | | O3Map = hangzhouAqiMapper.getHistoryO3EightHours(hashMap);
|
| | | }
|
| | | aqiMap.put("O3C_8H", O3Map.get("O3_8H").toString().replace("\"", ""));
|
| | |
|
| | | }
|
| | | //光照,该城市下所有设备光照平均值以及tvoc
|
| | | Map<String, Object> params = new HashMap<>();
|
| | | if (String.valueOf(code).endsWith("0000")) {
|
| | | params.put("provinceCode", code);
|
| | | } else if (String.valueOf(code).endsWith("00")) {
|
| | | params.put("cityCode", code);
|
| | | } else {
|
| | | params.put("areaCode", code);
|
| | | }
|
| | | List<Integer> monitorPointIds = monitorPointMapper.getMonitorPointIds(params);
|
| | | List<String> macs = deviceMapper.getMacsByMonitorPointIds(monitorPointIds);
|
| | | List<Map<String, Object>> beamList = new ArrayList<>();
|
| | | List<Map<String, Object>> tvocList = new ArrayList<>();
|
| | | if (macs.size() != 0) {
|
| | | parameters.put("macs", macs);
|
| | | beamList = historyHourlyMapper.getBeamByMacs(parameters);
|
| | | tvocList = historyHourlyMapper.getTVOCByMacs(parameters);
|
| | | }
|
| | |
|
| | | //实测天气
|
| | | int x = 0;
|
| | | parameters.put("cityCode", code);
|
| | | List<Map<String, Object>> realList = realWeatherMapper.getRealWeather(parameters);
|
| | | if (realList.size() == 0) {
|
| | | //1-23点数据必然是同年,00点可能是明年
|
| | | x = 1;
|
| | | timeUnits1 = time.substring(0, 4);
|
| | | timeUnits2 = sdf1.format(end).substring(0, 4);
|
| | | parameters.put("timeUnits", timeUnits1);
|
| | | List<Map<String, Object>> realList1 = realWeatherMapper.getHistoryWeather(parameters);
|
| | | parameters.put("timeUnits", timeUnits2);
|
| | | List<Map<String, Object>> realList2 = realWeatherMapper.getHistoryWeather(parameters);
|
| | | realList.addAll(realList1);
|
| | | realList.addAll(realList2);
|
| | | //获取上一级城市数据
|
| | | if (realList.size() == 0) {
|
| | | x = 0;
|
| | | parameters.put("cityCode", parentCode);
|
| | | realList = realWeatherMapper.getRealWeather(parameters);
|
| | | if (realList.size() == 0) {
|
| | | x = 1;
|
| | | parameters.put("timeUnits", timeUnits1);
|
| | | realList1 = realWeatherMapper.getHistoryWeather(parameters);
|
| | | parameters.put("timeUnits", timeUnits2);
|
| | | realList2 = realWeatherMapper.getHistoryWeather(parameters);
|
| | | realList.addAll(realList1);
|
| | | realList.addAll(realList2);
|
| | | }
|
| | | }
|
| | | }
|
| | | for (Map<String, Object> realMap : realList) {
|
| | | Map<String, Object> jsonMap;
|
| | | if (x == 0) {
|
| | | jsonMap = (Map<String, Object>) JSONObject.parse(realMap.remove("json").toString());
|
| | | } else {
|
| | | jsonMap = (Map<String, Object>) JSONObject.parse(realMap.remove("value").toString());
|
| | | }
|
| | | realMap.putAll(jsonMap);
|
| | | realMap.put("city", name);
|
| | | realMap.put("type", "实测");
|
| | | }
|
| | |
|
| | | //TVOC,光照合并
|
| | | for (Map<String, Object> beamMap : beamList) {
|
| | | for (Map<String, Object> tvocMap : tvocList) {
|
| | | Object bTime = beamMap.get("time");
|
| | | Object Ttime = tvocMap.get("time");
|
| | | if (bTime.equals(Ttime)) {
|
| | | beamMap.putAll(tvocMap);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | List<Map<String, Object>> list = new ArrayList<>();
|
| | | for (int i = 0; i < 24; i++) {
|
| | | Map<String, Object> map = new HashMap<>();
|
| | | String tt = time + " " + (i + 1) + ":00:00";
|
| | | if ((i + 1) < 10) {
|
| | | tt = time + " 0" + (i + 1) + ":00:00";
|
| | | } else if (i == 23) {
|
| | | tt = sdf.format(end) + " 00:00:00";
|
| | | }
|
| | | map.put("time", tt);
|
| | | list.add(map);
|
| | | }
|
| | |
|
| | | for (Map<String, Object> map1 : list) {
|
| | | String time1 = map1.get("time").toString();
|
| | | for (Map<String, Object> map2 : beamList) {
|
| | | if (time1.equals(map2.get("time"))) {
|
| | | map1.putAll(map2);
|
| | | }
|
| | | }
|
| | |
|
| | | for (Map<String, Object> realMap : realList) {
|
| | | if (time1.equals(realMap.get("time"))) {
|
| | | map1.putAll(realMap);
|
| | | }
|
| | | }
|
| | |
|
| | | //过去6小时降水量(实测)
|
| | | Date endTime = sdf1.parse(time1);
|
| | | cal.setTime(endTime);
|
| | | cal.add(Calendar.HOUR, -6);
|
| | | Date startTime = cal.getTime();
|
| | | Map<String, Object> hashMap = new HashMap<>();
|
| | | hashMap.put("cityCode", code);
|
| | | hashMap.put("start", startTime);
|
| | | hashMap.put("end", endTime);
|
| | | Map<String, Object> precip6HourMap = realWeatherMapper.getPrecip6Hour(hashMap);
|
| | | if (precip6HourMap != null) {
|
| | | map1.put("precip6", precip6HourMap.get("precip6").toString());
|
| | | }
|
| | | }
|
| | |
|
| | | for (Map<String, Object> listMap : list) {
|
| | | listMap.put("type", "实测");
|
| | | listMap.put("city", name);
|
| | | String time1 = listMap.get("time").toString();
|
| | | for (Map<String, Object> aqiMap : aqiList) {
|
| | | if (time1.equals(aqiMap.get("time"))) {
|
| | | listMap.putAll(aqiMap);
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | List<Map<String, Object>> resultList = new ArrayList<>();
|
| | | if (forecastList.size() == 0) {
|
| | | cal.setTime(selectTime);
|
| | | for (long d = cal.getTimeInMillis(); d < end.getTime();
|
| | | cal.set(Calendar.HOUR_OF_DAY, cal.get(Calendar.HOUR_OF_DAY) + 1),
|
| | | d = cal.getTimeInMillis()) {
|
| | | Map<String, Object> hashMap = new LinkedHashMap<>();
|
| | | hashMap.put("type", "预测");
|
| | | hashMap.put("city", name);
|
| | | hashMap.put("time", sdf1.format(d));
|
| | | forecastList.add(hashMap);
|
| | | }
|
| | | }
|
| | |
|
| | | for (Map<String, Object> foreMap : forecastList) {
|
| | | for (Map<String, Object> realMap : list) {
|
| | | if (foreMap.get("time").equals(realMap.get("time"))) {
|
| | | resultList.add(foreMap);
|
| | | resultList.add(realMap);
|
| | | }
|
| | | }
|
| | | }
|
| | | for (int i = list.size(); i < forecastList.size(); i++) {
|
| | | Map<String, Object> map = forecastList.get(i);
|
| | | resultList.add(map);
|
| | | Map<String, Object> hashMap = new HashMap<>();
|
| | | hashMap.put("time", map.get("time").toString());
|
| | | hashMap.put("type", "实测");
|
| | | hashMap.put("city", name);
|
| | | resultList.add(hashMap);
|
| | | }
|
| | |
|
| | | for (Map<String, Object> map : resultList) {
|
| | | String date = map.get("time").toString().substring(0, 16);
|
| | | map.put("time", date);
|
| | | if (map.get("precip") != null) {
|
| | | if ("0.0".equals(map.get("precip"))) {
|
| | | map.put("precip", "");
|
| | | }
|
| | | }
|
| | | if (map.get("precip6") != null) {
|
| | | if ("0.0".equals(map.get("precip6"))) {
|
| | | map.put("precip6", "");
|
| | | }
|
| | | }
|
| | | if ((!map.containsKey("SO2C")) && map.containsKey("SO2")) {
|
| | | map.put("SO2C", map.get("SO2").toString().replace("\\.", ""));
|
| | | map.remove("SO2");
|
| | | }
|
| | | if ((!map.containsKey("NO2C")) && map.containsKey("NO2")) {
|
| | | map.put("NO2C", map.get("NO2").toString().replace("\\.", ""));
|
| | | map.remove("NO2");
|
| | | }
|
| | | if ((!map.containsKey("O3C")) && map.containsKey("O3")) {
|
| | | map.put("O3C", map.get("O3").toString().replace("\\.", ""));
|
| | | map.remove("O3");
|
| | | }
|
| | | if ((!map.containsKey("COC")) && map.containsKey("CO")) {
|
| | | map.put("COC", map.get("CO").toString());
|
| | | map.remove("CO");
|
| | | }
|
| | | if ((!map.containsKey("PM25C")) && (map.containsKey("PM2_5") || map.containsKey("PM25"))) {
|
| | | if (map.get("PM2_5") != null) {
|
| | | map.put("PM25C", map.get("PM2_5").toString().replace("\\.", ""));
|
| | | map.remove("PM2_5");
|
| | | }
|
| | | if (map.get("PM25") != null) {
|
| | | map.put("PM25C", map.get("PM25").toString().replace("\\.", ""));
|
| | | map.remove("PM25");
|
| | | }
|
| | | }
|
| | | if ((!map.containsKey("PM10C")) && map.containsKey("PM10")) {
|
| | | map.put("PM10C", map.get("PM10").toString().replace("\\.", ""));
|
| | | map.remove("PM10");
|
| | | }
|
| | | if (map.get("windScale") != null) {
|
| | | String windScale = map.get("windScale").toString().replace("-", "~");
|
| | | map.put("windScale", windScale);
|
| | | }
|
| | | //温度取整
|
| | | if (map.get("temp") != null) {
|
| | | long temp = Math.round(Double.valueOf(map.get("temp").toString()));
|
| | | map.put("temp", String.valueOf(temp));
|
| | | }
|
| | | }
|
| | |
|
| | | //后4小时保良值
|
| | | double value = 160.0 * 8;
|
| | | int nowHour = Integer.valueOf(sdf1.format(now).substring(11, 13));
|
| | | int nowMinute = Integer.valueOf(sdf1.format(now).substring(14, 16));
|
| | | String format = sdf1.format(now).substring(0, 13) + ":00:00";
|
| | | if (nowHour < 8) {
|
| | | return resultList;
|
| | | }
|
| | |
|
| | | if (nowMinute >= 46) {
|
| | | nowHour = nowHour + 1;
|
| | | cal.setTime(now);
|
| | | cal.add(Calendar.HOUR_OF_DAY, 1);
|
| | | format = sdf1.format(cal.getTime()).substring(0, 13) + ":00:00";
|
| | | }
|
| | |
|
| | | for (Map<String, Object> resultMap : resultList) {
|
| | | if (resultMap.get("type").equals("实测")) {
|
| | | if (sdf.parse(time).getTime() == sdf.parse(sdf.format(now)).getTime()) {
|
| | | String rtime = resultMap.get("time").toString() + ":00";
|
| | | int rhour = Integer.valueOf(rtime.substring(11, 13));
|
| | | cal.setTime(sdf1.parse(rtime));
|
| | | cal.add(Calendar.HOUR_OF_DAY, -7);
|
| | | Date startTime = cal.getTime();
|
| | | Date endTime = sdf1.parse(format);
|
| | | Map<String, Object> hashMap = new HashMap<>();
|
| | | hashMap.put("cityCode", Integer.valueOf(parameters.get("cityCode").toString()));
|
| | | hashMap.put("start", startTime);
|
| | | hashMap.put("end", endTime);
|
| | | if (rhour == 0) {
|
| | | rhour = 24;
|
| | | }
|
| | |
|
| | | if (rhour == nowHour) {
|
| | | Map<String, Object> sumO3Map = hangzhouAqiMapper.getSumO3(hashMap);
|
| | | if (sumO3Map != null) {
|
| | | double v = value - Double.valueOf(sumO3Map.get("O3Sum").toString());
|
| | | resultMap.put("goodValue", v);
|
| | | }
|
| | | } else if (rhour == nowHour + 1) {
|
| | | Map<String, Object> sumO3Map = hangzhouAqiMapper.getSumO3(hashMap);
|
| | | if (sumO3Map != null) {
|
| | | double v = (value - Double.valueOf(sumO3Map.get("O3Sum").toString())) / 2;
|
| | | resultMap.put("goodValue", String.valueOf(Math.round(v)));
|
| | | }
|
| | | } else if (rhour == nowHour + 2) {
|
| | | Map<String, Object> sumO3Map = hangzhouAqiMapper.getSumO3(hashMap);
|
| | | if (sumO3Map != null) {
|
| | | double v = (value - Double.valueOf(sumO3Map.get("O3Sum").toString())) / 3;
|
| | | resultMap.put("goodValue", String.valueOf(Math.round(v)));
|
| | | }
|
| | | } else if (rhour == nowHour + 3) {
|
| | | Map<String, Object> sumO3Map = hangzhouAqiMapper.getSumO3(hashMap);
|
| | | if (sumO3Map != null) {
|
| | | double v = (value - Double.valueOf(sumO3Map.get("O3Sum").toString())) / 4;
|
| | | resultMap.put("goodValue", String.valueOf(Math.round(v)));
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | //地理特征
|
| | | Map<String, Object> geographyMap = shAreaMapper.getGeography(name);
|
| | | if (geographyMap != null) {
|
| | | for (Map<String, Object> map : resultList) {
|
| | | if (geographyMap.get("lng") != null) {
|
| | | double longitude = Double.valueOf(geographyMap.get("lng").toString());
|
| | | map.put("longitude", df.format(longitude));
|
| | | }
|
| | | if (geographyMap.get("lat") != null) {
|
| | | double latitude = Double.valueOf(geographyMap.get("lat").toString());
|
| | | map.put("latitude", df.format(latitude));
|
| | | }
|
| | | if (geographyMap.get("altitude") != null) {
|
| | | map.put("altitude", geographyMap.get("altitude").toString());
|
| | | }
|
| | | if (geographyMap.get("vegetation") != null) {
|
| | | map.put("vegetation", geographyMap.get("vegetation").toString());
|
| | | }
|
| | | if (geographyMap.get("river") != null) {
|
| | | map.put("river", geographyMap.get("river").toString());
|
| | | }
|
| | | }
|
| | | }
|
| | |
|
| | | for (int i = 0; i < resultList.size(); i = i + 2) {
|
| | | Map<String, Object> foreMap = resultList.get(i);
|
| | | Map<String, Object> realMap = resultList.get(i + 1);
|
| | | if (foreMap.get("O3C") != null && realMap.get("O3C") != null) {
|
| | | Double foreO3 = Double.valueOf(foreMap.get("O3C").toString());
|
| | | Double realO3 = Double.valueOf(realMap.get("O3C").toString());
|
| | | int round = (int) Math.round((realO3 - foreO3) / realO3 * 100);
|
| | | if (round < 0) {
|
| | | round = -round;
|
| | | }
|
| | | realMap.put("errorRate", round + "%");
|
| | | }
|
| | | }
|
| | | return resultList;
|
| | | }
|
| | | }
|