update:调整数据同步用户-部门,用户-岗位同步顺序

This commit is contained in:
hewencai
2025-12-22 11:03:15 +08:00
parent 9b0e63a33e
commit 7ef5545dc0
15 changed files with 494 additions and 102 deletions

View File

@@ -1,56 +1,101 @@
package com.zt.plat.framework.databus.client.handler.userdept; package com.zt.plat.framework.databus.client.handler.userdept;
import com.zt.plat.module.databus.api.data.DatabusUserDeptData; import com.zt.plat.module.databus.api.data.DatabusUserDeptData;
import com.zt.plat.module.system.api.userdept.UserDeptApi;
import com.zt.plat.module.system.api.userdept.dto.UserDeptSaveReqDTO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
* 用户-部门关系同步服务实现 * 用户-部门关系同步服务实现(通过 Feign API 调用远程服务)
* <p> * <p>
* 使用条件: * 使用条件:
* 1. zt.databus.sync.client.enabled=true * 1. zt.databus.sync.client.enabled=true
* 2. 系统中存在 UserDeptApi 接口Feign 客户端)
* <p> * <p>
* 注意:由于用户-部门关系通常集成在用户管理中,此实现为占位符。 * 如果分公司需要自定义实现,可以创建自己的 UserDeptSyncService Bean
* 分公司可以根据实际情况: * 此默认实现会自动失效(@ConditionalOnMissingBean
* 1. 自定义实现此接口,直接操作本地数据库
* 2. 或者通过用户管理 API 间接处理关联关系
* *
* @author ZT * @author ZT
*/ */
@Slf4j @Slf4j
@Service @Service
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true") @ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnClass(name = "com.zt.plat.module.system.api.userdept.UserDeptApi")
public class UserDeptSyncServiceImpl implements UserDeptSyncService { public class UserDeptSyncServiceImpl implements UserDeptSyncService {
@Autowired(required = false)
private UserDeptApi userDeptApi; // Feign 远程调用接口
@Override @Override
public void create(DatabusUserDeptData data) { public void create(DatabusUserDeptData data) {
log.info("[UserDeptSync] 收到创建用户-部门关系请求, userId={}, deptId={}", if (userDeptApi == null) {
data.getUserId(), data.getDeptId()); log.warn("[UserDeptSync] UserDeptApi未注入跳过创建用户-部门关系, userId={}", data.getUserId());
log.warn("[UserDeptSync] 用户-部门关系同步服务需要分公司自定义实现,当前为占位符实现"); return;
// TODO: 分公司需要实现此方法,通过本地 API 或直接数据库操作完成同步 }
UserDeptSaveReqDTO dto = buildUserDeptDTO(data);
userDeptApi.createUserDept(dto).checkError();
log.info("[UserDeptSync] 用户-部门关系创建成功, userId={}, deptId={}", data.getUserId(), data.getDeptId());
} }
@Override @Override
public void update(DatabusUserDeptData data) { public void update(DatabusUserDeptData data) {
log.info("[UserDeptSync] 收到更新用户-部门关系请求, userId={}, deptId={}", if (userDeptApi == null) {
data.getUserId(), data.getDeptId()); log.warn("[UserDeptSync] UserDeptApi未注入跳过更新用户-部门关系, userId={}", data.getUserId());
log.warn("[UserDeptSync] 用户-部门关系同步服务需要分公司自定义实现,当前为占位符实现"); return;
// TODO: 分公司需要实现此方法 }
UserDeptSaveReqDTO dto = buildUserDeptDTO(data);
userDeptApi.updateUserDept(dto).checkError();
log.info("[UserDeptSync] 用户-部门关系更新成功, userId={}, deptId={}", data.getUserId(), data.getDeptId());
} }
@Override @Override
public void delete(Long id) { public void delete(Long id) {
log.info("[UserDeptSync] 收到删除用户-部门关系请求, id={}", id); if (userDeptApi == null) {
log.warn("[UserDeptSync] 用户-部门关系同步服务需要分公司自定义实现,当前为占位符实现"); log.warn("[UserDeptSync] UserDeptApi未注入跳过删除用户-部门关系, id={}", id);
// TODO: 分公司需要实现此方法 return;
}
userDeptApi.deleteUserDept(id).checkError();
log.info("[UserDeptSync] 用户-部门关系删除成功, id={}", id);
} }
@Override @Override
public void fullSync(DatabusUserDeptData data) { public void fullSync(DatabusUserDeptData data) {
log.info("[UserDeptSync] 收到全量同步用户-部门关系请求, userId={}, deptId={}", if (userDeptApi == null) {
data.getUserId(), data.getDeptId()); log.warn("[UserDeptSync] UserDeptApi未注入跳过全量同步用户-部门关系, userId={}", data.getUserId());
log.warn("[UserDeptSync] 用户-部门关系同步服务需要分公司自定义实现,当前为占位符实现"); return;
// TODO: 分公司需要实现此方法,逻辑:存在则更新,不存在则插入 }
UserDeptSaveReqDTO dto = buildUserDeptDTO(data);
try {
// 尝试获取,存在则更新,不存在则创建
var existing = userDeptApi.getUserDept(dto.getId());
if (existing.isSuccess() && existing.getData() != null) {
userDeptApi.updateUserDept(dto).checkError();
log.info("[UserDeptSync] 用户-部门关系全量同步-更新成功, id={}", dto.getId());
} else {
userDeptApi.createUserDept(dto).checkError();
log.info("[UserDeptSync] 用户-部门关系全量同步-创建成功, id={}", dto.getId());
}
} catch (Exception e) {
// 获取失败,尝试创建
log.warn("[UserDeptSync] 用户-部门关系获取失败,尝试创建, id={}", dto.getId());
userDeptApi.createUserDept(dto).checkError();
log.info("[UserDeptSync] 用户-部门关系全量同步-创建成功, id={}", dto.getId());
}
}
/**
* 构建用户部门关系 DTO用于 Feign 调用)
*/
private UserDeptSaveReqDTO buildUserDeptDTO(DatabusUserDeptData data) {
UserDeptSaveReqDTO dto = new UserDeptSaveReqDTO();
dto.setId(data.getId());
dto.setUserId(data.getUserId());
dto.setDeptId(data.getDeptId());
dto.setRemark(data.getRemark());
return dto;
} }
} }

View File

@@ -1,56 +1,100 @@
package com.zt.plat.framework.databus.client.handler.userpost; package com.zt.plat.framework.databus.client.handler.userpost;
import com.zt.plat.module.databus.api.data.DatabusUserPostData; import com.zt.plat.module.databus.api.data.DatabusUserPostData;
import com.zt.plat.module.system.api.userpost.UserPostApi;
import com.zt.plat.module.system.api.userpost.dto.UserPostSaveReqDTO;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
/** /**
* 用户-岗位关系同步服务实现 * 用户-岗位关系同步服务实现(通过 Feign API 调用远程服务)
* <p> * <p>
* 使用条件: * 使用条件:
* 1. zt.databus.sync.client.enabled=true * 1. zt.databus.sync.client.enabled=true
* 2. 系统中存在 UserPostApi 接口Feign 客户端)
* <p> * <p>
* 注意:由于用户-岗位关系通常集成在用户管理中,此实现为占位符。 * 如果分公司需要自定义实现,可以创建自己的 UserPostSyncService Bean
* 分公司可以根据实际情况: * 此默认实现会自动失效(@ConditionalOnMissingBean
* 1. 自定义实现此接口,直接操作本地数据库
* 2. 或者通过用户管理 API 间接处理关联关系
* *
* @author ZT * @author ZT
*/ */
@Slf4j @Slf4j
@Service @Service
@ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true") @ConditionalOnProperty(prefix = "zt.databus.sync.client", name = "enabled", havingValue = "true")
@ConditionalOnClass(name = "com.zt.plat.module.system.api.userpost.UserPostApi")
public class UserPostSyncServiceImpl implements UserPostSyncService { public class UserPostSyncServiceImpl implements UserPostSyncService {
@Autowired(required = false)
private UserPostApi userPostApi; // Feign 远程调用接口
@Override @Override
public void create(DatabusUserPostData data) { public void create(DatabusUserPostData data) {
log.info("[UserPostSync] 收到创建用户-岗位关系请求, userId={}, postId={}", if (userPostApi == null) {
data.getUserId(), data.getPostId()); log.warn("[UserPostSync] UserPostApi未注入跳过创建用户-岗位关系, userId={}", data.getUserId());
log.warn("[UserPostSync] 用户-岗位关系同步服务需要分公司自定义实现,当前为占位符实现"); return;
// TODO: 分公司需要实现此方法,通过本地 API 或直接数据库操作完成同步 }
UserPostSaveReqDTO dto = buildUserPostDTO(data);
userPostApi.createUserPost(dto).checkError();
log.info("[UserPostSync] 用户-岗位关系创建成功, userId={}, postId={}", data.getUserId(), data.getPostId());
} }
@Override @Override
public void update(DatabusUserPostData data) { public void update(DatabusUserPostData data) {
log.info("[UserPostSync] 收到更新用户-岗位关系请求, userId={}, postId={}", if (userPostApi == null) {
data.getUserId(), data.getPostId()); log.warn("[UserPostSync] UserPostApi未注入跳过更新用户-岗位关系, userId={}", data.getUserId());
log.warn("[UserPostSync] 用户-岗位关系同步服务需要分公司自定义实现,当前为占位符实现"); return;
// TODO: 分公司需要实现此方法 }
UserPostSaveReqDTO dto = buildUserPostDTO(data);
userPostApi.updateUserPost(dto).checkError();
log.info("[UserPostSync] 用户-岗位关系更新成功, userId={}, postId={}", data.getUserId(), data.getPostId());
} }
@Override @Override
public void delete(Long id) { public void delete(Long id) {
log.info("[UserPostSync] 收到删除用户-岗位关系请求, id={}", id); if (userPostApi == null) {
log.warn("[UserPostSync] 用户-岗位关系同步服务需要分公司自定义实现,当前为占位符实现"); log.warn("[UserPostSync] UserPostApi未注入跳过删除用户-岗位关系, id={}", id);
// TODO: 分公司需要实现此方法 return;
}
userPostApi.deleteUserPost(id).checkError();
log.info("[UserPostSync] 用户-岗位关系删除成功, id={}", id);
} }
@Override @Override
public void fullSync(DatabusUserPostData data) { public void fullSync(DatabusUserPostData data) {
log.info("[UserPostSync] 收到全量同步用户-岗位关系请求, userId={}, postId={}", if (userPostApi == null) {
data.getUserId(), data.getPostId()); log.warn("[UserPostSync] UserPostApi未注入跳过全量同步用户-岗位关系, userId={}", data.getUserId());
log.warn("[UserPostSync] 用户-岗位关系同步服务需要分公司自定义实现,当前为占位符实现"); return;
// TODO: 分公司需要实现此方法,逻辑:存在则更新,不存在则插入 }
UserPostSaveReqDTO dto = buildUserPostDTO(data);
try {
// 尝试获取,存在则更新,不存在则创建
var existing = userPostApi.getUserPost(dto.getId());
if (existing.isSuccess() && existing.getData() != null) {
userPostApi.updateUserPost(dto).checkError();
log.info("[UserPostSync] 用户-岗位关系全量同步-更新成功, id={}", dto.getId());
} else {
userPostApi.createUserPost(dto).checkError();
log.info("[UserPostSync] 用户-岗位关系全量同步-创建成功, id={}", dto.getId());
}
} catch (Exception e) {
// 获取失败,尝试创建
log.warn("[UserPostSync] 用户-岗位关系获取失败,尝试创建, id={}", dto.getId());
userPostApi.createUserPost(dto).checkError();
log.info("[UserPostSync] 用户-岗位关系全量同步-创建成功, id={}", dto.getId());
}
}
/**
* 构建用户岗位关系 DTO用于 Feign 调用)
*/
private UserPostSaveReqDTO buildUserPostDTO(DatabusUserPostData data) {
UserPostSaveReqDTO dto = new UserPostSaveReqDTO();
dto.setId(data.getId());
dto.setUserId(data.getUserId());
dto.setPostId(data.getPostId());
return dto;
} }
} }

View File

@@ -34,7 +34,15 @@ public class DatabusUserChangeConsumer implements RocketMQListener<DatabusUserCh
@Override @Override
public void onMessage(DatabusUserChangeMessage message) { public void onMessage(DatabusUserChangeMessage message) {
log.info("[Databus] 收到用户变更消息, action={}, userId={}", message.getAction(), message.getUserId()); log.info("[Databus] 收到用户变更消息, action={}, userId={}, userSource={}",
message.getAction(), message.getUserId(), message.getUserSource());
// ⚠️ 只处理 userSource = 2 的用户
if (message.getUserSource() == null || message.getUserSource() != 2) {
log.info("[Databus] 跳过非集团用户的变更消息, userId={}, userSource={}",
message.getUserId(), message.getUserSource());
return;
}
try { try {
Map<String, Object> dataMap = new HashMap<>(); Map<String, Object> dataMap = new HashMap<>();

View File

@@ -9,6 +9,8 @@ import com.zt.plat.module.databus.api.provider.DatabusUserPostProviderApi;
import com.zt.plat.module.system.api.dept.DeptApi; import com.zt.plat.module.system.api.dept.DeptApi;
import com.zt.plat.module.system.api.dept.PostApi; import com.zt.plat.module.system.api.dept.PostApi;
import com.zt.plat.module.system.api.user.AdminUserApi; import com.zt.plat.module.system.api.user.AdminUserApi;
import com.zt.plat.module.system.api.userdept.UserDeptApi;
import com.zt.plat.module.system.api.userpost.UserPostApi;
import org.springframework.cloud.openfeign.EnableFeignClients; import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -27,6 +29,8 @@ import org.springframework.context.annotation.Configuration;
DatabusUserPostProviderApi.class, DatabusUserPostProviderApi.class,
PostApi.class, PostApi.class,
DeptApi.class, DeptApi.class,
UserDeptApi.class,
UserPostApi.class,
}) })
public class RpcConfiguration { public class RpcConfiguration {
} }

View File

@@ -0,0 +1,64 @@
package com.zt.plat.module.system.api.userdept;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.module.system.api.userdept.dto.UserDeptRespDTO;
import com.zt.plat.module.system.api.userdept.dto.UserDeptSaveReqDTO;
import com.zt.plat.module.system.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.List;
/**
* 用户-部门关系 Feign API
*
* @author ZT
*/
@FeignClient(name = ApiConstants.NAME)
@Tag(name = "RPC 服务 - 用户部门关系")
public interface UserDeptApi {
String PREFIX = ApiConstants.PREFIX + "/user-dept";
@PostMapping(PREFIX + "/create")
@Operation(summary = "新增用户部门关系")
CommonResult<Long> createUserDept(@RequestBody UserDeptSaveReqDTO reqVO);
@PutMapping(PREFIX + "/update")
@Operation(summary = "修改用户部门关系")
CommonResult<Boolean> updateUserDept(@RequestBody UserDeptSaveReqDTO reqVO);
@DeleteMapping(PREFIX + "/delete")
@Operation(summary = "删除用户部门关系")
@Parameter(name = "id", description = "关系编号", example = "1", required = true)
CommonResult<Boolean> deleteUserDept(@RequestParam("id") Long id);
@GetMapping(PREFIX + "/get")
@Operation(summary = "通过ID查询用户部门关系")
@Parameter(name = "id", description = "关系编号", example = "1", required = true)
CommonResult<UserDeptRespDTO> getUserDept(@RequestParam("id") Long id);
@GetMapping(PREFIX + "/list-by-user-id")
@Operation(summary = "通过用户ID查询用户部门关系列表")
@Parameter(name = "userId", description = "用户编号", example = "1", required = true)
CommonResult<List<UserDeptRespDTO>> getUserDeptListByUserId(@RequestParam("userId") Long userId);
@GetMapping(PREFIX + "/list-by-dept-id")
@Operation(summary = "通过部门ID查询用户部门关系列表")
@Parameter(name = "deptId", description = "部门编号", example = "1", required = true)
CommonResult<List<UserDeptRespDTO>> getUserDeptListByDeptId(@RequestParam("deptId") Long deptId);
@DeleteMapping(PREFIX + "/delete-by-user-id")
@Operation(summary = "通过用户ID删除用户部门关系")
@Parameter(name = "userId", description = "用户编号", example = "1", required = true)
CommonResult<Boolean> deleteUserDeptByUserId(@RequestParam("userId") Long userId);
@DeleteMapping(PREFIX + "/delete-by-dept-id")
@Operation(summary = "通过部门ID删除用户部门关系")
@Parameter(name = "deptId", description = "部门编号", example = "1", required = true)
CommonResult<Boolean> deleteUserDeptByDeptId(@RequestParam("deptId") Long deptId);
}

View File

@@ -0,0 +1,31 @@
package com.zt.plat.module.system.api.userdept.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 用户部门关系 Response DTO
*
* @author ZT
*/
@Schema(description = "RPC 服务 - 用户部门关系 Response DTO")
@Data
public class UserDeptRespDTO {
@Schema(description = "关系编号", example = "1024")
private Long id;
@Schema(description = "用户编号", example = "1")
private Long userId;
@Schema(description = "部门编号", example = "100")
private Long deptId;
@Schema(description = "备注", example = "主部门")
private String remark;
@Schema(description = "创建时间")
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,26 @@
package com.zt.plat.module.system.api.userdept.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 用户部门关系创建/修改 Request DTO
*
* @author ZT
*/
@Schema(description = "RPC 服务 - 用户部门关系创建/修改 Request DTO")
@Data
public class UserDeptSaveReqDTO {
@Schema(description = "关系编号", example = "1024")
private Long id;
@Schema(description = "用户编号", example = "1", required = true)
private Long userId;
@Schema(description = "部门编号", example = "100", required = true)
private Long deptId;
@Schema(description = "备注", example = "主部门")
private String remark;
}

View File

@@ -0,0 +1,64 @@
package com.zt.plat.module.system.api.userpost;
import com.zt.plat.framework.common.pojo.CommonResult;
import com.zt.plat.module.system.api.userpost.dto.UserPostRespDTO;
import com.zt.plat.module.system.api.userpost.dto.UserPostSaveReqDTO;
import com.zt.plat.module.system.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import java.util.List;
/**
* 用户-岗位关系 Feign API
*
* @author ZT
*/
@FeignClient(name = ApiConstants.NAME)
@Tag(name = "RPC 服务 - 用户岗位关系")
public interface UserPostApi {
String PREFIX = ApiConstants.PREFIX + "/user-post";
@PostMapping(PREFIX + "/create")
@Operation(summary = "新增用户岗位关系")
CommonResult<Long> createUserPost(@RequestBody UserPostSaveReqDTO reqVO);
@PutMapping(PREFIX + "/update")
@Operation(summary = "修改用户岗位关系")
CommonResult<Boolean> updateUserPost(@RequestBody UserPostSaveReqDTO reqVO);
@DeleteMapping(PREFIX + "/delete")
@Operation(summary = "删除用户岗位关系")
@Parameter(name = "id", description = "关系编号", example = "1", required = true)
CommonResult<Boolean> deleteUserPost(@RequestParam("id") Long id);
@GetMapping(PREFIX + "/get")
@Operation(summary = "通过ID查询用户岗位关系")
@Parameter(name = "id", description = "关系编号", example = "1", required = true)
CommonResult<UserPostRespDTO> getUserPost(@RequestParam("id") Long id);
@GetMapping(PREFIX + "/list-by-user-id")
@Operation(summary = "通过用户ID查询用户岗位关系列表")
@Parameter(name = "userId", description = "用户编号", example = "1", required = true)
CommonResult<List<UserPostRespDTO>> getUserPostListByUserId(@RequestParam("userId") Long userId);
@GetMapping(PREFIX + "/list-by-post-id")
@Operation(summary = "通过岗位ID查询用户岗位关系列表")
@Parameter(name = "postId", description = "岗位编号", example = "1", required = true)
CommonResult<List<UserPostRespDTO>> getUserPostListByPostId(@RequestParam("postId") Long postId);
@DeleteMapping(PREFIX + "/delete-by-user-id")
@Operation(summary = "通过用户ID删除用户岗位关系")
@Parameter(name = "userId", description = "用户编号", example = "1", required = true)
CommonResult<Boolean> deleteUserPostByUserId(@RequestParam("userId") Long userId);
@DeleteMapping(PREFIX + "/delete-by-post-id")
@Operation(summary = "通过岗位ID删除用户岗位关系")
@Parameter(name = "postId", description = "岗位编号", example = "1", required = true)
CommonResult<Boolean> deleteUserPostByPostId(@RequestParam("postId") Long postId);
}

View File

@@ -0,0 +1,28 @@
package com.zt.plat.module.system.api.userpost.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 用户岗位关系 Response DTO
*
* @author ZT
*/
@Schema(description = "RPC 服务 - 用户岗位关系 Response DTO")
@Data
public class UserPostRespDTO {
@Schema(description = "关系编号", example = "1024")
private Long id;
@Schema(description = "用户编号", example = "1")
private Long userId;
@Schema(description = "岗位编号", example = "100")
private Long postId;
@Schema(description = "创建时间")
private LocalDateTime createTime;
}

View File

@@ -0,0 +1,23 @@
package com.zt.plat.module.system.api.userpost.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
/**
* 用户岗位关系创建/修改 Request DTO
*
* @author ZT
*/
@Schema(description = "RPC 服务 - 用户岗位关系创建/修改 Request DTO")
@Data
public class UserPostSaveReqDTO {
@Schema(description = "关系编号", example = "1024")
private Long id;
@Schema(description = "用户编号", example = "1", required = true)
private Long userId;
@Schema(description = "岗位编号", example = "100", required = true)
private Long postId;
}

View File

@@ -35,34 +35,16 @@ public class DatabusUserDeptProviderApiImpl implements DatabusUserDeptProviderAp
@Override @Override
public CommonResult<CursorPageResult<DatabusUserDeptData>> getPageByCursor(CursorPageReqDTO reqDTO) { public CommonResult<CursorPageResult<DatabusUserDeptData>> getPageByCursor(CursorPageReqDTO reqDTO) {
// 构建游标查询条件
LambdaQueryWrapper<UserDeptDO> queryWrapper = new LambdaQueryWrapper<>();
// 游标条件create_time > cursorTime OR (create_time = cursorTime AND id > cursorId)
if (!reqDTO.isFirstPage()) {
queryWrapper.and(w -> w
.gt(UserDeptDO::getCreateTime, reqDTO.getCursorTime())
.or(o -> o
.eq(UserDeptDO::getCreateTime, reqDTO.getCursorTime())
.gt(UserDeptDO::getId, reqDTO.getCursorId())
)
);
}
// 租户过滤(如果指定)
if (reqDTO.getTenantId() != null) {
queryWrapper.eq(UserDeptDO::getTenantId, reqDTO.getTenantId());
}
// 按 create_time, id 升序排列,确保顺序稳定
queryWrapper.orderByAsc(UserDeptDO::getCreateTime)
.orderByAsc(UserDeptDO::getId);
// 多查一条判断是否有更多数据 // 多查一条判断是否有更多数据
int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100; int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100;
queryWrapper.last("LIMIT " + (limit + 1));
List<UserDeptDO> list = userDeptMapper.selectList(queryWrapper); // ⚠️ 使用关联查询,只查询 userSource = 2 的用户的部门关系
List<UserDeptDO> list = userDeptMapper.selectPageByCursorWithUserSource(
reqDTO.isFirstPage() ? null : reqDTO.getCursorTime(),
reqDTO.isFirstPage() ? null : reqDTO.getCursorId(),
reqDTO.getTenantId(),
limit + 1
);
// 判断是否有更多 // 判断是否有更多
boolean hasMore = list.size() > limit; boolean hasMore = list.size() > limit;
@@ -82,14 +64,11 @@ public class DatabusUserDeptProviderApiImpl implements DatabusUserDeptProviderAp
// 获取最后一条数据的游标 // 获取最后一条数据的游标
UserDeptDO last = list.get(list.size() - 1); UserDeptDO last = list.get(list.size() - 1);
// 首次查询时返总数 // 首次查询时返<EFBFBD><EFBFBD><EFBFBD>总数
Long total = null; Long total = null;
if (reqDTO.isFirstPage()) { if (reqDTO.isFirstPage()) {
LambdaQueryWrapper<UserDeptDO> countWrapper = new LambdaQueryWrapper<>(); // ⚠️ 只统计 userSource = 2 的用户的部门关系
if (reqDTO.getTenantId() != null) { total = userDeptMapper.countWithUserSource(reqDTO.getTenantId());
countWrapper.eq(UserDeptDO::getTenantId, reqDTO.getTenantId());
}
total = userDeptMapper.selectCount(countWrapper);
} }
return success(CursorPageResult.of( return success(CursorPageResult.of(
@@ -128,11 +107,8 @@ public class DatabusUserDeptProviderApiImpl implements DatabusUserDeptProviderAp
@Override @Override
public CommonResult<Long> count(Long tenantId) { public CommonResult<Long> count(Long tenantId) {
LambdaQueryWrapper<UserDeptDO> queryWrapper = new LambdaQueryWrapper<>(); // ⚠️ 只统计 userSource = 2 的用户的部门关系
if (tenantId != null) { return success(userDeptMapper.countWithUserSource(tenantId));
queryWrapper.eq(UserDeptDO::getTenantId, tenantId);
}
return success(userDeptMapper.selectCount(queryWrapper));
} }
/** /**

View File

@@ -35,31 +35,16 @@ public class DatabusUserPostProviderApiImpl implements DatabusUserPostProviderAp
@Override @Override
public CommonResult<CursorPageResult<DatabusUserPostData>> getPageByCursor(CursorPageReqDTO reqDTO) { public CommonResult<CursorPageResult<DatabusUserPostData>> getPageByCursor(CursorPageReqDTO reqDTO) {
// 构建游标查询条件
LambdaQueryWrapper<UserPostDO> queryWrapper = new LambdaQueryWrapper<>();
// 游标条件create_time > cursorTime OR (create_time = cursorTime AND id > cursorId)
if (!reqDTO.isFirstPage()) {
queryWrapper.and(w -> w
.gt(UserPostDO::getCreateTime, reqDTO.getCursorTime())
.or(o -> o
.eq(UserPostDO::getCreateTime, reqDTO.getCursorTime())
.gt(UserPostDO::getId, reqDTO.getCursorId())
)
);
}
// 注意UserPostDO 没有租户字段,忽略 tenantId 过滤
// 按 create_time, id 升序排列,确保顺序稳定
queryWrapper.orderByAsc(UserPostDO::getCreateTime)
.orderByAsc(UserPostDO::getId);
// 多查一条判断是否有更多数据 // 多查一条判断是否有更多数据
int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100; int limit = reqDTO.getBatchSize() != null ? reqDTO.getBatchSize() : 100;
queryWrapper.last("LIMIT " + (limit + 1));
List<UserPostDO> list = userPostMapper.selectList(queryWrapper); // ⚠️ 使用关联查询,只查询 userSource = 2 的用户的岗位关系
List<UserPostDO> list = userPostMapper.selectPageByCursorWithUserSource(
reqDTO.isFirstPage() ? null : reqDTO.getCursorTime(),
reqDTO.isFirstPage() ? null : reqDTO.getCursorId(),
reqDTO.getTenantId(),
limit + 1
);
// 判断是否有更多 // 判断是否有更多
boolean hasMore = list.size() > limit; boolean hasMore = list.size() > limit;
@@ -82,7 +67,8 @@ public class DatabusUserPostProviderApiImpl implements DatabusUserPostProviderAp
// 首次查询时返回总数 // 首次查询时返回总数
Long total = null; Long total = null;
if (reqDTO.isFirstPage()) { if (reqDTO.isFirstPage()) {
total = userPostMapper.selectCount(new LambdaQueryWrapper<>()); // ⚠️ 只统计 userSource = 2 的用户的岗位关系
total = userPostMapper.countWithUserSource(reqDTO.getTenantId());
} }
return success(CursorPageResult.of( return success(CursorPageResult.of(
@@ -121,8 +107,8 @@ public class DatabusUserPostProviderApiImpl implements DatabusUserPostProviderAp
@Override @Override
public CommonResult<Long> count(Long tenantId) { public CommonResult<Long> count(Long tenantId) {
// 注意UserPostDO 没有租户字段,返回全量总数 // ⚠️ 只统计 userSource = 2 的用户的岗位关系
return success(userPostMapper.selectCount(new LambdaQueryWrapper<>())); return success(userPostMapper.countWithUserSource(tenantId));
} }
/** /**

View File

@@ -54,6 +54,9 @@ public class DatabusUserProviderApiImpl implements DatabusUserProviderApi {
// 构建游标查询条件 // 构建游标查询条件
LambdaQueryWrapper<AdminUserDO> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AdminUserDO> queryWrapper = new LambdaQueryWrapper<>();
// ⚠️ 只同步 userSource = 2 的用户
queryWrapper.eq(AdminUserDO::getUserSource, 2);
// 游标条件create_time > cursorTime OR (create_time = cursorTime AND id > cursorId) // 游标条件create_time > cursorTime OR (create_time = cursorTime AND id > cursorId)
if (!reqDTO.isFirstPage()) { if (!reqDTO.isFirstPage()) {
queryWrapper.and(w -> w queryWrapper.and(w -> w
@@ -100,6 +103,8 @@ public class DatabusUserProviderApiImpl implements DatabusUserProviderApi {
Long total = null; Long total = null;
if (reqDTO.isFirstPage()) { if (reqDTO.isFirstPage()) {
LambdaQueryWrapper<AdminUserDO> countWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AdminUserDO> countWrapper = new LambdaQueryWrapper<>();
// ⚠️ 只统计 userSource = 2 的用户
countWrapper.eq(AdminUserDO::getUserSource, 2);
if (reqDTO.getTenantId() != null) { if (reqDTO.getTenantId() != null) {
countWrapper.eq(AdminUserDO::getTenantId, reqDTO.getTenantId()); countWrapper.eq(AdminUserDO::getTenantId, reqDTO.getTenantId());
} }
@@ -143,6 +148,8 @@ public class DatabusUserProviderApiImpl implements DatabusUserProviderApi {
@Override @Override
public CommonResult<Long> count(Long tenantId) { public CommonResult<Long> count(Long tenantId) {
LambdaQueryWrapper<AdminUserDO> queryWrapper = new LambdaQueryWrapper<>(); LambdaQueryWrapper<AdminUserDO> queryWrapper = new LambdaQueryWrapper<>();
// ⚠️ 只统计 userSource = 2 的用户
queryWrapper.eq(AdminUserDO::getUserSource, 2);
if (tenantId != null) { if (tenantId != null) {
queryWrapper.eq(AdminUserDO::getTenantId, tenantId); queryWrapper.eq(AdminUserDO::getTenantId, tenantId);
} }

View File

@@ -5,7 +5,10 @@ import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.system.dal.dataobject.dept.UserPostDO; import com.zt.plat.module.system.dal.dataobject.dept.UserPostDO;
import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
@@ -29,4 +32,44 @@ public interface UserPostMapper extends BaseMapperX<UserPostDO> {
default void deleteByUserId(Long userId) { default void deleteByUserId(Long userId) {
delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId)); delete(Wrappers.lambdaUpdate(UserPostDO.class).eq(UserPostDO::getUserId, userId));
} }
/**
* 游标分页查询用户-岗位关系(只查询 userSource = 2 的用户)
* @param cursorTime 游标时间
* @param cursorId 游标ID
* @param tenantId 租户ID可选
* @param limit 限制数量
* @return 用户岗位关系列表
*/
@Select("<script>" +
"SELECT up.* FROM system_user_post up " +
"INNER JOIN system_users u ON up.user_id = u.id " +
"WHERE u.user_source = 2 " +
"AND up.deleted = 0 " +
"<if test='tenantId != null'> AND up.tenant_id = #{tenantId} </if>" +
"<if test='cursorTime != null'>" +
" AND (up.create_time > #{cursorTime} " +
" OR (up.create_time = #{cursorTime} AND up.id > #{cursorId}))" +
"</if>" +
"ORDER BY up.create_time ASC, up.id ASC " +
"LIMIT #{limit}" +
"</script>")
List<UserPostDO> selectPageByCursorWithUserSource(@Param("cursorTime") LocalDateTime cursorTime,
@Param("cursorId") Long cursorId,
@Param("tenantId") Long tenantId,
@Param("limit") Integer limit);
/**
* 统计用户-岗位关系数量(只统计 userSource = 2 的用户)
* @param tenantId 租户ID可选
* @return 数量
*/
@Select("<script>" +
"SELECT COUNT(*) FROM system_user_post up " +
"INNER JOIN system_users u ON up.user_id = u.id " +
"WHERE u.user_source = 2 " +
"AND up.deleted = 0 " +
"<if test='tenantId != null'> AND up.tenant_id = #{tenantId} </if>" +
"</script>")
Long countWithUserSource(@Param("tenantId") Long tenantId);
} }

View File

@@ -4,7 +4,10 @@ import com.zt.plat.framework.mybatis.core.mapper.BaseMapperX;
import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX; import com.zt.plat.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.zt.plat.module.system.dal.dataobject.userdept.UserDeptDO; import com.zt.plat.module.system.dal.dataobject.userdept.UserDeptDO;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDateTime;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@@ -45,4 +48,44 @@ public interface UserDeptMapper extends BaseMapperX<UserDeptDO> {
); );
} }
/**
* 游标分页查询用户-部门关系(只查询 userSource = 2 的用户)
* @param cursorTime 游标时间
* @param cursorId 游标ID
* @param tenantId 租户ID可选
* @param limit 限制数量
* @return 用户部门关系列表
*/
@Select("<script>" +
"SELECT ud.* FROM system_user_dept ud " +
"INNER JOIN system_users u ON ud.user_id = u.id " +
"WHERE u.user_source = 2 " +
"AND ud.deleted = 0 " +
"<if test='tenantId != null'> AND ud.tenant_id = #{tenantId} </if>" +
"<if test='cursorTime != null'>" +
" AND (ud.create_time > #{cursorTime} " +
" OR (ud.create_time = #{cursorTime} AND ud.id > #{cursorId}))" +
"</if>" +
"ORDER BY ud.create_time ASC, ud.id ASC " +
"LIMIT #{limit}" +
"</script>")
List<UserDeptDO> selectPageByCursorWithUserSource(@Param("cursorTime") LocalDateTime cursorTime,
@Param("cursorId") Long cursorId,
@Param("tenantId") Long tenantId,
@Param("limit") Integer limit);
/**
* 统计用户-部门关系数量(只统计 userSource = 2 的用户)
* @param tenantId 租户ID可选
* @return 数量
*/
@Select("<script>" +
"SELECT COUNT(*) FROM system_user_dept ud " +
"INNER JOIN system_users u ON ud.user_id = u.id " +
"WHERE u.user_source = 2 " +
"AND ud.deleted = 0 " +
"<if test='tenantId != null'> AND ud.tenant_id = #{tenantId} </if>" +
"</script>")
Long countWithUserSource(@Param("tenantId") Long tenantId);
} }