jinpengyong
2021-11-08 100e952066f126467e128a672fb6a7fc3cf367c3
update
10 files added
465 ■■■■■ changed files
screen-job/src/main/java/com/moral/api/entity/CityAqiMonthly.java 45 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/entity/CityAqiYearly.java 45 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/mapper/CityAqiMonthlyMapper.java 16 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/mapper/CityAqiYearlyMapper.java 16 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/CityAqiMonthlyService.java 19 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/CityAqiYearlyService.java 19 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java 138 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/service/impl/CityAqiYearlyServiceImpl.java 143 ●●●●● patch | view | raw | blame | history
screen-job/src/main/resources/mapper/CityAqiMonthlyMapper.xml 12 ●●●●● patch | view | raw | blame | history
screen-job/src/main/resources/mapper/CityAqiYearlyMapper.xml 12 ●●●●● patch | view | raw | blame | history
screen-job/src/main/java/com/moral/api/entity/CityAqiMonthly.java
New file
@@ -0,0 +1,45 @@
package com.moral.api.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
 * <p>
 * 城市aqi月数据表
 * </p>
 *
 * @author moral
 * @since 2021-11-05
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class CityAqiMonthly extends Model<CityAqiMonthly> {
    private static final long serialVersionUID = 1L;
    /**
     * 城市编码code
     */
    private Integer cityCode;
    /**
     * 时间
     */
    private Date time;
    /**
     * 数据
     */
    private String value;
    @Override
    protected Serializable pkVal() {
        return null;
    }
}
screen-job/src/main/java/com/moral/api/entity/CityAqiYearly.java
New file
@@ -0,0 +1,45 @@
package com.moral.api.entity;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import java.io.Serializable;
import java.util.Date;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
 * <p>
 * 城市aqi年数据表
 * </p>
 *
 * @author moral
 * @since 2021-11-05
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class CityAqiYearly extends Model<CityAqiYearly> {
    private static final long serialVersionUID = 1L;
    /**
     * 城市编码code
     */
    private Integer cityCode;
    /**
     * 时间
     */
    private Date time;
    /**
     * 数据
     */
    private String value;
    @Override
    protected Serializable pkVal() {
        return null;
    }
}
screen-job/src/main/java/com/moral/api/mapper/CityAqiMonthlyMapper.java
New file
@@ -0,0 +1,16 @@
package com.moral.api.mapper;
import com.moral.api.entity.CityAqiMonthly;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
 * <p>
 * 城市aqi月数据表 Mapper 接口
 * </p>
 *
 * @author moral
 * @since 2021-11-05
 */
public interface CityAqiMonthlyMapper extends BaseMapper<CityAqiMonthly> {
}
screen-job/src/main/java/com/moral/api/mapper/CityAqiYearlyMapper.java
New file
@@ -0,0 +1,16 @@
package com.moral.api.mapper;
import com.moral.api.entity.CityAqiYearly;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
 * <p>
 * 城市aqi年数据表 Mapper 接口
 * </p>
 *
 * @author moral
 * @since 2021-11-05
 */
public interface CityAqiYearlyMapper extends BaseMapper<CityAqiYearly> {
}
screen-job/src/main/java/com/moral/api/service/CityAqiMonthlyService.java
New file
@@ -0,0 +1,19 @@
package com.moral.api.service;
import com.moral.api.entity.CityAqiMonthly;
import com.baomidou.mybatisplus.extension.service.IService;
/**
 * <p>
 * 城市aqi月数据表 服务类
 * </p>
 *
 * @author moral
 * @since 2021-11-04
 */
public interface CityAqiMonthlyService extends IService<CityAqiMonthly> {
    //城市aqi月数据统计
    void insertCityAqiMonthly();
}
screen-job/src/main/java/com/moral/api/service/CityAqiYearlyService.java
New file
@@ -0,0 +1,19 @@
package com.moral.api.service;
import com.moral.api.entity.CityAqiYearly;
import com.baomidou.mybatisplus.extension.service.IService;
/**
 * <p>
 * 城市aqi年数据表 服务类
 * </p>
 *
 * @author moral
 * @since 2021-11-04
 */
public interface CityAqiYearlyService extends IService<CityAqiYearly> {
    //城市aqi年数据统计
    void insertCityAqiYearly();
}
screen-job/src/main/java/com/moral/api/service/impl/CityAqiMonthlyServiceImpl.java
New file
@@ -0,0 +1,138 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityAqiDaily;
import com.moral.api.entity.CityAqiMonthly;
import com.moral.api.mapper.CityAqiMonthlyMapper;
import com.moral.api.service.CityAqiDailyService;
import com.moral.api.service.CityAqiMonthlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.AmendUtils;
import com.moral.util.ComprehensiveIndexUtils;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
/**
 * <p>
 * 城市aqi月数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-11-04
 */
@Service
public class CityAqiMonthlyServiceImpl extends ServiceImpl<CityAqiMonthlyMapper, CityAqiMonthly> implements CityAqiMonthlyService {
    @Autowired
    private CityAqiMonthlyMapper cityAqiMonthlyMapper;
    @Autowired
    private CityAqiDailyService cityAqiDailyService;
    @Override
    public void insertCityAqiMonthly() {
        //需要均值计算的因子
        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2");
        //开始时间,上月1号
        Date start = DateUtils.getFirstDayOfLastMonth();
        //上上月
        Date lastLastMonth = DateUtils.addMonths(start, -1);
        //结束时间,本月1号
        Date end = DateUtils.addMonths(start, 1);
        //获取所有城市aqi小时数据
        QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>();
        wrapper.select("city_code", "time", "value")
                .ge("time", DateUtils.dateToDateString(start))
                .lt("time", DateUtils.dateToDateString(end));
        List<Map<String, Object>> monthlyData = cityAqiDailyService.listMaps(wrapper);
        if (monthlyData.size() == 0) {
            return;
        }
        //按city_code分组
        Map<String, List<Map<String, Object>>> data = monthlyData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString()));
        data.forEach((cityCode, value) -> {
            Map<String, Object> jsonMap = new HashMap<>();
            CityAqiMonthly cityAqiMonthly = new CityAqiMonthly();
            cityAqiMonthly.setCityCode(Integer.parseInt(cityCode));
            cityAqiMonthly.setTime(start);
            Map<String, Object> params = new HashMap<>();
            List<Map<String, Object>> temp = new ArrayList<>();
            for (Map<String, Object> map : value) {
                Map<String, Object> sensorsValue = JSONObject.parseObject(map.get("value").toString(), Map.class);
                Map<String, Object> tempMap = new HashMap<>();
                tempMap.put(Constants.SENSOR_CODE_CO, sensorsValue.get("CO"));
                tempMap.put(Constants.SENSOR_CODE_O3, sensorsValue.get("O3"));
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("value", JSONObject.toJSONString(tempMap));
                temp.add(hashMap);
            }
            params.put("data", temp);
            //1. CO 95百分位计算并修约
            Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params);
            if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) {
                jsonMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO));
            }
            //2. O3 90百分位计算并修约
            Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params);
            if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) {
                jsonMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3));
            }
            sensors.forEach(sensor -> {
                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
                    double aDouble = Double.parseDouble(dataValue.get(sensor).toString());
                    return DoubleStream.of(aDouble);
                }).average();
                if (optionalDouble.isPresent()) {
                    //银行家算法修约
                    jsonMap.put(sensor, AmendUtils.sciCal(optionalDouble.getAsDouble(), 0));
                }
            });
            //本月月综指计算
            Double compositeIndex = ComprehensiveIndexUtils.dailyData(jsonMap);
            jsonMap.put("compositeIndex", compositeIndex);
            //本月综指同上月对比
            QueryWrapper<CityAqiMonthly> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value")
                    .eq("city_code", cityCode)
                    .eq("time", DateUtils.dateToDateString(lastLastMonth));
            //获取上上月数据
            CityAqiMonthly lastCityAqiMonthly = cityAqiMonthlyMapper.selectOne(queryWrapper);
            if (lastCityAqiMonthly != null) {
                Map<String, Object> map = JSONObject.parseObject(lastCityAqiMonthly.getValue(), Map.class);
                double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString());
                DecimalFormat decimalFormat = new DecimalFormat("0.00%");
                String format = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex);
                jsonMap.put("monthContrast", format);
            }
            cityAqiMonthly.setValue(JSONObject.toJSONString(jsonMap));
            cityAqiMonthlyMapper.insert(cityAqiMonthly);
        });
    }
}
screen-job/src/main/java/com/moral/api/service/impl/CityAqiYearlyServiceImpl.java
New file
@@ -0,0 +1,143 @@
package com.moral.api.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.moral.api.entity.CityAqiDaily;
import com.moral.api.entity.CityAqiYearly;
import com.moral.api.mapper.CityAqiYearlyMapper;
import com.moral.api.service.CityAqiDailyService;
import com.moral.api.service.CityAqiYearlyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.moral.constant.Constants;
import com.moral.util.AmendUtils;
import com.moral.util.ComprehensiveIndexUtils;
import com.moral.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.OptionalDouble;
import java.util.stream.Collectors;
import java.util.stream.DoubleStream;
/**
 * <p>
 * 城市aqi年数据表 服务实现类
 * </p>
 *
 * @author moral
 * @since 2021-11-04
 */
@Service
public class CityAqiYearlyServiceImpl extends ServiceImpl<CityAqiYearlyMapper, CityAqiYearly> implements CityAqiYearlyService {
    @Autowired
    private CityAqiDailyService cityAqiDailyService;
    @Autowired
    private CityAqiYearlyMapper cityAqiYearlyMapper;
    @Override
    public void insertCityAqiYearly() {
        //需要均值计算的因子
        List<String> sensors = Arrays.asList("PM2_5", "PM10", "SO2", "NO2");
        //开始时间,去年1号
        Date start = DateUtils.getFirstDayOfLastYear();
        //上上年
        Date lastLastYear = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), -1), DateUtils.yyyy);
        //结束时间,本年1号
        Date end = DateUtils.getDate(DateUtils.getDateAddYear(DateUtils.dateToDateString(start, DateUtils.yyyy), 1), DateUtils.yyyy);
        //获取所有城市aqi小时数据
        QueryWrapper<CityAqiDaily> wrapper = new QueryWrapper<>();
        wrapper.select("city_code", "time", "value")
                .ge("time", DateUtils.dateToDateString(start))
                .lt("time", DateUtils.dateToDateString(end));
        List<Map<String, Object>> monthlyData = cityAqiDailyService.listMaps(wrapper);
        if (monthlyData.size() == 0) {
            return;
        }
        //按city_code分组
        Map<String, List<Map<String, Object>>> data = monthlyData.parallelStream().collect(Collectors.groupingBy(o -> o.get("city_code").toString()));
        data.forEach((cityCode, value) -> {
            Map<String, Object> jsonMap = new HashMap<>();
            CityAqiYearly cityAqiYearly = new CityAqiYearly();
            cityAqiYearly.setCityCode(Integer.parseInt(cityCode));
            cityAqiYearly.setTime(start);
            Map<String, Object> params = new HashMap<>();
            List<Map<String, Object>> temp = new ArrayList<>();
            System.out.println(value);
            for (Map<String, Object> map : value) {
                Map<String, Object> sensorsValue = JSONObject.parseObject(map.get("value").toString(), Map.class);
                Map<String, Object> tempMap = new HashMap<>();
                tempMap.put(Constants.SENSOR_CODE_CO, sensorsValue.get("PM2_5"));
                tempMap.put(Constants.SENSOR_CODE_O3, sensorsValue.get("O3"));
                Map<String, Object> hashMap = new HashMap<>();
                hashMap.put("value", JSONObject.toJSONString(tempMap));
                temp.add(hashMap);
            }
            params.put("data", temp);
            //1. CO 95百分位计算并修约
            Map<String, Object> coAvgOfWeekOrMonth = AmendUtils.getCOAvgOfWeekOrMonth(params);
            if (!ObjectUtils.isEmpty(coAvgOfWeekOrMonth)) {
                jsonMap.put("CO", coAvgOfWeekOrMonth.get(Constants.SENSOR_CODE_CO));
            }
            //2. O3 90百分位计算并修约
            Map<String, Object> o3AvgOfWeekOrMonth = AmendUtils.getO3AvgOfWeekOrMonth(params);
            if (!ObjectUtils.isEmpty(o3AvgOfWeekOrMonth)) {
                jsonMap.put("O3", o3AvgOfWeekOrMonth.get(Constants.SENSOR_CODE_O3));
            }
            sensors.forEach(sensor -> {
                OptionalDouble optionalDouble = value.parallelStream().flatMapToDouble(v -> {
                    Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class);
                    double aDouble = Double.parseDouble(dataValue.get(sensor).toString());
                    return DoubleStream.of(aDouble);
                }).average();
                if (optionalDouble.isPresent()) {
                    //银行家算法修约
                    jsonMap.put(sensor, AmendUtils.sciCal(optionalDouble.getAsDouble(), 0));
                }
            });
            //本月月综指计算
            Double compositeIndex = ComprehensiveIndexUtils.dailyData(jsonMap);
            jsonMap.put("compositeIndex", compositeIndex);
            //本月综指同上月对比
            QueryWrapper<CityAqiYearly> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("value")
                    .eq("city_code", cityCode)
                    .eq("time", DateUtils.dateToDateString(lastLastYear));
            //获取上上月数据
            CityAqiYearly lastCityAqiYearly = cityAqiYearlyMapper.selectOne(queryWrapper);
            if (lastCityAqiYearly != null) {
                Map<String, Object> map = JSONObject.parseObject(lastCityAqiYearly.getValue(), Map.class);
                double lastCompositeIndex = Double.parseDouble(map.get("compositeIndex").toString());
                DecimalFormat decimalFormat = new DecimalFormat("0.00%");
                String format = decimalFormat.format((compositeIndex - lastCompositeIndex) / lastCompositeIndex);
                jsonMap.put("yearContrast", format);
            }
            cityAqiYearly.setValue(JSONObject.toJSONString(jsonMap));
            cityAqiYearlyMapper.insert(cityAqiYearly);
        });
    }
}
screen-job/src/main/resources/mapper/CityAqiMonthlyMapper.xml
New file
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.api.mapper.CityAqiMonthlyMapper">
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.moral.api.entity.CityAqiMonthly">
                    <result column="city_code" property="cityCode" />
                    <result column="time" property="time" />
                    <result column="value" property="value" />
        </resultMap>
</mapper>
screen-job/src/main/resources/mapper/CityAqiYearlyMapper.xml
New file
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.moral.api.mapper.CityAqiYearlyMapper">
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.moral.api.entity.CityAqiYearly">
                    <result column="city_code" property="cityCode" />
                    <result column="time" property="time" />
                    <result column="value" property="value" />
        </resultMap>
</mapper>