diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/projectdeclared/contants/ProjectCodeContant.java b/pmapi/src/main/java/com/ningdatech/pmapi/projectdeclared/contants/ProjectCodeContant.java new file mode 100644 index 0000000..c1b870e --- /dev/null +++ b/pmapi/src/main/java/com/ningdatech/pmapi/projectdeclared/contants/ProjectCodeContant.java @@ -0,0 +1,16 @@ +package com.ningdatech.pmapi.projectdeclared.contants; + +/** + * @Classname ProjectCodeContant + * @Description + * @Date 2023/8/10 15:20 + * @Author PoffyZhang + */ +public interface ProjectCodeContant { + + String REGION_LAST_3 = "000"; + + String PROJECT_TYPE_PREFIX = "0"; + + String SHUZI_5 = "%05d"; +} diff --git a/pmapi/src/main/java/com/ningdatech/pmapi/projectdeclared/utils/GenerateProjectCodeUtil.java b/pmapi/src/main/java/com/ningdatech/pmapi/projectdeclared/utils/GenerateProjectCodeUtil.java index 93788b4..3b8ee35 100644 --- a/pmapi/src/main/java/com/ningdatech/pmapi/projectdeclared/utils/GenerateProjectCodeUtil.java +++ b/pmapi/src/main/java/com/ningdatech/pmapi/projectdeclared/utils/GenerateProjectCodeUtil.java @@ -1,13 +1,9 @@ package com.ningdatech.pmapi.projectdeclared.utils; -import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.core.toolkit.Wrappers; -import com.ningdatech.basic.exception.BizException; -import com.ningdatech.basic.util.StrPool; -import com.ningdatech.pmapi.fiscal.entity.CompanyFiscalCode; +import com.ningdatech.pmapi.common.constant.BizConst; import com.ningdatech.pmapi.fiscal.service.ICompanyFiscalCodeService; -import com.ningdatech.pmapi.projectdeclared.contants.DeclaredProjectContant; -import com.ningdatech.pmapi.projectdeclared.model.dto.DefaultDeclaredDTO; +import com.ningdatech.pmapi.projectdeclared.contants.ProjectCodeContant; import com.ningdatech.pmapi.projectlib.model.dto.ProjectDTO; import com.ningdatech.pmapi.projectlib.model.entity.Project; import com.ningdatech.pmapi.projectlib.model.entity.ProjectCollection; @@ -15,8 +11,7 @@ import com.ningdatech.pmapi.projectlib.service.IProjectCollectionService; import com.ningdatech.pmapi.projectlib.service.IProjectService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; - -import java.util.List; +import java.time.LocalDateTime; import java.util.Objects; /** @@ -37,68 +32,40 @@ public class GenerateProjectCodeUtil { private IProjectCollectionService projectCollectionService; public String generateProjectCode(ProjectDTO project){ - // 获取所属行政区划代码(6位) - String areaCode = project.getAreaCode(); - // 获取建设年度后两位 - String year = String.valueOf(project.getProjectYear() % DeclaredProjectContant.Project.YEAR_DRAW_SURPLUS); - // 9-12位固定 - String fixedNumber = DeclaredProjectContant.Project.FIXED_NUMBER; - // 获取公司的财政编码 - CompanyFiscalCode companyFiscalCode = companyFiscalCodeService.getByOrganizationCode(project.getBuildOrgCode()); - - // 从表中查出单位配置的财政编码 - if (Objects.isNull(companyFiscalCode)){ - throw new BizException("申报单位未配置财政编码!"); - } - - String fiscalCode = companyFiscalCode.getFiscalCode(); - // 获取项目编号 - String projectIdStr; - // 获取项目库当前最大项目序号 - List projectList = projectService.list(Wrappers.lambdaQuery(Project.class).orderByDesc(Project::getId)); - Project maxProject = CollUtil.isEmpty(projectList) ? null : projectList.get(0); - Long projectId = Objects.isNull(maxProject) ? DeclaredProjectContant.Project.MIN_PROJECT_ID : maxProject.getId() + 1; - if (projectId > DeclaredProjectContant.Project.MAX_PROJECT_ID){ - // 超过999的项目号从1开始 1 - Long newProjectId = projectId % DeclaredProjectContant.Project.MAX_PROJECT_ID; - projectIdStr = String.format(StrPool.FORMAT_NUMBER_THREE, newProjectId); - }else { - projectIdStr = String.format(StrPool.FORMAT_NUMBER_THREE, projectId); - } - // 生成21位的项目编号 - return areaCode + year + fixedNumber + fiscalCode + projectIdStr; + // 获取所属行政区划代码(9位) + String areaCode = project.getAreaCode() + ProjectCodeContant.REGION_LAST_3; + // 获取建设年度 (10-13) + String year = Objects.nonNull(project.getProjectYear()) ? String.valueOf(project.getProjectYear()) + : String.valueOf(LocalDateTime.now().getYear()); + // 14-15 项目类型 + String projectType = ProjectCodeContant.PROJECT_TYPE_PREFIX + project.getProjectType(); + // 16-20 项目序号00001 + Project max = projectService.getOne(Wrappers.lambdaQuery(Project.class) + .select(Project::getId) + .orderByDesc(Project::getId) + .last(BizConst.LIMIT_1)); + Long maxId = Objects.nonNull(max) ? max.getId() : 0; + String serialNumber = String.format(ProjectCodeContant.SHUZI_5,maxId + 1); + // 生成20位的项目编号 + return areaCode + year + projectType + serialNumber; } public String generateProjectCode(ProjectCollection project){ - // 获取所属行政区划代码(6位) - String areaCode = project.getAreaCode(); - // 获取建设年度后两位 - String year = String.valueOf(project.getProjectYear() % DeclaredProjectContant.Project.YEAR_DRAW_SURPLUS); - // 9-12位固定 - String fixedNumber = DeclaredProjectContant.Project.FIXED_NUMBER; - // 获取公司的财政编码 - CompanyFiscalCode companyFiscalCode = companyFiscalCodeService.getByOrganizationCode(project.getBuildOrgCode()); - - // 从表中查出单位配置的财政编码 - if (Objects.isNull(companyFiscalCode)){ - throw new BizException("申报单位未配置财政编码!"); - } - - String fiscalCode = companyFiscalCode.getFiscalCode(); - // 获取项目编号 - String projectIdStr; - // 获取项目库当前最大项目序号 - List projectList = projectCollectionService.list(Wrappers.lambdaQuery(ProjectCollection.class).orderByDesc(ProjectCollection::getId)); - ProjectCollection maxProject = CollUtil.isEmpty(projectList) ? null : projectList.get(0); - Long projectId = Objects.isNull(maxProject) ? DeclaredProjectContant.Project.MIN_PROJECT_ID : maxProject.getId() + 1; - if (projectId > DeclaredProjectContant.Project.MAX_PROJECT_ID){ - // 超过999的项目号从1开始 1 - Long newProjectId = projectId % DeclaredProjectContant.Project.MAX_PROJECT_ID; - projectIdStr = String.format(StrPool.FORMAT_NUMBER_THREE, newProjectId); - }else { - projectIdStr = String.format(StrPool.FORMAT_NUMBER_THREE, projectId); - } - // 生成21位的项目编号 - return areaCode + year + fixedNumber + fiscalCode + projectIdStr; + // 获取所属行政区划代码(9位) + String areaCode = project.getAreaCode() + ProjectCodeContant.REGION_LAST_3; + // 获取建设年度 (10-13) + String year = Objects.nonNull(project.getProjectYear()) ? String.valueOf(project.getProjectYear()) + : String.valueOf(LocalDateTime.now().getYear()); + // 14-15 项目类型 + String projectType = ProjectCodeContant.PROJECT_TYPE_PREFIX + project.getProjectType(); + // 16-20 项目序号00001 + Project max = projectService.getOne(Wrappers.lambdaQuery(Project.class) + .select(Project::getId) + .orderByDesc(Project::getId) + .last(BizConst.LIMIT_1)); + Long maxId = Objects.nonNull(max) ? max.getId() : 0; + String serialNumber = String.format(ProjectCodeContant.SHUZI_5,maxId + 1); + // 生成20位的项目编号 + return areaCode + year + projectType + serialNumber; } }