From 0ea23e7309fa6b19ea72825d97579c2e096f5eba Mon Sep 17 00:00:00 2001
From: dzq <dzq@ys.com>
Date: Fri, 30 May 2025 11:12:59 +0800
Subject: [PATCH] =?UTF-8?q?fix(=E4=BC=81=E4=B8=9A=E5=BE=AE=E4=BF=A1):=20?=
 =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7=E5=90=8C=E6=AD=A5=E9=80=BB?=
 =?UTF-8?q?=E8=BE=91=E4=B8=AD=E7=9A=84=E7=94=A8=E6=88=B7=E5=88=A0=E9=99=A4?=
 =?UTF-8?q?=E5=A4=84=E7=90=86?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

将用户删除逻辑从物理删除改为逻辑删除,通过将用户状态设置为禁用(enable=0)来实现
修复了之前被注释掉的用户删除逻辑,确保数据一致性
---
 .../customize/service/QywxScheduleJob.java    | 272 +++++++++---------
 1 file changed, 141 insertions(+), 131 deletions(-)

diff --git a/agileboot-admin/src/main/java/com/agileboot/admin/customize/service/QywxScheduleJob.java b/agileboot-admin/src/main/java/com/agileboot/admin/customize/service/QywxScheduleJob.java
index 91f68ab..69431c8 100644
--- a/agileboot-admin/src/main/java/com/agileboot/admin/customize/service/QywxScheduleJob.java
+++ b/agileboot-admin/src/main/java/com/agileboot/admin/customize/service/QywxScheduleJob.java
@@ -33,6 +33,7 @@ import java.util.Map;
 import java.util.Objects;
 import java.util.function.Function;
 import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 import com.agileboot.domain.qywx.user.QyUserApplicationService;
 import com.agileboot.domain.qywx.user.command.AddQyUserCommand;
@@ -418,6 +419,7 @@ public class QywxScheduleJob {
                     .collect(Collectors.toList());
 
 
+            List<UserListResponse.UserInfo> wxUsers = new ArrayList<>();
             for (QyDepartmentEntity department : departmentList) {
                 // 获取部门用户列表
                 UserListResponse userList = QywxApiUtil.getUserList(accessToken.getAccessToken(), department.getDepartmentId());
@@ -430,146 +432,154 @@ public class QywxScheduleJob {
                     continue;
                 }
                 log.info("获取部门用户列表成功: {}", JSONUtil.toJsonStr(userList));
-                
-                List<UserListResponse.UserInfo> wxUsers = userList.getUserlist();
-                if (wxUsers == null || wxUsers.isEmpty()) {
-                    continue;
+
+                List<UserListResponse.UserInfo> wxUsersList = userList.getUserlist();
+                if (wxUsersList != null) {
+                    wxUsers.addAll(wxUsersList);
                 }
+            }
 
-                Map<String, UserListResponse.UserInfo> wxUserMap = wxUsers.stream()
-                    .collect(Collectors.toMap(UserListResponse.UserInfo::getUserid, Function.identity()));
+            Map<String, UserListResponse.UserInfo> wxUserMap = wxUsers.stream()
+                .collect(Collectors.toMap(UserListResponse.UserInfo::getUserid, Function.identity()));
 
-                List<QyUserEntity> qyUserList = qyUserApplicationService.selectAll();
-                if (null == qyUserList) {
-                    qyUserList = new ArrayList<>();
-                }
-                qyUserList = qyUserList.stream()
-                        .filter(u -> u.getCorpid().equals(authCorpInfo.getCorpid()) && u.getAppid().equals(appid))
-                        .collect(Collectors.toList());
-
-                // 识别需要新增的用户
-                List<QyUserEntity> finalQyUserList = qyUserList;
-                List<UserListResponse.UserInfo> toAdd = wxUsers.stream()
-                    .filter(wxUser -> finalQyUserList.stream().noneMatch(u -> u.getUserid().equals(wxUser.getUserid())))
+            List<QyUserEntity> qyUserList = qyUserApplicationService.selectAll();
+            if (null == qyUserList) {
+                qyUserList = new ArrayList<>();
+            }
+            qyUserList = qyUserList.stream()
+                    .filter(u -> u.getCorpid().equals(authCorpInfo.getCorpid()) && u.getAppid().equals(appid))
                     .collect(Collectors.toList());
-                log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd));
 
-                // 识别需要删除的用户
-/*                List<QyUserEntity> toRemove = qyUserList.stream()
-                    .filter(localUser -> !wxUserMap.containsKey(localUser.getUserid()))
-                    .collect(Collectors.toList());
-                log.info("syncUserInfo toRemove: {}", JSONUtil.toJsonStr(toRemove));*/
+            // 识别需要新增的用户
+            List<QyUserEntity> finalQyUserList = qyUserList;
+            List<UserListResponse.UserInfo> toAdd = wxUsers.stream()
+                .filter(wxUser -> finalQyUserList.stream().noneMatch(u -> u.getUserid().equals(wxUser.getUserid())))
+                .collect(Collectors.toList());
+            log.info("syncUserInfo toAdd: {}", JSONUtil.toJsonStr(toAdd));
 
-                // 识别需要更新的用户
-                List<UpdateQyUserCommand> toUpdate = qyUserList.stream()
-                    .filter(localUser -> wxUserMap.containsKey(localUser.getUserid()))
-                    .filter(localUser -> {
-                        UserListResponse.UserInfo wxUser = wxUserMap.get(localUser.getUserid());
-                        return !Objects.equals(localUser.getName(), wxUser.getName())
-                            || !Objects.equals(localUser.getDepartment(), StringUtils.join(wxUser.getDepartment(), ","))
-                            || !Objects.equals(localUser.getPosition(), wxUser.getPosition())
-                            || !Objects.equals(localUser.getUserOrder(), StringUtils.join(wxUser.getOrder(), ","))
-                            || !Objects.equals(localUser.getMobile(), wxUser.getMobile())
-                            || !Objects.equals(localUser.getGender(), String.valueOf(wxUser.getGender()))
-                            || !Objects.equals(localUser.getEmail(), wxUser.getEmail())
-                            || !Objects.equals(localUser.getBizMail(), wxUser.getBiz_mail())
-                            || !Objects.equals(localUser.getDirectLeader(), StringUtils.join(wxUser.getDirect_leader(), ","))
-                            || !Objects.equals(localUser.getIsLeaderInDept(), StringUtils.join(wxUser.getIs_leader_in_dept(), ","))
-                            || !Objects.equals(localUser.getTelephone(), wxUser.getTelephone())
-                            || !Objects.equals(localUser.getAlias(), wxUser.getAlias())
-                            || !Objects.equals(localUser.getAddress(), wxUser.getAddress())
-                            || !Objects.equals(localUser.getMainDepartment(), String.valueOf(wxUser.getMain_department()))
-                            || !Objects.equals(localUser.getAvatar(), wxUser.getAvatar())
-                            || !Objects.equals(localUser.getThumbAvatar(), wxUser.getThumb_avatar())
-                            || !Objects.equals(localUser.getStatus(), String.valueOf(wxUser.getStatus()))
-                            || !Objects.equals(localUser.getQrCode(), wxUser.getQr_code())
-                            || !Objects.equals(localUser.getExternalPosition(), wxUser.getExternal_position());
-                    })
-                    .peek(localUser -> {
-                        UserListResponse.UserInfo wxUser = wxUserMap.get(localUser.getUserid());
-                        localUser.setName(wxUser.getName());
-                        localUser.setDepartment(StringUtils.join(wxUser.getDepartment(), ","));
-                        localUser.setUserOrder(StringUtils.join(wxUser.getOrder(), ","));
-                        localUser.setPosition(wxUser.getPosition());
-                        localUser.setMobile(wxUser.getMobile());
-                        localUser.setGender(String.valueOf(wxUser.getGender()));
-                        localUser.setEmail(wxUser.getEmail());
-                        localUser.setBizMail(wxUser.getBiz_mail());
-                        localUser.setDirectLeader(StringUtils.join(wxUser.getDirect_leader(), ","));
-                        localUser.setIsLeaderInDept(StringUtils.join(wxUser.getIs_leader_in_dept(), ","));
-                        localUser.setTelephone(wxUser.getTelephone());
-                        localUser.setAlias(wxUser.getAlias());
-                        localUser.setAddress(wxUser.getAddress());
-                        localUser.setMainDepartment(String.valueOf(wxUser.getMain_department()));
-                        localUser.setAvatar(wxUser.getAvatar());
-                        localUser.setThumbAvatar(wxUser.getThumb_avatar());
-                        localUser.setStatus(String.valueOf(wxUser.getStatus()));
-                        localUser.setQrCode(wxUser.getQr_code());
-                        localUser.setExternalPosition(wxUser.getExternal_position());
-                        localUser.setUpdateTime(new Date());
-                    })
-                    .map(localUser -> {
-                        UpdateQyUserCommand command = new UpdateQyUserCommand();
-                        BeanUtils.copyProperties(localUser, command);
-                        return command;
-                    })
-                    .collect(Collectors.toList());
-                log.info("syncUserInfo toUpdate: {}", JSONUtil.toJsonStr(toUpdate));
+            // 识别需要删除的用户
+            List<QyUserEntity> toRemove = qyUserList.stream()
+                .filter(localUser -> !wxUserMap.containsKey(localUser.getUserid()))
+                .collect(Collectors.toList());
+            log.info("syncUserInfo toRemove: {}", JSONUtil.toJsonStr(toRemove));
 
-                // 新增用户
-                if (!toAdd.isEmpty()) {
-                    toAdd.forEach(wxUser -> {
-                        AddQyUserCommand newUser = new AddQyUserCommand();
-                        newUser.setUserid(wxUser.getUserid());
-                        newUser.setOpenUserid(wxUser.getOpen_userid());
-                        newUser.setName(wxUser.getName());
-                        newUser.setDepartment(StringUtils.join(wxUser.getDepartment(), ","));
-                        newUser.setUserOrder(StringUtils.join(wxUser.getOrder(), ","));
-                        newUser.setPosition(wxUser.getPosition());
-                        newUser.setMobile(wxUser.getMobile());
-                        newUser.setGender(String.valueOf(wxUser.getGender()));
-                        newUser.setEmail(wxUser.getEmail());
-                        newUser.setBizMail(wxUser.getBiz_mail());
-                        newUser.setDirectLeader(StringUtils.join(wxUser.getDirect_leader(), ","));
-                        newUser.setIsLeaderInDept(StringUtils.join(wxUser.getIs_leader_in_dept(), ","));
-                        newUser.setTelephone(wxUser.getTelephone());
-                        newUser.setAlias(wxUser.getAlias());
-                        newUser.setAddress(wxUser.getAddress());
-                        newUser.setMainDepartment(String.valueOf(wxUser.getMain_department()));
-                        newUser.setAvatar(wxUser.getAvatar());
-                        newUser.setThumbAvatar(wxUser.getThumb_avatar());
-                        newUser.setStatus(String.valueOf(wxUser.getStatus()));
-                        newUser.setQrCode(wxUser.getQr_code());
-                        newUser.setExternalPosition(wxUser.getExternal_position());
-                        newUser.setEnable(String.valueOf(1));
-                        newUser.setCorpid(authCorpInfo.getCorpid());
-                        newUser.setAppid(appid);
-                        newUser.setDeleted(false);
-                        newUser.setCreatorId(0L);
-                        newUser.setCreateTime(new Date());
-                        newUser.setUpdaterId(0L);
-                        newUser.setUpdateTime(new Date());
-                        // 北流铜州医院默认余额1000
-                        if (WeixinConstants.corpid.equals(newUser.getCorpid())) {
-                            newUser.setBalanceLimit(BigDecimal.valueOf(1000L));
-                            newUser.setBalance(BigDecimal.valueOf(1000L));
-                        }
-                        qyUserApplicationService.addUser(newUser);
-                    });
-                }
+            // 识别需要更新的用户
+            List<UpdateQyUserCommand> toUpdate = qyUserList.stream()
+                .filter(localUser -> wxUserMap.containsKey(localUser.getUserid()))
+                .filter(localUser -> {
+                    UserListResponse.UserInfo wxUser = wxUserMap.get(localUser.getUserid());
+                    return !Objects.equals(localUser.getName(), wxUser.getName())
+                        || !Objects.equals(localUser.getDepartment(), StringUtils.join(wxUser.getDepartment(), ","))
+                        || !Objects.equals(localUser.getPosition(), wxUser.getPosition())
+                        || !Objects.equals(localUser.getUserOrder(), StringUtils.join(wxUser.getOrder(), ","))
+                        || !Objects.equals(localUser.getMobile(), wxUser.getMobile())
+                        || !Objects.equals(localUser.getGender(), String.valueOf(wxUser.getGender()))
+                        || !Objects.equals(localUser.getEmail(), wxUser.getEmail())
+                        || !Objects.equals(localUser.getBizMail(), wxUser.getBiz_mail())
+                        || !Objects.equals(localUser.getDirectLeader(), StringUtils.join(wxUser.getDirect_leader(), ","))
+                        || !Objects.equals(localUser.getIsLeaderInDept(), StringUtils.join(wxUser.getIs_leader_in_dept(), ","))
+                        || !Objects.equals(localUser.getTelephone(), wxUser.getTelephone())
+                        || !Objects.equals(localUser.getAlias(), wxUser.getAlias())
+                        || !Objects.equals(localUser.getAddress(), wxUser.getAddress())
+                        || !Objects.equals(localUser.getMainDepartment(), String.valueOf(wxUser.getMain_department()))
+                        || !Objects.equals(localUser.getAvatar(), wxUser.getAvatar())
+                        || !Objects.equals(localUser.getThumbAvatar(), wxUser.getThumb_avatar())
+                        || !Objects.equals(localUser.getStatus(), String.valueOf(wxUser.getStatus()))
+                        || !Objects.equals(localUser.getQrCode(), wxUser.getQr_code())
+                        || !Objects.equals(localUser.getExternalPosition(), wxUser.getExternal_position());
+                })
+                .peek(localUser -> {
+                    UserListResponse.UserInfo wxUser = wxUserMap.get(localUser.getUserid());
+                    localUser.setName(wxUser.getName());
+                    localUser.setDepartment(StringUtils.join(wxUser.getDepartment(), ","));
+                    localUser.setUserOrder(StringUtils.join(wxUser.getOrder(), ","));
+                    localUser.setPosition(wxUser.getPosition());
+                    localUser.setMobile(wxUser.getMobile());
+                    localUser.setGender(String.valueOf(wxUser.getGender()));
+                    localUser.setEmail(wxUser.getEmail());
+                    localUser.setBizMail(wxUser.getBiz_mail());
+                    localUser.setDirectLeader(StringUtils.join(wxUser.getDirect_leader(), ","));
+                    localUser.setIsLeaderInDept(StringUtils.join(wxUser.getIs_leader_in_dept(), ","));
+                    localUser.setTelephone(wxUser.getTelephone());
+                    localUser.setAlias(wxUser.getAlias());
+                    localUser.setAddress(wxUser.getAddress());
+                    localUser.setMainDepartment(String.valueOf(wxUser.getMain_department()));
+                    localUser.setAvatar(wxUser.getAvatar());
+                    localUser.setThumbAvatar(wxUser.getThumb_avatar());
+                    localUser.setStatus(String.valueOf(wxUser.getStatus()));
+                    localUser.setQrCode(wxUser.getQr_code());
+                    localUser.setExternalPosition(wxUser.getExternal_position());
+                    localUser.setUpdateTime(new Date());
+                })
+                .map(localUser -> {
+                    UpdateQyUserCommand command = new UpdateQyUserCommand();
+                    BeanUtils.copyProperties(localUser, command);
+                    return command;
+                })
+                .collect(Collectors.toList());
+            log.info("syncUserInfo toUpdate: {}", JSONUtil.toJsonStr(toUpdate));
 
-                // 更新用户
-                if (!toUpdate.isEmpty()) {
-                    toUpdate.forEach(qyUserApplicationService::updateUser);
-                }
+            // 新增用户
+            if (!toAdd.isEmpty()) {
+                toAdd.forEach(wxUser -> {
+                    AddQyUserCommand newUser = new AddQyUserCommand();
+                    newUser.setUserid(wxUser.getUserid());
+                    newUser.setOpenUserid(wxUser.getOpen_userid());
+                    newUser.setName(wxUser.getName());
+                    newUser.setDepartment(StringUtils.join(wxUser.getDepartment(), ","));
+                    newUser.setUserOrder(StringUtils.join(wxUser.getOrder(), ","));
+                    newUser.setPosition(wxUser.getPosition());
+                    newUser.setMobile(wxUser.getMobile());
+                    newUser.setGender(String.valueOf(wxUser.getGender()));
+                    newUser.setEmail(wxUser.getEmail());
+                    newUser.setBizMail(wxUser.getBiz_mail());
+                    newUser.setDirectLeader(StringUtils.join(wxUser.getDirect_leader(), ","));
+                    newUser.setIsLeaderInDept(StringUtils.join(wxUser.getIs_leader_in_dept(), ","));
+                    newUser.setTelephone(wxUser.getTelephone());
+                    newUser.setAlias(wxUser.getAlias());
+                    newUser.setAddress(wxUser.getAddress());
+                    newUser.setMainDepartment(String.valueOf(wxUser.getMain_department()));
+                    newUser.setAvatar(wxUser.getAvatar());
+                    newUser.setThumbAvatar(wxUser.getThumb_avatar());
+                    newUser.setStatus(String.valueOf(wxUser.getStatus()));
+                    newUser.setQrCode(wxUser.getQr_code());
+                    newUser.setExternalPosition(wxUser.getExternal_position());
+                    newUser.setEnable(String.valueOf(1));
+                    newUser.setCorpid(authCorpInfo.getCorpid());
+                    newUser.setAppid(appid);
+                    newUser.setDeleted(false);
+                    newUser.setCreatorId(0L);
+                    newUser.setCreateTime(new Date());
+                    newUser.setUpdaterId(0L);
+                    newUser.setUpdateTime(new Date());
+                    // 北流铜州医院默认余额1000
+                    if (WeixinConstants.corpid.equals(newUser.getCorpid())) {
+                        newUser.setBalanceLimit(BigDecimal.valueOf(1000L));
+                        newUser.setBalance(BigDecimal.valueOf(1000L));
+                    }
+                    qyUserApplicationService.addUser(newUser);
+                });
+            }
 
-                // 删除用户
-                /*if (!toRemove.isEmpty()) {
-                    BulkOperationCommand<Integer> command = new BulkOperationCommand<>(
-                        toRemove.stream().map(QyUserEntity::getId).collect(Collectors.toList())
-                    );
-                    qyUserApplicationService.deleteUser(command);
-                }*/
+            // 更新用户
+            if (!toUpdate.isEmpty()) {
+                toUpdate.forEach(qyUserApplicationService::updateUser);
+            }
+
+            // 删除用户
+            if (!toRemove.isEmpty()) {
+                /*BulkOperationCommand<Integer> command = new BulkOperationCommand<>(
+                    toRemove.stream().map(QyUserEntity::getId).collect(Collectors.toList())
+                );
+                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) {
             log.error("syncUserInfo error", e);