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

This commit is contained in:
chenbowen
2025-12-15 21:14:06 +08:00
parent 2323ee5c3b
commit 69d3dbc61f
4 changed files with 79 additions and 9 deletions

View File

@@ -0,0 +1,32 @@
package com.zt.plat.module.system.enums.dept;
import lombok.AllArgsConstructor;
import lombok.Getter;
/**
* 外部系统 / 平台枚举
* <p>
* 与字典类型 {@code system_dept_external_system} 对应,用于声明常用的平台标识,便于代码与前端字典对齐。
*/
@AllArgsConstructor
@Getter
public enum ExternalPlatformEnum {
ERP("ERP", "企业资源计划"),
IWORK("IWORK", "iWork 同步");
private final String code;
private final String label;
public static boolean isValid(String code) {
if (code == null) {
return false;
}
for (ExternalPlatformEnum item : values()) {
if (item.code.equalsIgnoreCase(code.trim())) {
return true;
}
}
return false;
}
}

View File

@@ -18,6 +18,8 @@ import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import java.util.Objects;
import java.util.List;
import static com.zt.plat.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -41,6 +43,7 @@ public class DeptExternalCodeServiceImpl implements DeptExternalCodeService {
@CacheEvict(cacheNames = RedisKeyConstants.DEPT_EXTERNAL_CODE_LIST, key = "#createReqVO.deptId", beforeInvocation = false)
public Long createDeptExternalCode(DeptExternalCodeSaveReqVO createReqVO) {
normalizeRequest(createReqVO);
disableActiveMappingIfConflict(createReqVO.getDeptId(), createReqVO.getSystemCode(), createReqVO.getExternalDeptCode());
validateForCreateOrUpdate(null, createReqVO.getDeptId(), createReqVO.getSystemCode(),
createReqVO.getExternalDeptCode());
@@ -56,6 +59,7 @@ public class DeptExternalCodeServiceImpl implements DeptExternalCodeService {
public void updateDeptExternalCode(DeptExternalCodeSaveReqVO updateReqVO) {
normalizeRequest(updateReqVO);
DeptExternalCodeDO exists = validateExists(updateReqVO.getId());
disableActiveMappingIfConflict(updateReqVO.getDeptId(), updateReqVO.getSystemCode(), updateReqVO.getExternalDeptCode());
validateForCreateOrUpdate(updateReqVO.getId(), updateReqVO.getDeptId(), updateReqVO.getSystemCode(),
updateReqVO.getExternalDeptCode());
@@ -118,6 +122,8 @@ public class DeptExternalCodeServiceImpl implements DeptExternalCodeService {
String normalizedExternalCode = externalDeptCode.trim();
String normalizedExternalName = StrUtil.blankToDefault(StrUtil.trimToNull(externalDeptName), null);
disableActiveMappingIfConflict(deptId, normalizedSystemCode, normalizedExternalCode);
// 如果存在则更新,否则创建
DeptExternalCodeDO exists = deptExternalCodeMapper.selectBySystemCodeAndDeptId(normalizedSystemCode, deptId);
if (exists != null) {
@@ -183,7 +189,11 @@ public class DeptExternalCodeServiceImpl implements DeptExternalCodeService {
DeptExternalCodeDO sameExternal = deptExternalCodeMapper
.selectBySystemCodeAndExternalCode(normalizedSystemCode, normalizedExternalCode);
if (sameExternal != null && (id == null || !sameExternal.getId().equals(id))) {
throw exception(DEPT_EXTERNAL_CODE_DUPLICATE, normalizedSystemCode, normalizedExternalCode);
boolean sameDept = Objects.equals(deptId, sameExternal.getDeptId());
boolean activeConflict = !sameDept && CommonStatusEnum.isEnable(sameExternal.getStatus());
if (activeConflict) {
throw exception(DEPT_EXTERNAL_CODE_DUPLICATE, normalizedSystemCode, normalizedExternalCode);
}
}
}
}
@@ -203,6 +213,28 @@ public class DeptExternalCodeServiceImpl implements DeptExternalCodeService {
}
}
private void disableActiveMappingIfConflict(Long targetDeptId, String systemCode, String externalDeptCode) {
String normalizedSystem = StrUtil.trimToNull(systemCode);
String normalizedExternal = StrUtil.trimToNull(externalDeptCode);
if (StrUtil.hasEmpty(normalizedSystem, normalizedExternal) || targetDeptId == null) {
return;
}
DeptExternalCodeDO existing = deptExternalCodeMapper.selectBySystemCodeAndExternalCode(normalizedSystem, normalizedExternal);
if (existing == null) {
return;
}
if (Objects.equals(existing.getDeptId(), targetDeptId)) {
return;
}
if (CommonStatusEnum.isEnable(existing.getStatus())) {
DeptExternalCodeDO update = new DeptExternalCodeDO();
update.setId(existing.getId());
update.setStatus(CommonStatusEnum.DISABLE.getStatus());
deptExternalCodeMapper.updateById(update);
evictCacheSafely(existing.getDeptId());
}
}
private void evictCacheSafely(Long deptId) {
if (deptId == null || cacheManager == null) {
return;

View File

@@ -122,12 +122,13 @@ public class DeptServiceImpl implements DeptService {
validateParentDept(updateReqVO.getId(), updateReqVO.getParentId());
// 校验部门名的唯一性
validateDeptNameUnique(updateReqVO.getId(), updateReqVO.getParentId(), updateReqVO.getName());
// 如果上级发生变化,需要重新生成编码并同步子级
Long newParentId = normalizeParentId(updateReqVO.getParentId());
Long oldParentId = normalizeParentId(originalDept.getParentId());
boolean parentChanged = !Objects.equals(newParentId, oldParentId);
String resolvedCode = originalDept.getCode();
if (parentChanged || StrUtil.isBlank(resolvedCode)) {
String existingCode = originalDept.getCode();
boolean needRegenerateCode = StrUtil.isBlank(existingCode);
String resolvedCode = existingCode;
if (needRegenerateCode) {
resolvedCode = generateDeptCode(newParentId);
validateDeptCodeUnique(updateReqVO.getId(), resolvedCode);
}
@@ -143,7 +144,7 @@ public class DeptServiceImpl implements DeptService {
databusChangeProducer.sendDeptUpdatedMessage(updatedDept);
}
if (parentChanged) {
if (needRegenerateCode) {
refreshChildCodesRecursively(updateObj.getId(), updateReqVO.getCode());
}

View File

@@ -18,6 +18,7 @@ import com.zt.plat.module.system.dal.mysql.dept.PostMapper;
import com.zt.plat.module.system.dal.mysql.user.AdminUserMapper;
import com.zt.plat.module.system.enums.common.SexEnum;
import com.zt.plat.module.system.enums.dept.DeptSourceEnum;
import com.zt.plat.module.system.enums.dept.ExternalPlatformEnum;
import com.zt.plat.module.system.enums.user.UserSourceEnum;
import com.zt.plat.module.system.service.dept.DeptService;
import com.zt.plat.module.system.service.dept.PostService;
@@ -399,14 +400,16 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
DeptSaveReqVO req = new DeptSaveReqVO();
req.setId(deptId);
req.setName(limitLength(StrUtil.blankToDefault(data.getSubcompanyname(), "未命名分部"), 30));
// req.setShortName(limitLength(data.getSubcompanyname(), 20));
req.setCode(trimToNull(data.getSubcompanycode()));
// req.setShortName(limitLength(data.getSubcompanyname(), 20));
req.setParentId(parentId == null ? DeptDO.PARENT_ID_ROOT : parentId);
req.setSort(defaultSort(data.getShoworder()));
req.setStatus(toStatus(canceled));
req.setIsCompany(Boolean.TRUE);
req.setIsGroup(Boolean.FALSE);
req.setDeptSource(DeptSourceEnum.IWORK.getSource());
req.setExternalSystemCode(ExternalPlatformEnum.IWORK.getCode());
req.setExternalDeptCode(StrUtil.blankToDefault(trimToNull(data.getSubcompanycode()), String.valueOf(data.getId())));
req.setExternalDeptName(data.getSubcompanyname());
return req;
}
@@ -417,14 +420,16 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
DeptSaveReqVO req = new DeptSaveReqVO();
req.setId(deptId);
req.setName(limitLength(StrUtil.blankToDefault(data.getDepartmentname(), "未命名部门"), 30));
// req.setShortName(limitLength(StrUtil.blankToDefault(data.getDepartmentmark(), data.getDepartmentname()), 20));
req.setCode(trimToNull(data.getDepartmentcode()));
// req.setShortName(limitLength(StrUtil.blankToDefault(data.getDepartmentmark(), data.getDepartmentname()), 20));
req.setParentId(parentId == null ? DeptDO.PARENT_ID_ROOT : parentId);
req.setSort(defaultSort(data.getShoworder()));
req.setStatus(toStatus(canceled));
req.setIsCompany(Boolean.FALSE);
req.setIsGroup(Boolean.FALSE);
req.setDeptSource(DeptSourceEnum.IWORK.getSource());
req.setExternalSystemCode(ExternalPlatformEnum.IWORK.getCode());
req.setExternalDeptCode(StrUtil.blankToDefault(trimToNull(data.getDepartmentcode()), String.valueOf(data.getId())));
req.setExternalDeptName(data.getDepartmentname());
return req;
}