puzhibing
2026-09-28 fd15e6f7dd6a2f6bccf27a8b06fc60211e7df28e
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
package com.ruoyi.other.emind.service;
 
import com.ruoyi.other.emind.domain.EmindEnergyChain;
import com.ruoyi.other.emind.domain.EmindStation;
import com.ruoyi.other.emind.dto.EmindEnergyChainDto;
import com.ruoyi.other.emind.mapper.EmindEnergyChainMapper;
import com.ruoyi.other.emind.mapper.EmindStationMapper;
import com.ruoyi.other.emind.util.EmindCloudApiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
 
/** 根据有效储能站点全量同步 eMind 能量链。 */
@Service
@ConditionalOnProperty(prefix = "emind", name = "enabled", havingValue = "true")
public class EmindEnergyChainSyncService {
    private final EmindCloudApiUtil api;
    private final EmindStationMapper stationMapper;
    private final EmindEnergyChainMapper chainMapper;
    private final Clock clock;
 
    @Autowired
    public EmindEnergyChainSyncService(EmindCloudApiUtil api,
                                       EmindStationMapper stationMapper,
                                       EmindEnergyChainMapper chainMapper) {
        this(api, stationMapper, chainMapper, Clock.systemDefaultZone());
    }
 
    EmindEnergyChainSyncService(EmindCloudApiUtil api,
                                EmindStationMapper stationMapper,
                                EmindEnergyChainMapper chainMapper,
                                Clock clock) {
        this.api = api;
        this.stationMapper = stationMapper;
        this.chainMapper = chainMapper;
        this.clock = clock;
    }
 
    /** 所有站点请求成功后才更新本地冗余。 */
    @Transactional(rollbackFor = Exception.class)
    public int syncEnergyChains() {
        List<EmindStation> stations = stationMapper.selectRemoteActiveStations();
        LocalDateTime syncedAt = LocalDateTime.now(clock);
        List<EmindEnergyChain> chains = new ArrayList<>();
        for (EmindStation station : stations) {
            if(station.getSourceType() == 1) {
                List<EmindEnergyChainDto> remoteChains = api.getEnergyChains(station.getStationId());
                for (EmindEnergyChainDto remote : remoteChains) {
                    chains.add(toEntity(station, remote, syncedAt));
                }
            }
        }
        chainMapper.markAllDeleted(syncedAt);
        if (!chains.isEmpty()) {
            chainMapper.upsertBatch(chains);
        }
        return chains.size();
    }
 
    private EmindEnergyChain toEntity(EmindStation station,
                                      EmindEnergyChainDto source,
                                      LocalDateTime syncedAt) {
        EmindEnergyChain target = new EmindEnergyChain();
        target.setStationLocalId(station.getId());
        target.setStationId(station.getStationId());
        target.setStationName(station.getName());
        target.setElinkId(source.getElinkId());
        target.setUuid(source.getUuid());
        target.setElinkName(source.getElinkName());
        target.setTotalActivePower(source.getTotalActivePower());
        target.setTotalReactivePower(source.getTotalReactivePower());
        target.setMaxChargePower(source.getMaxChargePower());
        target.setMaxDischargePower(source.getMaxDischargePower());
        target.setPhaseACurrent(source.getPhaseACurrent());
        target.setPhaseBCurrent(source.getPhaseBCurrent());
        target.setPhaseCCurrent(source.getPhaseCCurrent());
        target.setDischargeableEnergy(source.getDischargeableEnergy());
        target.setSoc(source.getSoc());
        target.setChargedEnergyToday(source.getChargedEnergyToday());
        target.setDischargedEnergyToday(source.getDischargedEnergyToday());
        target.setRecordTime(source.getRecordTime());
        target.setSourceType(EmindEnergyChain.SOURCE_REMOTE);
        target.setManagementDeleted(0);
        target.setDelFlag(0);
        target.setSyncedAt(syncedAt);
        return target;
    }
}