KymCache.java 8.4 KB

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