增加空气土壤设别填充任务

This commit is contained in:
zhangyue 2026-09-21 11:18:32 +08:00
parent 85b598a712
commit 7d6062d539
7 changed files with 241 additions and 6 deletions

View File

@ -1,7 +1,10 @@
package com.lanhai.service;
import com.lanhai.entity.SurvDeviceDeploy;
import com.lanhai.entity.SurvHisdataAir;
import com.baomidou.mybatisplus.extension.service.IService;
import com.lanhai.entity.SurvTransdataAir;
import com.lanhai.o.iot.task.DataExamineDetail;
/**
* <p>
@ -11,4 +14,5 @@ import com.baomidou.mybatisplus.extension.service.IService;
*/
public interface ISurvHisdataAirService extends IService<SurvHisdataAir> {
SurvTransdataAir genDataAuto(SurvDeviceDeploy deploy, DataExamineDetail config);
}

View File

@ -1,11 +1,34 @@
package com.lanhai.service.Impl;
import cn.hutool.core.bean.BeanUtil;
import com.lanhai.constant.IotConstants;
import com.lanhai.entity.SurvDeviceDeploy;
import com.lanhai.entity.SurvHisdataAir;
import com.lanhai.entity.SurvStationInfo;
import com.lanhai.entity.SurvTransdataAir;
import com.lanhai.mapper.SurvHisdataAirMapper;
import com.lanhai.o.iot.task.DataExamineDetail;
import com.lanhai.o.iot.task.DataExamineRule;
import com.lanhai.service.ISurvHisdataAirService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lanhai.service.ISurvStationInfoService;
import com.lanhai.util.BigDecimalRandomAdjuster;
import com.lanhai.util.DateTimeConverter;
import com.lanhai.util.DateTimeUtils;
import com.lanhai.util.TUtil;
import com.xxl.job.core.context.XxlJobHelper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* <p>
* 空气污染监测历史数据 服务实现类
@ -13,6 +36,155 @@ import org.springframework.stereotype.Service;
*
*/
@Service
@Slf4j
public class SurvHisdataAirServiceImpl extends ServiceImpl<SurvHisdataAirMapper, SurvHisdataAir> implements ISurvHisdataAirService {
@Lazy
@Autowired
private SurvTransdataAirServiceImpl airService;
@Autowired
@Lazy
private ISurvStationInfoService survStationInfoService;
public SurvTransdataAir genDataAuto(SurvDeviceDeploy deploy, DataExamineDetail config) {
if(deploy!=null && config!=null) {
if (config.getRules() != null && !config.getRules().isEmpty()) {
String deployCode = deploy.getDeployCode();
try {
LocalDateTime nowTime = LocalDateTime.now();
SurvTransdataAir newestData = airService.getOneByDeviceId(deploy.getId());
String orgId = null;
if (newestData != null) {
orgId = newestData.getId();
if (newestData.getDataDateTime() != null) {
log.error(newestData.getDataDateTime()+"====================时间检查=============="+nowTime);
long secs = DateTimeUtils.getSecondsDiff(newestData.getDataDateTime(), nowTime);
if (secs < config.getExamineGap()) {//间隔不足时不生成
log.warn("-------------空气设备:{}:{},土壤/水间隔为:{}秒,限制时间:{}秒,跳过--------------", deploy.getDeployDes(),deploy.getDeployCode(),secs,config.getExamineGap());
XxlJobHelper.log("--------------空气设备:{}:{},土壤/水间隔为:{}秒,,限制时间:{}秒,跳过---------------", deploy.getDeployDes(),deploy.getDeployCode(),secs,config.getExamineGap());
return null;
}
}
}
Map<String, DataExamineRule> ruleMap = new HashMap<>();
for (DataExamineRule rule : config.getRules()) {
ruleMap.put(rule.getFields(), rule);
}
SurvHisdataAir saveEnt = new SurvHisdataAir();
if (newestData == null) {
newestData = new SurvTransdataAir();
SurvHisdataAir hisData = getRecentData(deployCode,config.getHistoryRange());
if(hisData!=null){ //第一逻辑,如果有最近的历史数据,则使用历史数据作为基准值
XxlJobHelper.log("=====空气设备:{}===查询到历史数据,使用历史数据作为基准值==========",deployCode);
BeanUtil.copyProperties(hisData,newestData);
saveEnt.setRuleType(IotConstants.RULE_HISTORY);
}else{//第二逻辑,使用预设的标准值
XxlJobHelper.log("=====空气设备:{}===使用预设标准值进行生成==========",deployCode);
for (String s : ruleMap.keySet()) {
TUtil.setFieldValue(newestData, s, ruleMap.get(s).getStandard());
}
saveEnt.setRuleType(IotConstants.RULE_PRE);
}
}else{
long dateGap = DateTimeUtils.getDaysDiff(newestData.getDataDateTime(), nowTime);
XxlJobHelper.log("======空气设备:{}-{}=====土壤/水数据间隔{}天===限制天数:{}====",deploy.getDeployDes(),deploy.getDeployCode(),dateGap,config.getHistoryRange());
if (dateGap > config.getHistoryRange()) {//最新数据已经超过时限范围,则使用预设数据
XxlJobHelper.log("xxxx使用预设数据xxxxx");
for (String s : ruleMap.keySet()) {
TUtil.setFieldValue(newestData, s, ruleMap.get(s).getStandard());
}
saveEnt.setRuleType(IotConstants.RULE_PRE);
}else{
XxlJobHelper.log("xxxx使用原数据规则类型:{}xxxxx",newestData.getRuleType());
saveEnt.setRuleType(newestData.getRuleType());
}
}
saveEnt.setDeployId(deploy.getId());
saveEnt.setCorpId(deploy.getId());
saveEnt.setTenantId(deploy.getTenantId());
saveEnt.setStationCode(deploy.getStationCode());
saveEnt.setDeviceName(deploy.getDeployDes());
if(StringUtils.isNotBlank(deploy.getStationCode())){
SurvStationInfo info = survStationInfoService.getByCode(deploy.getStationCode());
if(info!=null){
saveEnt.setStationName(info.getStationName());
saveEnt.setStationId(info.getId());
}
}
saveEnt.setDataDateTime(nowTime);
saveEnt.setDataGatherType(IotConstants.MARK_GENDATA);
saveEnt.setDeployCode(deploy.getDeployCode());
saveEnt.setTenantId(deploy.getTenantId());
saveEnt.setTransDate(nowTime);
saveEnt.setReVision(0);
saveEnt.setCreatedBy("taskGen");
saveEnt.setCreateTime(nowTime);
saveEnt.setUpdatedBy("taskGen");
saveEnt.setIsDel(0);
saveEnt.setUpdatedTime(nowTime);
//生成数据
for (String key : ruleMap.keySet()) {
DataExamineRule rule = ruleMap.get(key);
String vals = TUtil.getFieldValue(newestData, rule.getFields(), String.class);
if (StringUtils.isNotBlank(vals)) {
BigDecimal fixedRange = new BigDecimal(rule.getFactor());
int scale = rule.getScale();//小数位
String correctVal = BigDecimalRandomAdjuster.randomAdjustByFixedRange(new BigDecimal(vals), fixedRange, scale);
if(correctVal.contains("-") || "0".equals(correctVal)){//如果是负数或0用原来的值
correctVal = vals;
}
TUtil.setFieldValue(saveEnt, rule.getFields(), correctVal);
} else {
log.error("==========空气设备:{}指标:{}配置错误==============,跳过",deploy.getDeployCode(), rule.getFields());
XxlJobHelper.log("==========空气设备:{},编号:{}指标:{}配置错误==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
}
}
save(saveEnt);
BeanUtil.copyProperties(saveEnt, newestData);
newestData.setId(orgId);
newestData.setTenantId(deploy.getTenantId());
newestData.setDataDateTime(nowTime);
newestData.setRuleType(newestData.getRuleType());
newestData.setDataGatherType(newestData.getDataGatherType());
airService.saveOrUpdate(newestData);
return newestData;
}catch (Exception e) {
e.printStackTrace();
XxlJobHelper.log(e.getMessage());
log.error(e.getMessage());
}
}
}
return null;
}
private SurvHisdataAir getRecentData(String deployCode,Integer dayRange) {
LocalDateTime daysAgo = LocalDateTime.now().minusDays(dayRange);
SurvHisdataAir oldAir = lambdaQuery()
.eq(SurvHisdataAir::getDeployCode,deployCode)
.ge(SurvHisdataAir::getDataDateTime, DateTimeConverter.toDate(daysAgo))
.orderByDesc(SurvHisdataAir::getDataDateTime)
.last("limit 1")
.one();
return oldAir;
}
}

View File

@ -179,4 +179,4 @@ public class SurvHisdataSoilServiceImpl extends ServiceImpl<SurvHisdataSoilMappe
return oldWater;
}
}
}

View File

@ -2,6 +2,7 @@ package com.lanhai.service.Impl;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
@ -95,7 +96,8 @@ public class SurvTransdataSoilServiceImpl extends ServiceImpl<SurvTransdataSoilM
// soil.setDataSoilNoion(baowenMap.get("e13"));//土壤硝酸根离子
int soilCount = 0;
String formatSoilTime = kDateTime!=null?DateUtil.format(kDateTime,"yyyy-MM-dd HH:mm:ss"):"";
if(!formatSoilTime.equals(DataTime)){
boolean soilOk = checkDateOfData(kDateTime,DataTime);
if(soilOk){
if(!survDeviceDeploy.getScEquZhibiaoList().isEmpty()){
for (ScEquZhibiao scEquZhibiao : survDeviceDeploy.getScEquZhibiaoList()) {
boolean isSuccess = TUtil.setFieldValue(soil,scEquZhibiao.getEntityField(),baowenMap.get(scEquZhibiao.getCode()));
@ -174,7 +176,9 @@ public class SurvTransdataSoilServiceImpl extends ServiceImpl<SurvTransdataSoilM
// air.setDataLeafWet(baowenMap.get("e15"));//叶面湿度
int airCount = 0;
String formatAirTime = airkDateTime!=null?DateUtil.format(airkDateTime,"yyyy-MM-dd HH:mm:ss"):"";
if(!formatAirTime.equals(DataTime)){//数据时间不同才进行入库
boolean airOk = checkDateOfData(airkDateTime,DataTime);
if(airOk){//数据时间不同才进行入库
if(!survDeviceDeploy.getScEquZhibiaoList().isEmpty()){
for (ScEquZhibiao scEquZhibiao : survDeviceDeploy.getScEquZhibiaoList()) {
// System.out.println(baowenMap.get(scEquZhibiao.getCode())+"============="+scEquZhibiao.getCode()+"=========airCheck=============="+scEquZhibiao.getEquId()+"==========="+scEquZhibiao.getEntityField());
@ -403,4 +407,18 @@ public class SurvTransdataSoilServiceImpl extends ServiceImpl<SurvTransdataSoilM
alertRecordService.saveBaojingV2(baowenMap,deploy,deviceDeployMap);
}
}
public boolean checkDateOfData(LocalDateTime localDateTime,String dataStr){
if(localDateTime!=null && StringUtils.isNotBlank(dataStr)){
LocalDateTime parsDataTime = LocalDateTimeUtil.parse(dataStr,"yyyy-MM-dd HH:mm:ss");
if(parsDataTime.isAfter(localDateTime)){
return Boolean.TRUE;
}else{
return Boolean.FALSE;
}
}
return Boolean.TRUE;
}
}

View File

@ -42,6 +42,9 @@ public class DataExamineTask {
private ISurvHisdataSoilService soilService;
@Autowired
private ISurvHisdataVocsService vocsService;
@Autowired
private ISurvHisdataAirService airService;
@XxlJob("DataExamineGather")
public void TaskHandler() throws Exception{
@ -112,6 +115,26 @@ public class DataExamineTask {
}else{
log.error("----------new设备号4:{},跳过------------",deploy.getDeployCode());
}
}else if (deploy.getDeployType().equals(PollutionConstants.AIR_SURV)) {
SurvTransdataAir air = airService.genDataAuto(deploy,config);
if(air!=null){
updDeploy.add(deploy);
log.error("----------空气new设备号:{},生成成功,时间:{}-----------",deploy.getDeployCode(),sdf.format(air.getDataDateTime()));
XxlJobHelper.log("----------空气new设备号:{},生成成功,时间:{}-----------",deploy.getDeployCode(),sdf.format(air.getDataDateTime()));
}else{
log.error("----------空气new设备号:{},跳过------------",deploy.getDeployCode());
XxlJobHelper.log("----------空气new设备号:{},跳过------------",deploy.getDeployCode());
}
}else if (deploy.getDeployType().equals(PollutionConstants.SOIL_SURV)) {
SurvTransdataSoil soil = soilService.genDataAuto(deploy,config);
if(soil!=null){
updDeploy.add(deploy);
log.error("----------土壤new设备号:{},生成成功,时间:{}-----------",deploy.getDeployCode(),sdf.format(soil.getDataDateTime()));
XxlJobHelper.log("----------土壤ew设备号:{},生成成功,时间:{}-----------",deploy.getDeployCode(),sdf.format(soil.getDataDateTime()));
}else{
log.error("----------土壤new设备号:{},跳过------------",deploy.getDeployCode());
XxlJobHelper.log("----------土壤new设备号:{},跳过------------",deploy.getDeployCode());
}
}
}
}

View File

@ -7,6 +7,7 @@ import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
@ -22,6 +23,24 @@ public class DateTimeUtils {
return Duration.between(start, end).getSeconds();
}
/**
* 1. 基础用法:获取总秒数差
*/
public static long getSecondsDiff(Date start, Date end) {
validateParams(start, end);
long diffInMillis = end.getTime() - start.getTime();
return diffInMillis / 1000;
}
/**
* 参数校验
*/
private static void validateParams(Date start, Date end) {
if (start == null || end == null) {
throw new IllegalArgumentException("开始时间和结束时间不能为null");
}
}
/**
* 2. 获取总毫秒数差
*/

View File

@ -3,8 +3,7 @@ package com.lanhai.test;
public class testway {
public static void main(String[] args) {
long now = -300;
long f= -400;
System.out.println(f < now);
String ss1= "2026-05-01";
String ss2="2026-05-02";
}
}