1. 移除多余 java 文件
This commit is contained in:
@@ -1,224 +0,0 @@
|
||||
package com.zt.plat.module.system.api.databus;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.databus.api.data.DatabusDeptData;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageReqDTO;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageResult;
|
||||
import com.zt.plat.module.databus.api.provider.DatabusDeptProviderApi;
|
||||
import com.zt.plat.module.system.dal.dataobject.dept.DeptDO;
|
||||
import com.zt.plat.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
|
||||
import com.zt.plat.module.system.dal.mysql.user.AdminUserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.zt.plat.framework.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* Databus 部门数据提供者 API 实现
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@Validated
|
||||
public class DatabusDeptProviderApiImpl implements DatabusDeptProviderApi {
|
||||
|
||||
@Resource
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
@Resource
|
||||
private AdminUserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public CommonResult<CursorPageResult<DatabusDeptData>> getPageByCursor(CursorPageReqDTO reqDTO) {
|
||||
// 构建游标查询条件
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
// 游标条件:create_time > cursorTime OR (create_time = cursorTime AND id > cursorId)
|
||||
if (!reqDTO.isFirstPage()) {
|
||||
queryWrapper.and(w -> w
|
||||
.gt(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.or(o -> o
|
||||
.eq(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.gt(DeptDO::getId, reqDTO.getCursorId())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 租户过滤(如果指定)
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
|
||||
// 按 create_time, id 升序排列,确保顺序稳定
|
||||
queryWrapper.orderByAsc(DeptDO::getCreateTime)
|
||||
.orderByAsc(DeptDO::getId);
|
||||
|
||||
// 多查一条判断是否有更多数据
|
||||
int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100;
|
||||
queryWrapper.last("LIMIT " + (limit + 1));
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectList(queryWrapper);
|
||||
|
||||
// 判断是否有更多
|
||||
boolean hasMore = deptList.size() > limit;
|
||||
if (hasMore) {
|
||||
deptList = deptList.subList(0, limit);
|
||||
}
|
||||
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(CursorPageResult.empty());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
// 转换为同步数据
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 获取最后一条数据的游标
|
||||
DeptDO lastDept = deptList.get(deptList.size() - 1);
|
||||
|
||||
// 首次查询时返回总数
|
||||
Long total = null;
|
||||
if (reqDTO.isFirstPage()) {
|
||||
LambdaQueryWrapper<DeptDO> countWrapper = new LambdaQueryWrapper<>();
|
||||
countWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
countWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
total = deptMapper.selectCount(countWrapper);
|
||||
}
|
||||
|
||||
return success(CursorPageResult.of(
|
||||
dataList,
|
||||
lastDept.getCreateTime(),
|
||||
lastDept.getId(),
|
||||
hasMore,
|
||||
total
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<DatabusDeptData> getById(Long id) {
|
||||
DeptDO dept = deptMapper.selectById(id);
|
||||
if (dept == null) {
|
||||
return success(null);
|
||||
}
|
||||
|
||||
// 查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (dept.getLeaderUserId() != null) {
|
||||
AdminUserDO user = userMapper.selectById(dept.getLeaderUserId());
|
||||
if (user != null) {
|
||||
userNameMap.put(user.getId(), user.getNickname());
|
||||
}
|
||||
}
|
||||
|
||||
return success(convertToDatabusDeptData(dept, userNameMap));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DatabusDeptData>> getListByIds(List<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectBatchIds(ids);
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return success(dataList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Long> count(Long tenantId) {
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
<<<<<<< HEAD
|
||||
// ⚠️ 只统计 userSource = 3 的用户
|
||||
=======
|
||||
>>>>>>> test
|
||||
queryWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
if (tenantId != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, tenantId);
|
||||
}
|
||||
return success(deptMapper.selectCount(queryWrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 DeptDO 转换为 DatabusDeptData
|
||||
*/
|
||||
private DatabusDeptData convertToDatabusDeptData(DeptDO dept, Map<Long, String> userNameMap) {
|
||||
// 根据 isCompany 反推 deptType
|
||||
Integer deptType = null;
|
||||
if (Boolean.TRUE.equals(dept.getIsCompany())) {
|
||||
deptType = 28; // 公司
|
||||
} else if (Boolean.FALSE.equals(dept.getIsCompany())) {
|
||||
deptType = 26; // 部门
|
||||
}
|
||||
|
||||
return DatabusDeptData.builder()
|
||||
.id(dept.getId())
|
||||
.code(dept.getCode())
|
||||
.name(dept.getName())
|
||||
.shortName(dept.getShortName())
|
||||
.parentId(dept.getParentId())
|
||||
.sort(dept.getSort())
|
||||
.status(dept.getStatus())
|
||||
.deptType(deptType)
|
||||
.isGroup(dept.getIsGroup())
|
||||
.isCompany(dept.getIsCompany())
|
||||
.deptSource(dept.getDeptSource())
|
||||
.leaderUserId(dept.getLeaderUserId())
|
||||
.leaderUserName(dept.getLeaderUserId() != null ? userNameMap.get(dept.getLeaderUserId()) : null)
|
||||
.phone(dept.getPhone())
|
||||
.email(dept.getEmail())
|
||||
.tenantId(dept.getTenantId())
|
||||
.createTime(dept.getCreateTime())
|
||||
.updateTime(dept.getUpdateTime())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,218 +0,0 @@
|
||||
package com.zt.plat.module.system.api.databus;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.databus.api.data.DatabusDeptData;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageReqDTO;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageResult;
|
||||
import com.zt.plat.module.databus.api.provider.DatabusDeptProviderApi;
|
||||
import com.zt.plat.module.system.dal.dataobject.dept.DeptDO;
|
||||
import com.zt.plat.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
|
||||
import com.zt.plat.module.system.dal.mysql.user.AdminUserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.zt.plat.framework.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* Databus 部门数据提供者 API 实现
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@Validated
|
||||
public class DatabusDeptProviderApiImpl implements DatabusDeptProviderApi {
|
||||
|
||||
@Resource
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
@Resource
|
||||
private AdminUserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public CommonResult<CursorPageResult<DatabusDeptData>> getPageByCursor(CursorPageReqDTO reqDTO) {
|
||||
// 构建游标查询条件
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
|
||||
// 游标条件:create_time > cursorTime OR (create_time = cursorTime AND id > cursorId)
|
||||
if (!reqDTO.isFirstPage()) {
|
||||
queryWrapper.and(w -> w
|
||||
.gt(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.or(o -> o
|
||||
.eq(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.gt(DeptDO::getId, reqDTO.getCursorId())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 租户过滤(如果指定)
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
|
||||
// 按 create_time, id 升序排列,确保顺序稳定
|
||||
queryWrapper.orderByAsc(DeptDO::getCreateTime)
|
||||
.orderByAsc(DeptDO::getId);
|
||||
|
||||
// 多查一条判断是否有更多数据
|
||||
int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100;
|
||||
queryWrapper.last("LIMIT " + (limit + 1));
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectList(queryWrapper);
|
||||
|
||||
// 判断是否有更多
|
||||
boolean hasMore = deptList.size() > limit;
|
||||
if (hasMore) {
|
||||
deptList = deptList.subList(0, limit);
|
||||
}
|
||||
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(CursorPageResult.empty());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
// 转换为同步数据
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 获取最后一条数据的游标
|
||||
DeptDO lastDept = deptList.get(deptList.size() - 1);
|
||||
|
||||
// 首次查询时返回总数
|
||||
Long total = null;
|
||||
if (reqDTO.isFirstPage()) {
|
||||
LambdaQueryWrapper<DeptDO> countWrapper = new LambdaQueryWrapper<>();
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
countWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
total = deptMapper.selectCount(countWrapper);
|
||||
}
|
||||
|
||||
return success(CursorPageResult.of(
|
||||
dataList,
|
||||
lastDept.getCreateTime(),
|
||||
lastDept.getId(),
|
||||
hasMore,
|
||||
total
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<DatabusDeptData> getById(Long id) {
|
||||
DeptDO dept = deptMapper.selectById(id);
|
||||
if (dept == null) {
|
||||
return success(null);
|
||||
}
|
||||
|
||||
// 查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (dept.getLeaderUserId() != null) {
|
||||
AdminUserDO user = userMapper.selectById(dept.getLeaderUserId());
|
||||
if (user != null) {
|
||||
userNameMap.put(user.getId(), user.getNickname());
|
||||
}
|
||||
}
|
||||
|
||||
return success(convertToDatabusDeptData(dept, userNameMap));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DatabusDeptData>> getListByIds(List<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectBatchIds(ids);
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return success(dataList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Long> count(Long tenantId) {
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
if (tenantId != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, tenantId);
|
||||
}
|
||||
return success(deptMapper.selectCount(queryWrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 DeptDO 转换为 DatabusDeptData
|
||||
*/
|
||||
private DatabusDeptData convertToDatabusDeptData(DeptDO dept, Map<Long, String> userNameMap) {
|
||||
// 根据 isCompany 反推 deptType
|
||||
Integer deptType = null;
|
||||
if (Boolean.TRUE.equals(dept.getIsCompany())) {
|
||||
deptType = 28; // 公司
|
||||
} else if (Boolean.FALSE.equals(dept.getIsCompany())) {
|
||||
deptType = 26; // 部门
|
||||
}
|
||||
|
||||
return DatabusDeptData.builder()
|
||||
.id(dept.getId())
|
||||
.code(dept.getCode())
|
||||
.name(dept.getName())
|
||||
.shortName(dept.getShortName())
|
||||
.parentId(dept.getParentId())
|
||||
.sort(dept.getSort())
|
||||
.status(dept.getStatus())
|
||||
.deptType(deptType)
|
||||
.isGroup(dept.getIsGroup())
|
||||
.isCompany(dept.getIsCompany())
|
||||
.deptSource(dept.getDeptSource())
|
||||
.leaderUserId(dept.getLeaderUserId())
|
||||
.leaderUserName(dept.getLeaderUserId() != null ? userNameMap.get(dept.getLeaderUserId()) : null)
|
||||
.phone(dept.getPhone())
|
||||
.email(dept.getEmail())
|
||||
.tenantId(dept.getTenantId())
|
||||
.createTime(dept.getCreateTime())
|
||||
.updateTime(dept.getUpdateTime())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,221 +0,0 @@
|
||||
package com.zt.plat.module.system.api.databus;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.databus.api.data.DatabusDeptData;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageReqDTO;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageResult;
|
||||
import com.zt.plat.module.databus.api.provider.DatabusDeptProviderApi;
|
||||
import com.zt.plat.module.system.dal.dataobject.dept.DeptDO;
|
||||
import com.zt.plat.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
|
||||
import com.zt.plat.module.system.dal.mysql.user.AdminUserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.zt.plat.framework.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* Databus 部门数据提供者 API 实现
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@Validated
|
||||
public class DatabusDeptProviderApiImpl implements DatabusDeptProviderApi {
|
||||
|
||||
@Resource
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
@Resource
|
||||
private AdminUserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public CommonResult<CursorPageResult<DatabusDeptData>> getPageByCursor(CursorPageReqDTO reqDTO) {
|
||||
// 构建游标查询条件
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
// 游标条件:create_time > cursorTime OR (create_time = cursorTime AND id > cursorId)
|
||||
if (!reqDTO.isFirstPage()) {
|
||||
queryWrapper.and(w -> w
|
||||
.gt(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.or(o -> o
|
||||
.eq(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.gt(DeptDO::getId, reqDTO.getCursorId())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 租户过滤(如果指定)
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
|
||||
// 按 create_time, id 升序排列,确保顺序稳定
|
||||
queryWrapper.orderByAsc(DeptDO::getCreateTime)
|
||||
.orderByAsc(DeptDO::getId);
|
||||
|
||||
// 多查一条判断是否有更多数据
|
||||
int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100;
|
||||
queryWrapper.last("LIMIT " + (limit + 1));
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectList(queryWrapper);
|
||||
|
||||
// 判断是否有更多
|
||||
boolean hasMore = deptList.size() > limit;
|
||||
if (hasMore) {
|
||||
deptList = deptList.subList(0, limit);
|
||||
}
|
||||
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(CursorPageResult.empty());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
// 转换为同步数据
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 获取最后一条数据的游标
|
||||
DeptDO lastDept = deptList.get(deptList.size() - 1);
|
||||
|
||||
// 首次查询时返回总数
|
||||
Long total = null;
|
||||
if (reqDTO.isFirstPage()) {
|
||||
LambdaQueryWrapper<DeptDO> countWrapper = new LambdaQueryWrapper<>();
|
||||
countWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
countWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
total = deptMapper.selectCount(countWrapper);
|
||||
}
|
||||
|
||||
return success(CursorPageResult.of(
|
||||
dataList,
|
||||
lastDept.getCreateTime(),
|
||||
lastDept.getId(),
|
||||
hasMore,
|
||||
total
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<DatabusDeptData> getById(Long id) {
|
||||
DeptDO dept = deptMapper.selectById(id);
|
||||
if (dept == null) {
|
||||
return success(null);
|
||||
}
|
||||
|
||||
// 查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (dept.getLeaderUserId() != null) {
|
||||
AdminUserDO user = userMapper.selectById(dept.getLeaderUserId());
|
||||
if (user != null) {
|
||||
userNameMap.put(user.getId(), user.getNickname());
|
||||
}
|
||||
}
|
||||
|
||||
return success(convertToDatabusDeptData(dept, userNameMap));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DatabusDeptData>> getListByIds(List<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectBatchIds(ids);
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return success(dataList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Long> count(Long tenantId) {
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
// ⚠️ 只统计 userSource = 3 的用户
|
||||
queryWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
if (tenantId != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, tenantId);
|
||||
}
|
||||
return success(deptMapper.selectCount(queryWrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 DeptDO 转换为 DatabusDeptData
|
||||
*/
|
||||
private DatabusDeptData convertToDatabusDeptData(DeptDO dept, Map<Long, String> userNameMap) {
|
||||
// 根据 isCompany 反推 deptType
|
||||
Integer deptType = null;
|
||||
if (Boolean.TRUE.equals(dept.getIsCompany())) {
|
||||
deptType = 28; // 公司
|
||||
} else if (Boolean.FALSE.equals(dept.getIsCompany())) {
|
||||
deptType = 26; // 部门
|
||||
}
|
||||
|
||||
return DatabusDeptData.builder()
|
||||
.id(dept.getId())
|
||||
.code(dept.getCode())
|
||||
.name(dept.getName())
|
||||
.shortName(dept.getShortName())
|
||||
.parentId(dept.getParentId())
|
||||
.sort(dept.getSort())
|
||||
.status(dept.getStatus())
|
||||
.deptType(deptType)
|
||||
.isGroup(dept.getIsGroup())
|
||||
.isCompany(dept.getIsCompany())
|
||||
.deptSource(dept.getDeptSource())
|
||||
.leaderUserId(dept.getLeaderUserId())
|
||||
.leaderUserName(dept.getLeaderUserId() != null ? userNameMap.get(dept.getLeaderUserId()) : null)
|
||||
.phone(dept.getPhone())
|
||||
.email(dept.getEmail())
|
||||
.tenantId(dept.getTenantId())
|
||||
.createTime(dept.getCreateTime())
|
||||
.updateTime(dept.getUpdateTime())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,220 +0,0 @@
|
||||
package com.zt.plat.module.system.api.databus;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.zt.plat.framework.common.pojo.CommonResult;
|
||||
import com.zt.plat.module.databus.api.data.DatabusDeptData;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageReqDTO;
|
||||
import com.zt.plat.module.databus.api.dto.CursorPageResult;
|
||||
import com.zt.plat.module.databus.api.provider.DatabusDeptProviderApi;
|
||||
import com.zt.plat.module.system.dal.dataobject.dept.DeptDO;
|
||||
import com.zt.plat.module.system.dal.dataobject.user.AdminUserDO;
|
||||
import com.zt.plat.module.system.dal.mysql.dept.DeptMapper;
|
||||
import com.zt.plat.module.system.dal.mysql.user.AdminUserMapper;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static com.zt.plat.framework.common.pojo.CommonResult.success;
|
||||
|
||||
/**
|
||||
* Databus 部门数据提供者 API 实现
|
||||
*
|
||||
* @author ZT
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@Validated
|
||||
public class DatabusDeptProviderApiImpl implements DatabusDeptProviderApi {
|
||||
|
||||
@Resource
|
||||
private DeptMapper deptMapper;
|
||||
|
||||
@Resource
|
||||
private AdminUserMapper userMapper;
|
||||
|
||||
@Override
|
||||
public CommonResult<CursorPageResult<DatabusDeptData>> getPageByCursor(CursorPageReqDTO reqDTO) {
|
||||
// 构建游标查询条件
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
// 游标条件:create_time > cursorTime OR (create_time = cursorTime AND id > cursorId)
|
||||
if (!reqDTO.isFirstPage()) {
|
||||
queryWrapper.and(w -> w
|
||||
.gt(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.or(o -> o
|
||||
.eq(DeptDO::getCreateTime, reqDTO.getCursorTime())
|
||||
.gt(DeptDO::getId, reqDTO.getCursorId())
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
// 租户过滤(如果指定)
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
|
||||
// 按 create_time, id 升序排列,确保顺序稳定
|
||||
queryWrapper.orderByAsc(DeptDO::getCreateTime)
|
||||
.orderByAsc(DeptDO::getId);
|
||||
|
||||
// 多查一条判断是否有更多数据
|
||||
int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100;
|
||||
queryWrapper.last("LIMIT " + (limit + 1));
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectList(queryWrapper);
|
||||
|
||||
// 判断是否有更多
|
||||
boolean hasMore = deptList.size() > limit;
|
||||
if (hasMore) {
|
||||
deptList = deptList.subList(0, limit);
|
||||
}
|
||||
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(CursorPageResult.empty());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
// 转换为同步数据
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
// 获取最后一条数据的游标
|
||||
DeptDO lastDept = deptList.get(deptList.size() - 1);
|
||||
|
||||
// 首次查询时返回总数
|
||||
Long total = null;
|
||||
if (reqDTO.isFirstPage()) {
|
||||
LambdaQueryWrapper<DeptDO> countWrapper = new LambdaQueryWrapper<>();
|
||||
countWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
if (reqDTO.getTenantId() != null) {
|
||||
countWrapper.eq(DeptDO::getTenantId, reqDTO.getTenantId());
|
||||
}
|
||||
total = deptMapper.selectCount(countWrapper);
|
||||
}
|
||||
|
||||
return success(CursorPageResult.of(
|
||||
dataList,
|
||||
lastDept.getCreateTime(),
|
||||
lastDept.getId(),
|
||||
hasMore,
|
||||
total
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<DatabusDeptData> getById(Long id) {
|
||||
DeptDO dept = deptMapper.selectById(id);
|
||||
if (dept == null) {
|
||||
return success(null);
|
||||
}
|
||||
|
||||
// 查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (dept.getLeaderUserId() != null) {
|
||||
AdminUserDO user = userMapper.selectById(dept.getLeaderUserId());
|
||||
if (user != null) {
|
||||
userNameMap.put(user.getId(), user.getNickname());
|
||||
}
|
||||
}
|
||||
|
||||
return success(convertToDatabusDeptData(dept, userNameMap));
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<List<DatabusDeptData>> getListByIds(List<Long> ids) {
|
||||
if (CollUtil.isEmpty(ids)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
List<DeptDO> deptList = deptMapper.selectBatchIds(ids);
|
||||
if (CollUtil.isEmpty(deptList)) {
|
||||
return success(Collections.emptyList());
|
||||
}
|
||||
|
||||
// 收集负责人用户ID
|
||||
Set<Long> leaderUserIds = deptList.stream()
|
||||
.map(DeptDO::getLeaderUserId)
|
||||
.filter(Objects::nonNull)
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
// 批量查询负责人用户名
|
||||
Map<Long, String> userNameMap = new HashMap<>();
|
||||
if (CollUtil.isNotEmpty(leaderUserIds)) {
|
||||
List<AdminUserDO> users = userMapper.selectBatchIds(leaderUserIds);
|
||||
userNameMap = users.stream()
|
||||
.collect(Collectors.toMap(AdminUserDO::getId, AdminUserDO::getNickname, (v1, v2) -> v1));
|
||||
}
|
||||
|
||||
Map<Long, String> finalUserNameMap = userNameMap;
|
||||
List<DatabusDeptData> dataList = deptList.stream()
|
||||
.map(dept -> convertToDatabusDeptData(dept, finalUserNameMap))
|
||||
.collect(Collectors.toList());
|
||||
|
||||
return success(dataList);
|
||||
}
|
||||
|
||||
@Override
|
||||
public CommonResult<Long> count(Long tenantId) {
|
||||
LambdaQueryWrapper<DeptDO> queryWrapper = new LambdaQueryWrapper<>();
|
||||
queryWrapper.eq(DeptDO::getDeptSource, 3);
|
||||
if (tenantId != null) {
|
||||
queryWrapper.eq(DeptDO::getTenantId, tenantId);
|
||||
}
|
||||
return success(deptMapper.selectCount(queryWrapper));
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 DeptDO 转换为 DatabusDeptData
|
||||
*/
|
||||
private DatabusDeptData convertToDatabusDeptData(DeptDO dept, Map<Long, String> userNameMap) {
|
||||
// 根据 isCompany 反推 deptType
|
||||
Integer deptType = null;
|
||||
if (Boolean.TRUE.equals(dept.getIsCompany())) {
|
||||
deptType = 28; // 公司
|
||||
} else if (Boolean.FALSE.equals(dept.getIsCompany())) {
|
||||
deptType = 26; // 部门
|
||||
}
|
||||
|
||||
return DatabusDeptData.builder()
|
||||
.id(dept.getId())
|
||||
.code(dept.getCode())
|
||||
.name(dept.getName())
|
||||
.shortName(dept.getShortName())
|
||||
.parentId(dept.getParentId())
|
||||
.sort(dept.getSort())
|
||||
.status(dept.getStatus())
|
||||
.deptType(deptType)
|
||||
.isGroup(dept.getIsGroup())
|
||||
.isCompany(dept.getIsCompany())
|
||||
.deptSource(dept.getDeptSource())
|
||||
.leaderUserId(dept.getLeaderUserId())
|
||||
.leaderUserName(dept.getLeaderUserId() != null ? userNameMap.get(dept.getLeaderUserId()) : null)
|
||||
.phone(dept.getPhone())
|
||||
.email(dept.getEmail())
|
||||
.tenantId(dept.getTenantId())
|
||||
.createTime(dept.getCreateTime())
|
||||
.updateTime(dept.getUpdateTime())
|
||||
.build();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user