KymCache.java 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.kym.service.cache;
  2. import cn.hutool.extra.spring.SpringUtil;
  3. import com.kym.common.exception.BusinessException;
  4. import com.kym.entity.admin.ConnectorInfo;
  5. import com.kym.entity.admin.EquipmentRelation;
  6. import com.kym.entity.admin.Station;
  7. import com.kym.entity.common.RedisKeys;
  8. import com.kym.service.admin.ConnectorInfoService;
  9. import com.kym.service.admin.EquipmentRelationService;
  10. import com.kym.service.admin.StationService;
  11. import org.springframework.boot.context.event.ApplicationStartedEvent;
  12. import org.springframework.context.ApplicationListener;
  13. import org.springframework.data.redis.core.StringRedisTemplate;
  14. import org.springframework.stereotype.Component;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.Objects;
  18. import java.util.concurrent.ConcurrentHashMap;
  19. import java.util.stream.Collectors;
  20. /**
  21. * @author skyline
  22. * @description 缓存
  23. * @date 2023-08-25 22:58
  24. */
  25. public enum KymCache {
  26. INSTANCE;
  27. private static ConcurrentHashMap<String, String> CONNECTOR_ID_SHORT_ID_MAPPING = new ConcurrentHashMap<>();
  28. /**
  29. * 获取枪头状态
  30. *
  31. * @param connectorId
  32. * @return
  33. */
  34. public Integer getConnectorStatus(String connectorId) {
  35. return Integer.valueOf(Objects.requireNonNull(KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_TO_STATUS + connectorId)));
  36. }
  37. /**
  38. * 缓存枪头状态
  39. *
  40. * @param map
  41. */
  42. public void putConnectorId2Status(Map<String, Integer> map) {
  43. map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_STATUS + k, String.valueOf(v)));
  44. }
  45. /**
  46. * 缓存枪头短编号
  47. *
  48. * @param map
  49. */
  50. public void putConnectorId2ShortId(Map<String, String> map) {
  51. CONNECTOR_ID_SHORT_ID_MAPPING.putAll(map);
  52. map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_SHORT_ID + k, v));
  53. }
  54. /**
  55. * 通过设备编号或者枪头编号获取设备短编号
  56. *
  57. * @param id
  58. * @return
  59. */
  60. public String getShortIdByEquipmentIdOrConnectorId(String id) {
  61. var connectorId = getConnectorId(id);
  62. return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_TO_SHORT_ID + connectorId);
  63. }
  64. public void putConnectorId2StationId(Map<String, String> map) {
  65. map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_STATION_ID + k, v));
  66. }
  67. /**
  68. * 通过设备编号或者枪头编号获取站点id
  69. *
  70. * @param id
  71. * @return
  72. */
  73. public String getStationIdByEquipmentIdOrConnectorId(String id) {
  74. var connectorId = getConnectorId(id);
  75. return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_TO_STATION_ID + connectorId);
  76. }
  77. public void putStationId2Name(Map<String, String> map) {
  78. map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.STATION_ID_TO_NAME + k, v));
  79. }
  80. /**
  81. * 通过站点id获取站点名称
  82. *
  83. * @param stationId
  84. * @return
  85. */
  86. public String getStationNameById(String stationId) {
  87. return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.STATION_ID_TO_NAME + stationId);
  88. }
  89. /**
  90. * 通过充电接口id获取站点名称
  91. *
  92. * @param connectorId
  93. * @return
  94. */
  95. public String getStationNameByConnectorId(String connectorId) {
  96. var stationId = getStationIdByEquipmentIdOrConnectorId(connectorId);
  97. return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.STATION_ID_TO_NAME + stationId);
  98. }
  99. /**
  100. * 操作员对应有权限的站点
  101. *
  102. * @param map
  103. */
  104. public void putAdminUser2Stations(Map<Long, List<String>> map) {
  105. map.forEach((k, v) -> {
  106. String key = RedisKeys.ADMIN_USER_STATION_IDS + k;
  107. String[] values = v.toArray(new String[0]); // 优化数组创建方式,使用varargs来避免不必要的数组拷贝
  108. KymCacheInjector.redisTemplate.opsForSet().add(key, values);
  109. });
  110. }
  111. /**
  112. * 获取运营平台操作员有权限的站点ID
  113. *
  114. * @param adminUserId
  115. * @return
  116. */
  117. public List<String> getAdminUserStationIds(Long adminUserId) {
  118. return KymCacheInjector.redisTemplate.opsForSet().members(RedisKeys.ADMIN_USER_STATION_IDS + adminUserId).stream().toList();
  119. }
  120. public void putConnectorId2ParkingNo(Map<String, String> map) {
  121. map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.CONNECTOR_ID_TO_PARKING_NO + k, v));
  122. }
  123. /**
  124. * @param id
  125. * @return
  126. */
  127. public String getParkNoByEquipmentIdOrConnectorId(String id) {
  128. var connectorId = getConnectorId(id);
  129. return KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.CONNECTOR_ID_TO_PARKING_NO + connectorId);
  130. }
  131. /**
  132. * 将equipmentId,connectorId,shortId转化成17为的connectorId
  133. *
  134. * @param id
  135. * @return
  136. */
  137. public String getConnectorId(String id) {
  138. return switch (id.length()) {
  139. case 17 -> id;
  140. case 16 -> id.concat("1");
  141. case 6 ->
  142. CONNECTOR_ID_SHORT_ID_MAPPING.entrySet().stream().filter(entry -> id.equals(entry.getValue())).map(Map.Entry::getKey).findFirst().get();
  143. default -> null;
  144. };
  145. }
  146. /**
  147. * 缓存短编号和设备信息
  148. *
  149. * @param map
  150. */
  151. public void putWashShortId2ProductKeyAndDeviceName(Map<String, String> map) {
  152. map.forEach((k, v) -> KymCacheInjector.redisTemplate.opsForValue().set(RedisKeys.WASH_SHORT_ID_TO_PRODUCT_KEY_AND_DEVICE_NAME + k, v));
  153. }
  154. /**
  155. * 通过短编号获取产品密钥和设备名称
  156. *
  157. * @param shortId
  158. * @return
  159. */
  160. public String[] getProductKeyAndDeviceNameByWashShortId(String shortId) {
  161. var res = Objects.requireNonNull(KymCacheInjector.redisTemplate.opsForValue().get(RedisKeys.WASH_SHORT_ID_TO_PRODUCT_KEY_AND_DEVICE_NAME + shortId)).split(",");
  162. if (res.length != 2) {
  163. throw new BusinessException("设备信息异常");
  164. }
  165. return res;
  166. }
  167. @Component
  168. public static class KymCacheInjector implements ApplicationListener<ApplicationStartedEvent> {
  169. private static final StringRedisTemplate redisTemplate = SpringUtil.getBean(StringRedisTemplate.class);
  170. private final EquipmentRelationService equipmentRelationService;
  171. private final StationService stationService;
  172. private final ConnectorInfoService connectorInfoService;
  173. private KymCacheInjector(EquipmentRelationService equipmentRelationService, StationService stationService, ConnectorInfoService connectorInfoService) {
  174. this.equipmentRelationService = equipmentRelationService;
  175. this.stationService = stationService;
  176. this.connectorInfoService = connectorInfoService;
  177. }
  178. @Override
  179. public void onApplicationEvent(ApplicationStartedEvent event) {
  180. // 将数据库数据缓存到redis
  181. var equipmentRelations = equipmentRelationService.list();
  182. var connectorId2ShortId = equipmentRelations.stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getShortId));
  183. KymCache.INSTANCE.putConnectorId2ShortId(connectorId2ShortId);
  184. var connector2Station = equipmentRelations.stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getStationId));
  185. KymCache.INSTANCE.putConnectorId2StationId(connector2Station);
  186. var stationId2Name = stationService.list().stream().collect(Collectors.toMap(Station::getStationId, Station::getStationName));
  187. KymCache.INSTANCE.putStationId2Name(stationId2Name);
  188. var connectorId2ParkingNo = equipmentRelations.stream().collect(Collectors.toMap(EquipmentRelation::getConnectorId, EquipmentRelation::getParkingNo));
  189. KymCache.INSTANCE.putConnectorId2ParkingNo(connectorId2ParkingNo);
  190. var connectorId2Status = connectorInfoService.list().stream().collect(Collectors.toMap(ConnectorInfo::getConnectorId, ConnectorInfo::getStatus));
  191. KymCache.INSTANCE.putConnectorId2Status(connectorId2Status);
  192. }
  193. }
  194. }