1. 新增生成自有组织 CODE,同步其他系统额外生成编码映射关系

This commit is contained in:
chenbowen
2025-12-15 19:25:19 +08:00
parent 8d25f4224b
commit 2323ee5c3b
13 changed files with 442 additions and 54 deletions

View File

@@ -0,0 +1,105 @@
package com.zt.plat.module.system.service.dept;
import com.zt.plat.framework.common.enums.CommonStatusEnum;
import com.zt.plat.framework.test.core.ut.BaseDbUnitTest;
import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.DeptExternalCodeSaveReqVO;
import com.zt.plat.module.system.dal.dataobject.dept.DeptDO;
import com.zt.plat.module.system.dal.mysql.dept.DeptExternalCodeMapper;
import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
import com.zt.plat.module.system.dal.redis.RedisKeyConstants;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import java.util.List;
import static com.zt.plat.module.system.dal.redis.RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST;
import static org.junit.jupiter.api.Assertions.*;
@Import({DeptExternalCodeServiceImpl.class, DeptExternalCodeServiceImplTest.CacheConfig.class})
class DeptExternalCodeServiceImplTest extends BaseDbUnitTest {
@Resource
private DeptExternalCodeServiceImpl deptExternalCodeService;
@Resource
private DeptExternalCodeMapper deptExternalCodeMapper;
@Resource
private CacheManager cacheManager;
@Resource
private DeptMapper deptMapper;
@TestConfiguration
@EnableCaching
static class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager(RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST);
}
}
@Test
void testCacheEvictOnCreateAndUpdate() {
Long deptId = 100L;
DeptDO dept = new DeptDO();
dept.setId(deptId);
dept.setName("总部");
dept.setCode("ZT001");
dept.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(dept);
// 预热缓存(空结果)
List<?> firstCall = deptExternalCodeService.getDeptExternalCodeListByDeptId(deptId);
assertTrue(firstCall.isEmpty());
assertNotNull(cacheManager.getCache(DEPT_EXTERNAL_CODE_LIST).get(deptId));
// 创建映射应触发缓存失效
DeptExternalCodeSaveReqVO createReq = new DeptExternalCodeSaveReqVO();
createReq.setDeptId(deptId);
createReq.setSystemCode("ERP");
createReq.setExternalDeptCode("ERP-001");
deptExternalCodeService.createDeptExternalCode(createReq);
List<?> refreshed = deptExternalCodeService.getDeptExternalCodeListByDeptId(deptId);
assertEquals(1, refreshed.size());
assertNotNull(cacheManager.getCache(DEPT_EXTERNAL_CODE_LIST).get(deptId));
// 更新映射也会清理缓存
DeptExternalCodeSaveReqVO updateReq = new DeptExternalCodeSaveReqVO();
Long id = deptExternalCodeMapper.selectListByDeptId(deptId).get(0).getId();
updateReq.setId(id);
updateReq.setDeptId(deptId);
updateReq.setSystemCode("ERP");
updateReq.setExternalDeptCode("ERP-002");
updateReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptExternalCodeService.updateDeptExternalCode(updateReq);
List<?> refreshedAfterUpdate = deptExternalCodeService.getDeptExternalCodeListByDeptId(deptId);
assertEquals(1, refreshedAfterUpdate.size());
assertEquals("ERP-002", deptExternalCodeMapper.selectById(id).getExternalDeptCode());
}
@Test
void testSaveOrUpdateDeptExternalCodeUpsert() {
Long deptId = 101L;
DeptDO dept = new DeptDO();
dept.setId(deptId);
dept.setName("事业部");
dept.setCode("ZT002");
dept.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(dept);
Long firstId = deptExternalCodeService.saveOrUpdateDeptExternalCode(deptId, "OA", "OA-001", "OA-总部", null);
assertNotNull(firstId);
// upsert: 同一 system + dept 更新编码
Long secondId = deptExternalCodeService.saveOrUpdateDeptExternalCode(deptId, "OA", "OA-002", "OA-新编码", CommonStatusEnum.ENABLE.getStatus());
assertEquals(firstId, secondId);
assertEquals("OA-002", deptExternalCodeMapper.selectById(firstId).getExternalDeptCode());
}
}

View File

@@ -5,11 +5,20 @@ import com.zt.plat.framework.common.util.object.ObjectUtils;
import com.zt.plat.framework.test.core.ut.BaseDbUnitTest;
import com.zt.plat.module.system.controller.admin.dept.vo.dept.DeptListReqVO;
import com.zt.plat.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO;
import com.zt.plat.module.system.controller.admin.dept.vo.depexternalcode.DeptExternalCodeSaveReqVO;
import com.zt.plat.module.system.dal.dataobject.dept.DeptDO;
import com.zt.plat.module.system.dal.mysql.dept.DeptExternalCodeMapper;
import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
import com.zt.plat.module.system.service.dept.DeptExternalCodeServiceImpl;
import com.zt.plat.module.system.dal.redis.RedisKeyConstants;
import jakarta.annotation.Resource;
import org.junit.jupiter.api.Test;
import org.springframework.context.annotation.Import;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
import java.util.Arrays;
import java.util.List;
@@ -27,13 +36,31 @@ import static org.junit.jupiter.api.Assertions.*;
*
* @author niudehua
*/
@Import(DeptServiceImpl.class)
@Import({DeptServiceImpl.class, DeptExternalCodeServiceImpl.class, DeptServiceImplTest.CacheConfig.class})
public class DeptServiceImplTest extends BaseDbUnitTest {
@Resource
private DeptServiceImpl deptService;
@Resource
private DeptMapper deptMapper;
@Resource
private DeptExternalCodeServiceImpl deptExternalCodeService;
@Resource
private DeptExternalCodeMapper deptExternalCodeMapper;
@Resource
private CacheManager cacheManager;
@TestConfiguration
@EnableCaching
static class CacheConfig {
@Bean
public CacheManager cacheManager() {
return new ConcurrentMapCacheManager(
RedisKeyConstants.DEPT_CHILDREN_ID_LIST,
RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST
);
}
}
private Long createDept(Long parentId, String name, int sort) {
DeptSaveReqVO reqVO = new DeptSaveReqVO();
@@ -108,7 +135,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
}
@Test
public void testCreateDept_topLevelRespectCustomCode() {
public void testCreateDept_topLevelAutoCode_ignoreCustomInput() {
String customCode = "ROOT-001";
DeptSaveReqVO topLevelReq = new DeptSaveReqVO();
topLevelReq.setParentId(DeptDO.PARENT_ID_ROOT);
@@ -120,7 +147,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
Long deptId = deptService.createDept(topLevelReq);
DeptDO created = deptMapper.selectById(deptId);
assertEquals(customCode, created.getCode());
assertEquals("ZT001", created.getCode());
}
@Test
@@ -187,6 +214,29 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
assertNull(deptMapper.selectById(id));
}
@Test
public void testDeleteDept_cascadeExternalCodesAndEvictCache() {
Long deptId = createDept(DeptDO.PARENT_ID_ROOT, "总部", 1);
// 创建映射并预热缓存
DeptExternalCodeSaveReqVO createReq = new DeptExternalCodeSaveReqVO();
createReq.setDeptId(deptId);
createReq.setSystemCode("ERP");
createReq.setExternalDeptCode("ERP-001");
deptExternalCodeService.createDeptExternalCode(createReq);
deptExternalCodeService.getDeptExternalCodeListByDeptId(deptId);
assertNotNull(cacheManager.getCache(com.zt.plat.module.system.dal.redis.RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST)
.get(deptId));
// 删除部门
deptService.deleteDept(deptId);
// 校验映射被删除且缓存被清理
assertTrue(deptExternalCodeMapper.selectListByDeptId(deptId).isEmpty());
assertNull(cacheManager.getCache(com.zt.plat.module.system.dal.redis.RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST)
.get(deptId));
}
@Test
public void testDeleteDept_exitsChildren() {
// mock 数据

View File

@@ -1,4 +1,5 @@
DELETE FROM "system_dept";
DELETE FROM "system_dept_external_code";
DELETE FROM "system_dict_data";
DELETE FROM "system_role";
DELETE FROM "system_role_menu";

View File

@@ -34,6 +34,27 @@ CREATE TABLE IF NOT EXISTS "system_dept" (
PRIMARY KEY ("id")
) COMMENT '部门表';
CREATE TABLE IF NOT EXISTS "system_dept_external_code" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"dept_id" bigint NOT NULL,
"system_code" varchar(64) NOT NULL,
"external_dept_code" varchar(128) NOT NULL,
"external_dept_name" varchar(255),
"status" tinyint DEFAULT 0 NOT NULL,
"remark" varchar(512),
"tenant_id" bigint DEFAULT 0,
"creator" varchar(64),
"create_time" timestamp DEFAULT CURRENT_TIMESTAMP,
"updater" varchar(64),
"update_time" timestamp DEFAULT CURRENT_TIMESTAMP,
"deleted" tinyint DEFAULT 0 NOT NULL,
PRIMARY KEY ("id")
) COMMENT '部门外部组织编码映射';
CREATE UNIQUE INDEX IF NOT EXISTS "uk_system_dept_external_code_ext" ON "system_dept_external_code" ("tenant_id", "system_code", "external_dept_code");
CREATE UNIQUE INDEX IF NOT EXISTS "uk_system_dept_external_code_dept" ON "system_dept_external_code" ("tenant_id", "system_code", "dept_id");
CREATE INDEX IF NOT EXISTS "idx_system_dept_external_code_dept" ON "system_dept_external_code" ("tenant_id", "dept_id");
CREATE TABLE IF NOT EXISTS "system_dict_data" (
"id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY,
"sort" int NOT NULL DEFAULT '0',