1. 修复 databus 在多层嵌套的 json 报文,签名存在异常的 bug
This commit is contained in:
@@ -76,4 +76,7 @@ public class DeptSaveReqVO {
|
||||
@Schema(description = "部门来源类型", example = "1")
|
||||
private Integer deptSource;
|
||||
|
||||
@Schema(description = "内部使用:延迟生成部门编码", hidden = true)
|
||||
private Boolean delayCodeGeneration;
|
||||
|
||||
}
|
||||
|
||||
@@ -94,8 +94,9 @@ public class UserController {
|
||||
|
||||
@GetMapping({"/list-all-simple", "/simple-list"})
|
||||
@Operation(summary = "获取用户精简信息列表", description = "只包含被开启的用户,主要用于前端的下拉选项")
|
||||
public CommonResult<List<UserSimpleRespVO>> getSimpleUserList() {
|
||||
List<AdminUserDO> list = userService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus(), SIMPLE_LIST_LIMIT);
|
||||
public CommonResult<List<UserSimpleRespVO>> getSimpleUserList(
|
||||
@RequestParam(value = "keyword", required = false) String keyword) {
|
||||
List<AdminUserDO> list = userService.getUserListByStatus(CommonStatusEnum.ENABLE.getStatus(), SIMPLE_LIST_LIMIT, keyword);
|
||||
return success(UserConvert.INSTANCE.convertSimpleList(list));
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,9 @@ public class UserPageReqVO extends PageParam {
|
||||
@Schema(description = "用户账号,模糊匹配", example = "zt")
|
||||
private String username;
|
||||
|
||||
@Schema(description = "用户昵称,模糊匹配", example = "张三")
|
||||
private String nickname;
|
||||
|
||||
@Schema(description = "工号,模糊匹配", example = "A00123")
|
||||
private String workcode;
|
||||
|
||||
|
||||
@@ -114,12 +114,15 @@ public interface DeptMapper extends BaseMapperX<DeptDO> {
|
||||
* @param parentId 父部门ID
|
||||
* @return 编码最大的子部门
|
||||
*/
|
||||
default DeptDO selectLastChildByCode(Long parentId) {
|
||||
return selectOne(new LambdaQueryWrapper<DeptDO>()
|
||||
default DeptDO selectLastChildByCode(Long parentId, String prefix) {
|
||||
LambdaQueryWrapper<DeptDO> wrapper = new LambdaQueryWrapper<DeptDO>()
|
||||
.eq(DeptDO::getParentId, parentId)
|
||||
.isNotNull(DeptDO::getCode)
|
||||
.orderByDesc(DeptDO::getCode)
|
||||
.last("LIMIT 1"));
|
||||
.isNotNull(DeptDO::getCode);
|
||||
if (StrUtil.isNotBlank(prefix)) {
|
||||
wrapper.likeRight(DeptDO::getCode, prefix);
|
||||
}
|
||||
wrapper.orderByDesc(DeptDO::getCode).last("LIMIT 1");
|
||||
return selectOne(wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -40,6 +40,7 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {
|
||||
MPJLambdaWrapperX<AdminUserDO> query = new MPJLambdaWrapperX<>();
|
||||
query.leftJoin(UserDeptDO.class, UserDeptDO::getUserId, AdminUserDO::getId);
|
||||
query.likeIfPresent(AdminUserDO::getUsername, reqVO.getUsername());
|
||||
query.likeIfPresent(AdminUserDO::getNickname, reqVO.getNickname());
|
||||
query.likeIfPresent(AdminUserDO::getWorkcode, reqVO.getWorkcode());
|
||||
query.likeIfPresent(AdminUserDO::getMobile, reqVO.getMobile());
|
||||
query.eqIfPresent(AdminUserDO::getStatus, reqVO.getStatus());
|
||||
@@ -70,9 +71,16 @@ public interface AdminUserMapper extends BaseMapperX<AdminUserDO> {
|
||||
return selectList(new LambdaQueryWrapperX<AdminUserDO>().like(AdminUserDO::getNickname, nickname));
|
||||
}
|
||||
|
||||
default List<AdminUserDO> selectListByStatus(Integer status, Integer limit) {
|
||||
default List<AdminUserDO> selectListByStatus(Integer status, Integer limit, String keyword) {
|
||||
LambdaQueryWrapperX<AdminUserDO> query = new LambdaQueryWrapperX<AdminUserDO>()
|
||||
.eq(AdminUserDO::getStatus, status);
|
||||
if (StrUtil.isNotBlank(keyword)) {
|
||||
String trimmed = keyword.trim();
|
||||
query.and(w -> w.like(AdminUserDO::getNickname, trimmed)
|
||||
.or().like(AdminUserDO::getUsername, trimmed)
|
||||
.or().like(AdminUserDO::getMobile, trimmed)
|
||||
.or().like(AdminUserDO::getWorkcode, trimmed));
|
||||
}
|
||||
if (limit != null && limit > 0) {
|
||||
query.last("LIMIT " + limit);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
import com.zt.plat.framework.common.biz.system.permission.dto.DeptDataPermissionRespDTO;
|
||||
import com.zt.plat.framework.common.enums.CommonStatusEnum;
|
||||
import com.zt.plat.framework.common.pojo.CompanyDeptInfo;
|
||||
import com.zt.plat.framework.common.util.object.BeanUtils;
|
||||
@@ -11,18 +12,13 @@ import com.zt.plat.framework.datapermission.core.annotation.DataPermission;
|
||||
import com.zt.plat.framework.tenant.core.aop.TenantIgnore;
|
||||
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.dict.vo.data.DictDataSaveReqVO;
|
||||
import com.zt.plat.module.system.controller.admin.dict.vo.type.DictTypeSaveReqVO;
|
||||
import com.zt.plat.module.system.dal.dataobject.dept.DeptDO;
|
||||
import com.zt.plat.module.system.dal.dataobject.dict.DictTypeDO;
|
||||
import com.zt.plat.module.system.dal.dataobject.userdept.UserDeptDO;
|
||||
import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
|
||||
import com.zt.plat.module.system.dal.mysql.userdept.UserDeptMapper;
|
||||
import com.zt.plat.module.system.dal.redis.RedisKeyConstants;
|
||||
import com.zt.plat.module.system.enums.dept.DeptSourceEnum;
|
||||
import com.zt.plat.module.system.enums.DictTypeConstants;
|
||||
import com.zt.plat.module.system.service.dict.DictDataService;
|
||||
import com.zt.plat.module.system.service.dict.DictTypeService;
|
||||
import com.zt.plat.module.system.service.permission.PermissionService;
|
||||
import org.apache.seata.spring.annotation.GlobalTransactional;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -57,17 +53,15 @@ public class DeptServiceImpl implements DeptService {
|
||||
@Resource
|
||||
private UserDeptMapper userDeptMapper;
|
||||
@Resource
|
||||
private PermissionService permissionService;
|
||||
@Resource
|
||||
private com.zt.plat.module.system.mq.producer.databus.DatabusChangeProducer databusChangeProducer;
|
||||
@Resource
|
||||
private DeptExternalCodeService deptExternalCodeService;
|
||||
@Resource
|
||||
private DictTypeService dictTypeService;
|
||||
@Resource
|
||||
private DictDataService dictDataService;
|
||||
|
||||
private static final String ROOT_CODE_PREFIX = "ZT";
|
||||
private static final String EXTERNAL_CODE_PREFIX = "CU";
|
||||
private static final int CODE_SEGMENT_LENGTH = 3;
|
||||
private static final int MAX_SEQUENCE = 999;
|
||||
private static final int BATCH_SIZE = 1000;
|
||||
private static final Comparator<DeptDO> DEPT_COMPARATOR = Comparator
|
||||
.comparing(DeptDO::getSort, Comparator.nullsLast(Comparator.naturalOrder()))
|
||||
.thenComparing(DeptDO::getId, Comparator.nullsLast(Comparator.naturalOrder()));
|
||||
@@ -82,27 +76,38 @@ public class DeptServiceImpl implements DeptService {
|
||||
createReqVO.setParentId(normalizeParentId(createReqVO.getParentId()));
|
||||
// 创建时默认有效
|
||||
createReqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
// 默认部门来源:未指定时视为外部部门
|
||||
if (createReqVO.getDeptSource() == null) {
|
||||
createReqVO.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
|
||||
}
|
||||
// 校验父部门的有效性
|
||||
validateParentDept(null, createReqVO.getParentId());
|
||||
// 校验部门名的唯一性
|
||||
validateDeptNameUnique(null, createReqVO.getParentId(), createReqVO.getName());
|
||||
// 生成并校验部门编码
|
||||
Long effectiveParentId = normalizeParentId(createReqVO.getParentId());
|
||||
String resolvedCode = generateDeptCode(effectiveParentId);
|
||||
validateDeptCodeUnique(null, resolvedCode);
|
||||
createReqVO.setCode(resolvedCode);
|
||||
boolean isIWorkSource = Objects.equals(createReqVO.getDeptSource(), DeptSourceEnum.IWORK.getSource());
|
||||
if (isIWorkSource) {
|
||||
// iWork 来源直接使用提供的编码,不再生成
|
||||
String providedCode = StrUtil.blankToDefault(createReqVO.getCode(), null);
|
||||
createReqVO.setCode(providedCode);
|
||||
} else {
|
||||
if (Boolean.TRUE.equals(createReqVO.getDelayCodeGeneration())) {
|
||||
createReqVO.setCode(null);
|
||||
} else {
|
||||
String resolvedCode = generateDeptCode(createReqVO.getParentId(), createReqVO.getDeptSource());
|
||||
validateDeptCodeUnique(null, resolvedCode);
|
||||
createReqVO.setCode(resolvedCode);
|
||||
}
|
||||
}
|
||||
|
||||
// 插入部门
|
||||
DeptDO dept = BeanUtils.toBean(createReqVO, DeptDO.class);
|
||||
// 设置部门来源:如果未指定,默认为外部部门
|
||||
// 设置部门来源(前置已默认化,此处兜底)
|
||||
if (dept.getDeptSource() == null) {
|
||||
dept.setDeptSource(DeptSourceEnum.EXTERNAL.getSource());
|
||||
}
|
||||
deptMapper.insert(dept);
|
||||
|
||||
// 维护外部系统编码映射(若有传入)
|
||||
upsertExternalCodeMapping(createReqVO, dept.getId());
|
||||
|
||||
// 发布部门创建事件
|
||||
databusChangeProducer.sendDeptCreatedMessage(dept);
|
||||
|
||||
@@ -122,17 +127,29 @@ 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 existingCode = originalDept.getCode();
|
||||
boolean needRegenerateCode = StrUtil.isBlank(existingCode);
|
||||
String resolvedCode = existingCode;
|
||||
if (needRegenerateCode) {
|
||||
resolvedCode = generateDeptCode(newParentId);
|
||||
validateDeptCodeUnique(updateReqVO.getId(), resolvedCode);
|
||||
boolean isIWorkSource = Objects.equals(originalDept.getDeptSource(), DeptSourceEnum.IWORK.getSource());
|
||||
if (isIWorkSource) {
|
||||
// iWork 来源直接使用提供的编码,不再生成
|
||||
String providedCode = StrUtil.blankToDefault(updateReqVO.getCode(), originalDept.getCode());
|
||||
updateReqVO.setCode(providedCode);
|
||||
} else {
|
||||
Integer source = ObjectUtil.defaultIfNull(updateReqVO.getDeptSource(), originalDept.getDeptSource());
|
||||
if (source == null) {
|
||||
source = DeptSourceEnum.EXTERNAL.getSource();
|
||||
}
|
||||
String existingCode = originalDept.getCode();
|
||||
if (StrUtil.isBlank(existingCode)) {
|
||||
if (Boolean.TRUE.equals(updateReqVO.getDelayCodeGeneration())) {
|
||||
updateReqVO.setCode(null);
|
||||
} else {
|
||||
String newCode = generateDeptCode(updateReqVO.getParentId(), source);
|
||||
validateDeptCodeUnique(updateReqVO.getId(), newCode);
|
||||
updateReqVO.setCode(newCode);
|
||||
}
|
||||
} else {
|
||||
updateReqVO.setCode(existingCode);
|
||||
}
|
||||
}
|
||||
updateReqVO.setCode(resolvedCode);
|
||||
|
||||
// 更新部门
|
||||
DeptDO updateObj = BeanUtils.toBean(updateReqVO, DeptDO.class);
|
||||
@@ -144,12 +161,6 @@ public class DeptServiceImpl implements DeptService {
|
||||
databusChangeProducer.sendDeptUpdatedMessage(updatedDept);
|
||||
}
|
||||
|
||||
if (needRegenerateCode) {
|
||||
refreshChildCodesRecursively(updateObj.getId(), updateReqVO.getCode());
|
||||
}
|
||||
|
||||
// 维护外部系统编码映射(若有传入)
|
||||
upsertExternalCodeMapping(updateReqVO, updateReqVO.getId());
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -167,9 +178,6 @@ public class DeptServiceImpl implements DeptService {
|
||||
DeptDO dept = deptMapper.selectById(id);
|
||||
Long tenantId = (dept != null) ? dept.getTenantId() : null;
|
||||
|
||||
// 级联删除外部编码映射并清理缓存
|
||||
deptExternalCodeService.deleteDeptExternalCodesByDeptId(id);
|
||||
|
||||
// 删除部门
|
||||
deptMapper.deleteById(id);
|
||||
|
||||
@@ -268,26 +276,16 @@ public class DeptServiceImpl implements DeptService {
|
||||
}
|
||||
}
|
||||
|
||||
private String generateDeptCode(Long parentId) {
|
||||
private String generateDeptCode(Long parentId, Integer deptSource) {
|
||||
Long effectiveParentId = normalizeParentId(parentId);
|
||||
Long codeParentId = effectiveParentId;
|
||||
String prefix = ROOT_CODE_PREFIX;
|
||||
if (!DeptDO.PARENT_ID_ROOT.equals(effectiveParentId)) {
|
||||
DeptDO parentDept = deptMapper.selectById(effectiveParentId);
|
||||
if (parentDept == null || StrUtil.isBlank(parentDept.getCode())) {
|
||||
codeParentId = DeptDO.PARENT_ID_ROOT;
|
||||
} else {
|
||||
prefix = parentDept.getCode();
|
||||
}
|
||||
}
|
||||
|
||||
int nextSequence = determineNextSequence(codeParentId, prefix);
|
||||
String prefix = resolveCodePrefix(effectiveParentId, deptSource);
|
||||
int nextSequence = determineNextSequence(effectiveParentId, prefix);
|
||||
assertSequenceRange(nextSequence);
|
||||
return prefix + formatSequence(nextSequence);
|
||||
}
|
||||
|
||||
private int determineNextSequence(Long parentId, String prefix) {
|
||||
DeptDO lastChild = deptMapper.selectLastChildByCode(parentId);
|
||||
DeptDO lastChild = deptMapper.selectLastChildByCode(parentId, prefix);
|
||||
Integer sequence = parseSequence(lastChild != null ? lastChild.getCode() : null, prefix);
|
||||
if (sequence != null) {
|
||||
return sequence + 1;
|
||||
@@ -365,12 +363,36 @@ public class DeptServiceImpl implements DeptService {
|
||||
candidate = candidate.trim();
|
||||
}
|
||||
if (StrUtil.isBlank(candidate)) {
|
||||
candidate = generateDeptCode(DeptDO.PARENT_ID_ROOT);
|
||||
candidate = generateDeptCode(DeptDO.PARENT_ID_ROOT, DeptSourceEnum.EXTERNAL.getSource());
|
||||
}
|
||||
validateDeptCodeUnique(currentDeptId, candidate);
|
||||
return candidate;
|
||||
}
|
||||
|
||||
private String resolveCodePrefix(Long parentId, Integer deptSource) {
|
||||
boolean isExternal = Objects.equals(deptSource, DeptSourceEnum.EXTERNAL.getSource());
|
||||
if (DeptDO.PARENT_ID_ROOT.equals(parentId)) {
|
||||
return isExternal ? EXTERNAL_CODE_PREFIX : ROOT_CODE_PREFIX;
|
||||
}
|
||||
|
||||
DeptDO parentDept = deptMapper.selectById(parentId);
|
||||
if (parentDept == null || StrUtil.isBlank(parentDept.getCode())) {
|
||||
return isExternal ? EXTERNAL_CODE_PREFIX : ROOT_CODE_PREFIX;
|
||||
}
|
||||
|
||||
String parentCode = parentDept.getCode();
|
||||
if (isExternal) {
|
||||
if (parentCode.startsWith(EXTERNAL_CODE_PREFIX)) {
|
||||
return parentCode;
|
||||
}
|
||||
if (parentCode.startsWith(ROOT_CODE_PREFIX)) {
|
||||
return EXTERNAL_CODE_PREFIX + parentCode.substring(ROOT_CODE_PREFIX.length());
|
||||
}
|
||||
return EXTERNAL_CODE_PREFIX;
|
||||
}
|
||||
return parentCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DeptDO getDept(Long id) {
|
||||
return deptMapper.selectById(id);
|
||||
@@ -558,37 +580,59 @@ public class DeptServiceImpl implements DeptService {
|
||||
|
||||
@Override
|
||||
public List<DeptDO> getTopLevelDeptList() {
|
||||
// 获取当前用户所属的部门列表
|
||||
Set<Long> deptIds = userDeptMapper.selectValidListByUserIds(singleton(getLoginUserId()))
|
||||
.stream()
|
||||
.map(UserDeptDO::getDeptId)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
Long loginUserId = getLoginUserId();
|
||||
|
||||
// 当前用户所属部门
|
||||
Set<Long> userDeptIds = Optional.ofNullable(userDeptMapper.selectValidListByUserIds(singleton(loginUserId)))
|
||||
.orElseGet(Collections::emptyList)
|
||||
.stream()
|
||||
.map(UserDeptDO::getDeptId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 数据权限部门
|
||||
DeptDataPermissionRespDTO dataPerm = permissionService.getDeptDataPermission(loginUserId);
|
||||
Set<Long> permDeptIds = Optional.ofNullable(dataPerm)
|
||||
.map(DeptDataPermissionRespDTO::getDeptIds)
|
||||
.orElse(Collections.emptySet());
|
||||
|
||||
// all=true 直接返回根级启用部门
|
||||
if (dataPerm != null && Boolean.TRUE.equals(dataPerm.getAll())) {
|
||||
List<DeptDO> roots = deptMapper.selectListByParentId(DeptDO.PARENT_ID_ROOT, CommonStatusEnum.ENABLE.getStatus());
|
||||
roots.sort(DEPT_COMPARATOR);
|
||||
return roots;
|
||||
}
|
||||
|
||||
// 合并两类部门 ID,仅在并集为空时返回空
|
||||
Set<Long> deptIds = new HashSet<>();
|
||||
deptIds.addAll(userDeptIds);
|
||||
deptIds.addAll(Optional.ofNullable(permDeptIds).orElse(Collections.emptySet()));
|
||||
|
||||
if (CollUtil.isEmpty(deptIds)) {
|
||||
// 如果用户没有关联任何部门,返回空列表
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
// 获取用户所属部门的最顶层祖先部门
|
||||
Set<Long> topLevelDeptIds = new HashSet<>();
|
||||
for (Long deptId : deptIds) {
|
||||
DeptDO dept = getDept(deptId);
|
||||
if (dept != null && CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus())) {
|
||||
// 找到该部门的最顶层祖先
|
||||
DeptDO topLevelDept = findTopLevelAncestor(dept);
|
||||
if (topLevelDept != null) {
|
||||
topLevelDeptIds.add(topLevelDept.getId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 根据顶层部门ID获取部门详情
|
||||
return topLevelDeptIds.stream()
|
||||
.map(this::getDept)
|
||||
.filter(Objects::nonNull)
|
||||
.filter(dept -> CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus()))
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 缓存已加载的部门,避免重复 IO
|
||||
Map<Long, DeptDO> deptCache = new HashMap<>();
|
||||
|
||||
// 批量解析最顶层祖先(到 ROOT 或上级禁用即停),减少循环 IO
|
||||
Map<Long, Long> topLevelMap = findTopLevelAncestorIdsBatch(deptIds, deptCache);
|
||||
|
||||
// 汇总顶层部门 ID 并取实体(使用缓存避免再查)
|
||||
Set<Long> topLevelDeptIds = topLevelMap.values().stream()
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
List<DeptDO> topLevelDepts = topLevelDeptIds.stream()
|
||||
.map(id -> deptCache.computeIfAbsent(id, this::getDept))
|
||||
.filter(Objects::nonNull)
|
||||
.filter(dept -> CommonStatusEnum.ENABLE.getStatus().equals(dept.getStatus()))
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 按 sort(nullsLast)再按 id 排序
|
||||
topLevelDepts.sort(DEPT_COMPARATOR);
|
||||
return topLevelDepts;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -741,64 +785,111 @@ public class DeptServiceImpl implements DeptService {
|
||||
return dept;
|
||||
}
|
||||
|
||||
private void upsertExternalCodeMapping(DeptSaveReqVO reqVO, Long deptId) {
|
||||
if (reqVO == null || deptId == null) {
|
||||
return;
|
||||
/**
|
||||
* 批量查找部门的最顶层祖先(到 ROOT 或遇到禁用/缺失的父部门即停止)
|
||||
* 使用 1000 条分片批量查询,减少循环 IO
|
||||
*
|
||||
* @param deptIds 待解析的部门 ID 集合
|
||||
* @param deptCache 部门缓存(可复用外部缓存)
|
||||
* @return 原始部门 ID -> 顶层祖先部门 ID 映射(若未找到则为 null)
|
||||
*/
|
||||
private Map<Long, Long> findTopLevelAncestorIdsBatch(Set<Long> deptIds, Map<Long, DeptDO> deptCache) {
|
||||
Map<Long, Long> result = new HashMap<>();
|
||||
if (CollUtil.isEmpty(deptIds)) {
|
||||
return result;
|
||||
}
|
||||
String systemCode = StrUtil.trimToNull(reqVO.getExternalSystemCode());
|
||||
String externalCode = StrUtil.trimToNull(reqVO.getExternalDeptCode());
|
||||
if (StrUtil.isBlank(systemCode) || StrUtil.isBlank(externalCode)) {
|
||||
return;
|
||||
|
||||
// 当前指针:原始部门 -> 当前向上追溯的部门 ID
|
||||
Map<Long, Long> cursorMap = new HashMap<>();
|
||||
for (Long id : deptIds) {
|
||||
cursorMap.put(id, id);
|
||||
}
|
||||
// 缺失的外部系统字典类型或数据会自动补齐
|
||||
ensureExternalSystemDict(systemCode);
|
||||
deptExternalCodeService.saveOrUpdateDeptExternalCode(
|
||||
deptId,
|
||||
systemCode,
|
||||
externalCode,
|
||||
reqVO.getExternalDeptName(),
|
||||
CommonStatusEnum.ENABLE.getStatus());
|
||||
|
||||
// 预先加载首批部门
|
||||
loadDeptBatch(cursorMap.values(), deptCache);
|
||||
|
||||
int safety = 0;
|
||||
while (!cursorMap.isEmpty() && safety++ < Short.MAX_VALUE) {
|
||||
// 收集本轮需要加载的父部门 ID(避免重复加载)
|
||||
Set<Long> parentIdsToLoad = new HashSet<>();
|
||||
for (Long currentId : cursorMap.values()) {
|
||||
DeptDO current = deptCache.get(currentId);
|
||||
if (current == null) {
|
||||
continue;
|
||||
}
|
||||
Long parentId = current.getParentId();
|
||||
if (parentId != null && !DeptDO.PARENT_ID_ROOT.equals(parentId) && !deptCache.containsKey(parentId)) {
|
||||
parentIdsToLoad.add(parentId);
|
||||
}
|
||||
}
|
||||
loadDeptBatch(parentIdsToLoad, deptCache);
|
||||
|
||||
// 遍历当前指针,决定是否上卷或结束
|
||||
Iterator<Map.Entry<Long, Long>> iterator = cursorMap.entrySet().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
Map.Entry<Long, Long> entry = iterator.next();
|
||||
Long originalId = entry.getKey();
|
||||
Long currentId = entry.getValue();
|
||||
DeptDO current = deptCache.get(currentId);
|
||||
|
||||
if (current == null) {
|
||||
result.put(originalId, null);
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
Long parentId = current.getParentId();
|
||||
if (parentId == null || DeptDO.PARENT_ID_ROOT.equals(parentId)) {
|
||||
// 已到达 ROOT(顶层)
|
||||
result.put(originalId, current.getId());
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
DeptDO parent = deptCache.get(parentId);
|
||||
if (parent == null || !CommonStatusEnum.ENABLE.getStatus().equals(parent.getStatus())) {
|
||||
// 父部门缺失或禁用,则当前部门视为顶层
|
||||
result.put(originalId, current.getId());
|
||||
iterator.remove();
|
||||
continue;
|
||||
}
|
||||
|
||||
// 向上继续追溯
|
||||
entry.setValue(parentId);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 确保外部系统字典存在(含字典类型与对应值),若缺失则自动创建
|
||||
* 将给定的部门 ID 集合按批次加载到缓存
|
||||
*/
|
||||
private void ensureExternalSystemDict(String systemCode) {
|
||||
String normalizedCode = StrUtil.trimToNull(systemCode);
|
||||
if (normalizedCode == null) {
|
||||
private void loadDeptBatch(Collection<Long> ids, Map<Long, DeptDO> deptCache) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return;
|
||||
}
|
||||
List<Long> toLoad = ids.stream()
|
||||
.filter(Objects::nonNull)
|
||||
.filter(id -> !deptCache.containsKey(id))
|
||||
.distinct()
|
||||
.collect(Collectors.toList());
|
||||
if (CollUtil.isEmpty(toLoad)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
DictTypeDO dictType = dictTypeService.getDictType(DictTypeConstants.DEPT_EXTERNAL_SYSTEM);
|
||||
if (dictType == null) {
|
||||
DictTypeSaveReqVO typeReq = new DictTypeSaveReqVO();
|
||||
typeReq.setName("部门外部系统标识");
|
||||
typeReq.setType(DictTypeConstants.DEPT_EXTERNAL_SYSTEM);
|
||||
typeReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
typeReq.setRemark("外部组织同步自动创建");
|
||||
dictTypeService.createDictType(typeReq);
|
||||
} else if (!CommonStatusEnum.ENABLE.getStatus().equals(dictType.getStatus())) {
|
||||
DictTypeSaveReqVO updateReq = new DictTypeSaveReqVO();
|
||||
updateReq.setId(dictType.getId());
|
||||
updateReq.setName(dictType.getName());
|
||||
updateReq.setType(dictType.getType());
|
||||
updateReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
updateReq.setRemark(dictType.getRemark());
|
||||
dictTypeService.updateDictType(updateReq);
|
||||
}
|
||||
|
||||
if (dictDataService.getDictData(DictTypeConstants.DEPT_EXTERNAL_SYSTEM, normalizedCode) == null) {
|
||||
DictDataSaveReqVO dataReq = new DictDataSaveReqVO();
|
||||
dataReq.setDictType(DictTypeConstants.DEPT_EXTERNAL_SYSTEM);
|
||||
dataReq.setLabel(normalizedCode);
|
||||
dataReq.setValue(normalizedCode);
|
||||
dataReq.setSort(0);
|
||||
dataReq.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
||||
dataReq.setRemark("外部组织同步自动创建");
|
||||
dictDataService.createDictData(dataReq);
|
||||
for (int i = 0; i < toLoad.size(); i += BATCH_SIZE) {
|
||||
int end = Math.min(i + BATCH_SIZE, toLoad.size());
|
||||
List<Long> batch = toLoad.subList(i, end);
|
||||
List<DeptDO> depts = getDeptList(batch);
|
||||
if (CollUtil.isEmpty(depts)) {
|
||||
continue;
|
||||
}
|
||||
for (DeptDO dept : depts) {
|
||||
if (dept != null && dept.getId() != null) {
|
||||
deptCache.putIfAbsent(dept.getId(), dept);
|
||||
}
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.warn("[Dept] Ensure external system dict failed, systemCode={}", normalizedCode, ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,6 +57,7 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
}
|
||||
result.increasePulled(records.size());
|
||||
List<IWorkHrSubcompanyPageRespVO.Subcompany> queue = new ArrayList<>(records);
|
||||
Set<Long> readyParentIds = new HashSet<>();
|
||||
int guard = 0;
|
||||
int maxPasses = Math.max(1, queue.size() * 2);
|
||||
while (!queue.isEmpty() && guard++ < maxPasses) {
|
||||
@@ -79,6 +80,9 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
}
|
||||
Long deptId = externalId.longValue();
|
||||
ParentHolder parentHolder = resolveSubcompanyParent(sub.getSupsubcomid());
|
||||
if (!isParentReady(parentHolder.parentId(), readyParentIds)) {
|
||||
continue;
|
||||
}
|
||||
boolean canceled = isCanceledFlag(sub.getCanceled());
|
||||
DeptSaveReqVO saveReq = buildSubcompanySaveReq(sub, deptId, parentHolder.parentId(), canceled);
|
||||
try {
|
||||
@@ -87,6 +91,9 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
canceled,
|
||||
options);
|
||||
applyDeptOutcome(result, outcome, "分部", sub.getSubcompanyname());
|
||||
if (outcome.deptId() != null) {
|
||||
readyParentIds.add(outcome.deptId());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("[iWork] 同步分部失败: id={} name={}", sub.getId(), sub.getSubcompanyname(), ex);
|
||||
result.increaseFailed();
|
||||
@@ -101,8 +108,19 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
}
|
||||
if (!queue.isEmpty()) {
|
||||
for (IWorkHrSubcompanyPageRespVO.Subcompany remaining : queue) {
|
||||
log.warn("[iWork] 分部因父级缺失未同步: id={} name={}", remaining.getId(), remaining.getSubcompanyname());
|
||||
result.increaseFailed();
|
||||
log.warn("[iWork] 分部父级缺失,延迟生成编码插入占位: id={} name={}", remaining.getId(), remaining.getSubcompanyname());
|
||||
DeptSaveReqVO saveReq = buildSubcompanySaveReq(remaining,
|
||||
remaining.getId() == null ? null : remaining.getId().longValue(),
|
||||
resolveSubcompanyParent(remaining.getSupsubcomid()).parentId(),
|
||||
isCanceledFlag(remaining.getCanceled()));
|
||||
saveReq.setDelayCodeGeneration(true);
|
||||
try {
|
||||
DeptSyncOutcome outcome = upsertDept(saveReq.getId(), saveReq, isCanceledFlag(remaining.getCanceled()), options);
|
||||
applyDeptOutcome(result, outcome, "分部", remaining.getSubcompanyname());
|
||||
} catch (Exception ex) {
|
||||
log.error("[iWork] 分部占位插入失败: id={} name={}", remaining.getId(), remaining.getSubcompanyname(), ex);
|
||||
result.increaseFailed();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -117,6 +135,7 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
}
|
||||
result.increasePulled(records.size());
|
||||
List<IWorkHrDepartmentPageRespVO.Department> queue = new ArrayList<>(records);
|
||||
Set<Long> readyParentIds = new HashSet<>();
|
||||
int guard = 0;
|
||||
int maxPasses = Math.max(1, queue.size() * 2);
|
||||
while (!queue.isEmpty() && guard++ < maxPasses) {
|
||||
@@ -139,6 +158,9 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
}
|
||||
Long deptId = externalId.longValue();
|
||||
ParentHolder parentHolder = resolveDepartmentParent(dept);
|
||||
if (!isParentReady(parentHolder.parentId(), readyParentIds)) {
|
||||
continue;
|
||||
}
|
||||
boolean canceled = isCanceledFlag(dept.getCanceled());
|
||||
DeptSaveReqVO saveReq = buildDepartmentSaveReq(dept, deptId, parentHolder.parentId(), canceled);
|
||||
try {
|
||||
@@ -147,6 +169,9 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
canceled,
|
||||
options);
|
||||
applyDeptOutcome(result, outcome, "部门", dept.getDepartmentname());
|
||||
if (outcome.deptId() != null) {
|
||||
readyParentIds.add(outcome.deptId());
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
log.error("[iWork] 同步部门失败: id={} name={}", dept.getId(), dept.getDepartmentname(), ex);
|
||||
result.increaseFailed();
|
||||
@@ -161,8 +186,19 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
}
|
||||
if (!queue.isEmpty()) {
|
||||
for (IWorkHrDepartmentPageRespVO.Department remaining : queue) {
|
||||
log.warn("[iWork] 部门因父级缺失未同步: id={} name={}", remaining.getId(), remaining.getDepartmentname());
|
||||
result.increaseFailed();
|
||||
log.warn("[iWork] 部门父级缺失,延迟生成编码插入占位: id={} name={}", remaining.getId(), remaining.getDepartmentname());
|
||||
DeptSaveReqVO saveReq = buildDepartmentSaveReq(remaining,
|
||||
remaining.getId() == null ? null : remaining.getId().longValue(),
|
||||
resolveDepartmentParent(remaining).parentId(),
|
||||
isCanceledFlag(remaining.getCanceled()));
|
||||
saveReq.setDelayCodeGeneration(true);
|
||||
try {
|
||||
DeptSyncOutcome outcome = upsertDept(saveReq.getId(), saveReq, isCanceledFlag(remaining.getCanceled()), options);
|
||||
applyDeptOutcome(result, outcome, "部门", remaining.getDepartmentname());
|
||||
} catch (Exception ex) {
|
||||
log.error("[iWork] 部门占位插入失败: id={} name={}", remaining.getId(), remaining.getDepartmentname(), ex);
|
||||
result.increaseFailed();
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
@@ -493,6 +529,16 @@ public class IWorkSyncProcessorImpl implements IWorkSyncProcessor {
|
||||
return new ParentHolder(DeptDO.PARENT_ID_ROOT);
|
||||
}
|
||||
|
||||
private boolean isParentReady(Long parentId, Set<Long> readyParentIds) {
|
||||
if (parentId == null || DeptDO.PARENT_ID_ROOT.equals(parentId)) {
|
||||
return true;
|
||||
}
|
||||
if (readyParentIds.contains(parentId)) {
|
||||
return true;
|
||||
}
|
||||
return deptService.getDept(parentId) != null;
|
||||
}
|
||||
|
||||
private PostDO resolvePostByCode(String code) {
|
||||
String key = buildPostCacheKey(code);
|
||||
PostDO cached = postCache.get(key);
|
||||
|
||||
@@ -193,10 +193,14 @@ public interface AdminUserService {
|
||||
* @param status 状态
|
||||
* @return 用户们
|
||||
*/
|
||||
List<AdminUserDO> getUserListByStatus(Integer status, Integer limit);
|
||||
List<AdminUserDO> getUserListByStatus(Integer status, Integer limit, String keyword);
|
||||
|
||||
default List<AdminUserDO> getUserListByStatus(Integer status, Integer limit) {
|
||||
return getUserListByStatus(status, limit, null);
|
||||
}
|
||||
|
||||
default List<AdminUserDO> getUserListByStatus(Integer status) {
|
||||
return getUserListByStatus(status, null);
|
||||
return getUserListByStatus(status, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -664,8 +664,8 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<AdminUserDO> getUserListByStatus(Integer status, Integer limit) {
|
||||
List<AdminUserDO> users = userMapper.selectListByStatus(status, limit);
|
||||
public List<AdminUserDO> getUserListByStatus(Integer status, Integer limit, String keyword) {
|
||||
List<AdminUserDO> users = userMapper.selectListByStatus(status, limit, keyword);
|
||||
fillUserDeptInfo(users);
|
||||
return users;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user