|
|
|
@ -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)) |
|
|
|
{ |
|
|
|
//并且不能以am,hh,dd,mm,yy结尾 |
|
|
|
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)) |
|
|
|
{ |
|
|
|
//并且不能以am,hh,dd,mm,yy结尾 |
|
|
|
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())); |
|
|
|
|