diff --git a/src/main/java/com/chint/domain/aggregates/approval/platform/ApprovalPlatformAN.java b/src/main/java/com/chint/domain/aggregates/approval/platform/ApprovalPlatformAN.java index 234e6db8..46201b78 100644 --- a/src/main/java/com/chint/domain/aggregates/approval/platform/ApprovalPlatformAN.java +++ b/src/main/java/com/chint/domain/aggregates/approval/platform/ApprovalPlatformAN.java @@ -101,6 +101,7 @@ public class ApprovalPlatformAN implements ApprovalPlatform { approvalPlatformInfo.changeUrl() .ifPresent(url -> { ApprovalScheduleParam approvalScheduleParam = createApprovalScheduleParam(approvalData); + System.out.println(Json.gson().toJson(approvalScheduleParam)); CompletableFuture.runAsync(() -> DelayDispatch.attemptToSend(() -> postRequest.post(url, approvalScheduleParam, ANResponse.class) .getSuccess(), 0)); }); diff --git a/src/main/java/com/chint/domain/aggregates/order/Leg.java b/src/main/java/com/chint/domain/aggregates/order/Leg.java index 11b6cd4c..767b332e 100644 --- a/src/main/java/com/chint/domain/aggregates/order/Leg.java +++ b/src/main/java/com/chint/domain/aggregates/order/Leg.java @@ -446,4 +446,13 @@ public class Leg implements Serializable, EventManageable { } return this; } + + public Optional getLastChangeEvent() { + if (this.eventList == null) { + return Optional.empty(); + } + return this.eventList.stream() + .filter(legEvent -> legEvent.getEventType().equals(LEG_EVENT_CHANGE)) + .max(Comparator.comparing(LegEvent::getHappenTime)); + } } \ No newline at end of file diff --git a/src/main/java/com/chint/domain/repository/SystemCodeRepository.java b/src/main/java/com/chint/domain/repository/SystemCodeRepository.java index 4d0ba74d..379d0ed4 100644 --- a/src/main/java/com/chint/domain/repository/SystemCodeRepository.java +++ b/src/main/java/com/chint/domain/repository/SystemCodeRepository.java @@ -15,5 +15,7 @@ public interface SystemCodeRepository { SystemCode findById(Long id); + List findByIds(Set ids); + List findBySysCodeIn(Set sysCodes); } diff --git a/src/main/java/com/chint/domain/service/RouteApprovalDomainService.java b/src/main/java/com/chint/domain/service/RouteApprovalDomainService.java index 7464f0e0..e0778551 100644 --- a/src/main/java/com/chint/domain/service/RouteApprovalDomainService.java +++ b/src/main/java/com/chint/domain/service/RouteApprovalDomainService.java @@ -11,14 +11,12 @@ import com.chint.domain.aggregates.system.SystemCode; import com.chint.domain.aggregates.system.SystemOrganization; import com.chint.domain.repository.*; import com.chint.domain.value_object.enums.RoutePermission; +import com.chint.infrastructure.util.StringCheck; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.stream.Collectors; import static com.chint.domain.aggregates.approval.ApprovalType.LEG_ADD_CHANGE_BATCH; @@ -79,6 +77,7 @@ public class RouteApprovalDomainService { // 提取所有 approveOrderNo 和 sysCode Set accountCompanyCodes = routeOrderList.stream() .map(routeOrder -> routeOrder.getApproveOrderNo().getAccountCompany()) + .filter(it-> !StringCheck.isFirstCharacterChinese(it)) .collect(Collectors.toSet()); Set sysCodes = routeOrderList.stream() @@ -88,11 +87,13 @@ public class RouteApprovalDomainService { // 批量查询 systemOrganizationRepository 和 systemCodeRepository Map accountCompanyToApprovalType = systemOrganizationRepository.findByOrgCodeIn(accountCompanyCodes) .stream() + .filter(Objects::nonNull) .filter(it -> it.getApprovalType() != null && !it.getApprovalType().isEmpty()) .collect(Collectors.toMap(SystemOrganization::getOrgCode, SystemOrganization::getApprovalType)); Map sysCodeToApprovalType = systemCodeRepository.findBySysCodeIn(sysCodes) .stream() + .filter(Objects::nonNull) .filter(it -> it.getApprovalType() != null && !it.getApprovalType().isEmpty()) .collect(Collectors.toMap(SystemCode::getSystemCode, SystemCode::getApprovalType)); diff --git a/src/main/java/com/chint/infrastructure/echo_framework/repository/AbstractGenericRepository.java b/src/main/java/com/chint/infrastructure/echo_framework/repository/AbstractGenericRepository.java new file mode 100644 index 00000000..d73015f9 --- /dev/null +++ b/src/main/java/com/chint/infrastructure/echo_framework/repository/AbstractGenericRepository.java @@ -0,0 +1,69 @@ +package com.chint.infrastructure.echo_framework.repository; + +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; + +public abstract class AbstractGenericRepository { + +// protected final GenericCacheRepository cacheRepository; +// protected final JdbcGenericRepository jdbcRepository; +// protected final String cachePrefix; +// +// protected AbstractGenericRepository(GenericCacheRepository cacheRepository, +// JdbcGenericRepository jdbcRepository, +// String cachePrefix) { +// this.cacheRepository = cacheRepository; +// this.jdbcRepository = jdbcRepository; +// this.cachePrefix = cachePrefix; +// } +// +// public T save(T entity) { +// T savedEntity = jdbcRepository.save(entity); +// updateCache(savedEntity); +// return savedEntity; +// } +// +// public T findById(ID id) { +// T entity = cacheRepository.findFromCache(cachePrefix, id); +// if (entity == null) { +// Optional optionalEntity = jdbcRepository.findById(id); +// if (optionalEntity.isPresent()) { +// entity = optionalEntity.get(); +// T finalEntity = entity; +// CompletableFuture.runAsync(() -> updateCache(finalEntity)); +// } +// } +// return entity; +// } + +// public T findByField(Function fieldGetter, String fieldPrefix, String fieldValue) { +// ID id = cacheRepository.findIdFromCache(fieldPrefix, fieldValue.toString()); +// if (id == null) { +// T entity = findByFieldFromDatabase(fieldValue); +// if (entity != null) { +// CompletableFuture.runAsync(() -> updateCache(entity)); +// } +// return entity; +// } +// return findById(id); +// } + +// protected abstract String getPrefix(); +// +// protected abstract List findByFieldFromDatabase(String fieldValue); +// +// protected abstract List findByFieldInFromDatabase(List fieldValueList); +// +// protected abstract void updateCache(T entity); +// +// protected void updateFieldCache(String fieldName, String fieldValue, ID id) { +// cacheRepository.cacheEvictFieldToId(fieldName, fieldValue); +// cacheRepository.cacheFieldToId(fieldName, fieldValue, id); +// } +// +// protected String getFieldPrefix(String fieldName) { +// return cachePrefix + "::" + fieldName; +// } +} \ No newline at end of file diff --git a/src/main/java/com/chint/infrastructure/echo_framework/repository/GenericCacheRepository.java b/src/main/java/com/chint/infrastructure/echo_framework/repository/GenericCacheRepository.java new file mode 100644 index 00000000..a4a9935c --- /dev/null +++ b/src/main/java/com/chint/infrastructure/echo_framework/repository/GenericCacheRepository.java @@ -0,0 +1,71 @@ +package com.chint.infrastructure.echo_framework.repository; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +import java.util.List; + +//@Component +public class GenericCacheRepository { + +// @Autowired +// private RedisTemplate redisTemplate; +// +// @Autowired +// private RedisTemplate redisTemplateId; +// +// private final Class entityType; +// private final Class idType; +// +// public GenericCacheRepository(Class entityType, Class idType) { +// this.entityType = entityType; +// this.idType = idType; +// } +// +// private String getKey(String prefix, Object value) { +// return prefix + "::" + value; +// } +// +// public T cache(String prefix, ID id, T entity) { +// String key = getKey(prefix, id); +// redisTemplate.opsForValue().set(key, entity); +// return entity; +// } +// +// public void cacheEvict(String prefix, ID id) { +// String key = getKey(prefix, id); +// redisTemplate.delete(key); +// } +// +// public T findFromCache(String prefix, ID id) { +// String key = getKey(prefix, id); +// return redisTemplate.opsForValue().get(key); +// } +// +// public List cacheBatchByIds(String prefix, List ids) { +// List keys = ids.stream().map(id -> getKey(prefix, id)).toList(); +// return redisTemplate.opsForValue().multiGet(keys); +// } +// +// public ID cacheFieldToId(String fieldPrefix, String fieldValue, ID id) { +// String key = getKey(fieldPrefix, fieldValue); +// redisTemplateId.opsForValue().set(key, id); +// return id; +// } +// +// public ID findIdFromCache(String fieldPrefix, String fieldValue) { +// String key = getKey(fieldPrefix, fieldValue); +// return redisTemplateId.opsForValue().get(key); +// } +// +// public List findIdsFromCache(String fieldPrefix, List fieldValueList) { +// List keys = fieldValueList.stream().map(fieldValue -> getKey(fieldPrefix, fieldValue)).toList(); +// return redisTemplateId.opsForValue().multiGet(keys); +// } +// +// public void cacheEvictFieldToId(String fieldPrefix, String fieldValue) { +// String key = getKey(fieldPrefix, fieldValue); +// redisTemplateId.delete(key); +// } +} \ No newline at end of file diff --git a/src/main/java/com/chint/infrastructure/echo_framework/repository/JdbcGenericRepository.java b/src/main/java/com/chint/infrastructure/echo_framework/repository/JdbcGenericRepository.java new file mode 100644 index 00000000..8caeed5c --- /dev/null +++ b/src/main/java/com/chint/infrastructure/echo_framework/repository/JdbcGenericRepository.java @@ -0,0 +1,14 @@ +package com.chint.infrastructure.echo_framework.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Optional; + +@Repository +public interface JdbcGenericRepository extends CrudRepository { + Optional findById(ID id); + + List findAll(); +} \ No newline at end of file diff --git a/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java index 411f5b52..9136c025 100644 --- a/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/PermissionConfigRepositoryImpl.java @@ -2,6 +2,7 @@ package com.chint.infrastructure.repository; import com.chint.domain.aggregates.base.PermissionConfig; import com.chint.domain.repository.PermissionConfigRepository; +import com.chint.infrastructure.repository.cache.CachePermissionConfigRepository; import com.chint.infrastructure.repository.jdbc.JdbcPermissionConfigRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; @@ -11,6 +12,7 @@ import java.util.List; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.concurrent.CompletableFuture; @Repository public class PermissionConfigRepositoryImpl implements PermissionConfigRepository { @@ -18,14 +20,41 @@ public class PermissionConfigRepositoryImpl implements PermissionConfigRepositor @Autowired private JdbcPermissionConfigRepository jdbcPermissionConfigRepository; + @Autowired + private CachePermissionConfigRepository cachePermissionConfigRepository; + @Cacheable(value = "PermissionConfig", key = "#permissionName") @Override public Optional findByPermissionName(String permissionName) { - return jdbcPermissionConfigRepository.findByPermissionName(permissionName).stream().findFirst(); + return jdbcPermissionConfigRepository.findByPermissionName(permissionName) + .stream() + .findFirst(); } @Override public List findByPermissionNameIn(Set approvalTypes) { - return jdbcPermissionConfigRepository.findByPermissionNameIn(approvalTypes.stream().filter(Objects::nonNull).toList()); + List permissionConfigs = cachePermissionConfigRepository + .cachePermissionConfigBatch(approvalTypes); + List cachedNames = permissionConfigs.stream() + .filter(Objects::nonNull) + .map(PermissionConfig::getPermissionName) + .toList(); + List missingNames = approvalTypes.stream() + .filter(Objects::nonNull) + .filter(name -> !cachedNames.contains(name)) + .toList(); + if (!missingNames.isEmpty()) { + List fetchedPermissionConfigs = jdbcPermissionConfigRepository + .findByPermissionNameIn(missingNames); + permissionConfigs.addAll(fetchedPermissionConfigs); + CompletableFuture.runAsync(() -> fetchedPermissionConfigs.forEach(this::updateCache)); + } + return permissionConfigs; + } + + private void updateCache(PermissionConfig permissionConfig) { + // 更新缓存的逻辑 + cachePermissionConfigRepository.cacheEvictByName(permissionConfig.getPermissionName()); + cachePermissionConfigRepository.cacheByName(permissionConfig); } } diff --git a/src/main/java/com/chint/infrastructure/repository/SystemCodeRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/SystemCodeRepositoryImpl.java index 316f8879..b9fad542 100644 --- a/src/main/java/com/chint/infrastructure/repository/SystemCodeRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/SystemCodeRepositoryImpl.java @@ -1,41 +1,108 @@ package com.chint.infrastructure.repository; import com.chint.domain.aggregates.system.SystemCode; -import com.chint.domain.exceptions.NotFoundException; import com.chint.domain.repository.SystemCodeRepository; +import com.chint.infrastructure.echo_framework.repository.AbstractGenericRepository; +import com.chint.infrastructure.echo_framework.repository.GenericCacheRepository; +import com.chint.infrastructure.echo_framework.repository.JdbcGenericRepository; +import com.chint.infrastructure.repository.cache.CacheSystemCodeRepository; import com.chint.infrastructure.repository.jdbc.JdbcSystemCodeRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Repository; -import java.util.List; -import java.util.Objects; -import java.util.Set; +import java.util.*; +import java.util.concurrent.CompletableFuture; @Repository -public class SystemCodeRepositoryImpl implements SystemCodeRepository { +public class SystemCodeRepositoryImpl implements SystemCodeRepository { @Autowired private JdbcSystemCodeRepository jdbcSystemCodeRepository; + @Autowired + private CacheSystemCodeRepository cacheSystemCodeRepository; + +// private static final String SYSTEM_CODE_PREFIX = "SystemCode"; + +// @Autowired +// public SystemCodeRepositoryImpl(GenericCacheRepository cacheRepository, +// JdbcGenericRepository jdbcRepository) { +// super(cacheRepository, jdbcRepository, SYSTEM_CODE_PREFIX); +// } + @Override public SystemCode save(SystemCode systemCode) { - return jdbcSystemCodeRepository.save(systemCode); + SystemCode save = jdbcSystemCodeRepository.save(systemCode); + updateCache(save); + return save; } - @Cacheable(value = "SystemCode", key = "#sysCode") @Override public SystemCode findBySysCode(String sysCode) { - return jdbcSystemCodeRepository.findBySystemCode(sysCode); + Long id = cacheSystemCodeRepository.cacheSysCodeMapToId(sysCode, null); + if (id == null) { + SystemCode systemCode = jdbcSystemCodeRepository.findBySystemCode(sysCode); + if (systemCode != null) { + CompletableFuture.runAsync(() -> updateCache(systemCode)); + } + return systemCode; + } + return findById(id); } @Override public SystemCode findById(Long id) { - return jdbcSystemCodeRepository.findById(id).orElseThrow(() -> new NotFoundException("该产业公司不在实施范围内")); + SystemCode systemCode = cacheSystemCodeRepository.cache(id, null); + if (systemCode == null) { + Optional optionalSystemCode = jdbcSystemCodeRepository.findById(id); + if (optionalSystemCode.isPresent()) { + SystemCode foundSystemCode = optionalSystemCode.get(); + systemCode = foundSystemCode; + CompletableFuture.runAsync(() -> updateCache(foundSystemCode)); + } + } + return systemCode; + } + + + + @Override + public List findByIds(Set ids) { + List systemCodes = cacheSystemCodeRepository.cacheBatchByIds(new ArrayList<>(ids)); + List cachedIds = systemCodes.stream().filter(Objects::nonNull).map(SystemCode::getId).toList(); + List missingIds = ids.stream().filter(Objects::nonNull).filter(id -> !cachedIds.contains(id)).toList(); + if (!missingIds.isEmpty()) { + List fetchedSystemCodes = jdbcSystemCodeRepository.findByIdIn(missingIds); + systemCodes.addAll(fetchedSystemCodes); + CompletableFuture.runAsync(() -> fetchedSystemCodes.forEach(this::updateCache)); + } + return systemCodes; } @Override public List findBySysCodeIn(Set sysCodes) { - return jdbcSystemCodeRepository.findBySystemCodeIn(sysCodes.stream().filter(Objects::nonNull).toList()); + List cachedIds = cacheSystemCodeRepository.cacheIdsBatchBySysCodeList(new ArrayList<>(sysCodes)); + List systemCodes = findByIds(new HashSet<>(cachedIds)); + List cachedSysCodes = systemCodes.stream().filter(Objects::nonNull).map(SystemCode::getSystemCode).toList(); + List missingSysCodes = sysCodes.stream().filter(Objects::nonNull).filter(sysCode -> !cachedSysCodes.contains(sysCode)).toList(); + if (!missingSysCodes.isEmpty()) { + List fetchedSystemCodes = jdbcSystemCodeRepository.findBySystemCodeIn(missingSysCodes); + systemCodes = new ArrayList<>(systemCodes); + systemCodes.addAll(fetchedSystemCodes); + CompletableFuture.runAsync(() -> fetchedSystemCodes.forEach(this::updateCache)); + } + return systemCodes; + } + +// @Override +// protected void updateCache(SystemCode entity) { +// +// } + + private void updateCache(SystemCode systemCode) { + cacheSystemCodeRepository.cacheEvict(systemCode); + cacheSystemCodeRepository.cache(systemCode.getId(), systemCode); + cacheSystemCodeRepository.cacheEvictSysCodeMapToId(systemCode.getSystemCode()); + cacheSystemCodeRepository.cacheSysCodeMapToId(systemCode.getSystemCode(), systemCode.getId()); } } diff --git a/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java b/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java index 6b1e4b00..8ad20561 100644 --- a/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java +++ b/src/main/java/com/chint/infrastructure/repository/SystemOrganizationRepositoryImpl.java @@ -5,10 +5,10 @@ import com.chint.domain.repository.SystemOrganizationRepository; import com.chint.infrastructure.repository.cache.CacheSystemOrganizationRepository; import com.chint.infrastructure.repository.jdbc.JdbcSystemOrganizationRepository; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Repository; import java.util.*; +import java.util.concurrent.CompletableFuture; import java.util.stream.Stream; @Repository @@ -48,6 +48,40 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos return systemOrganizationList; } + + @Override + public Optional findByOrgCode(String orgCode) { + return jdbcSystemOrganizationRepository.findByOrgCode(orgCode).stream().findFirst(); + } + + @Override + public List findByOrgCodeIn(Set accountCompanyCodes) { + List inCacheIdList = cacheSystemOrganizationRepository + .getOrgIdsFromCacheByOrgCodeList(accountCompanyCodes.stream().filter(Objects::nonNull).toList()); + List byIdIn = findByIdIn(inCacheIdList); + List codes = byIdIn.stream().filter(Objects::nonNull).map(SystemOrganization::getOrgCode).toList(); + List notInCache = accountCompanyCodes.stream() + .filter(Objects::nonNull) + .filter(code -> !codes.contains(code)).toList(); + if (!notInCache.isEmpty()) { + //如果确实有些没有缓存到redis, 将从数据库查询出来然后异步保存到redis,然后返回 + List byOrgCodeIn = jdbcSystemOrganizationRepository + .findByOrgCodeIn(notInCache); + CompletableFuture.runAsync(() -> { + byOrgCodeIn.forEach(this::updateSystemOrganizationCache); + }); + byIdIn.addAll(byOrgCodeIn); + } + return byIdIn; + } + + private void updateSystemOrganizationCache(SystemOrganization systemOrganization) { + cacheSystemOrganizationRepository.cacheEvict(systemOrganization); + cacheSystemOrganizationRepository.cache(systemOrganization); + cacheSystemOrganizationRepository.cacheEvictOrgCodeById(systemOrganization.getOrgCode()); + cacheSystemOrganizationRepository.cacheOrgCodeById(systemOrganization.getOrgCode(), systemOrganization.getId()); + } + @Override public List findByIdIn(List ids) { List fromCacheByIdIn = cacheSystemOrganizationRepository.getFromCacheByIdIn(ids); @@ -55,7 +89,7 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos .filter(Objects::nonNull) .map(SystemOrganization::getId) .toList(); - List list = ids.stream().filter(it -> !fromCacheOrg.contains(it)).toList(); + List list = ids.stream().filter(it -> !fromCacheOrg.contains(it)).filter(Objects::nonNull).toList(); if (!list.isEmpty()) { List fromDBByIdIn = jdbcSystemOrganizationRepository.findByIdIn(list); fromDBByIdIn.forEach(it -> cacheSystemOrganizationRepository.cache(it)); // 更新缓存 @@ -143,10 +177,7 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos } - @Override - public Optional findByOrgCode(String orgCode) { - return jdbcSystemOrganizationRepository.findByOrgCode(orgCode).stream().findFirst(); - } + @Override public List queryOrgCodeListByUserId(Long userId) { @@ -154,11 +185,6 @@ public class SystemOrganizationRepositoryImpl implements SystemOrganizationRepos } - @Override - public List findByOrgCodeIn(Set accountCompanyCodes) { - return jdbcSystemOrganizationRepository.findByOrgCodeIn(accountCompanyCodes.stream().filter(Objects::nonNull).toList()); - } - private void expandOrganizationRecursively(SystemOrganization org, List allOrganizations) { if (org == null) { diff --git a/src/main/java/com/chint/infrastructure/repository/cache/CachePermissionConfigRepository.java b/src/main/java/com/chint/infrastructure/repository/cache/CachePermissionConfigRepository.java new file mode 100644 index 00000000..34d0f209 --- /dev/null +++ b/src/main/java/com/chint/infrastructure/repository/cache/CachePermissionConfigRepository.java @@ -0,0 +1,34 @@ +package com.chint.infrastructure.repository.cache; + +import com.chint.domain.aggregates.base.PermissionConfig; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Objects; +import java.util.Set; + +@Repository +public class CachePermissionConfigRepository { + + @Autowired + private RedisTemplate redisTemplate; + + public List cachePermissionConfigBatch(Set permissionNameList) { + List keys = permissionNameList.stream() + .filter(Objects::nonNull) + .map(name -> "PermissionConfig::" + name).toList(); + return redisTemplate.opsForValue().multiGet(keys); + } + + @Cacheable(value = "PermissionConfig", key = "#permissionConfig.permissionName") + public PermissionConfig cacheByName(PermissionConfig permissionConfig) { + return permissionConfig; + } + + @Cacheable(value = "PermissionConfig", key = "#permissionName") + public void cacheEvictByName(String permissionName) { + } +} diff --git a/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemCodeRepository.java b/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemCodeRepository.java new file mode 100644 index 00000000..3bceb9ee --- /dev/null +++ b/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemCodeRepository.java @@ -0,0 +1,60 @@ +package com.chint.infrastructure.repository.cache; + + +import com.chint.domain.aggregates.system.SystemCode; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Repository; + +import java.util.List; +import java.util.Objects; + +@Repository +public class CacheSystemCodeRepository { + + + @Autowired + private RedisTemplate redisTemplate; + + @Autowired + private RedisTemplate redisTemplateId; + + public List cacheBatchByIds(List ids) { + List keys = ids.stream().filter(Objects::nonNull).map(id -> "SystemCode::" + id).toList(); + if(keys.isEmpty()){ + return List.of(); + } + List systemCodes = redisTemplate.opsForValue().multiGet(keys); + return systemCodes == null ? List.of() : systemCodes; + } + + public List cacheIdsBatchBySysCodeList(List systemCodeList) { + List keys = systemCodeList.stream().filter(Objects::nonNull).map(systemCode -> "SystemCode::mapToId::" + systemCode).toList(); + if(keys.isEmpty()){ + return List.of(); + } + List ids = redisTemplateId.opsForValue().multiGet(keys); + return ids == null ? List.of() : ids; + } + + @Cacheable(value = "SystemCode", key = "#id") + public SystemCode cache(Long id, SystemCode systemCode) { + return systemCode; + } + + @CacheEvict(value = "SystemCode", key = "#systemCode.id") + public void cacheEvict(SystemCode systemCode) { + } + + + @Cacheable(value = "SystemCode::mapToId", key = "#sysCode") + public Long cacheSysCodeMapToId(String sysCode, Long systemId) { + return systemId; + } + + @CacheEvict(value = "SystemCode::mapToId", key = "#sysCode") + public void cacheEvictSysCodeMapToId(String sysCode) { + } +} diff --git a/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java b/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java index de6096b7..f70ac86f 100644 --- a/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/cache/CacheSystemOrganizationRepository.java @@ -17,6 +17,9 @@ public class CacheSystemOrganizationRepository { @Autowired private RedisTemplate redisTemplate; + @Autowired + private RedisTemplate redisTemplateId; + public List getFromCacheByIdIn(List ids) { List routeOrderKeys = ids.stream().map(id -> "SystemOrganization::" + id).toList(); List systemOrganizationList = redisTemplate.opsForValue().multiGet(routeOrderKeys); @@ -42,4 +45,19 @@ public class CacheSystemOrganizationRepository { @CacheEvict(value = "SystemOrganization::ParentId", key = "#parentId") public void cacheEvictIdsByParentId(Long parentId) { } + + @Cacheable(value = "SystemOrganization::OrgCode", key = "#orgCode") + public Long cacheOrgCodeById(String orgCode, Long systemOrganizationId) { + return systemOrganizationId; + } + + @CacheEvict(value = "SystemOrganization::OrgCode", key = "#orgCode") + public void cacheEvictOrgCodeById(String orgCode) { + } + + public List getOrgIdsFromCacheByOrgCodeList(List OrgCodeList) { + List routeOrderKeys = OrgCodeList.stream().map(orgCode -> "SystemOrganization::OrgCode::" + orgCode).toList(); + List ids = redisTemplateId.opsForValue().multiGet(routeOrderKeys); + return Objects.requireNonNullElseGet(ids, List::of); + } } diff --git a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcSystemCodeRepository.java b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcSystemCodeRepository.java index ea78ba64..31814997 100644 --- a/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcSystemCodeRepository.java +++ b/src/main/java/com/chint/infrastructure/repository/jdbc/JdbcSystemCodeRepository.java @@ -4,6 +4,7 @@ import com.chint.domain.aggregates.system.SystemCode; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; +import java.util.Collection; import java.util.List; @Repository @@ -12,4 +13,6 @@ public interface JdbcSystemCodeRepository extends CrudRepository findBySystemCodeIn(List systemCodes); + + List findByIdIn(Collection id); } diff --git a/src/main/java/com/chint/infrastructure/util/StringCheck.java b/src/main/java/com/chint/infrastructure/util/StringCheck.java index 36fa41c5..791192ef 100644 --- a/src/main/java/com/chint/infrastructure/util/StringCheck.java +++ b/src/main/java/com/chint/infrastructure/util/StringCheck.java @@ -23,4 +23,16 @@ public class StringCheck { char firstChar = str.charAt(0); return Character.isLetter(firstChar); } + + public boolean containsChinese(String str) { + if (str == null || str.isEmpty()) { + return false; + } + for (char c : str.toCharArray()) { + if (Character.UnicodeScript.of(c) == Character.UnicodeScript.HAN) { + return true; + } + } + return false; + } } \ No newline at end of file