baoshiwei
2025-05-06 8dd7af51db4cf768fbf92002b5b62f7fb9d1c929
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
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);
    }
}