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;
|
}
|
}
|