1. 修复 databus 在多层嵌套的 json 报文,签名存在异常的 bug

This commit is contained in:
chenbowen
2025-12-17 14:39:23 +08:00
parent 12157d5dcb
commit d81413e239
10 changed files with 472 additions and 164 deletions

View File

@@ -3,23 +3,23 @@ package com.zt.plat.module.system.service.dept;
import com.zt.plat.framework.common.enums.CommonStatusEnum;
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.depexternalcode.DeptExternalCodeSaveReqVO;
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.dataobject.dept.DeptExternalCodeDO;
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 com.zt.plat.module.system.enums.dept.DeptSourceEnum;
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.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import java.util.Arrays;
import java.util.List;
@@ -69,7 +69,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
reqVO.setName(name);
reqVO.setSort(sort);
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setDeptSource(1);
reqVO.setDeptSource(DeptSourceEnum.SYNC.getSource());
reqVO.setIsCompany(false);
reqVO.setIsGroup(false);
return deptService.createDept(reqVO);
@@ -83,7 +83,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
o.setParentId(DeptDO.PARENT_ID_ROOT);
o.setStatus(randomCommonStatus());
o.setCode(null);
}).setDeptSource(1);
}).setDeptSource(DeptSourceEnum.SYNC.getSource());
// 调用
Long deptId = deptService.createDept(reqVO);
@@ -119,7 +119,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
topLevelReq.setName("总部");
topLevelReq.setSort(1);
topLevelReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
topLevelReq.setDeptSource(1);
topLevelReq.setDeptSource(DeptSourceEnum.SYNC.getSource());
Long topLevelId = deptService.createDept(topLevelReq);
DeptDO firstTop = deptMapper.selectById(topLevelId);
assertEquals("ZT001", firstTop.getCode());
@@ -129,12 +129,117 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
secondTopLevelReq.setName("总部");
secondTopLevelReq.setSort(2);
secondTopLevelReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
secondTopLevelReq.setDeptSource(1);
secondTopLevelReq.setDeptSource(DeptSourceEnum.SYNC.getSource());
Long secondTopId = deptService.createDept(secondTopLevelReq);
DeptDO secondTop = deptMapper.selectById(secondTopId);
assertEquals("ZT002", secondTop.getCode());
}
@Test
public void testCreateDept_externalUsesCuPrefixAndIndependentSequence() {
// 自建 EXTERNAL 顶级生成 CU001且不受 ZT 序列影响
DeptSaveReqVO externalTop = new DeptSaveReqVO();
externalTop.setParentId(DeptDO.PARENT_ID_ROOT);
externalTop.setName("自建总部");
externalTop.setSort(1);
externalTop.setStatus(CommonStatusEnum.ENABLE.getStatus());
externalTop.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
Long cuTopId = deptService.createDept(externalTop);
DeptDO cuTop = deptMapper.selectById(cuTopId);
assertEquals("CU001", cuTop.getCode());
// 同时创建同步来源(非 EXTERNAL仍使用 ZT 序列
DeptSaveReqVO syncTop = new DeptSaveReqVO();
syncTop.setParentId(DeptDO.PARENT_ID_ROOT);
syncTop.setName("同步总部");
syncTop.setSort(2);
syncTop.setStatus(CommonStatusEnum.ENABLE.getStatus());
syncTop.setDeptSource(DeptSourceEnum.SYNC.getSource());
Long ztTopId = deptService.createDept(syncTop);
DeptDO ztTop = deptMapper.selectById(ztTopId);
assertEquals("ZT001", ztTop.getCode());
// 再创建一个自建顶级,应独立递增为 CU002
DeptSaveReqVO externalTop2 = new DeptSaveReqVO();
externalTop2.setParentId(DeptDO.PARENT_ID_ROOT);
externalTop2.setName("自建二部");
externalTop2.setSort(3);
externalTop2.setStatus(CommonStatusEnum.ENABLE.getStatus());
externalTop2.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
Long cuTop2Id = deptService.createDept(externalTop2);
DeptDO cuTop2 = deptMapper.selectById(cuTop2Id);
assertEquals("CU002", cuTop2.getCode());
}
@Test
public void testCreateDept_externalChildFollowsCuPrefix() {
DeptSaveReqVO externalTop = new DeptSaveReqVO();
externalTop.setParentId(DeptDO.PARENT_ID_ROOT);
externalTop.setName("自建根");
externalTop.setSort(1);
externalTop.setStatus(CommonStatusEnum.ENABLE.getStatus());
externalTop.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
Long topId = deptService.createDept(externalTop);
DeptDO top = deptMapper.selectById(topId);
assertEquals("CU001", top.getCode());
DeptSaveReqVO childReq = new DeptSaveReqVO();
childReq.setParentId(topId);
childReq.setName("自建子");
childReq.setSort(1);
childReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
childReq.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
Long childId = deptService.createDept(childReq);
DeptDO child = deptMapper.selectById(childId);
assertEquals("CU001001", child.getCode());
}
@Test
public void testCreateDept_externalChildUnderSyncParentUsesCuPrefix() {
// 同步来源父级,使用 ZT 序列
DeptSaveReqVO syncTop = new DeptSaveReqVO();
syncTop.setParentId(DeptDO.PARENT_ID_ROOT);
syncTop.setName("同步父");
syncTop.setSort(1);
syncTop.setStatus(CommonStatusEnum.ENABLE.getStatus());
syncTop.setDeptSource(DeptSourceEnum.SYNC.getSource());
Long syncTopId = deptService.createDept(syncTop);
DeptDO syncTopDept = deptMapper.selectById(syncTopId);
assertEquals("ZT001", syncTopDept.getCode());
// 在同步父级下新增外部子部门,前缀替换为 CU序列与 ZT 独立
DeptSaveReqVO externalChild1 = new DeptSaveReqVO();
externalChild1.setParentId(syncTopId);
externalChild1.setName("外部子1");
externalChild1.setSort(1);
externalChild1.setStatus(CommonStatusEnum.ENABLE.getStatus());
externalChild1.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
Long child1Id = deptService.createDept(externalChild1);
DeptDO child1 = deptMapper.selectById(child1Id);
assertEquals("CU001001", child1.getCode());
DeptSaveReqVO externalChild2 = new DeptSaveReqVO();
externalChild2.setParentId(syncTopId);
externalChild2.setName("外部子2");
externalChild2.setSort(2);
externalChild2.setStatus(CommonStatusEnum.ENABLE.getStatus());
externalChild2.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
Long child2Id = deptService.createDept(externalChild2);
DeptDO child2 = deptMapper.selectById(child2Id);
assertEquals("CU001002", child2.getCode());
// 同步子部门仍使用 ZT 序列,不受 CU 序列影响
DeptSaveReqVO syncChild = new DeptSaveReqVO();
syncChild.setParentId(syncTopId);
syncChild.setName("同步子");
syncChild.setSort(3);
syncChild.setStatus(CommonStatusEnum.ENABLE.getStatus());
syncChild.setDeptSource(DeptSourceEnum.SYNC.getSource());
Long syncChildId = deptService.createDept(syncChild);
DeptDO syncChildDept = deptMapper.selectById(syncChildId);
assertEquals("ZT001001", syncChildDept.getCode());
}
@Test
public void testCreateDept_topLevelAutoCode_ignoreCustomInput() {
String customCode = "ROOT-001";
@@ -143,7 +248,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
topLevelReq.setName("集团");
topLevelReq.setSort(1);
topLevelReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
topLevelReq.setDeptSource(1);
topLevelReq.setDeptSource(DeptSourceEnum.SYNC.getSource());
topLevelReq.setCode(customCode);
Long deptId = deptService.createDept(topLevelReq);
@@ -166,7 +271,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
o.setParentId(DeptDO.PARENT_ID_ROOT);
o.setId(dbDeptDO.getId());
o.setStatus(randomCommonStatus());
}).setDeptSource(1);
}).setDeptSource(DeptSourceEnum.SYNC.getSource());
reqVO.setCode(dbDeptDO.getCode());
// 调用
@@ -195,7 +300,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
updateReq.setParentId(parentBId);
updateReq.setSort(1);
updateReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
updateReq.setDeptSource(1);
updateReq.setDeptSource(DeptSourceEnum.SYNC.getSource());
deptService.updateDept(updateReq);
DeptDO updatedChild = deptMapper.selectById(childId);
@@ -223,7 +328,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
updateReq1.setName("多系统部门");
updateReq1.setSort(1);
updateReq1.setStatus(CommonStatusEnum.ENABLE.getStatus());
updateReq1.setDeptSource(1);
updateReq1.setDeptSource(DeptSourceEnum.SYNC.getSource());
updateReq1.setExternalSystemCode("ERP");
updateReq1.setExternalDeptCode("ERP-100");
deptService.updateDept(updateReq1);
@@ -235,7 +340,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
updateReq2.setName("多系统部门");
updateReq2.setSort(1);
updateReq2.setStatus(CommonStatusEnum.ENABLE.getStatus());
updateReq2.setDeptSource(1);
updateReq2.setDeptSource(DeptSourceEnum.SYNC.getSource());
updateReq2.setExternalSystemCode("OA");
updateReq2.setExternalDeptCode("OA-100");
deptService.updateDept(updateReq2);
@@ -257,7 +362,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
createA.setName("iWork-A");
createA.setSort(1);
createA.setStatus(CommonStatusEnum.ENABLE.getStatus());
createA.setDeptSource(1);
createA.setDeptSource(DeptSourceEnum.SYNC.getSource());
createA.setExternalSystemCode("IWORK");
createA.setExternalDeptCode("IW-001");
Long deptAId = deptService.createDept(createA);
@@ -272,7 +377,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
createB.setName("iWork-B");
createB.setSort(2);
createB.setStatus(CommonStatusEnum.ENABLE.getStatus());
createB.setDeptSource(1);
createB.setDeptSource(DeptSourceEnum.SYNC.getSource());
createB.setExternalSystemCode("IWORK");
createB.setExternalDeptCode("IW-001");
Long deptBId = deptService.createDept(createB);
@@ -300,7 +405,7 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
updateReq.setName("子-更新");
updateReq.setSort(1);
updateReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
updateReq.setDeptSource(1);
updateReq.setDeptSource(DeptSourceEnum.SYNC.getSource());
updateReq.setExternalSystemCode("IWORK");
updateReq.setExternalDeptCode("IW-CHILD");
@@ -689,4 +794,48 @@ public class DeptServiceImplTest extends BaseDbUnitTest {
assertEquals("ZT001002", updatedChild2.getCode());
}
@Test
public void testCreateDept_delayCodeGeneration_thenGenerateWhenParentReady() {
Long missingParentId = 900L;
DeptSaveReqVO childReq = new DeptSaveReqVO();
childReq.setParentId(missingParentId);
childReq.setName("延迟子部门");
childReq.setSort(1);
childReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
childReq.setDeptSource(DeptSourceEnum.SYNC.getSource());
childReq.setDelayCodeGeneration(true);
Long childId = deptService.createDept(childReq);
DeptDO child = deptMapper.selectById(childId);
assertNotNull(childId);
assertNull(child.getCode());
// 后补父级并赋予编码
DeptDO parent = new DeptDO();
parent.setId(missingParentId);
parent.setParentId(DeptDO.PARENT_ID_ROOT);
parent.setName("后补父级");
parent.setCode("ZT900");
parent.setSort(1);
parent.setStatus(CommonStatusEnum.ENABLE.getStatus());
parent.setDeptSource(DeptSourceEnum.SYNC.getSource());
deptMapper.insert(parent);
// 触发子部门生成编码
DeptSaveReqVO updateReq = new DeptSaveReqVO();
updateReq.setId(childId);
updateReq.setParentId(missingParentId);
updateReq.setName("延迟子部门");
updateReq.setSort(1);
updateReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
updateReq.setDeptSource(DeptSourceEnum.SYNC.getSource());
updateReq.setDelayCodeGeneration(false);
deptService.updateDept(updateReq);
DeptDO updatedChild = deptMapper.selectById(childId);
assertEquals(parent.getCode() + "001", updatedChild.getCode());
}
}