package com.zhitan.model.service.impl;
|
|
import com.zhitan.common.enums.CalcType;
|
import com.zhitan.common.enums.TimeType;
|
import com.zhitan.common.utils.StringUtils;
|
import com.zhitan.model.domain.*;
|
import com.zhitan.model.mapper.IndexStorageMapper;
|
import com.zhitan.model.service.IEnergyIndexService;
|
import com.zhitan.model.service.IIndexStorageService;
|
import com.zhitan.model.service.IndexFormulaService;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
|
import java.util.*;
|
import java.util.stream.Collectors;
|
|
import static org.apache.commons.lang3.StringUtils.equalsIgnoreCase;
|
|
/**
|
* 指标存储服务实现类
|
*/
|
@Service
|
public class IndexStorageServiceImpl implements IIndexStorageService {
|
|
@Autowired
|
private IndexStorageMapper indexStorageMapper;
|
@Autowired
|
private IndexFormulaService indexFormulaService;
|
@Autowired
|
private IEnergyIndexService energyIndexService;
|
|
/**
|
* 保存指标存储信息
|
* @param indexId 指标ID
|
* @param indexStorage 指标存储列表
|
*/
|
@Override
|
public void saveIndexStorage(String indexId, List<IndexStorage> indexStorage) {
|
// 遍历每个存储对象进行存储操作
|
indexStorage.forEach(storage -> {
|
storage.setIndexId(indexId); // 设置指标ID
|
if (StringUtils.isEmpty(storage.getId())) { // 新增记录
|
storage.setId(UUID.randomUUID().toString());
|
indexStorageMapper.insertIndexStorage(storage);
|
} else { // 更新记录
|
indexStorageMapper.updateIndexStorage(storage);
|
}
|
// 保存参数关联关系
|
indexStorageMapper.saveParams(storage.getId(), storage.getParamIndex());
|
});
|
}
|
|
/**
|
* 保存公式及存储信息(事务方法)
|
* @param indexFormula 公式对象
|
* @param indexStorage 存储列表
|
* @param indexId 指标ID
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void saveFormulaAndStorage(IndexFormula indexFormula, List<IndexStorage> indexStorage,
|
String indexId) {
|
// 1. 保存公式信息
|
indexFormulaService.saveIndexFormula(indexFormula);
|
|
// 2. 处理公式文本参数替换
|
String calcText = indexFormula.getFormulaText();
|
List<String> paramNames = new ArrayList<>();
|
for (IndexFormulaParam param : indexFormula.getIndexFormulaParams()) {
|
calcText = calcText
|
.replace("(" + param.getParamName() + ")", "(" + String.format("'%s'", param.getParamValue()) + ")");
|
paramNames.add(param.getParamValue()); // 记录参数名称
|
}
|
|
// 3. 获取参数指标
|
List<EnergyIndex> energyIndexList = energyIndexService.getEnergyIndexByCodes(paramNames);
|
List<String> paramIndex = energyIndexList.stream()
|
.map(EnergyIndex::getIndexId)
|
.collect(Collectors.toList());
|
|
// 4. 补充存储对象信息
|
for (IndexStorage storage : indexStorage) {
|
storage.setCalcText(calcText); // 存储计算公式文本
|
storage.setIsPvCalc(indexFormula.getIsPvCalc()); // 存储实时计算标识
|
storage.getParamIndex().addAll(paramIndex); // 添加参数指标ID
|
}
|
|
// 5. 保存存储信息
|
saveIndexStorage(indexId, indexStorage);
|
}
|
|
/**
|
* 根据指标ID获取存储信息
|
* @param indexId 指标ID
|
* @return 指标存储列表
|
*/
|
@Override
|
public List<IndexStorage> getIndexStorage(String indexId) {
|
return indexStorageMapper.getIndexStorage(indexId);
|
}
|
|
/**
|
* 获取所有计算类存储信息
|
* @return 包含参数的存储列表
|
*/
|
@Override
|
public List<IndexStorage> getAllCalcIndexStorage() {
|
// 1. 查询计算类型存储
|
List<IndexStorage> indexStorageList = indexStorageMapper.getAllCalcIndexStorage(CalcType.CALC);
|
|
// 2. 查询所有参数信息
|
List<IndexStorageParam> params = indexStorageMapper.getAllParameter();
|
|
// 3. 将参数信息合并到存储对象
|
indexStorageList.forEach(storage -> {
|
List<String> indexIds = params.stream()
|
.filter(f -> equalsIgnoreCase(f.getStorageId(), storage.getId()))
|
.map(IndexStorageParam::getIndexId)
|
.collect(Collectors.toList());
|
if (!indexIds.isEmpty()) {
|
storage.getParamIndex().addAll(indexIds);
|
}
|
});
|
return indexStorageList;
|
}
|
|
/**
|
* 根据指标ID和时间类型获取特定存储
|
* @param indexId 指标ID
|
* @param timeType 时间类型
|
* @return 指定时间类型的存储信息
|
*/
|
@Override
|
public IndexStorage getIndexStorage(String indexId, TimeType timeType) {
|
return indexStorageMapper.getWithTimetype(indexId, timeType);
|
}
|
}
|