@@ -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; | |||
/** | |||
* <p> | |||
* ProjectIdCodeCacheUtil | |||
* </p> | |||
* | |||
* @author WendyYang | |||
* @since 18:40 2024/3/25 | |||
*/ | |||
@Slf4j | |||
@Component | |||
@RequiredArgsConstructor | |||
public class ProjectIdCodeCacheUtil { | |||
private final IProjectService projectService; | |||
@PostConstruct | |||
public void initProjectIdCodeCache() { | |||
Wrapper<Project> 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<Long, String> CACHE = new ConcurrentHashMap<>(); | |||
public static String get(Long projectId) { | |||
return CACHE.get(projectId); | |||
} | |||
public static List<Long> 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)); | |||
} | |||
} |
@@ -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; | |||
/** | |||
* <p> | |||
@@ -27,51 +28,36 @@ import java.util.concurrent.ConcurrentHashMap; | |||
*/ | |||
public interface IProjectService extends IService<Project> { | |||
Map<Long, String> PROJECT_CODE_MAP_ID = new ConcurrentHashMap<>(); | |||
default Optional<String> 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<Project> 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<Long> getNewProjectId(String projectCode) { | |||
LambdaQueryWrapper<Project> 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<Long> allVersionProjectIds(String projectCode); | |||
default List<Long> allVersionProjectIds(String projectCode) { | |||
return ProjectIdCodeCacheUtil.get(projectCode); | |||
} | |||
List<Long> allVersionProjectIds(Long projectId); | |||
default List<Long> 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<ProjectPO> pageAllWithPermission(Page<ProjectPO> page, ProjectListReq req); | |||
@@ -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; | |||
/** | |||
* <p> | |||
* 服务实现类 | |||
@@ -33,62 +26,9 @@ import java.util.stream.Collectors; | |||
@RequiredArgsConstructor | |||
public class ProjectServiceImpl extends ServiceImpl<ProjectMapper, Project> implements IProjectService { | |||
private final ProjectMapper projectMapper; | |||
@Override | |||
public List<Long> allVersionProjectIds(String projectCode) { | |||
//查出 项目code | |||
List<Project> 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<Long> allVersionProjectIds(Long projectId) { | |||
Optional<String> 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<Project> 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<Project> 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<ProjectMapper, Project> impl | |||
@Override | |||
public Page<ProjectPO> pageAllWithPermission(Page<ProjectPO> page, ProjectListReq req) { | |||
return projectMapper.pageAllWithPermission(page, req); | |||
return baseMapper.pageAllWithPermission(page, req); | |||
} | |||
@Override | |||
public Page<ProjectPO> pageLibWithPermission(Page<ProjectPO> page, ProjectListReq req) { | |||
return projectMapper.pageLibWithPermission(page, req); | |||
return baseMapper.pageLibWithPermission(page, req); | |||
} | |||
} |