z1415143022
2025-03-25 75f9894ebac12b13c7ead27c3e16db833ee9251a
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
package com.zhitan.dataitem.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhitan.common.enums.TimeType;
import com.zhitan.consumptionanalysis.domain.vo.RankingEnergyData;
import com.zhitan.dataitem.domain.StagseDataEntry;
import com.zhitan.dataitem.domain.vo.NodeIndexValueVO;
import com.zhitan.dataitem.mapper.DataItemMapper;
import com.zhitan.dataitem.service.IDataItemService;
import com.zhitan.model.domain.ModelNode;
import com.zhitan.model.mapper.EnergyIndexMapper;
import com.zhitan.model.mapper.ModelNodeMapper;
import com.zhitan.model.mapper.NodeIndexMapper;
import com.zhitan.realtimedata.domain.DataItem;
import com.zhitan.statisticalAnalysis.domain.dto.FlowChartsDTO;
import com.zhitan.statisticalAnalysis.domain.vo.FlowChartsItemVO;
import com.zhitan.statisticalAnalysis.domain.vo.FlowChartsVO;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.temporal.TemporalAdjusters;
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * stagseDataEntryService业务层处理
 *
 * @author sys
 * @date 2020-03-25
 */
@Service
@AllArgsConstructor
public class DataItemServiceImpl implements IDataItemService {
 
    @Resource
    private DataItemMapper dataItemMapper;
    @Resource
    private ModelNodeMapper modelNodeMapper;
    @Resource
    private EnergyIndexMapper energyIndexMapper;
    @Resource
    private NodeIndexMapper nodeIndexMapper;
 
 
    /**
     * 查询需要手动录入的阶段数据
     *
     * @param stagseDataEntry
     * @return 结果
     */
    @Override
    public List<StagseDataEntry> getSettingIndex(StagseDataEntry stagseDataEntry) {
        return dataItemMapper.getSettingIndex(stagseDataEntry);
    }
 
    /**
     * 查询修改手动录入的阶段数据
     *
     * @param stagseDataEntry
     * @return 结果
     */
    @Override
    public List<StagseDataEntry> getSettingEdit(StagseDataEntry stagseDataEntry) {
        return dataItemMapper.getSettingEdit(stagseDataEntry);
    }
 
    @Override
    public List<StagseDataEntry> stagseDataEntry(String nodeId, List<String> indexCodes, TimeType timeType, Date beginTime, Date endTime, String calcType) {
        return dataItemMapper.stagseDataByCode(nodeId, indexCodes, timeType, beginTime, endTime, calcType);
    }
 
    /**
     * 根据indexId与时间范围查询dataitem合计信息
     *
     * @param beginTime 开始时间
     * @param endTime   截止时间
     * @param timeType  时间类型
     * @param indexIds  点位集合
     * @return
     */
    @Override
    public BigDecimal getDataItemTimeRangeValueByIndexIds(Date beginTime, Date endTime, String timeType, List<String> indexIds) {
        return dataItemMapper.getDataItemTimeRangeValueByIndexIds(beginTime, endTime, timeType, indexIds);
    }
 
    /**
     * 根据indexId与时间范围查询dataitem信息
     *
     * @param beginTime 开始时间
     * @param endTime   截止时间
     * @param timeType  时间类型
     * @param indexIds  点位集合
     * @return
     */
    @Override
    public List<DataItem> getDataItemTimeRangeInforByIndexIds(Date beginTime, Date endTime, String timeType, List<String> indexIds) {
        return dataItemMapper.getDataItemTimeRangeInforByIndexIds(beginTime, endTime, timeType, indexIds);
    }
 
    /**
     * 根据indexId与时间编码查询点位值合计
     *
     * @param timeCode 时间编码
     * @param indexIds 点位id集合
     * @return
     */
    @Override
    public List<DataItem> getDataItemInforByIndexIds(String timeCode, List<String> indexIds) {
        return dataItemMapper.getDataItemInforByIndexIds(timeCode, indexIds);
    }
 
    /**
     * 根据indexId与时间编码查询合计值
     *
     * @param timeCode 时间编码
     * @param indexIds 点位id集合
     * @return
     */
    @Override
    public BigDecimal getDataItemValueByIndexIds(String timeCode, List<String> indexIds) {
        return dataItemMapper.getDataItemValueByIndexIds(timeCode, indexIds);
    }
 
    /**
     * 根据indexId与时间范围查询小时的dataitem信息
     *
     * @param beginTime 开始时间
     * @param endTime   截止时间
     * @param timeType  时间类型
     * @param indexIds  点位集合
     * @return
     */
    @Override
    public List<DataItem> getDataItemHourInforByIndexIds(Date beginTime, Date endTime, String timeType, List<String> indexIds) {
        return dataItemMapper.getDataItemHourInforByIndexIds(beginTime, endTime, timeType, indexIds);
    }
 
    /**
     * 获取能流图形分析
     *
     * @param dto 请求参数
     * @return 结果
     */
    @Override
    public FlowChartsVO getFlowCharts(FlowChartsDTO dto) {
        FlowChartsVO flowChartsVO = new FlowChartsVO();
        // 父节点id
        LambdaQueryWrapper<ModelNode> wrapper = Wrappers.<ModelNode>lambdaQuery()
                .eq(ModelNode::getModelCode, dto.getModelCode())
                .isNull(ModelNode::getParentId);
        List<ModelNode> modelNodes = modelNodeMapper.selectList(wrapper);
        if (ObjectUtils.isEmpty(modelNodes)) {
            throw new RuntimeException("未查询到模型信息");
        }
        ModelNode modelNode = modelNodes.stream().findFirst().get();
        String nodeId = modelNode.getNodeId();
        dto.setNodeId(nodeId);
        String energyType = dto.getEnergyType();
        LocalDate queryTime = dto.getQueryTime();
        TimeType timeType = dto.getTimeType();
 
        // 获取查询时间
        Map<String, LocalDateTime> dateTimeMap = getDataItemByIndexId(timeType, queryTime);
 
        // 获取节点和点位的累积量
        List<NodeIndexValueVO> parentDataItemList = modelNodeMapper.getDataItemByNodeId(nodeId, energyType, timeType, dateTimeMap);
 
        // 获取子节点下的点位的累积量
        List<NodeIndexValueVO> childDataItemList = modelNodeMapper.getDataItemByParentNodeId(nodeId, energyType, timeType, dateTimeMap);
 
        // 获取父节点下的能耗数据总和
        if (ObjectUtils.isNotEmpty(parentDataItemList)) {
            // 父节点下的能耗数据总和
            BigDecimal totalValue = parentDataItemList.stream().map(NodeIndexValueVO::getValue)
                    .filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            // 总累积量
            flowChartsVO.setTotalAccumulatedAmount(totalValue);
        }
        // 获取子节点下的能耗数据
        if (ObjectUtils.isNotEmpty(childDataItemList)) {
            // 子节点下的能耗数据总和
            BigDecimal childTotalValue = childDataItemList.stream().map(NodeIndexValueVO::getValue)
                    .filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
            // 子节点累积量
            flowChartsVO.setChildNodeAccumulatedAmount(childTotalValue);
 
            // 根据子节点id分组
            Map<String, List<NodeIndexValueVO>> voMap = childDataItemList.stream()
                    .collect(Collectors.groupingBy(NodeIndexValueVO::getNodeId));
            List<FlowChartsItemVO> itemList = new ArrayList<>();
 
            for (String childNodeId : voMap.keySet()) {
                FlowChartsItemVO vo = new FlowChartsItemVO();
                vo.setSource(modelNode.getName());
                List<NodeIndexValueVO> valueList = voMap.getOrDefault(childNodeId, Collections.emptyList());
                if (ObjectUtils.isNotEmpty(valueList)) {
                    // 各个子节点的能耗数据总和
                    BigDecimal value = valueList.stream().map(NodeIndexValueVO::getValue)
                            .filter(Objects::nonNull).reduce(BigDecimal.ZERO, BigDecimal::add);
                    valueList.stream().findFirst().ifPresent(nodeIndexValueVO -> vo.setTarget(nodeIndexValueVO.getNodeName()));
                    vo.setValue(value);
                }
                itemList.add(vo);
            }
            flowChartsVO.setItemVOList(itemList);
        }
        return flowChartsVO;
    }
 
    @Override
    public List<RankingEnergyData> getHomePageConsumptionRanking(List<String> nodeIds, String timeType, Date beginTime, Date endTime) {
        return dataItemMapper.getHomePageConsumptionRanking(nodeIds, timeType, beginTime, endTime);
    }
 
    /**
     * 根据indexId查询能耗数据
     */
    private Map<String, LocalDateTime> getDataItemByIndexId(TimeType timeType, LocalDate queryTime) {
        LocalDateTime startTime;
        LocalDateTime endTime;
        LocalDate startDate;
        LocalDate endDate;
        switch (timeType) {
            case DAY:
                // 当天的开始时间
                startTime = LocalDateTime.of(queryTime, LocalTime.MIN);
                // 当天的结束时间
                endTime = LocalDateTime.of(queryTime, LocalTime.MAX);
                break;
            case MONTH:
                // 当月的开始时间
                startDate = queryTime.with(TemporalAdjusters.firstDayOfMonth());
                startTime = LocalDateTime.of(startDate, LocalTime.MIN);
                // 当月的结束时间
                endDate = queryTime.with(TemporalAdjusters.lastDayOfMonth());
                endTime = LocalDateTime.of(endDate, LocalTime.MAX);
                break;
            case YEAR:
                // 当年的开始时间
                startDate = queryTime.with(TemporalAdjusters.firstDayOfYear());
                startTime = LocalDateTime.of(startDate, LocalTime.MIN);
                // 当年的结束时间
                endDate = queryTime.with(TemporalAdjusters.lastDayOfYear());
                endTime = LocalDateTime.of(endDate, LocalTime.MAX);
                break;
            default:
                // 当天的开始时间
                startTime = LocalDateTime.of(queryTime, LocalTime.MIN);
                // 当天的结束时间
                endTime = LocalDateTime.of(queryTime, LocalTime.MAX);
                break;
        }
        Map<String, LocalDateTime> localDateTimeMap = new HashMap<>();
        localDateTimeMap.put("startTime", startTime);
        localDateTimeMap.put("endTime", endTime);
        return localDateTimeMap;
    }
}