kaiyu
2021-12-30 bd9a7f2d97af968d43ccd5516644f6933b42f5b2
screen-api
增加城市优良天气对比接口
2 files added
5 files modified
382 ■■■■ changed files
screen-api/src/main/java/com/moral/api/controller/AqiController.java 29 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/dto/cityAQI/CityPollutionLevel.java 51 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/pojo/form/aqi/AreaPollutionLevelForm.java 68 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/CityAqiDailyServiceImpl.java 153 ●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/service/impl/SysAreaServiceImpl.java 7 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/utils/UnitConvertUtils.java 1 ●●●● patch | view | raw | blame | history
screen-common/src/main/java/com/moral/util/DateUtils.java 73 ●●●●● patch | view | raw | blame | history
screen-api/src/main/java/com/moral/api/controller/AqiController.java
@@ -1,10 +1,12 @@
package com.moral.api.controller;
import com.moral.api.pojo.dto.cityAQI.CityPollutionLevel;
import com.moral.api.pojo.dto.cityAQI.MonthlyPollutionLevel;
import com.moral.api.pojo.form.aqi.AreaPollutionLevelForm;
import com.moral.api.pojo.vo.cityAQI.BarChartOfPollutionLevelVO;
import com.moral.api.pojo.vo.cityAQI.BarChartTableOfPollutionLevelVO;
import com.moral.api.pojo.vo.cityAQI.PieChartOfPollutionLevelVO;
import com.moral.api.pojo.form.aqi.QueryChartOfPollutionLevelForm;
import com.moral.api.pojo.form.aqi.ChartOfPollutionLevelForm;
import com.moral.api.service.CityAqiDailyService;
import io.swagger.annotations.Api;
@@ -99,7 +101,7 @@
     * @Date: 2021/11/11
     */
    @GetMapping("queryPieChartOfPollutionLevel")
    public ResultMessage queryPieChartOfPollutionLevel(QueryChartOfPollutionLevelForm form) {
    public ResultMessage queryPieChartOfPollutionLevel(ChartOfPollutionLevelForm form) {
        //判断是否缺少参数
        if (!form.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
@@ -117,7 +119,7 @@
            * @Date: 2021/11/22
            */
    @GetMapping("queryBarChartOfPollutionLevel")
    public ResultMessage queryBarChartOfPollutionLevel(QueryChartOfPollutionLevelForm form){
    public ResultMessage queryBarChartOfPollutionLevel(ChartOfPollutionLevelForm form){
        //判断是否缺少参数
        if (!form.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
@@ -138,7 +140,7 @@
            * @Date: 2021/11/22
            */
    @GetMapping("queryBarChatTableOfPollutionLevel")
    public ResultMessage queryBarChatTableOfPollutionLevel(QueryChartOfPollutionLevelForm form){
    public ResultMessage queryBarChatTableOfPollutionLevel(ChartOfPollutionLevelForm form){
        //判断是否缺少参数
        if (!form.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
@@ -196,4 +198,23 @@
        return ResultMessage.ok(response);
    }
    /**
    * @Description: 查询一个城市所有县级市的优良天气
            * @Param: [regionCode]
            * @return: com.moral.constant.ResultMessage
            * @Author: 陈凯裕
            * @Date: 2021/12/30
            */
    @GetMapping("queryAreaPollutionLevel")
    public ResultMessage queryAreaPollutionLevel(AreaPollutionLevelForm form){
        //判断是否缺少参数
        if (!form.valid())
            return ResultMessage.fail(ResponseCodeEnum.PARAMETERS_IS_MISSING.getCode(),
                    ResponseCodeEnum.PARAMETERS_IS_MISSING.getMsg());
        //查询业务数据
        List<CityPollutionLevel> datas = cityAqiDailyService.queryAreaPollutionLevel(form);
        return ResultMessage.ok(datas);
    }
}
screen-api/src/main/java/com/moral/api/pojo/dto/cityAQI/CityPollutionLevel.java
New file
@@ -0,0 +1,51 @@
package com.moral.api.pojo.dto.cityAQI;
import com.moral.constant.Constants;
import lombok.Data;
/**
 * @ClassName AreaPollutionLevel
 * @Description 城市污染等级以及天数占比实体
 * @Author 陈凯裕
 * @Date 2021/12/30 10:30
 * @Version TODO
 **/
@Data
public class CityPollutionLevel {
    /*
    * 地区名称
    * */
    private String regionName;
    /*
    * 优天气天数
    * */
    private Integer excellentWeatherDays;
    /*
    * 良天气天数
    * */
    private Integer goodWeatherDays;
    /*
    * 轻度污染天气天数
    * */
    private Integer mildWeatherDays;
    /*
    * 重度污染天气天数
    * */
    private Integer middleWeatherDays;
    /*
    * 重度污染天气天数
    * */
    private Integer seriousWeatherDays;
    /*
    * 严重污染天气天数
    * */
    private Integer serverWeatherDays;
}
screen-api/src/main/java/com/moral/api/pojo/form/aqi/AreaPollutionLevelForm.java
New file
@@ -0,0 +1,68 @@
package com.moral.api.pojo.form.aqi;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.moral.util.DateUtils;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;
/**
 * @ClassName AreaPollutionLevelForm
 * @Description TODO
 * @Author 陈凯裕
 * @Date 2021/12/30 10:37
 * @Version TODO
 **/
@Data
public class AreaPollutionLevelForm {
    /*
     * 地区码
     * */
    private Integer regionCode;
    /*
     * 如果是查年数据则该参数不能为空
     * */
    @DateTimeFormat(pattern = "yyyy")
    @JsonFormat(pattern = "yyyy", timezone = "GMT+8")
    private Date year;
    /*
     * 如果是查月数据则该参数不能为空
     * */
    @DateTimeFormat(pattern = "yyyy-MM")
    @JsonFormat(pattern = "yyyy-MM", timezone = "GMT+8")
    private Date month;
    /*
     * 用于查询数据的开始时间,由form自行转换
     * */
    private Date startDate;
    /*
     * 用于查询数据的结束时间,由form自行转换
     * */
    private Date endDate;
    public boolean valid() {
        if (regionCode == null || (year == null && month == null)||(year != null && month != null))
            return false;
        //检查regionCode格式
        String regionCodeStr = regionCode.toString();
        if (!regionCodeStr.substring(regionCodeStr.length() - 2, regionCodeStr.length()).equals("00"))
            return false;
        //时间转换
        if (year != null) {
            startDate = DateUtils.getFirstDayOfYear(year);
            endDate = DateUtils.getLastDayOfYear(year);
            return true;
        }
        startDate = DateUtils.getFirstDayOfMonth(month);
        endDate = DateUtils.getLastDayOfMonth(month);
        return true;
    }
}
screen-api/src/main/java/com/moral/api/service/impl/CityAqiDailyServiceImpl.java
@@ -4,13 +4,17 @@
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityAqiDaily;
import com.moral.api.entity.SysArea;
import com.moral.api.mapper.CityAqiDailyMapper;
import com.moral.api.pojo.dto.cityAQI.CityPollutionLevel;
import com.moral.api.pojo.dto.cityAQI.MonthlyPollutionLevel;
import com.moral.api.pojo.dto.cityAQI.PollutionDaysAndProportion;
import com.moral.api.pojo.form.aqi.AreaPollutionLevelForm;
import com.moral.api.pojo.vo.cityAQI.PieChartOfPollutionLevelVO;
import com.moral.api.pojo.form.aqi.QueryChartOfPollutionLevelForm;
import com.moral.api.pojo.form.aqi.ChartOfPollutionLevelForm;
import com.moral.api.service.CityAqiDailyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.api.service.SysAreaService;
import com.moral.constant.Constants;
import com.moral.util.AQIUtils;
import com.moral.util.DateUtils;
@@ -35,9 +39,11 @@
    @Autowired
    CityAqiDailyMapper cityAqiDailyMapper;
    @Autowired
    SysAreaService sysAreaService;
    @Override
    public PieChartOfPollutionLevelVO queryPieChartOfPollutionLevels(QueryChartOfPollutionLevelForm form) {
    public PieChartOfPollutionLevelVO queryPieChartOfPollutionLevels(ChartOfPollutionLevelForm form) {
        //取参
        Integer regionCode = form.getRegionCode();
        Date startDate = form.getStartDate();
@@ -106,7 +112,7 @@
    }
    @Override
    public List<MonthlyPollutionLevel> queryBarChartOfPollutionLevels(QueryChartOfPollutionLevelForm form) {
    public List<MonthlyPollutionLevel> queryBarChartOfPollutionLevels(ChartOfPollutionLevelForm form) {
        //取参
        Integer regionCode = form.getRegionCode();
        Date startDate = form.getStartDate();
@@ -138,7 +144,7 @@
        List<MonthlyPollutionLevel> monthlyPollutionLevels = new ArrayList<>();
        Set<Map.Entry<String, List<CityAqiDaily>>> monthlyEntries = monthlyDataMap.entrySet();
        Iterator<Map.Entry<String, List<CityAqiDaily>>> monthlyIterator = monthlyEntries.iterator();
        while(monthlyIterator.hasNext()){
        while (monthlyIterator.hasNext()) {
            Map.Entry<String, List<CityAqiDaily>> monthlyEntry = monthlyIterator.next();
            List<CityAqiDaily> monthlyValues = monthlyEntry.getValue();
            //用于统计各个污染等级的天数
@@ -187,44 +193,44 @@
    }
    @Override
    public Map<String, Map<String,Object>> oneYearsData(Map map) {
    public Map<String, Map<String, Object>> oneYearsData(Map map) {
        int city_code = Integer.parseInt(map.get("cityCode").toString());
        String year = map.get("year").toString();
        String startTime = year+"-01-01 00:00:00";
        String endTime = year+"-12-31 23:59:59";
        String startTime = year + "-01-01 00:00:00";
        String endTime = year + "-12-31 23:59:59";
        QueryWrapper<CityAqiDaily> cityAqiDailyQueryWrapper = new QueryWrapper<>();
        cityAqiDailyQueryWrapper.eq("city_code", city_code);
        cityAqiDailyQueryWrapper.between("time",startTime,endTime);
        cityAqiDailyQueryWrapper.between("time", startTime, endTime);
        List<CityAqiDaily> cityAqiDailies = cityAqiDailyMapper.selectList(cityAqiDailyQueryWrapper);
        Map<String,Map<String,Object>> resultMap = new HashMap<>();
        for (int i=1;i<13;i++){
        Map<String, Map<String, Object>> resultMap = new HashMap<>();
        for (int i = 1; i < 13; i++) {
            Map<String, Object> monthMap = new HashMap<>();
            String month = null;
            if (i<10){
                month = "0"+i;
            }else {
            if (i < 10) {
                month = "0" + i;
            } else {
                month = String.valueOf(i);
            }
            String monthDate = year+"-"+month;
            String monthFirstDay = year+"-"+month+"-01";
            String monthDate = year + "-" + month;
            String monthFirstDay = year + "-" + month + "-01";
            int monthDay = DateUtils.getMonthDay(monthFirstDay);
            for (int j=1;j<monthDay+1;j++){
            for (int j = 1; j < monthDay + 1; j++) {
                String day = null;
                if (j<10){
                    day = "0"+j;
                }else {
                if (j < 10) {
                    day = "0" + j;
                } else {
                    day = String.valueOf(j);
                }
                String dayDate = year+"-"+month+"-"+day;
                monthMap.put(dayDate,null);
                String dayDate = year + "-" + month + "-" + day;
                monthMap.put(dayDate, null);
            }
            resultMap.put(monthDate,monthMap);
            resultMap.put(monthDate, monthMap);
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (CityAqiDaily cityAqiDaily:cityAqiDailies) {
        for (CityAqiDaily cityAqiDaily : cityAqiDailies) {
            String time = sdf.format(cityAqiDaily.getTime());
            String monthDate = time.substring(0,7);
            String dayDate = time.substring(0,10);
            String monthDate = time.substring(0, 7);
            String dayDate = time.substring(0, 10);
            Double PM2_5 = null;
            Double PM10 = null;
            Double SO2 = null;
@@ -236,45 +242,102 @@
            String value = cityAqiDaily.getValue();
            JSONObject jsonObject = new JSONObject();
            jsonObject = JSONObject.parseObject(value);
            if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("PM2_5"))) {
                PM2_5 = (Double.parseDouble(jsonObject.get("PM2_5").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("PM10"))) {
                PM10 = (Double.parseDouble(jsonObject.get("PM10").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("SO2"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("SO2"))) {
                SO2 = (Double.parseDouble(jsonObject.get("SO2").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("NO2"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("NO2"))) {
                NO2 = (Double.parseDouble(jsonObject.get("NO2").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("CO"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("CO"))) {
                CO = (Double.parseDouble(jsonObject.get("CO").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("O3"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("O3"))) {
                O3 = (Double.parseDouble(jsonObject.get("O3").toString()));
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("primaryPollutant"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("primaryPollutant"))) {
                primaryPollutant = jsonObject.get("primaryPollutant").toString();
            }
            if (!ObjectUtils.isEmpty(jsonObject.get("AQI"))){
            if (!ObjectUtils.isEmpty(jsonObject.get("AQI"))) {
                AQI = jsonObject.get("AQI").toString();
            }
            Map<String,Object> valueMap = new HashMap<>();
            valueMap.put("PM2_5",PM2_5);
            valueMap.put("PM10",PM10);
            valueMap.put("SO2",SO2);
            valueMap.put("NO2",NO2);
            valueMap.put("CO",CO);
            valueMap.put("O3",O3);
            valueMap.put("AQI",AQI);
            valueMap.put("primaryPollutant",primaryPollutant);
            Map<String,Object> monthMap = new HashMap<>();
            Map<String, Object> valueMap = new HashMap<>();
            valueMap.put("PM2_5", PM2_5);
            valueMap.put("PM10", PM10);
            valueMap.put("SO2", SO2);
            valueMap.put("NO2", NO2);
            valueMap.put("CO", CO);
            valueMap.put("O3", O3);
            valueMap.put("AQI", AQI);
            valueMap.put("primaryPollutant", primaryPollutant);
            Map<String, Object> monthMap = new HashMap<>();
            monthMap = resultMap.get(monthDate);
            monthMap.put(dayDate,valueMap);
            resultMap.put(monthDate,monthMap);
            monthMap.put(dayDate, valueMap);
            resultMap.put(monthDate, monthMap);
        }
        return resultMap;
    }
    @Override
    public List<CityPollutionLevel> queryAreaPollutionLevel(AreaPollutionLevelForm form) {
        //取参
        Integer regionCode = form.getRegionCode();
        Date startDate = form.getStartDate();
        Date endDate = form.getEndDate();
        //查询该市区下所有的县
        List<SysArea> allAreas = sysAreaService.getChildren(regionCode);
        //创建返回数据对象
        List<CityPollutionLevel> datas = new ArrayList<>();
        //遍历每个县级市 查询数据
        for (SysArea area : allAreas) {
            //查询数据
            QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>();
            wrapper.eq("city_code", area.getAreaCode());
            wrapper.between("time", startDate, endDate);
            wrapper.orderByAsc("time");
            List<CityAqiDaily> cityAqiDailies = cityAqiDailyMapper.selectList(wrapper);
            //如果数据的时间有重复则去重
            Map<String, CityAqiDaily> tmpMap = new LinkedHashMap<>();
            for (CityAqiDaily cityAqiDaily : cityAqiDailies) {
                String tmpTime = DateUtils.dateToDateString(cityAqiDaily.getTime(), "yyyy-MM-dd");
                tmpMap.put(tmpTime, cityAqiDaily);
            }
            cityAqiDailies = new ArrayList<CityAqiDaily>(tmpMap.values());
            //用于统计各个污染等级的天数
            Map<String, Integer> pollutionDaysMap = new LinkedHashMap<>();
            pollutionDaysMap.put(Constants.EXCELLENT_WEATHER_CODE, 0);
            pollutionDaysMap.put(Constants.GOOD_WEATHER_CODE, 0);
            pollutionDaysMap.put(Constants.MILD_WEATHER_CODE, 0);
            pollutionDaysMap.put(Constants.MIDDLE_WEATHER_CODE, 0);
            pollutionDaysMap.put(Constants.SERIOUS_WEATHER_CODE, 0);
            pollutionDaysMap.put(Constants.SERVER_WEATHER_CODE, 0);
            for (CityAqiDaily cityAqiDaily : cityAqiDailies) {
                Map<String, Object> valueMap = JSON.parseObject(cityAqiDaily.getValue(), Map.class);
                if (valueMap.get("AQI") == null)
                    continue;
                Integer aqi = Integer.valueOf(valueMap.get("AQI").toString());
                //计算AQI污染等级
                String pollutionClass = AQIUtils.classCodeOfPollutionByAqi(aqi);
                Integer days = pollutionDaysMap.get(pollutionClass);
                pollutionDaysMap.put(pollutionClass, days + 1);
            }
            //封装对象
            CityPollutionLevel cityPollutionLevel = new CityPollutionLevel();
            cityPollutionLevel.setRegionName(area.getAreaName());
            cityPollutionLevel.setExcellentWeatherDays(pollutionDaysMap.get(Constants.EXCELLENT_WEATHER_CODE));
            cityPollutionLevel.setGoodWeatherDays(pollutionDaysMap.get(Constants.GOOD_WEATHER_CODE));
            cityPollutionLevel.setMildWeatherDays(pollutionDaysMap.get(Constants.MILD_WEATHER_CODE));
            cityPollutionLevel.setMiddleWeatherDays(pollutionDaysMap.get(Constants.MIDDLE_WEATHER_CODE));
            cityPollutionLevel.setSeriousWeatherDays(pollutionDaysMap.get(Constants.SERIOUS_WEATHER_CODE));
            cityPollutionLevel.setServerWeatherDays(pollutionDaysMap.get(Constants.SERVER_WEATHER_CODE));
            datas.add(cityPollutionLevel);
        }
        return datas;
    }
}
screen-api/src/main/java/com/moral/api/service/impl/SysAreaServiceImpl.java
@@ -96,6 +96,13 @@
        return newList;
    }
    @Override
    public List<SysArea> getChildren(Integer regionCode) {
        QueryWrapper<SysArea> wrapper = new QueryWrapper<>();
        wrapper.eq("parent_code",regionCode);
        return sysAreaMapper.selectList(wrapper);
    }
    //获取层级城市列表
    private List<Map<String, Object>> getAreasByRecursion(SysArea sysArea, List<SysArea> sysAreas) {
        SysArea newSysArea = new SysArea();
screen-api/src/main/java/com/moral/api/utils/UnitConvertUtils.java
@@ -177,4 +177,5 @@
    }
}
screen-common/src/main/java/com/moral/util/DateUtils.java
@@ -977,6 +977,35 @@
    }
    /*
    * 获取指定日期月的第一天
    * */
    public static Date getFirstDayOfMonth(Date date){
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_MONTH, last);
        cal.set(Calendar.HOUR_OF_DAY,0);
        cal.set(Calendar.MINUTE,0);
        cal.set(Calendar.SECOND,0);
        return cal.getTime();
    }
    /*
     * 获取指定日期月的最后一天
     * */
    public static Date getLastDayOfMonth(Date date) {
        final Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        final int last = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        cal.set(Calendar.DAY_OF_YEAR, last);
        cal.set(Calendar.HOUR_OF_DAY, 23);
        cal.set(Calendar.MINUTE, 59);
        cal.set(Calendar.SECOND, 59);
        return cal.getTime();
    }
    /*
     * 判断日期是否为今年
     * */
    public static boolean isCurrentYear(Date date) {
@@ -1018,50 +1047,6 @@
            throw new RuntimeException(e);
        }
        return date;
    }
    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int lastDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        DateFormat df = getDateFormat(yyyy_MM_dd_EN);
        return df.format(cal.getTime());
    }
    /**
     * 获取指定年月的第一天
     *
     * @param year
     * @param month
     * @return
     */
    public static String getLastDayOfMonth(int year, int month) {
        Calendar cal = Calendar.getInstance();
        // 设置年份
        cal.set(Calendar.YEAR, year);
        // 设置月份
        cal.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        // 格式化日期
        DateFormat df = getDateFormat(yyyy_MM_dd_EN);
        return df.format(cal.getTime());
    }
    /**