fix(企业微信): 修复用户同步逻辑中的用户删除处理

将用户删除逻辑从物理删除改为逻辑删除,通过将用户状态设置为禁用(enable=0)来实现
修复了之前被注释掉的用户删除逻辑,确保数据一致性
This commit is contained in:
dzq 2025-05-30 11:12:59 +08:00
parent a6c12859e3
commit 0ea23e7309
1 changed files with 141 additions and 131 deletions

View File

@ -33,6 +33,7 @@ import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
import com.agileboot.domain.qywx.user.QyUserApplicationService; import com.agileboot.domain.qywx.user.QyUserApplicationService;
import com.agileboot.domain.qywx.user.command.AddQyUserCommand; import com.agileboot.domain.qywx.user.command.AddQyUserCommand;
@ -418,6 +419,7 @@ public class QywxScheduleJob {
.collect(Collectors.toList()); .collect(Collectors.toList());
List<UserListResponse.UserInfo> wxUsers = new ArrayList<>();
for (QyDepartmentEntity department : departmentList) { for (QyDepartmentEntity department : departmentList) {
// 获取部门用户列表 // 获取部门用户列表
UserListResponse userList = QywxApiUtil.getUserList(accessToken.getAccessToken(), department.getDepartmentId()); UserListResponse userList = QywxApiUtil.getUserList(accessToken.getAccessToken(), department.getDepartmentId());
@ -431,9 +433,10 @@ public class QywxScheduleJob {
} }
log.info("获取部门用户列表成功: {}", JSONUtil.toJsonStr(userList)); log.info("获取部门用户列表成功: {}", JSONUtil.toJsonStr(userList));
List<UserListResponse.UserInfo> wxUsers = userList.getUserlist(); List<UserListResponse.UserInfo> wxUsersList = userList.getUserlist();
if (wxUsers == null || wxUsers.isEmpty()) { if (wxUsersList != null) {
continue; wxUsers.addAll(wxUsersList);
}
} }
Map<String, UserListResponse.UserInfo> wxUserMap = wxUsers.stream() Map<String, UserListResponse.UserInfo> wxUserMap = wxUsers.stream()
@ -455,10 +458,10 @@ public class QywxScheduleJob {
log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd)); log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd));
// 识别需要删除的用户 // 识别需要删除的用户
/* List<QyUserEntity> toRemove = qyUserList.stream() List<QyUserEntity> toRemove = qyUserList.stream()
.filter(localUser -> !wxUserMap.containsKey(localUser.getUserid())) .filter(localUser -> !wxUserMap.containsKey(localUser.getUserid()))
.collect(Collectors.toList()); .collect(Collectors.toList());
log.info("syncUserInfo toRemove: {}", JSONUtil.toJsonStr(toRemove));*/ log.info("syncUserInfo toRemove: {}", JSONUtil.toJsonStr(toRemove));
// 识别需要更新的用户 // 识别需要更新的用户
List<UpdateQyUserCommand> toUpdate = qyUserList.stream() List<UpdateQyUserCommand> toUpdate = qyUserList.stream()
@ -564,12 +567,19 @@ public class QywxScheduleJob {
} }
// 删除用户 // 删除用户
/*if (!toRemove.isEmpty()) { if (!toRemove.isEmpty()) {
BulkOperationCommand<Integer> command = new BulkOperationCommand<>( /*BulkOperationCommand<Integer> command = new BulkOperationCommand<>(
toRemove.stream().map(QyUserEntity::getId).collect(Collectors.toList()) toRemove.stream().map(QyUserEntity::getId).collect(Collectors.toList())
); );
qyUserApplicationService.deleteUser(command); qyUserApplicationService.deleteUser(command);*/
}*/ toRemove.stream()
.map(removeUser -> {
UpdateQyUserCommand deleteCommand = new UpdateQyUserCommand();
deleteCommand.setId(removeUser.getId());
deleteCommand.setEnable("0");
return deleteCommand;
})
.forEach(qyUserApplicationService::updateUser);
} }
} catch (Exception e) { } catch (Exception e) {
log.error("syncUserInfo error", e); log.error("syncUserInfo error", e);