增加水样生成逻辑

This commit is contained in:
zy 2025-12-06 16:44:51 +08:00
parent 8a9985dd8b
commit 9963d93d61
3 changed files with 147 additions and 17 deletions

View File

@ -301,19 +301,26 @@ public class SurvHisdataLivestockwaterServiceImpl extends ServiceImpl<SurvHisdat
//生成数据
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(hisdataLivestockwater, rule.getFields(), correctVal);
} else {
log.error("==========设备:{},编号:{}指标:{}配置错误==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
XxlJobHelper.log("==========设备:{},编号:{}指标:{}配置错误==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
}
XxlJobHelper.log("====={}===生成==={}=====",key,rule.getFields());
boolean hasField = TUtil.hasField(SurvTransdataLivestockwater.class,rule.getFields());
if(hasField){
String vals = TUtil.getFieldValue(newestData, rule.getFields(), String.class);
if (StringUtils.isBlank(vals)) {//没有值使用预设值
XxlJobHelper.log("畜禽历史数据没有{}值,使用预设值{}",rule.getFields(),rule.getStandard());
vals = rule.getStandard();
}
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(hisdataLivestockwater, rule.getFields(), correctVal);
}else{
log.error("==========设备:{},编号:{}指标:{}配置错误无效字段==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
XxlJobHelper.log("==========设备:{},编号:{}指标:{}配置错误无效字段==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
}
}
save(hisdataLivestockwater);

View File

@ -322,8 +322,14 @@ public class SurvHisdataOrientwaterServiceImpl extends ServiceImpl<SurvHisdataOr
for (String key : ruleMap.keySet()) {
DataExamineRule rule = ruleMap.get(key);
String vals = TUtil.getFieldValue(newestData, rule.getFields(), String.class);
if (StringUtils.isNotBlank(vals)) {
XxlJobHelper.log("====={}===生成==={}=====",key,rule.getFields());
boolean hasField = TUtil.hasField(SurvTransdataOrientwater.class,rule.getFields());
if(hasField){
String vals = TUtil.getFieldValue(newestData, rule.getFields(), String.class);
if (StringUtils.isBlank(vals)) {//没有值使用预设值
XxlJobHelper.log("面源历史数据没有{}值,使用预设值{}",rule.getFields(),rule.getStandard());
vals = rule.getStandard();
}
BigDecimal fixedRange = new BigDecimal(rule.getFactor());
int scale = rule.getScale();//小数位
String correctVal = BigDecimalRandomAdjuster.randomAdjustByFixedRange(new BigDecimal(vals), fixedRange, scale);
@ -332,9 +338,10 @@ public class SurvHisdataOrientwaterServiceImpl extends ServiceImpl<SurvHisdataOr
}
TUtil.setFieldValue(hisdataOrientwater, rule.getFields(), correctVal);
} else {
log.error("==========设备:{},编号:{}指标:{}配置错误==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
XxlJobHelper.log("==========设备:{},编号:{}指标:{}配置错误==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
log.error("==========设备:{},编号:{}指标:{}配置错误无效字段==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
XxlJobHelper.log("==========设备:{},编号:{}指标:{}配置错误无效字段==============,跳过", deploy.getDeployDes(), deploy.getDeployCode(), rule.getFields());
}
}
save(hisdataOrientwater);

View File

@ -1,6 +1,7 @@
package com.lanhai.util;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@ -178,4 +179,119 @@ public class TUtil {
return result;
}
/**
* 判断实体类中是否存在指定字段
* @param clazz 实体类Class对象
* @param fieldName 字段名
* @return 是否存在该字段
*/
public static boolean hasField(Class<?> clazz, String fieldName) {
if (clazz == null || fieldName == null || fieldName.trim().isEmpty()) {
return false;
}
try {
// 获取本类及其所有父类的字段
Class<?> currentClass = clazz;
while (currentClass != null && currentClass != Object.class) {
Field field = currentClass.getDeclaredField(fieldName);
if (field != null) {
return true;
}
currentClass = currentClass.getSuperclass();
}
} catch (NoSuchFieldException e) {
// 字段不存在继续查找父类
return hasFieldInSuperClass(clazz.getSuperclass(), fieldName);
}
return false;
}
private static boolean hasFieldInSuperClass(Class<?> clazz, String fieldName) {
if (clazz == null || clazz == Object.class) {
return false;
}
try {
Field field = clazz.getDeclaredField(fieldName);
return field != null;
} catch (NoSuchFieldException e) {
return hasFieldInSuperClass(clazz.getSuperclass(), fieldName);
}
}
/**
* 判断实体类中是否存在指定字段不区分大小写
* @param clazz 实体类Class对象
* @param fieldName 字段名
* @return 是否存在该字段
*/
public static boolean hasFieldIgnoreCase(Class<?> clazz, String fieldName) {
if (clazz == null || fieldName == null || fieldName.trim().isEmpty()) {
return false;
}
Field[] fields = getAllFields(clazz);
for (Field field : fields) {
if (field.getName().equalsIgnoreCase(fieldName)) {
return true;
}
}
return false;
}
/**
* 获取实体类所有字段包括父类
* @param clazz 实体类Class对象
* @return 字段数组
*/
public static Field[] getAllFields(Class<?> clazz) {
List<Field> fieldList = new ArrayList<>();
Class<?> currentClass = clazz;
while (currentClass != null && currentClass != Object.class) {
Field[] fields = currentClass.getDeclaredFields();
for (Field field : fields) {
// 跳过静态字段
if (!Modifier.isStatic(field.getModifiers())) {
fieldList.add(field);
}
}
currentClass = currentClass.getSuperclass();
}
return fieldList.toArray(new Field[0]);
}
/**
* 获取字段类型
* @param clazz 实体类Class对象
* @param fieldName 字段名
* @return 字段类型不存在返回null
*/
public static Class<?> getFieldType(Class<?> clazz, String fieldName) {
if (!hasField(clazz, fieldName)) {
return null;
}
try {
Class<?> currentClass = clazz;
while (currentClass != null && currentClass != Object.class) {
try {
Field field = currentClass.getDeclaredField(fieldName);
return field.getType();
} catch (NoSuchFieldException e) {
currentClass = currentClass.getSuperclass();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}