diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java new file mode 100644 index 0000000..13e4b96 --- /dev/null +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectdeclared/utils/ProjectIdCodeCacheUtil.java @@ -0,0 +1,76 @@ +package com.hz.pm.api.projectdeclared.utils; + +import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import com.baomidou.mybatisplus.core.conditions.Wrapper; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.hz.pm.api.projectlib.model.entity.Project; +import com.hz.pm.api.projectlib.service.IProjectService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.stream.Collectors; + +/** + *

+ * ProjectIdCodeCacheUtil + *

+ * + * @author WendyYang + * @since 18:40 2024/3/25 + */ +@Slf4j +@Component +@RequiredArgsConstructor +public class ProjectIdCodeCacheUtil { + + private final IProjectService projectService; + + @PostConstruct + public void initProjectIdCodeCache() { + Wrapper query = Wrappers.lambdaQuery(Project.class) + .select(Project::getProjectCode, Project::getId); + for (Project project : projectService.list(query)) { + put(project.getId(), project.getProjectCode()); + } + log.info("初始化项目ID编码缓存:{}", CACHE.size()); + } + + //================================================================================================================== + + private static final Map CACHE = new ConcurrentHashMap<>(); + + public static String get(Long projectId) { + return CACHE.get(projectId); + } + + public static List get(String projectCode) { + return CACHE.entrySet() + .stream() + .filter(w -> w.getValue().equals(projectCode)) + .map(Map.Entry::getKey) + .collect(Collectors.toList()); + } + + public static void put(Long projectId, String projectCode) { + CACHE.put(projectId, projectCode); + } + + public static Long newest(String projectCode) { + return CollUtil.max(get(projectCode)); + } + + public static Long newest(Long projectId) { + String projectCode = get(projectId); + if (StrUtil.isBlank(projectCode)) { + return projectId; + } + return CollUtil.max(get(projectCode)); + } + +} diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectService.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectService.java index 4cfd2e6..1e6c355 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectService.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/IProjectService.java @@ -1,21 +1,22 @@ package com.hz.pm.api.projectlib.service; +import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.IService; import com.hz.pm.api.common.model.constant.BizConst; +import com.hz.pm.api.projectdeclared.utils.ProjectIdCodeCacheUtil; import com.hz.pm.api.projectlib.model.entity.Project; -import com.baomidou.mybatisplus.extension.service.IService; import com.hz.pm.api.projectlib.model.po.ProjectPO; import com.hz.pm.api.projectlib.model.req.ProjectListReq; import java.util.Collection; +import java.util.Collections; import java.util.List; -import java.util.Map; import java.util.Optional; -import java.util.concurrent.ConcurrentHashMap; /** *

@@ -27,51 +28,36 @@ import java.util.concurrent.ConcurrentHashMap; */ public interface IProjectService extends IService { - Map PROJECT_CODE_MAP_ID = new ConcurrentHashMap<>(); - default Optional getProjectCode(Long projectId) { - if (projectId == null) { - return Optional.empty(); - } - String projectCode = PROJECT_CODE_MAP_ID.get(projectId); - if (projectCode == null) { - synchronized (this) { - projectCode = PROJECT_CODE_MAP_ID.get(projectId); - if (projectCode == null) { - Wrapper query = Wrappers.lambdaQuery(Project.class) - .select(Project::getProjectCode) - .eq(Project::getId, projectId) - .last(BizConst.LIMIT_1); - Project project = getOne(query); - if (project != null) { - projectCode = project.getProjectCode(); - PROJECT_CODE_MAP_ID.put(projectId, projectCode); - } - } - - } - } - return Optional.ofNullable(projectCode); + return Optional.ofNullable(ProjectIdCodeCacheUtil.get(projectId)); + } + + @Override + default boolean save(Project entity) { + boolean state = IService.super.save(entity); + // 将项目ID与编码的对应关系保存下来 + ProjectIdCodeCacheUtil.put(entity.getId(), entity.getProjectCode()); + return state; } default Optional getNewProjectId(String projectCode) { - LambdaQueryWrapper query = Wrappers.lambdaQuery(Project.class) - .select(Project::getId) - .eq(Project::getProjectCode, projectCode) - .orderByDesc(Project::getVersion) - .last(BizConst.LIMIT_1); - return Optional.ofNullable(getOne(query)) - .flatMap(w -> Optional.of(w.getId())); + return Optional.ofNullable(ProjectIdCodeCacheUtil.newest(projectCode)); } - List allVersionProjectIds(String projectCode); + default List allVersionProjectIds(String projectCode) { + return ProjectIdCodeCacheUtil.get(projectCode); + } - List allVersionProjectIds(Long projectId); + default List allVersionProjectIds(Long projectId) { + String projectCode = ProjectIdCodeCacheUtil.get(projectId); + if (StrUtil.isBlank(projectCode)) { + return Collections.emptyList(); + } + return ProjectIdCodeCacheUtil.get(projectCode); + } Project getNewProject(Long projectId); - Project getNewConstructProject(Long projectId); - Project getProjectByCode(String projectCode); Page pageAllWithPermission(Page page, ProjectListReq req); diff --git a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectServiceImpl.java b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectServiceImpl.java index 0afae21..baada85 100644 --- a/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectServiceImpl.java +++ b/hz-pm-api/src/main/java/com/hz/pm/api/projectlib/service/impl/ProjectServiceImpl.java @@ -1,12 +1,11 @@ package com.hz.pm.api.projectlib.service.impl; -import cn.hutool.core.collection.CollUtil; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.hz.pm.api.common.model.constant.BizConst; import com.hz.pm.api.common.model.constant.BooleanBit; +import com.hz.pm.api.projectdeclared.utils.ProjectIdCodeCacheUtil; import com.hz.pm.api.projectlib.mapper.ProjectMapper; import com.hz.pm.api.projectlib.model.entity.Project; import com.hz.pm.api.projectlib.model.po.ProjectPO; @@ -15,12 +14,6 @@ import com.hz.pm.api.projectlib.service.IProjectService; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; -import java.util.Collections; -import java.util.List; -import java.util.Objects; -import java.util.Optional; -import java.util.stream.Collectors; - /** *

* 服务实现类 @@ -33,62 +26,9 @@ import java.util.stream.Collectors; @RequiredArgsConstructor public class ProjectServiceImpl extends ServiceImpl implements IProjectService { - private final ProjectMapper projectMapper; - - @Override - public List allVersionProjectIds(String projectCode) { - //查出 项目code - List projects = list(Wrappers.lambdaQuery(Project.class) - .select(Project::getId).eq(Project::getProjectCode, projectCode)); - - if (CollUtil.isEmpty(projects)) { - return Collections.emptyList(); - } - return projects.stream().map(Project::getId).collect(Collectors.toList()); - } - - @Override - public List allVersionProjectIds(Long projectId) { - Optional projectCode = getProjectCode(projectId); - return projectCode.flatMap(code -> Optional.of(allVersionProjectIds(code))) - .orElse(Collections.emptyList()); - } - @Override public Project getNewProject(Long projectId) { - Project project = getById(projectId); - - if (Objects.isNull(project)) { - return null; - } - - String projectCode = project.getProjectCode(); - - LambdaQueryWrapper last = Wrappers.lambdaQuery(Project.class) - .eq(Project::getNewest, Boolean.TRUE) - .eq(Project::getProjectCode, projectCode) - .last(BizConst.LIMIT_1); - - return getOne(last); - } - - @Override - public Project getNewConstructProject(Long projectId) { - Project project = getById(projectId); - - if (Objects.isNull(project)) { - return null; - } - - String projectCode = project.getProjectCode(); - - LambdaQueryWrapper last = Wrappers.lambdaQuery(Project.class) - .eq(Project::getNewest, BooleanBit.TRUE) - .eq(Project::getProjectCode, projectCode) - .eq(Project::getIsConstruct, BooleanBit.TRUE) - .last(BizConst.LIMIT_1); - - return getOne(last); + return getById(ProjectIdCodeCacheUtil.newest(projectId)); } @Override @@ -101,11 +41,11 @@ public class ProjectServiceImpl extends ServiceImpl impl @Override public Page pageAllWithPermission(Page page, ProjectListReq req) { - return projectMapper.pageAllWithPermission(page, req); + return baseMapper.pageAllWithPermission(page, req); } @Override public Page pageLibWithPermission(Page page, ProjectListReq req) { - return projectMapper.pageLibWithPermission(page, req); + return baseMapper.pageLibWithPermission(page, req); } }