Browse Source

修复物计算

2025年7月14日16:33:01
qingyuan_dev_new
lishuai 6 months ago
parent
commit
230bfc8406
  1. 5
      common/tskv/src/main/java/com/thing/common/tskv/service/TsKvService.java
  2. 10
      common/tskv/src/main/java/com/thing/common/tskv/service/TsKvServiceImpl.java
  3. 5
      common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestBaseService.java
  4. 10
      common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestCkServiceImpl.java
  5. 10
      common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestMyServiceImpl.java
  6. 27
      common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestPgServiceImpl.java
  7. 87
      modules/calculation/src/main/java/com/thing/calculation/handler/CalcExecuteHandler.java
  8. 2
      modules/calculation/src/main/java/com/thing/calculation/service/impl/CalcLogServiceImpl.java

5
common/tskv/src/main/java/com/thing/common/tskv/service/TsKvService.java

@ -105,6 +105,10 @@ public interface TsKvService {
*/
TsKvDTO findLatestByCodeAndAttr(String code, String attrKey);
TsKvDTO findLatestByCodeAndAttrAndTime(String code, String attrKey,Long time);
TsKvDTO findLatestByCodeAndAttrLtTime(String code, String attrKey, Long time);
/**
* 单设备 多属性 最新值
@ -147,6 +151,7 @@ public interface TsKvService {
List<TsKvDTO> findLatestByMultiMap(Map<String, Collection<String>> multiMap, Long startTime, Long endTime, Boolean isAsc);
/**
* 多设备 多属性 最新值 分页
*

10
common/tskv/src/main/java/com/thing/common/tskv/service/TsKvServiceImpl.java

@ -119,6 +119,16 @@ public class TsKvServiceImpl implements TsKvService {
return latestBaseService.findLatestByCodeAndAttr(code, attrKey);
}
@Override
public TsKvDTO findLatestByCodeAndAttrAndTime(String code, String attrKey, Long time) {
return null;
}
@Override
public TsKvDTO findLatestByCodeAndAttrLtTime(String code, String attrKey, Long time) {
return null;
}
@Override
public List<TsKvDTO> findLatestByCodeAndAttrs(String code, Collection<String> attrList, Boolean isAsc) {
return latestBaseService.findLatestByCodeAndAttrs(code, attrList, isAsc);

5
common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestBaseService.java

@ -23,6 +23,11 @@ public interface LatestBaseService {
*/
TsKvDTO findLatestByCodeAndAttr(String code, String attrKey);
TsKvDTO findLatestByCodeAndAttrAndTime(String code, String attrKey,Long time);
TsKvDTO findLatestByCodeAndAttrLeTime(String code, String attrKey,Long time);
/**
* 单设备 多属性 最新值
*

10
common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestCkServiceImpl.java

@ -58,6 +58,16 @@ public class LatestCkServiceImpl extends ServiceImpl<TsKvLatestCkMapper, TsKvLat
return viewLatestCkService.findLatestByCodeAndAttr(code, attrKey);
}
@Override
public TsKvDTO findLatestByCodeAndAttrAndTime(String code, String attrKey, Long time) {
return null;
}
@Override
public TsKvDTO findLatestByCodeAndAttrLeTime(String code, String attrKey, Long time) {
return null;
}
@Override
public List<TsKvDTO> findLatestByCodeAndAttrs(String code, Collection<String> attrList, Boolean isAsc) {
return viewLatestCkService.findLatestByCodeAndAttrs(code, attrList, isAsc);

10
common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestMyServiceImpl.java

@ -69,6 +69,16 @@ public class LatestMyServiceImpl extends ServiceImpl<TsKvLatestMyMapper, TsKvLat
return this.getOneAs(queryWrapper, TsKvDTO.class);
}
@Override
public TsKvDTO findLatestByCodeAndAttrAndTime(String code, String attrKey, Long time) {
return null;
}
@Override
public TsKvDTO findLatestByCodeAndAttrLeTime(String code, String attrKey, Long time) {
return null;
}
@Override
public List<TsKvDTO> findLatestByCodeAndAttrs(String code, Collection<String> attrList, Boolean isAsc) {

27
common/tskv/src/main/java/com/thing/common/tskv/service/latest/LatestPgServiceImpl.java

@ -53,10 +53,10 @@ public class LatestPgServiceImpl extends ServiceImpl<TsKvLatestPgMapper, TsKvLat
private QueryWrapper getQueryWrapper(String code,Collection<String> codeList, String attrKey,Collection<String> attrList,Boolean isAsc) {
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq(TsKvLatestMy::getThingCode, code, StringUtils::isNotBlank)
.eq(TsKvLatestMy::getAttrKey, attrKey, StringUtils::isNotBlank)
.in(TsKvLatestMy::getAttrKey, attrList, CollectionUtils.isNotEmpty(attrList))
.in(TsKvLatestMy::getThingCode, codeList, CollectionUtils.isNotEmpty(codeList))
.orderBy(TsKvLatestMy::getTs, isAsc);
.eq(TsKvLatestPg::getAttrKey, attrKey, StringUtils::isNotBlank)
.in(TsKvLatestPg::getAttrKey, attrList, CollectionUtils.isNotEmpty(attrList))
.in(TsKvLatestPg::getThingCode, codeList, CollectionUtils.isNotEmpty(codeList))
.orderBy(TsKvLatestPg::getTs, isAsc);
return queryWrapper;
}
@ -66,6 +66,25 @@ public class LatestPgServiceImpl extends ServiceImpl<TsKvLatestPgMapper, TsKvLat
return this.getOneAs(queryWrapper, TsKvDTO.class);
}
@Override
public TsKvDTO findLatestByCodeAndAttrAndTime(String code, String attrKey, Long time) {
QueryWrapper queryWrapper = QueryWrapper.create()
.eq(TsKvLatestPg::getThingCode, code)
.eq(TsKvLatestPg::getAttrKey, attrKey)
.eq(TsKvLatestPg::getTs, time)
.orderBy(TsKvLatestPg::getTs, false);
return this.getOneAs(queryWrapper, TsKvDTO.class);
}
@Override
public TsKvDTO findLatestByCodeAndAttrLeTime(String code, String attrKey, Long time) {
QueryWrapper queryWrapper = QueryWrapper.create()
.eq(TsKvLatestPg::getThingCode, code)
.eq(TsKvLatestPg::getAttrKey, attrKey)
.lt(TsKvLatestPg::getTs, time).orderBy(TsKvLatestPg::getTs, false).limit(1);
return this.getOneAs(queryWrapper, TsKvDTO.class);
}
@Override
public List<TsKvDTO> findLatestByCodeAndAttrs(String code, Collection<String> attrList, Boolean isAsc) {
QueryWrapper queryWrapper = getQueryWrapper(code, null,null,attrList,isAsc);

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

@ -1,5 +1,6 @@
package com.thing.calculation.handler;
import cn.hutool.core.map.MapUtil;
import com.thing.calculation.dto.CalcTargetConfigDTO;
import com.thing.calculation.dto.ExecuteCalcRequest;
import com.thing.calculation.dto.LogSourceInfoValue;
@ -13,6 +14,8 @@ import com.thing.calculation.service.CalcTargetConfigService;
import com.thing.common.core.utils.FormulaUtil;
import com.thing.common.data.event.QueueConsumerEvent;
import com.thing.common.data.proto.QueueProto;
import com.thing.common.data.tskv.TsKvDTO;
import com.thing.common.tskv.service.TsKvService;
import com.thing.queue.util.Topics;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@ -29,6 +32,8 @@ import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.thing.calculation.service.impl.CalcLogServiceImpl.MAX_CALC_COUNT;
/**
* @author siyang
* @date 2024-03-05
@ -43,6 +48,7 @@ public class CalcExecuteHandler {
private final ApplicationEventPublisher publisher;
@Lazy @Resource private CalcTargetConfigService calcTargetConfigService;
@Lazy @Resource private CalcSourceConfigService calcSourceConfigService;
private final TsKvService tsKvService;
/**
* 执行计算的入口<br>
@ -141,20 +147,46 @@ public class CalcExecuteHandler {
}
}
/** 映射计算:无论是一个还是多个,统统加起来总是不会错的 */
/** 映射计算:无论是一个还是多个,统统加起来总是不会错的 MAX_CALC_COUNT */
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"));
if(calcLog.getCalcCount() >= MAX_CALC_COUNT){
//找配置并且遍历配置
List<CalcSourceConfigEntity> sourceConfigEntities = calcSourceConfigService.findByTargetId(calcLog.getCalcTargetConfigId());
for (CalcSourceConfigEntity sourceConfigEntity : sourceConfigEntities) {
//找别名因为上面的data是别名作为key的
String sourceAttrAlias = sourceConfigEntity.getSourceAttrAlias();
if(MapUtil.isNotEmpty(data)){
//找数据若是数据是空的那就设置为0否则 就找相同时间的数据若是相同时间的数据没有就当前时间最近的一笔但是仅仅是非 年的属性
LogSourceInfoValue logSourceInfoValue = data.get(sourceAttrAlias);
if(Objects.isNull(logSourceInfoValue))
{
//并且不能以amhhddmmyy结尾
if(!sourceConfigEntity.getSourceAttrCode().endsWith("am") || !sourceConfigEntity.getSourceAttrCode().endsWith("hh")
|| !sourceConfigEntity.getSourceAttrCode().endsWith("dd") || !sourceConfigEntity.getSourceAttrCode().endsWith("mm")
|| !sourceConfigEntity.getSourceAttrCode().endsWith("yy")){
TsKvDTO tsKvDTO = tsKvService.findLatestByCodeAndAttrAndTime(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(), calcLog.getTime());
if(!Objects.isNull(tsKvDTO)){
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),tsKvDTO.getVal()));
}else{
TsKvDTO tsKvDTO1 = tsKvService.findLatestByCodeAndAttrLtTime(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(), calcLog.getTime());
if(!Objects.isNull(tsKvDTO1)){
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),tsKvDTO1.getVal()));
}else{
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),"0"));
}
}
}else{
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),"0"));
}
}
}else{
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),"0"));
}
}
}
try {
BigDecimal totalVal =
data.values().stream()
@ -173,6 +205,43 @@ public class CalcExecuteHandler {
/** 公式计算 */
private void doCalc(CalcLogEntity calcLog) {
Map<String, LogSourceInfoValue> data = calcLog.parseSourceInfo();
if(calcLog.getCalcCount() >= MAX_CALC_COUNT){
//找配置并且遍历配置
List<CalcSourceConfigEntity> sourceConfigEntities = calcSourceConfigService.findByTargetId(calcLog.getCalcTargetConfigId());
for (CalcSourceConfigEntity sourceConfigEntity : sourceConfigEntities) {
//找别名因为上面的data是别名作为key的
String sourceAttrAlias = sourceConfigEntity.getSourceAttrAlias();
if(MapUtil.isNotEmpty(data)){
//找数据若是数据是空的那就设置为0否则 就找相同时间的数据若是相同时间的数据没有就当前时间最近的一笔但是仅仅是非 年的属性
LogSourceInfoValue logSourceInfoValue = data.get(sourceAttrAlias);
if(Objects.isNull(logSourceInfoValue))
{
//并且不能以amhhddmmyy结尾
if(!sourceConfigEntity.getSourceAttrCode().endsWith("am") || !sourceConfigEntity.getSourceAttrCode().endsWith("hh")
|| !sourceConfigEntity.getSourceAttrCode().endsWith("dd") || !sourceConfigEntity.getSourceAttrCode().endsWith("mm")
|| !sourceConfigEntity.getSourceAttrCode().endsWith("yy")){
TsKvDTO tsKvDTO = tsKvService.findLatestByCodeAndAttrAndTime(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(), calcLog.getTime());
if(!Objects.isNull(tsKvDTO)){
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),tsKvDTO.getVal()));
}else{
TsKvDTO tsKvDTO1 = tsKvService.findLatestByCodeAndAttrLtTime(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(), calcLog.getTime());
if(!Objects.isNull(tsKvDTO1)){
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),tsKvDTO1.getVal()));
}else{
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),"0"));
}
}
}else{
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),"0"));
}
}
}else{
data.put(sourceAttrAlias, new LogSourceInfoValue(sourceConfigEntity.getSourceThingCode(), sourceConfigEntity.getSourceAttrCode(),"0"));
}
}
}
try {
Map<String, Object> variableMap = new HashMap<>();
data.forEach((k, v) -> variableMap.put(k, v.convertVal()));

2
modules/calculation/src/main/java/com/thing/calculation/service/impl/CalcLogServiceImpl.java

@ -47,7 +47,7 @@ public class CalcLogServiceImpl extends BaseServiceImpl<CalcLogMapper, CalcLogEn
private static final Long TIMEOUT_INTERVAL = 1000 * 60 * 60 * 24 * 7L;
/** 计算失败后,计算异常状态下的最大计算次数 */
private static final Integer MAX_CALC_COUNT = 50;
public static final Integer MAX_CALC_COUNT = 10;
@Override
public QueryWrapper getWrapper(Map<String, Object> params) {

Loading…
Cancel
Save