|
|
@@ -1,11 +1,13 @@
|
|
|
package com.kym.service.admin.impl;
|
|
|
|
|
|
+import com.alibaba.fastjson2.JSON;
|
|
|
import com.alibaba.fastjson2.JSONObject;
|
|
|
import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
|
|
|
-import com.github.yulichang.base.MPJBaseServiceImpl;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|
|
import com.kym.common.annotation.DynamicCache;
|
|
|
import com.kym.common.enums.EnPlusApi;
|
|
|
+import com.kym.common.exception.BusinessException;
|
|
|
import com.kym.common.utils.AESUtil;
|
|
|
import com.kym.common.utils.CommUtil;
|
|
|
import com.kym.entity.admin.*;
|
|
|
@@ -15,19 +17,18 @@ import com.kym.entity.enplus.EnEquipmentInfo;
|
|
|
import com.kym.entity.enplus.EnStationStatsInfo;
|
|
|
import com.kym.entity.enplus.EnStationStatusInfo;
|
|
|
import com.kym.mapper.admin.StationMapper;
|
|
|
+import com.kym.service.MyBaseServiceImpl;
|
|
|
import com.kym.service.admin.*;
|
|
|
import com.kym.service.cache.KymCache;
|
|
|
import com.kym.service.enplus.EnPlusService;
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.BeanUtils;
|
|
|
import org.springframework.context.annotation.Lazy;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
-import java.util.Comparator;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.List;
|
|
|
+import java.util.*;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
@@ -40,7 +41,8 @@ import java.util.stream.Collectors;
|
|
|
*/
|
|
|
@Service
|
|
|
@DS("db-admin")
|
|
|
-public class StationServiceImpl extends MPJBaseServiceImpl<StationMapper, Station> implements StationService {
|
|
|
+@Slf4j
|
|
|
+public class StationServiceImpl extends MyBaseServiceImpl<StationMapper, Station> implements StationService {
|
|
|
|
|
|
private final EnPlusService enPlusService;
|
|
|
private final EquipmentInfoService equipmentInfoService;
|
|
|
@@ -156,60 +158,60 @@ public class StationServiceImpl extends MPJBaseServiceImpl<StationMapper, Statio
|
|
|
*/
|
|
|
@Override
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
- public void pullEnStationInfos() {
|
|
|
- var stationVoList = queryStationInfo(1, 1000);
|
|
|
- var stationList = new ArrayList<Station>();
|
|
|
+ public void pullEnStationInfos(String stationId) {
|
|
|
+ // 指定的站点信息
|
|
|
+ var stationVo = queryStationInfo(1, 1000).stream().filter(vo -> stationId.equals(vo.getStationId())).findFirst().orElse(null);
|
|
|
+
|
|
|
+ var station = new Station();
|
|
|
var equipmentList = new ArrayList<EquipmentInfo>();
|
|
|
var connectorList = new ArrayList<ConnectorInfo>();
|
|
|
- stationVoList.forEach(vo -> {
|
|
|
- if (!vo.getEquipmentInfos().isEmpty()) {
|
|
|
- vo.getEquipmentInfos().forEach(item -> {
|
|
|
- // 桩体
|
|
|
- var equipment = new EquipmentInfo()
|
|
|
- .setStationId(vo.getStationId())
|
|
|
- .setShortId(item.getShortId())
|
|
|
- .setPower(item.getConnectorInfos().get(0).getPower());
|
|
|
- BeanUtils.copyProperties(item, equipment);
|
|
|
- equipmentList.add(equipment);
|
|
|
- // 枪
|
|
|
- var connectorInfos = item.getConnectorInfos();
|
|
|
- if (!CommUtil.isEmptyOrNull(connectorInfos)) {
|
|
|
- connectorInfos.forEach(connector -> {
|
|
|
- var connectorInfo = new ConnectorInfo();
|
|
|
- BeanUtils.copyProperties(connector, connectorInfo);
|
|
|
- connectorList.add(connectorInfo);
|
|
|
- });
|
|
|
- }
|
|
|
- });
|
|
|
- equipmentList.sort(Comparator.comparing(EquipmentInfo::getShortId));
|
|
|
- }
|
|
|
- var station = new Station();
|
|
|
- BeanUtils.copyProperties(vo, station);
|
|
|
- stationList.add(station);
|
|
|
- });
|
|
|
- // 查询已存在的站点,只更新新增的站点和设备
|
|
|
- var stations = list().stream().map(Station::getStationId).toList();
|
|
|
- var equipments = equipmentInfoService.list().stream().map(EquipmentInfo::getEquipmentId).toList();
|
|
|
- var connectors = connectorInfoService.list().stream().map(ConnectorInfo::getConnectorId).toList();
|
|
|
|
|
|
- // 新增的数据
|
|
|
- var newStations = stationList.stream().filter(station -> !stations.contains(station.getStationId())).toList();
|
|
|
- var newEquipmentInfos = equipmentList.stream().filter(equipmentInfo -> !equipments.contains(equipmentInfo.getEquipmentId())).toList();
|
|
|
- var newConnectors = connectorList.stream().filter(connector -> !connectors.contains(connector.getConnectorId())).toList();
|
|
|
+ // 组装数据
|
|
|
+ assert stationVo != null;
|
|
|
+ if (!stationVo.getEquipmentInfos().isEmpty()) {
|
|
|
+ stationVo.getEquipmentInfos().forEach(item -> {
|
|
|
+ // 桩体
|
|
|
+ var equipment = new EquipmentInfo()
|
|
|
+ .setStationId(stationVo.getStationId())
|
|
|
+ .setShortId(item.getShortId())
|
|
|
+ .setPower(item.getConnectorInfos().get(0).getPower());
|
|
|
+ BeanUtils.copyProperties(item, equipment);
|
|
|
+ equipmentList.add(equipment);
|
|
|
+ // 枪
|
|
|
+ var connectorInfos = item.getConnectorInfos();
|
|
|
+ if (!CommUtil.isEmptyOrNull(connectorInfos)) {
|
|
|
+ connectorInfos.forEach(connector -> {
|
|
|
+ var connectorInfo = new ConnectorInfo();
|
|
|
+ BeanUtils.copyProperties(connector, connectorInfo);
|
|
|
+ connectorList.add(connectorInfo);
|
|
|
+ });
|
|
|
+ }
|
|
|
+ });
|
|
|
+ equipmentList.sort(Comparator.comparing(EquipmentInfo::getShortId));
|
|
|
+ }
|
|
|
+ BeanUtils.copyProperties(stationVo, station);
|
|
|
|
|
|
- // 将充电站相关数据写入KymCache
|
|
|
- // 手动切换数据源
|
|
|
DynamicDataSourceContextHolder.push("db-admin");
|
|
|
- KymCache.INSTANCE.putStationId2Name(newStations.stream().collect(Collectors.toMap(Station::getStationId, Station::getStationName)));
|
|
|
-// KymCache.INSTANCE.putConnectorId2ParkingNo(newEquipmentInfos.stream().collect(Collectors.toMap(equipmentInfo -> equipmentInfo.getEquipmentId().concat("1"), EquipmentInfo::getParkingNo?)));
|
|
|
+ // 更新站点信息
|
|
|
+ updateByQueryWrapper(station, s -> new QueryWrapper<Station>().eq("station_id", station.getStationId()));
|
|
|
+
|
|
|
+ // 更新设备信息
|
|
|
+ equipmentInfoService.updateBatchByQueryWrapper(equipmentList, equipmentInfo ->
|
|
|
+ new QueryWrapper<EquipmentInfo>().eq("equipment_id", equipmentInfo.getEquipmentId()));
|
|
|
+
|
|
|
+ // 更新枪信息
|
|
|
+ connectorInfoService.updateBatchByQueryWrapper(connectorList, connectorInfo ->
|
|
|
+ new QueryWrapper<ConnectorInfo>().eq("connector_id", connectorInfo.getConnectorId()));
|
|
|
+
|
|
|
DynamicDataSourceContextHolder.poll();
|
|
|
|
|
|
- // 新数据写入数据库
|
|
|
- saveBatch(newStations);
|
|
|
- equipmentInfoService.saveBatch(newEquipmentInfos);
|
|
|
- connectorInfoService.saveBatch(newConnectors);
|
|
|
+ // 将充电站相关数据写入KymCache
|
|
|
+ KymCache.INSTANCE.putStationId2Name(Map.of(stationVo.getStationId(), stationVo.getStationName()));
|
|
|
+
|
|
|
+
|
|
|
}
|
|
|
|
|
|
+
|
|
|
@Override
|
|
|
@Transactional
|
|
|
public void modifyStation(Station station) {
|
|
|
@@ -250,4 +252,42 @@ public class StationServiceImpl extends MPJBaseServiceImpl<StationMapper, Statio
|
|
|
// TODO redis发布订阅?
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void importData(JSONObject data) {
|
|
|
+ log.debug("导入数据:{}", data.toString());
|
|
|
+ var fieldIndexes = data.getJSONArray("fieldIndexes");
|
|
|
+ if (fieldIndexes.size() != 7) {
|
|
|
+ throw new BusinessException("导入数据格式错误,请正确匹配对应数据列");
|
|
|
+ }
|
|
|
+
|
|
|
+ String dataList = data.getString("dataList");
|
|
|
+ var list = JSON.parseArray(dataList, EquipmentRelation.class);
|
|
|
+
|
|
|
+ // t_station
|
|
|
+ var stations = list.stream().filter(CommUtil.distinctByKey(EquipmentRelation::getStationId))
|
|
|
+ .map(item -> Map.of("stationId", item.getStationId(), "stationName", item.getStationName())).toList();
|
|
|
+ var stationList = stations.stream().map(map -> new Station().setStationId(map.get("stationId")).setStationName(map.get("stationName"))).toList();
|
|
|
+ saveBatch(stationList);
|
|
|
+
|
|
|
+ // t_equipment_info
|
|
|
+ var equipmentInfoList = list.stream().map(item -> {
|
|
|
+ var equipmentInfo = new EquipmentInfo();
|
|
|
+ BeanUtils.copyProperties(item, equipmentInfo, "id");
|
|
|
+ return equipmentInfo;
|
|
|
+ }).toList();
|
|
|
+ equipmentInfoService.saveBatch(equipmentInfoList);
|
|
|
+
|
|
|
+ // t_connector_info
|
|
|
+ var connectorInfoList = list.stream().map(item -> {
|
|
|
+ var connectorInfo = new ConnectorInfo();
|
|
|
+ BeanUtils.copyProperties(item, connectorInfo, "id");
|
|
|
+ return connectorInfo;
|
|
|
+ }).toList();
|
|
|
+ connectorInfoService.saveBatch(connectorInfoList);
|
|
|
+
|
|
|
+ // t_equipment_relation
|
|
|
+ equipmentRelationService.saveBatch(list);
|
|
|
+ }
|
|
|
+
|
|
|
}
|