| | |
| | | import com.ruoyi.other.service.TStationInfoService; |
| | | import com.ruoyi.other.query.StationInfoQuery; |
| | | import com.ruoyi.other.util.KsolarUtils; |
| | | import com.ruoyi.other.util.HuWeiUtils; |
| | | import com.ruoyi.other.vo.StationInfoStatusVO; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.collections4.CollectionUtils; |
| | |
| | | import java.math.BigDecimal; |
| | | import java.time.LocalDate; |
| | | import java.time.LocalDateTime; |
| | | import java.time.OffsetDateTime; |
| | | import java.time.ZonedDateTime; |
| | | import java.time.format.DateTimeFormatter; |
| | | import java.time.format.DateTimeParseException; |
| | | import java.util.*; |
| | | import java.util.function.Function; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | |
| | | @Autowired |
| | | private TStationEarnService stationEarnService; |
| | | |
| | | @Autowired |
| | | private HuWeiUtils huWeiUtils; |
| | | |
| | | /** |
| | | * 每天同步科士达数据到数据库 |
| | |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void syncKsolarDataTask() { |
| | | String token = KsolarUtils.token(); |
| | | JSONObject statusJsonObj = KsolarUtils.getListStatus(token); |
| | | //JSONObject statusJsonObj = KsolarUtils.getListStatus(token); |
| | | |
| | | log.info("-------1开始同步科士达-电站列表信息----"); |
| | | // 电站列表 |
| | |
| | | // 电站基本信息列表 |
| | | JSONArray listBaseJsonArray = KsolarUtils.getStationListBase(1, 1000, token); |
| | | List<TStationInfo> stationInfoList = build(listInfoJsonArray, listBaseJsonArray); |
| | | |
| | | if(CollectionUtils.isNotEmpty(stationInfoList)) { |
| | | List<TStationInfo> existingStationList = lambdaQuery().select(TStationInfo::getStationId).list(); |
| | | Set<String> existingStationIds = new HashSet<>(); |
| | | for (TStationInfo stationInfo : existingStationList) { |
| | | if (stationInfo.getStationId() != null) { |
| | | existingStationIds.add(stationInfo.getStationId()); |
| | | // 去除stationInfoList中的重复stationId |
| | | Map<String, TStationInfo> stationMap = stationInfoList.stream().filter(item -> StringUtils.isNotEmpty(item.getStationId())) |
| | | .collect(Collectors.toMap(TStationInfo::getStationId, Function.identity(), (o1, o2) -> o2)); |
| | | |
| | | stationInfoList = new ArrayList<>(stationMap.values()); |
| | | |
| | | // 查询数据库已有数据 |
| | | List<TStationInfo> existingStationList = lambdaQuery().in(TStationInfo::getStationId, stationMap.keySet()).list(); |
| | | |
| | | // 转Map,方便查找 |
| | | Map<String, TStationInfo> existingMap = existingStationList.stream().collect(Collectors.toMap(TStationInfo::getStationId, Function.identity())); |
| | | |
| | | List<TStationInfo> insertList = new ArrayList<>(); |
| | | List<TStationInfo> updateList = new ArrayList<>(); |
| | | |
| | | for (TStationInfo station : stationInfoList) { |
| | | TStationInfo dbStation = existingMap.get(station.getStationId()); |
| | | if (dbStation == null) { |
| | | // 数据库不存在 |
| | | insertList.add(station); |
| | | } else { |
| | | // 数据库存在,需要更新;保留数据库主键,否则updateBatchById无法更新 |
| | | station.setId(dbStation.getId()); |
| | | updateList.add(station); |
| | | } |
| | | } |
| | | Set<String> pendingStationIds = new HashSet<>(); |
| | | stationInfoList.removeIf(stationInfo -> stationInfo.getStationId() == null || existingStationIds.contains(stationInfo.getStationId()) || !pendingStationIds.add(stationInfo.getStationId())); |
| | | |
| | | if (CollectionUtils.isNotEmpty(stationInfoList)) { |
| | | saveBatch(stationInfoList); |
| | | // 批量新增 |
| | | if (CollectionUtils.isNotEmpty(insertList)) { |
| | | saveBatch(insertList); |
| | | } |
| | | // 批量更新 |
| | | if (CollectionUtils.isNotEmpty(updateList)) { |
| | | updateBatchById(updateList); |
| | | } |
| | | } |
| | | log.info("-------1结束同步科士达-电站列表信息----"); |
| | |
| | | } |
| | | } |
| | | |
| | | private LocalDateTime parseHuaweiDateTime(String value) { |
| | | if (StringUtils.isBlank(value)) { |
| | | return null; |
| | | } |
| | | try { |
| | | return OffsetDateTime.parse(value).toLocalDateTime(); |
| | | } catch (DateTimeParseException ignored) { |
| | | try { |
| | | return ZonedDateTime.parse(value).toLocalDateTime(); |
| | | } catch (DateTimeParseException ignoredAgain) { |
| | | return parseDataTime(value); |
| | | } |
| | | } |
| | | } |
| | | |
| | | private String firstNotBlank(String preferred, String fallback) { |
| | | return StringUtils.isNotBlank(preferred) ? preferred : fallback; |
| | | } |
| | |
| | | statusVO.setOnStatusCount(onStatusList.size()); |
| | | statusVO.setOffStatusCount(stationInfoList.size() - onStatusList.size()); |
| | | } |
| | | // 电站状态数量加上非科士达的数据 |
| | | List<TStationInfo> noKeShiDaList = stationInfoList.stream().filter(s -> s.getPlatformId() != 0).collect(Collectors.toList()); |
| | | statusVO.setTotal(statusVO.getTotal() + noKeShiDaList.size()); |
| | | statusVO.setOnline(statusVO.getOnline() + noKeShiDaList.size()); |
| | | return statusVO; |
| | | } |
| | | |
| | |
| | | return true; |
| | | } |
| | | |
| | | /** |
| | | * 同步华为 FusionSolar 电站数据。 |
| | | * 电站编号 plantCode 作为系统中的 stationId,仅插入数据库中不存在的电站。 |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void syncHuaWeiDataTask() { |
| | | log.info("-------1、开始同步华为电站列表信息----"); |
| | | String token = huWeiUtils.getXsrfToken(); |
| | | JSONArray stationArray = huWeiUtils.getStations(1, token); |
| | | Set<String> stationIds = new HashSet<>(); |
| | | if (CollectionUtils.isNotEmpty(stationArray)) { |
| | | List<TStationInfo> insertList = new ArrayList<>(); |
| | | huaWeiBuild(stationArray, stationIds, insertList); |
| | | |
| | | if (CollectionUtils.isNotEmpty(insertList)) { |
| | | List<TStationInfo> existing = lambdaQuery().eq(TStationInfo::getPlatformId, 1L).in(TStationInfo::getStationId, stationIds).list(); |
| | | Set<String> existingIds = existing.stream().map(TStationInfo::getStationId).collect(Collectors.toSet()); |
| | | insertList.removeIf(item -> existingIds.contains(item.getStationId())); |
| | | if (CollectionUtils.isNotEmpty(insertList)) { |
| | | saveBatch(insertList); |
| | | } |
| | | } |
| | | log.info("-------1、结束同步华为电站列表信息,本次新增{}条----", insertList.size()); |
| | | } |
| | | |
| | | log.info("-------2、开始同步华为设备列表信息----"); |
| | | if(CollectionUtils.isNotEmpty(stationIds)) { |
| | | String codes = String.join(",", stationIds); |
| | | JSONArray devList = huWeiUtils.getDevList(codes, token); |
| | | collectService.saveCollectHuaWeiList(devList); |
| | | } |
| | | |
| | | log.info("-------3、开始同步华为电站日数据信息----"); |
| | | List<TStationInfo> existInfos = lambdaQuery().eq(TStationInfo::getPlatformId, 1L).list(); |
| | | Set<String> existIds = existInfos.stream().map(TStationInfo::getStationId).collect(Collectors.toSet()); |
| | | if(CollectionUtils.isNotEmpty(existIds)) { |
| | | String codes = String.join(",", existIds); |
| | | JSONArray stationDayList = huWeiUtils.getKpiStationDay(codes, System.currentTimeMillis(), token); |
| | | stationEarnService.saveStationHuaWeiList(stationDayList); |
| | | } |
| | | log.info("-------3、结束同步华为电站日数据信息----"); |
| | | |
| | | |
| | | } |
| | | |
| | | public void huaWeiBuild(JSONArray stationArray, Set<String> stationIds, List<TStationInfo> insertList) { |
| | | for (Object item : stationArray) { |
| | | JSONObject json = JSONObject.parseObject(item.toString()); |
| | | String stationId = json.getString("plantCode"); |
| | | if (StringUtils.isBlank(stationId) || !stationIds.add(stationId)) { |
| | | continue; |
| | | } |
| | | TStationInfo info = new TStationInfo(); |
| | | info.setPlatformId(1L); |
| | | info.setStationId(stationId); |
| | | info.setStationName(json.getString("plantName")); |
| | | info.setStationType("1"); |
| | | info.setRunStatus("0"); |
| | | info.setInstalledCapacity(toBigDecimal(json, "capacity")); |
| | | info.setContactName(json.getString("contactPerson")); |
| | | info.setPhone(json.getString("contactMethod")); |
| | | info.setLatitude(json.getString("latitude")); |
| | | info.setLongitude(json.getString("longitude")); |
| | | info.setAddress(json.getString("plantAddress")); |
| | | LocalDateTime gridTime = parseHuaweiDateTime(json.getString("gridConnectionDate")); |
| | | info.setDataTime(gridTime); |
| | | info.setLoanStatus(gridTime != null && gridTime.isBefore(LocalDateTime.now()) ? 3 : 1); |
| | | info.setGridStatus(info.getLoanStatus() == 3 ? "1" : "0"); |
| | | info.setDataSource(2); |
| | | info.setSyncTime(LocalDateTime.now()); |
| | | info.setCreateTime(LocalDateTime.now()); |
| | | info.setUpdateTime(LocalDateTime.now()); |
| | | fillDefaults(info); |
| | | insertList.add(info); |
| | | } |
| | | } |
| | | |
| | | } |