刘帅
2026-09-17 b3221701c387ab536adf66cf315c75d3b429fe04
cloud-server-management/src/main/java/com/dsh/guns/modular/system/controller/system/UserMgrController.java
@@ -1,6 +1,7 @@
package com.dsh.guns.modular.system.controller.system;
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.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
@@ -331,6 +332,7 @@
    @BussinessLog(value = "添加管理员", key = "account", dict = UserDict.class)
    @Permission(Const.ADMIN_NAME)
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public Object add(@Valid UserDto user, BindingResult result) {
        if (result.hasErrors()) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
@@ -340,7 +342,10 @@
        if (StringUtils.isEmpty(user.getPhone())) {
            return 5002;
        }
        User theUser = userService.getOne(new QueryWrapper<User>().eq("phone", user.getPhone()));
        // 已逻辑删除的员工(status=3)不占用手机号,可用同一手机号重新创建员工。
        User theUser = userService.getOne(new QueryWrapper<User>()
                .eq("phone", user.getPhone())
                .ne("status", ManagerStatus.DELETED.getCode()));
        if (theUser != null || userService.getByAccount(user.getPhone()) != null){
            return 5001;
        }
@@ -404,8 +409,7 @@
        objectUser.setVersion(UserExt.getUser().getId());
        this.userService.save(objectUser);
        if (!assignedStores.isEmpty()) {
            userMapper.deleteStoreRelations(objectUser.getId());
            userMapper.insertStoreRelations(objectUser.getId(), assignedStores.stream().map(TStore::getId).collect(Collectors.toList()));
            replaceStoreRelations(objectUser.getId(), assignedStores);
            // 各所选门店同步负责人账号;旧系统仍可通过 storeStaffId 查询主负责人。
            for (TStore store : assignedStores) {
                store.setStoreStaffId(objectUser.getId());
@@ -433,7 +437,7 @@
        if (user.getPhone() != null && user.getPhone().contains("*")) {
            // The edit view displays a masked phone. Treat an unchanged masked
            // value as "keep original", never as a value to persist.
        } else if (!oldUser.getPhone().equals(user.getPhone())){
        } else if (!Objects.equals(oldUser.getPhone(), user.getPhone())) {
            User phone = userService.getOne(new QueryWrapper<User>().eq("phone", user.getPhone()));
            if (phone == null){
                oldUser.setPhone(user.getPhone());
@@ -452,12 +456,11 @@
        if (user.getStoreIds() != null) {
            List<TStore> assignedStores = validateStores(user.getStoreIds());
            if (assignedStores == null) return ResultUtil.error("所选门店不存在、已停用或不在当前账号管理范围内");
            userMapper.deleteStoreRelations(oldUser.getId());
            replaceStoreRelations(oldUser.getId(), assignedStores);
            if (!assignedStores.isEmpty()) {
                oldUser.setObjectType(3);
                oldUser.setObjectId(assignedStores.get(0).getId());
                oldUser.setRoleid("3");
                userMapper.insertStoreRelations(oldUser.getId(), assignedStores.stream().map(TStore::getId).collect(Collectors.toList()));
                for (TStore store : assignedStores) {
                    store.setStoreStaffId(oldUser.getId());
                    storeService.updateById(store);
@@ -498,6 +501,14 @@
        return stores;
    }
    /** 每条关联记录单独生成雪花主键,避免 MyBatis foreach 复用同一个绑定变量。 */
    private void replaceStoreRelations(Integer userId, List<TStore> stores) {
        userMapper.deleteStoreRelations(userId);
        for (TStore store : stores) {
            userMapper.insertStoreRelation(IdWorker.getId(), userId, store.getId());
        }
    }
    /**
     * 删除管理员(逻辑删除)
     * Delete administrator (logical deletion)