screen-common/src/main/java/com/moral/util/DateUtils.java
@@ -1314,8 +1314,16 @@ //获取上周一 public static Date geLastWeekMonday() { Calendar cal = Calendar.getInstance(); cal.setTime(getDate(getMondayOfThisWeek(),yyyy_MM_dd_EN) ); cal.setTime(getDate(getMondayOfThisWeek(), yyyy_MM_dd_EN)); cal.add(Calendar.DATE, -7); return cal.getTime(); } //获取上月第一天 public static Date getFirstDayOfLastMonth() { Calendar calendar = Calendar.getInstance(); calendar.add(Calendar.MONTH, -1); calendar.set(Calendar.DAY_OF_MONTH, 1); return getDate(dateToDateString(calendar.getTime(), yyyy_MM_dd_EN)); } } screen-job/src/main/java/com/moral/api/entity/HistoryMonthly.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> * 月数据 * </p> * * @author moral * @since 2021-07-01 */ @Data @EqualsAndHashCode(callSuper = false) public class HistoryMonthly extends Model<HistoryMonthly> { private static final long serialVersionUID = 1L; /** * 设备mac */ private String mac; /** * 时间 */ private Date time; /** * 数据 */ private String value; @Override protected Serializable pkVal() { return null; } } screen-job/src/main/java/com/moral/api/entity/HistoryWeekly.java
@@ -1,8 +1,9 @@ package com.moral.api.entity; import com.baomidou.mybatisplus.extension.activerecord.Model; import java.time.LocalDateTime; import java.io.Serializable; import java.util.Date; import lombok.Data; import lombok.EqualsAndHashCode; @@ -28,7 +29,7 @@ /** * 时间 */ private LocalDateTime time; private Date time; /** * 数据 screen-job/src/main/java/com/moral/api/entity/Sensor.java
@@ -3,7 +3,6 @@ import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.extension.activerecord.Model; import com.baomidou.mybatisplus.annotation.TableId; import java.time.LocalDateTime; import java.io.Serializable; import java.util.Date; screen-job/src/main/java/com/moral/api/mapper/HistoryMonthlyMapper.java
New file @@ -0,0 +1,21 @@ package com.moral.api.mapper; import java.util.List; import java.util.Map; import com.moral.api.entity.HistoryMonthly; import com.baomidou.mybatisplus.core.mapper.BaseMapper; /** * <p> * 月数据 Mapper 接口 * </p> * * @author moral * @since 2021-07-01 */ public interface HistoryMonthlyMapper extends BaseMapper<HistoryMonthly> { void insertHistoryMonthly(List<Map<String,Object>> list); } screen-job/src/main/java/com/moral/api/service/HistoryMonthlyService.java
New file @@ -0,0 +1,18 @@ package com.moral.api.service; import com.moral.api.entity.HistoryMonthly; import com.baomidou.mybatisplus.extension.service.IService; /** * <p> * 月数据 服务类 * </p> * * @author moral * @since 2021-07-01 */ public interface HistoryMonthlyService extends IService<HistoryMonthly> { void insertHistoryMonthly(); } screen-job/src/main/java/com/moral/api/service/impl/HistoryMonthlyServiceImpl.java
New file @@ -0,0 +1,133 @@ package com.moral.api.service.impl; import com.alibaba.fastjson.JSONObject; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.moral.api.entity.HistoryDaily; import com.moral.api.entity.HistoryMonthly; import com.moral.api.entity.Sensor; import com.moral.api.mapper.HistoryMonthlyMapper; import com.moral.api.service.HistoryDailyService; import com.moral.api.service.HistoryMonthlyService; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moral.api.service.SensorService; import com.moral.constant.Constants; import com.moral.util.AmendUtils; import com.moral.util.DateUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.util.ObjectUtils; import java.util.ArrayList; 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> * 月数据 服务实现类 * </p> * * @author moral * @since 2021-07-01 */ @Service public class HistoryMonthlyServiceImpl extends ServiceImpl<HistoryMonthlyMapper, HistoryMonthly> implements HistoryMonthlyService { @Autowired private HistoryMonthlyMapper historyMonthlyMapper; @Autowired private SensorService sensorService; @Autowired private HistoryDailyService historyDailyService; @Override public void insertHistoryMonthly() { Date now = new Date(); //开始时间,上月1号 Date start = DateUtils.getFirstDayOfLastMonth(); //因子 QueryWrapper<Sensor> sensorQueryWrapper = new QueryWrapper<>(); sensorQueryWrapper.select("code").eq("is_delete", Constants.NOT_DELETE); List<Object> sensorCodes = sensorService.listObjs(sensorQueryWrapper); //获取所有设备日数据 QueryWrapper<HistoryDaily> historyDailyQueryWrapper = new QueryWrapper<>(); historyDailyQueryWrapper.ge("time", DateUtils.dateToDateString(start)).lt("time", DateUtils.dateToDateString(now)); List<Map<String, Object>> weeklyData = historyDailyService.listMaps(historyDailyQueryWrapper); //按mac分组 Map<String, List<Map<String, Object>>> data = weeklyData.parallelStream().collect(Collectors.groupingBy(o -> (String) o.get("mac"))); //存入数据库的结果集 List<Map<String, Object>> insertData = new ArrayList<>(); data.forEach((key, value) -> { Map<String, Object> dataMap = new HashMap<>(); Map<String, Object> jsonMap = new HashMap<>(); dataMap.put("mac", key); dataMap.put("time", start); //风向均值计算并修约 Object windDirAvg = AmendUtils.getWindDirAvg(value); if (windDirAvg != null) { jsonMap.put(Constants.SENSOR_CODE_WIND_DIR, windDirAvg); } //CO 95百分位计算并修约 Object coAvg = AmendUtils.getCOAvgOfWeek(value); if (coAvg != null) { jsonMap.put(Constants.SENSOR_CODE_CO, coAvg); } //O3 90百分位计算并修约 Object o3Avg = AmendUtils.getO3AvgOfWeek(value); if (o3Avg != null) { jsonMap.put(Constants.SENSOR_CODE_O3, o3Avg); } sensorCodes.forEach(sensorCode -> { OptionalDouble optionalDouble = value.parallelStream() .flatMapToDouble(v -> { Map<String, Object> dataValue = JSONObject.parseObject((String) v.get("value"), Map.class); Object sensorValue = dataValue.get(sensorCode.toString()); if (ObjectUtils.isEmpty(sensorValue)) { return null; } //风向另外计算 if (Constants.SENSOR_CODE_WIND_DIR.equals(sensorCode)) { return null; } //CO另外计算 if (Constants.SENSOR_CODE_CO.equals(sensorCode)) { return null; } //O3另外计算 if (Constants.SENSOR_CODE_O3.equals(sensorCode)) { return null; } return DoubleStream.of(Double.parseDouble(sensorValue.toString())); }).average(); if (optionalDouble.isPresent()) { //银行家算法修约 double sciCal = AmendUtils.sciCal(optionalDouble.getAsDouble(), 4); jsonMap.put(sensorCode.toString(), sciCal); } }); dataMap.put("value", JSONObject.toJSONString(jsonMap)); insertData.add(dataMap); }); //存入数据库 historyMonthlyMapper.insertHistoryMonthly(insertData); } } screen-job/src/main/java/com/moral/api/service/impl/HistoryWeeklyServiceImpl.java
@@ -86,7 +86,7 @@ jsonMap.put(Constants.SENSOR_CODE_CO, coAvg); } //CO 90百分位计算并修约 //O3 90百分位计算并修约 Object o3Avg = AmendUtils.getO3AvgOfWeek(value); if (o3Avg != null) { jsonMap.put(Constants.SENSOR_CODE_O3, o3Avg); screen-job/src/main/java/com/moral/api/task/HistoryTableInsertTask.java
@@ -5,6 +5,7 @@ import com.moral.api.service.HistoryDailyService; import com.moral.api.service.HistoryFiveMinutelyService; import com.moral.api.service.HistoryMonthlyService; import com.moral.api.service.HistoryWeeklyService; import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.context.XxlJobHelper; @@ -21,6 +22,9 @@ @Autowired private HistoryWeeklyService historyWeeklyService; @Autowired private HistoryMonthlyService historyMonthlyService; //5分钟数据统计 @XxlJob("insertHistoryFiveMinutely") @@ -57,4 +61,16 @@ } return ReturnT.SUCCESS; } //月数据统计 @XxlJob("insertHistoryMonthly") public ReturnT insertHistoryMonthly() { try { historyMonthlyService.insertHistoryMonthly(); } catch (Exception e) { XxlJobHelper.log(e.getMessage()); return ReturnT.FAIL; } return ReturnT.SUCCESS; } } screen-job/src/main/resources/mapper/HistoryMonthlyMapper.xml
New file @@ -0,0 +1,20 @@ <?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.HistoryMonthlyMapper"> <!-- 通用查询映射结果 --> <resultMap id="BaseResultMap" type="com.moral.api.entity.HistoryMonthly"> <result column="mac" property="mac"/> <result column="time" property="time"/> <result column="value" property="value"/> </resultMap> <insert id="insertHistoryMonthly"> INSERT INTO history_weekly VALUES <foreach collection="list" item="item" separator=","> (#{item.mac},#{item.time},#{item.value}) </foreach> </insert> </mapper>