Browse Source

物计算给默认值

2025年5月7日12:05:57
qingyuan_dev_new
lishuai 8 months ago
parent
commit
3ba3af45e8
  1. 10
      common/script/src/main/java/com/thing/api/nashorn/DefaultNashornInvokeService.java
  2. 12
      common/tskv/src/main/java/com/thing/common/tskv/service/TsKvNativeSQL.java
  3. 14
      modules/calculation/src/main/java/com/thing/calculation/handler/CalcExecuteHandler.java

10
common/script/src/main/java/com/thing/api/nashorn/DefaultNashornInvokeService.java

@ -13,6 +13,7 @@ import com.thing.common.util.thread.ThingExecutors;
import delight.nashornsandbox.NashornSandbox;
import delight.nashornsandbox.NashornSandboxes;
import delight.nashornsandbox.exceptions.ScriptCPUAbuseException;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
@ -22,12 +23,14 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.ReentrantLock;
import javax.script.CompiledScript;
import javax.script.ScriptException;
/**
@ -126,4 +129,11 @@ public class DefaultNashornInvokeService extends AbstractScriptInvokeService imp
NashornScript script = scriptInfoMap.remove(scriptId);
sandbox.eval(script.getFunctionName() + " = undefined;");
}
public static void main(String[] args) throws ScriptException {
NashornSandbox sandbox = NashornSandboxes.create();
CompiledScript script = sandbox.compile("10 + 1");
int result1 = (int) sandbox.eval(script);
int result2 = (int) sandbox.eval(script);
}
}

12
common/tskv/src/main/java/com/thing/common/tskv/service/TsKvNativeSQL.java

@ -15,6 +15,7 @@ import org.joda.time.DateTime;
import java.math.BigDecimal;
import java.util.*;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
@ -41,7 +42,16 @@ public class TsKvNativeSQL {
if (protoList.isEmpty()) {
return 0;
}
List<DataProto> list = new LinkedHashSet<>(protoList).stream().toList();
// List<DataProto> list = new LinkedHashSet<>(protoList).stream().toList();
List<DataProto> list = protoList.stream()
.collect(Collectors.toMap(d -> List.of(
d.getTskvProto().getThingCode(),
d.getTskvProto().getTs(),
d.getTskvProto().getKey()
), Function.identity(), BinaryOperator.maxBy(Comparator.comparing(d -> d.getTskvProto().getVal()))))
.values()
.stream()
.toList();
String sql = sqlSaveProtoTsKv(list, dataType);
try {
if(StringUtils.isNotEmpty(sql)){

14
modules/calculation/src/main/java/com/thing/calculation/handler/CalcExecuteHandler.java

@ -4,9 +4,11 @@ import com.thing.calculation.dto.CalcTargetConfigDTO;
import com.thing.calculation.dto.ExecuteCalcRequest;
import com.thing.calculation.dto.LogSourceInfoValue;
import com.thing.calculation.entity.CalcLogEntity;
import com.thing.calculation.entity.CalcSourceConfigEntity;
import com.thing.calculation.enumeration.CalcConfigType;
import com.thing.calculation.enumeration.CalcStatus;
import com.thing.calculation.service.CalcLogService;
import com.thing.calculation.service.CalcSourceConfigService;
import com.thing.calculation.service.CalcTargetConfigService;
import com.thing.common.core.utils.FormulaUtil;
import com.thing.common.data.event.QueueConsumerEvent;
@ -40,6 +42,7 @@ public class CalcExecuteHandler {
private final CalcLogService calcLogService;
private final ApplicationEventPublisher publisher;
@Lazy @Resource private CalcTargetConfigService calcTargetConfigService;
@Lazy @Resource private CalcSourceConfigService calcSourceConfigService;
/**
* 执行计算的入口<br>
@ -141,6 +144,17 @@ public class CalcExecuteHandler {
/** 映射计算:无论是一个还是多个,统统加起来总是不会错的 */
private void doMapping(CalcLogEntity calcLog) {
Map<String, LogSourceInfoValue> data = calcLog.parseSourceInfo();
if(calcLog.getCalcCount()>= 5){
String formula = calcLog.getFormula();
String[] split = formula.split("\\+");
List<CalcSourceConfigEntity> calcSourceConfigEntities = calcSourceConfigService.findByTargetId(calcLog.getCalcTargetConfigId());
for (String s : split) {
Optional<CalcSourceConfigEntity> first = calcSourceConfigEntities.stream().filter(config -> config.getSourceAttrAlias().equals(s)).findFirst();
if (first.isPresent() && !data.containsKey(s)) {
data.put(s, new LogSourceInfoValue(first.get().getSourceThingCode(), first.get().getSourceAttrCode(),"0"));
}
}
}
try {
BigDecimal totalVal =
data.values().stream()

Loading…
Cancel
Save