VppUnPropertiesRegistry.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package com.usky.vpp.config;
  2. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  3. import com.usky.common.core.exception.BusinessException;
  4. import com.usky.vpp.domain.VppRegistration;
  5. import com.usky.vpp.domain.VppTenantConfig;
  6. import com.usky.vpp.mapper.VppRegistrationMapper;
  7. import com.usky.vpp.mapper.VppTenantConfigMapper;
  8. import com.usky.vpp.util.VppAuditHelper;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.beans.factory.annotation.Autowired;
  12. import org.springframework.stereotype.Component;
  13. import org.springframework.util.CollectionUtils;
  14. import org.springframework.util.StringUtils;
  15. import java.util.Collection;
  16. import java.util.Collections;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.concurrent.ConcurrentHashMap;
  20. /**
  21. * 租户 UN 配置全局内存缓存,key=tenantId,value=VppUnProperties
  22. */
  23. @Component
  24. public class VppUnPropertiesRegistry {
  25. private static final Logger log = LoggerFactory.getLogger(VppUnPropertiesRegistry.class);
  26. private final ConcurrentHashMap<Integer, VppUnProperties> cache = new ConcurrentHashMap<>();
  27. private final ConcurrentHashMap<String, Integer> dnIdToTenantId = new ConcurrentHashMap<>();
  28. @Autowired
  29. private VppTenantConfigMapper tenantConfigMapper;
  30. @Autowired
  31. private VppRegistrationMapper registrationMapper;
  32. public void refreshAll() {
  33. List<VppTenantConfig> configs = tenantConfigMapper.selectList(new LambdaQueryWrapper<>());
  34. if (CollectionUtils.isEmpty(configs)) {
  35. cache.clear();
  36. dnIdToTenantId.clear();
  37. log.warn("vpp_tenant_config 无配置记录,UN 对接配置缓存为空");
  38. return;
  39. }
  40. Map<Integer, VppUnProperties> nextCache = new ConcurrentHashMap<>();
  41. Map<String, Integer> nextDnIndex = new ConcurrentHashMap<>();
  42. for (VppTenantConfig config : configs) {
  43. if (config.getTenantId() == null) {
  44. continue;
  45. }
  46. VppUnProperties properties = toProperties(config);
  47. enrichRegistrationId(properties);
  48. nextCache.put(config.getTenantId(), properties);
  49. if (StringUtils.hasText(config.getDnId())) {
  50. nextDnIndex.put(config.getDnId(), config.getTenantId());
  51. }
  52. }
  53. cache.clear();
  54. cache.putAll(nextCache);
  55. dnIdToTenantId.clear();
  56. dnIdToTenantId.putAll(nextDnIndex);
  57. log.info("已加载 {} 条租户 UN 配置至内存", cache.size());
  58. }
  59. public VppUnProperties get(Integer tenantId) {
  60. if (tenantId == null) {
  61. return null;
  62. }
  63. return cache.get(tenantId);
  64. }
  65. public VppUnProperties require(Integer tenantId) {
  66. VppUnProperties properties = get(tenantId);
  67. if (properties == null) {
  68. throw new BusinessException("租户 " + tenantId + " 未配置运管平台 UN 对接信息");
  69. }
  70. return properties;
  71. }
  72. public VppUnProperties getCurrent() {
  73. // 定时任务:runWithTenant 传入的 tenantId;接口调用:SecurityUtils.getTenantId()
  74. return require(VppUnTenantContext.resolveTenantId());
  75. }
  76. public VppUnProperties getByDnId(String dnId) {
  77. if (!StringUtils.hasText(dnId)) {
  78. return null;
  79. }
  80. Integer tenantId = dnIdToTenantId.get(dnId);
  81. return tenantId != null ? get(tenantId) : null;
  82. }
  83. public Collection<VppUnProperties> getAll() {
  84. return Collections.unmodifiableCollection(cache.values());
  85. }
  86. public boolean hasCryptoEnabledTenant() {
  87. return cache.values().stream().anyMatch(this::isCryptoActive);
  88. }
  89. private boolean isCryptoActive(VppUnProperties properties) {
  90. return Boolean.TRUE.equals(properties.getCryptoEnabled())
  91. && StringUtils.hasText(properties.getUnPublicKey())
  92. && StringUtils.hasText(properties.getDnPrivateKey());
  93. }
  94. public void updateRegistrationId(Integer tenantId, String registrationId) {
  95. VppUnProperties properties = get(tenantId);
  96. if (properties != null) {
  97. properties.setRegistrationId(registrationId);
  98. }
  99. }
  100. public void updateToken(Integer tenantId, String token) {
  101. VppUnProperties properties = get(tenantId);
  102. if (properties != null) {
  103. properties.setToken(token);
  104. }
  105. }
  106. private VppUnProperties toProperties(VppTenantConfig config) {
  107. VppUnProperties properties = new VppUnProperties();
  108. properties.setTenantId(config.getTenantId());
  109. properties.setDnId(config.getDnId());
  110. properties.setDnName(config.getDnName());
  111. properties.setBaseUrl(config.getBaseUrl());
  112. properties.setTransportAddress(config.getTransportAddress());
  113. properties.setUnPublicKey(config.getUnPublicKey());
  114. properties.setDnPublicKey(config.getDnPublicKey());
  115. properties.setDnPrivateKey(config.getDnPrivateKey());
  116. properties.setToken(config.getToken());
  117. properties.setPollIntervalSec(config.getPollIntervalSec() != null ? config.getPollIntervalSec() : 10);
  118. properties.setOutboundEnabled(config.getOutboundEnabled());
  119. properties.setPollEnabled(config.getPollEnabled());
  120. properties.setCryptoEnabled(config.getCryptoEnabled());
  121. properties.setPriceDownCoeff(StringUtils.hasText(config.getPriceDownCoeff()) ? config.getPriceDownCoeff() : "0.8");
  122. properties.setAutoAckClearing(config.getAutoAckClearing());
  123. properties.setAutoRegisterOnStartup(config.getAutoRegisterOnStartup());
  124. properties.setTokenTtlMinutes(25);
  125. properties.setConnectTimeoutMs(10000);
  126. properties.setReadTimeoutMs(30000);
  127. return properties;
  128. }
  129. private void enrichRegistrationId(VppUnProperties properties) {
  130. if (!StringUtils.hasText(properties.getDnId())) {
  131. return;
  132. }
  133. LambdaQueryWrapper<VppRegistration> wrapper = new LambdaQueryWrapper<VppRegistration>()
  134. .eq(VppRegistration::getDnId, properties.getDnId())
  135. .eq(VppRegistration::getDeleteFlag, VppAuditHelper.NOT_DELETED)
  136. .orderByDesc(VppRegistration::getRegisteredAt)
  137. .last("LIMIT 1");
  138. if (properties.getTenantId() != null) {
  139. wrapper.eq(VppRegistration::getTenantId, properties.getTenantId());
  140. }
  141. VppRegistration registration = registrationMapper.selectOne(wrapper);
  142. if (registration != null && StringUtils.hasText(registration.getRegistrationId())) {
  143. properties.setRegistrationId(registration.getRegistrationId());
  144. }
  145. }
  146. }