刘帅
2026-09-17 45dccc43c06c4fe96c406f24a8da331e7c816971
feat&bug:员工和门店手机号同步
1个文件已修改
49 ■■■■■ 已修改文件
cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/system/UserMgrController.java 49 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/system/UserMgrController.java
@@ -3,6 +3,7 @@
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dsh.course.mapper.UserMapper;
@@ -410,11 +411,7 @@
        this.userService.save(objectUser);
        if (!assignedStores.isEmpty()) {
            replaceStoreRelations(objectUser.getId(), assignedStores);
            // 各所选门店同步负责人账号;旧系统仍可通过 storeStaffId 查询主负责人。
            for (TStore store : assignedStores) {
                store.setStoreStaffId(objectUser.getId());
                storeService.updateById(store);
            }
            syncStoreManagers(objectUser, Collections.<Integer>emptyList(), assignedStores);
        }
        return SUCCESS_TIP;
    }
@@ -454,6 +451,7 @@
        if (user.getAvatar()!=null && (!user.getAvatar().equals("")))
        oldUser.setAvatar(user.getAvatar());
        if (user.getStoreIds() != null) {
            List<Integer> previousStoreIds = userMapper.getStoreIdsByUserId(oldUser.getId());
            List<TStore> assignedStores = validateStores(user.getStoreIds());
            if (assignedStores == null) return ResultUtil.error("所选门店不存在、已停用或不在当前账号管理范围内");
            replaceStoreRelations(oldUser.getId(), assignedStores);
@@ -461,13 +459,20 @@
                oldUser.setObjectType(3);
                oldUser.setObjectId(assignedStores.get(0).getId());
                oldUser.setRoleid("3");
                for (TStore store : assignedStores) {
                    store.setStoreStaffId(oldUser.getId());
                    storeService.updateById(store);
                }
            } else if (Integer.valueOf(3).equals(oldUser.getObjectType())) {
                // 清除旧字段关联,避免已移除的首个门店仍被当作该员工所属门店。
                oldUser.setObjectId(null);
            }
            syncStoreManagers(oldUser, previousStoreIds, assignedStores);
        }
        this.userService.updateById(oldUser);
        // 门店表仅保存店长 ID 和联系电话;店长名称由 storeStaffId 关联 sys_user.name 展示。
        // 员工资料修改但门店未修改时,也要将其手机号同步给当前负责的门店。
        if (user.getStoreIds() == null) {
            storeService.update(new LambdaUpdateWrapper<TStore>()
                    .eq(TStore::getStoreStaffId, oldUser.getId())
                    .set(TStore::getPhone, oldUser.getPhone()));
        }
        return SUCCESS_TIP;
    }
@@ -510,6 +515,32 @@
    }
    /**
     * 同步员工与门店的负责人信息。门店移除时,只清理仍由该员工负责的门店,
     * 防止并发或后续操作已为门店设置新店长时被错误覆盖。
     */
    private void syncStoreManagers(User user, List<Integer> previousStoreIds, List<TStore> assignedStores) {
        Set<Integer> assignedStoreIds = assignedStores.stream()
                .map(TStore::getId)
                .collect(Collectors.toSet());
        for (Integer previousStoreId : previousStoreIds) {
            if (!assignedStoreIds.contains(previousStoreId)) {
                storeService.update(new LambdaUpdateWrapper<TStore>()
                        .eq(TStore::getId, previousStoreId)
                        .eq(TStore::getStoreStaffId, user.getId())
                        .set(TStore::getStoreStaffId, null)
                        .set(TStore::getPhone, null));
            }
        }
        for (TStore store : assignedStores) {
            store.setStoreStaffId(user.getId());
            store.setPhone(user.getPhone());
            storeService.updateById(store);
        }
    }
    /**
     * 删除管理员(逻辑删除)
     * Delete administrator (logical deletion)
     */