|
|
@@ -1,129 +1,129 @@
|
|
|
package com.kym.service.cache;
|
|
|
|
|
|
-import com.baomidou.dynamic.datasource.annotation.DS;
|
|
|
-import com.kym.entity.admin.AdminUserStation;
|
|
|
-import com.kym.entity.admin.EquipmentInfo;
|
|
|
-import com.kym.entity.admin.EquipmentRelation;
|
|
|
-import com.kym.entity.admin.Station;
|
|
|
-import com.kym.service.admin.AdminUserStationService;
|
|
|
-import com.kym.service.admin.EquipmentInfoService;
|
|
|
-import com.kym.service.admin.EquipmentRelationService;
|
|
|
-import com.kym.service.admin.StationService;
|
|
|
-import lombok.Data;
|
|
|
-import org.springframework.stereotype.Component;
|
|
|
-
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author skyline
|
|
|
* @description
|
|
|
* @date 2023-08-25 22:58
|
|
|
*/
|
|
|
-@Component
|
|
|
-@Data
|
|
|
-public class KymCache {
|
|
|
+public enum KymCache {
|
|
|
+ INSTANCE;
|
|
|
|
|
|
- private static ConcurrentHashMap<String, String> SHORT_ID_MAPPING = new ConcurrentHashMap<>();
|
|
|
- private static ConcurrentHashMap<String, String> CONNECTOR_STATION_MAPPING = new ConcurrentHashMap<>();
|
|
|
+ private static ConcurrentHashMap<String, String> CONNECTOR_ID_SHORT_ID_MAPPING = new ConcurrentHashMap<>();
|
|
|
+ private static ConcurrentHashMap<String, String> CONNECTOR_ID_STATION_ID_MAPPING = new ConcurrentHashMap<>();
|
|
|
private static ConcurrentHashMap<String, String> STATION_MAPPING = new ConcurrentHashMap<>();
|
|
|
private static ConcurrentHashMap<Long, String> ADMIN_USER_STATION = new ConcurrentHashMap<>();
|
|
|
private static ConcurrentHashMap<String, String> EQUIPMENT_ID_PARKING_NO_MAPPING = new ConcurrentHashMap<>();
|
|
|
- private final EquipmentRelationService relationService;
|
|
|
- private final StationService stationService;
|
|
|
- private final AdminUserStationService adminUserStationService;
|
|
|
- private final EquipmentInfoService equipmentInfoService;
|
|
|
-
|
|
|
- public KymCache(EquipmentRelationService relationService, StationService stationService, AdminUserStationService adminUserStationService, EquipmentInfoService equipmentInfoService) {
|
|
|
- this.relationService = relationService;
|
|
|
- this.stationService = stationService;
|
|
|
- this.adminUserStationService = adminUserStationService;
|
|
|
- this.equipmentInfoService = equipmentInfoService;
|
|
|
-
|
|
|
- SHORT_ID_MAPPING.putAll( relationService.list().stream().collect(Collectors.toMap(EquipmentRelation::getEquipmentId, EquipmentRelation::getShortId)));
|
|
|
- CONNECTOR_STATION_MAPPING.putAll( relationService.list().stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getStationId)));
|
|
|
|
|
|
- STATION_MAPPING.putAll(stationService.list().stream().collect(Collectors.toMap(Station::getStationId, Station::getStationName)));
|
|
|
- ADMIN_USER_STATION.putAll(adminUserStationService.list().stream().collect(Collectors.toMap(AdminUserStation::getAdminUserId, AdminUserStation::getStationId)));
|
|
|
- EQUIPMENT_ID_PARKING_NO_MAPPING.putAll(equipmentInfoService.list().stream().collect(Collectors.toMap(EquipmentInfo::getEquipmentId, EquipmentInfo::getParkingNo)));
|
|
|
+ public void putShort2EquipmentId(Map<String, String> map) {
|
|
|
+ CONNECTOR_ID_SHORT_ID_MAPPING.putAll(map);
|
|
|
}
|
|
|
|
|
|
- @DS("db-admin")
|
|
|
- public String getShortId(String equipmentId) {
|
|
|
- if (equipmentId.length() == 17) {
|
|
|
- equipmentId = equipmentId.substring(0, 16);
|
|
|
- }
|
|
|
- var res = SHORT_ID_MAPPING.get(equipmentId);
|
|
|
- if (res == null) {
|
|
|
- var relation = relationService.lambdaQuery().eq(EquipmentRelation::getEquipmentId, equipmentId).one();
|
|
|
- if (relation != null) {
|
|
|
- res = relation.getShortId();
|
|
|
- SHORT_ID_MAPPING.put(equipmentId, res);
|
|
|
- }
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 通过设备编号或者枪头编号获取设备短编号
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getShortIdByEquipmentIdOrConnectorId(String id) {
|
|
|
+ var connectorId = getConnectorId(id);
|
|
|
+ var res = CONNECTOR_ID_SHORT_ID_MAPPING.get(connectorId);
|
|
|
+// if (res == null) {
|
|
|
+// var relation = relationService.lambdaQuery().eq(EquipmentRelation::getEquipmentId, equipmentId).one();
|
|
|
+// if (relation != null) {
|
|
|
+// res = relation.getShortId();
|
|
|
+// CONNECTOR_ID_SHORT_ID_MAPPING.put(equipmentId, res);
|
|
|
+// }
|
|
|
+// }
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+ public void putConnector2Station(Map<String, String> map) {
|
|
|
+ CONNECTOR_ID_STATION_ID_MAPPING.putAll(map);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
- * 转化成17为的connectorId
|
|
|
+ * 通过设备编号或者枪头编号获取站点id
|
|
|
*
|
|
|
- * @param equipmentId
|
|
|
+ * @param id
|
|
|
* @return
|
|
|
*/
|
|
|
- public String getConnectorId(String equipmentId) {
|
|
|
- return switch (equipmentId.length()) {
|
|
|
- case 17 -> equipmentId;
|
|
|
- case 16 -> equipmentId.concat("1");
|
|
|
- case 6 ->
|
|
|
- SHORT_ID_MAPPING.entrySet().stream().filter(entry -> equipmentId.equals(entry.getValue())).map(Map.Entry::getKey).findFirst().get();
|
|
|
- default -> null;
|
|
|
- };
|
|
|
+ public String getStationIdByEquipmentIdOrConnectorId(String id) {
|
|
|
+ var connectorId = getConnectorId(id);
|
|
|
+ var res = CONNECTOR_ID_STATION_ID_MAPPING.get(connectorId);
|
|
|
+// if (res == null) {
|
|
|
+// var relation = relationService.lambdaQuery().eq(EquipmentRelation::getConnectorId, connectorId).one();
|
|
|
+// if (relation != null) {
|
|
|
+// res = relation.getStationId();
|
|
|
+// CONNECTOR_STATION_MAPPING.put(connectorId, res);
|
|
|
+// }
|
|
|
+// }
|
|
|
+ return res;
|
|
|
}
|
|
|
|
|
|
- @DS("db-admin")
|
|
|
- public String getStationId(String connectorId) {
|
|
|
- var res = CONNECTOR_STATION_MAPPING.get(connectorId);
|
|
|
- if (res == null) {
|
|
|
- var relation = relationService.lambdaQuery().eq(EquipmentRelation::getConnectorId, connectorId).one();
|
|
|
- if (relation != null) {
|
|
|
- res = relation.getStationId();
|
|
|
- CONNECTOR_STATION_MAPPING.put(connectorId, res);
|
|
|
- }
|
|
|
- }
|
|
|
- return res;
|
|
|
+ public void putStationId2Name(Map<String, String> map) {
|
|
|
+ STATION_MAPPING.putAll(map);
|
|
|
}
|
|
|
|
|
|
- public String getStationNameByConnectorId(String connectorId) {
|
|
|
- connectorId = getConnectorId(connectorId);
|
|
|
- var stationId = CONNECTOR_STATION_MAPPING.get(connectorId);
|
|
|
- return getStationName(stationId);
|
|
|
+ /**
|
|
|
+ * 通过站点id获取站点名称
|
|
|
+ *
|
|
|
+ * @param stationId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getStationNameById(String stationId) {
|
|
|
+ return STATION_MAPPING.get(stationId);
|
|
|
}
|
|
|
|
|
|
- @DS("db-admin")
|
|
|
- public String getStationName(String stationId) {
|
|
|
- var res = STATION_MAPPING.get(stationId);
|
|
|
- if (res == null) {
|
|
|
- var station = stationService.lambdaQuery().eq(Station::getStationId, stationId).one();
|
|
|
- if (station != null) {
|
|
|
- res = station.getStationId();
|
|
|
- STATION_MAPPING.put(stationId, res);
|
|
|
- }
|
|
|
- }
|
|
|
- return res;
|
|
|
+ // TODO: 2023-11-24 这里目前只有一对一关系,后续改一对多
|
|
|
+ public void putAdminUser2Station(Map<Long, String> map) {
|
|
|
+ ADMIN_USER_STATION.putAll(map);
|
|
|
}
|
|
|
|
|
|
- @DS("db-admin")
|
|
|
+ /**
|
|
|
+ * 获取运营平台操作员有权限的站点ID
|
|
|
+ *
|
|
|
+ * @param adminUserId
|
|
|
+ * @return
|
|
|
+ */
|
|
|
public String getAdminUserStationId(Long adminUserId) {
|
|
|
var res = ADMIN_USER_STATION.get(adminUserId);
|
|
|
- if (res == null) {
|
|
|
- var adminUserStation = adminUserStationService.lambdaQuery().eq(AdminUserStation::getAdminUserId, adminUserId).one();
|
|
|
- res = adminUserStation == null ? null : adminUserStation.getStationId();
|
|
|
- }
|
|
|
+// if (res == null) {
|
|
|
+// var adminUserStation = adminUserStationService.lambdaQuery().eq(AdminUserStation::getAdminUserId, adminUserId).one();
|
|
|
+// res = adminUserStation == null ? null : adminUserStation.getStationId();
|
|
|
+// }
|
|
|
return res;
|
|
|
}
|
|
|
|
|
|
- public String getParkNoByEquipmentId(String equipmentId){
|
|
|
- return EQUIPMENT_ID_PARKING_NO_MAPPING.get(equipmentId);
|
|
|
+ public void putConnectorId2ParkingNo(Map<String, String> map) {
|
|
|
+ EQUIPMENT_ID_PARKING_NO_MAPPING.putAll(map);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getParkNoByEquipmentIdOrConnectorId(String id) {
|
|
|
+ var connectorId = getConnectorId(id);
|
|
|
+ return EQUIPMENT_ID_PARKING_NO_MAPPING.get(connectorId);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 将equipmentId,connectorId,shortId转化成17为的connectorId
|
|
|
+ *
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String getConnectorId(String id) {
|
|
|
+ return switch (id.length()) {
|
|
|
+ case 17 -> id;
|
|
|
+ case 16 -> id.concat("1");
|
|
|
+ case 6 ->
|
|
|
+ CONNECTOR_ID_SHORT_ID_MAPPING.entrySet().stream().filter(entry -> id.equals(entry.getValue())).map(Map.Entry::getKey).findFirst().get();
|
|
|
+ default -> null;
|
|
|
+ };
|
|
|
}
|
|
|
}
|