소스 검색

商家pc端 分权限 第九笔提交

liudongzhi 2 달 전
부모
커밋
7d21ddcf24

+ 33 - 0
alien-store-platform/src/main/java/shop/alien/storeplatform/controller/StorePlatformUserRoleController.java

@@ -10,6 +10,7 @@ import shop.alien.entity.store.vo.SubAccountVo;
 import shop.alien.storeplatform.dto.AssignRolesDto;
 import shop.alien.storeplatform.dto.BatchDeleteSubAccountDto;
 import shop.alien.storeplatform.dto.CreateAccountDto;
+import shop.alien.storeplatform.dto.UpdateAccountDto;
 import shop.alien.storeplatform.service.StorePlatformUserRoleService;
 import shop.alien.storeplatform.annotation.PlatformOperationLog;
 
@@ -211,5 +212,37 @@ public class StorePlatformUserRoleController {
         return R.fail("批量删除失败");
     }
 
+    @ApiOperation("更新子账号信息")
+    @ApiOperationSupport(order = 10)
+    @PlatformOperationLog(
+            module = "账号操作记录",
+            type = "修改子账号",
+            content = "修改子账号(用户ID=#{#p0.userId},手机号:#{#p0.phone},账号名:#{#p0.accountName})"
+    )
+    @PostMapping("/updateSubAccount")
+    public R<String> updateSubAccount(@RequestBody UpdateAccountDto updateAccountDto) {
+        log.info("StorePlatformUserRoleController.updateSubAccount?userId={}, phone={}, accountName={}, storeId={}, roleId={}", 
+                updateAccountDto.getUserId(), updateAccountDto.getPhone(), updateAccountDto.getAccountName(), 
+                updateAccountDto.getStoreId(), updateAccountDto.getRoleId());
+        
+        if (updateAccountDto.getUserId() == null) {
+            return R.fail("用户ID不能为空");
+        }
+        if (updateAccountDto.getStoreId() == null) {
+            return R.fail("店铺ID不能为空");
+        }
+        
+        boolean result = storePlatformUserRoleService.updateSubAccount(
+                updateAccountDto.getUserId(),
+                updateAccountDto.getPhone(),
+                updateAccountDto.getAccountName(),
+                updateAccountDto.getStoreId(),
+                updateAccountDto.getRoleId());
+        if (result) {
+            return R.success("更新子账号信息成功");
+        }
+        return R.fail("更新子账号信息失败");
+    }
+
 }
 

+ 32 - 0
alien-store-platform/src/main/java/shop/alien/storeplatform/dto/UpdateAccountDto.java

@@ -0,0 +1,32 @@
+package shop.alien.storeplatform.dto;
+
+import io.swagger.annotations.ApiModel;
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+/**
+ * 更新子账号请求DTO
+ *
+ * @author system
+ * @since 2025-01-XX
+ */
+@Data
+@ApiModel(value = "UpdateAccountDto", description = "更新子账号请求参数")
+public class UpdateAccountDto {
+
+    @ApiModelProperty(value = "用户ID", required = true)
+    private Integer userId;
+
+    @ApiModelProperty(value = "手机号")
+    private String phone;
+
+    @ApiModelProperty(value = "账号名称")
+    private String accountName;
+
+    @ApiModelProperty(value = "店铺ID", required = true)
+    private Integer storeId;
+
+    @ApiModelProperty(value = "角色ID")
+    private Long roleId;
+}
+

+ 12 - 0
alien-store-platform/src/main/java/shop/alien/storeplatform/service/StorePlatformUserRoleService.java

@@ -107,5 +107,17 @@ public interface StorePlatformUserRoleService extends IService<StorePlatformUser
      * @return 是否成功
      */
     boolean batchDeleteSubAccounts(List<Integer> userIds, Integer storeId);
+
+    /**
+     * 更新子账号信息(账号名、手机号和角色信息)
+     *
+     * @param userId      用户ID
+     * @param phone       手机号(可选)
+     * @param accountName 账号名称(可选)
+     * @param storeId     店铺ID
+     * @param roleId      角色ID(可选)
+     * @return 是否成功
+     */
+    boolean updateSubAccount(Integer userId, String phone, String accountName, Integer storeId, Long roleId);
 }
 

+ 228 - 0
alien-store-platform/src/main/java/shop/alien/storeplatform/service/impl/StorePlatformUserRoleServiceImpl.java

@@ -110,6 +110,41 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
         }
 
         try {
+            // 0. 校验当前店铺中是否已存在相同手机号或用户名的子账号
+            // 先根据手机号查询用户
+            LambdaQueryWrapper<StoreUser> checkUserWrapper = new LambdaQueryWrapper<>();
+            checkUserWrapper.eq(StoreUser::getPhone, phone)
+                    .eq(StoreUser::getDeleteFlag, 0);
+            StoreUser checkUser = storeUserMapper.selectOne(checkUserWrapper);
+            
+            if (checkUser != null) {
+                // 如果用户存在,检查该用户在当前店铺是否已有子账号记录
+                LambdaQueryWrapper<StorePlatformUserRole> checkRoleWrapper = new LambdaQueryWrapper<>();
+                checkRoleWrapper.eq(StorePlatformUserRole::getUserId, checkUser.getId())
+                        .eq(StorePlatformUserRole::getStoreId, storeId)
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                StorePlatformUserRole existingSubAccount = storePlatformUserRoleMapper.selectOne(checkRoleWrapper);
+                
+                if (existingSubAccount != null) {
+                    log.error("该手机号在当前店铺已存在子账号,不支持创建: phone={}, storeId={}", phone, storeId);
+                    return false;
+                }
+            }
+            
+            // 如果提供了账号名称,检查当前店铺中是否已存在相同账号名称的子账号
+            if (accountName != null && !accountName.trim().isEmpty()) {
+                LambdaQueryWrapper<StorePlatformUserRole> checkAccountNameWrapper = new LambdaQueryWrapper<>();
+                checkAccountNameWrapper.eq(StorePlatformUserRole::getAccountName, accountName)
+                        .eq(StorePlatformUserRole::getStoreId, storeId)
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                StorePlatformUserRole existingAccountName = storePlatformUserRoleMapper.selectOne(checkAccountNameWrapper);
+                
+                if (existingAccountName != null) {
+                    log.error("该账号名称在当前店铺已存在子账号,不支持创建: accountName={}, storeId={}", accountName, storeId);
+                    return false;
+                }
+            }
+
             // 1. 根据手机号查询 store_user 表
             LambdaQueryWrapper<StoreUser> userQueryWrapper = new LambdaQueryWrapper<>();
             userQueryWrapper.eq(StoreUser::getPhone, phone);
@@ -316,5 +351,198 @@ public class StorePlatformUserRoleServiceImpl extends ServiceImpl<StorePlatformU
             return false;
         }
     }
+
+    @Override
+    public boolean updateSubAccount(Integer userId, String phone, String accountName, Integer storeId, Long roleId) {
+        if (userId == null || storeId == null) {
+            log.error("用户ID或店铺ID不能为空: userId={}, storeId={}", userId, storeId);
+            return false;
+        }
+
+        try {
+            // 1. 验证用户是否存在
+            StoreUser storeUser = storeUserMapper.selectById(userId);
+            if (storeUser == null || storeUser.getDeleteFlag() != 0) {
+                log.error("用户不存在或已删除: userId={}", userId);
+                return false;
+            }
+
+            // 2. 如果提供了手机号,更新手机号
+            if (phone != null && !phone.trim().isEmpty()) {
+                // 检查新手机号是否与其他用户重复(排除当前用户)
+                LambdaQueryWrapper<StoreUser> phoneCheckWrapper = new LambdaQueryWrapper<>();
+                phoneCheckWrapper.eq(StoreUser::getPhone, phone)
+                        .ne(StoreUser::getId, userId)
+                        .eq(StoreUser::getDeleteFlag, 0);
+                StoreUser existingUser = storeUserMapper.selectOne(phoneCheckWrapper);
+                
+                if (existingUser != null) {
+                    // 检查该手机号对应的用户是否在当前店铺已有子账号
+                    LambdaQueryWrapper<StorePlatformUserRole> checkRoleWrapper = new LambdaQueryWrapper<>();
+                    checkRoleWrapper.eq(StorePlatformUserRole::getUserId, existingUser.getId())
+                            .eq(StorePlatformUserRole::getStoreId, storeId)
+                            .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                    StorePlatformUserRole existingSubAccount = storePlatformUserRoleMapper.selectOne(checkRoleWrapper);
+                    
+                    if (existingSubAccount != null) {
+                        log.error("该手机号在当前店铺已存在子账号,不支持更新: phone={}, storeId={}", phone, storeId);
+                        return false;
+                    }
+                }
+
+                // 更新用户手机号、姓名和昵称
+                LambdaUpdateWrapper<StoreUser> userUpdateWrapper = new LambdaUpdateWrapper<>();
+                userUpdateWrapper.eq(StoreUser::getId, userId)
+                        .set(StoreUser::getPhone, phone);
+                
+                // 如果提供了账号名称,同时更新姓名和昵称;否则使用手机号作为姓名和昵称
+                if (accountName != null && !accountName.trim().isEmpty()) {
+                    userUpdateWrapper.set(StoreUser::getName, accountName)
+                            .set(StoreUser::getNickName, accountName);
+                } else {
+                    userUpdateWrapper.set(StoreUser::getName, phone)
+                            .set(StoreUser::getNickName, phone);
+                }
+                
+                int userUpdateResult = storeUserMapper.update(null, userUpdateWrapper);
+                if (userUpdateResult <= 0) {
+                    log.error("更新用户手机号失败: userId={}, phone={}", userId, phone);
+                    return false;
+                }
+                log.info("成功更新用户手机号: userId={}, phone={}", userId, phone);
+            }
+
+            // 3. 查询当前用户的角色关联记录
+            // 如果提供了角色ID,通过userId + storeId + roleId查询;否则通过userId + storeId查询第一个
+            StorePlatformUserRole existingUserRole = null;
+            Long targetRoleId = roleId; // 用于确定要查询/更新的角色ID
+            
+            if (roleId != null) {
+                // 如果提供了角色ID,先查询是否存在该角色关联记录
+                LambdaQueryWrapper<StorePlatformUserRole> userRoleQueryWrapper = new LambdaQueryWrapper<>();
+                userRoleQueryWrapper.eq(StorePlatformUserRole::getUserId, userId)
+                        .eq(StorePlatformUserRole::getStoreId, storeId)
+                        .eq(StorePlatformUserRole::getRoleId, roleId)
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                existingUserRole = storePlatformUserRoleMapper.selectOne(userRoleQueryWrapper);
+            }
+            
+            // 如果通过roleId没找到,或者没有提供roleId,则查询第一个记录
+            if (existingUserRole == null) {
+                LambdaQueryWrapper<StorePlatformUserRole> userRoleQueryWrapper = new LambdaQueryWrapper<>();
+                userRoleQueryWrapper.eq(StorePlatformUserRole::getUserId, userId)
+                        .eq(StorePlatformUserRole::getStoreId, storeId)
+                        .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                        .last("LIMIT 1");
+                existingUserRole = storePlatformUserRoleMapper.selectOne(userRoleQueryWrapper);
+                
+                // 如果没有提供roleId,使用现有记录的roleId
+                if (targetRoleId == null && existingUserRole != null) {
+                    targetRoleId = existingUserRole.getRoleId();
+                }
+            }
+
+            // 4. 更新或创建角色关联记录
+            if (existingUserRole == null) {
+                // 如果不存在角色关联记录,需要创建一条(至少需要角色ID)
+                if (roleId == null) {
+                    log.error("用户在该店铺没有角色关联记录,且未提供角色ID,无法创建: userId={}, storeId={}", userId, storeId);
+                    return false;
+                }
+                
+                // 检查账号名称是否重复
+                if (accountName != null && !accountName.trim().isEmpty()) {
+                    LambdaQueryWrapper<StorePlatformUserRole> accountNameCheckWrapper = new LambdaQueryWrapper<>();
+                    accountNameCheckWrapper.eq(StorePlatformUserRole::getAccountName, accountName)
+                            .eq(StorePlatformUserRole::getStoreId, storeId)
+                            .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                    StorePlatformUserRole existingAccountName = storePlatformUserRoleMapper.selectOne(accountNameCheckWrapper);
+                    
+                    if (existingAccountName != null) {
+                        log.error("该账号名称在当前店铺已存在,不支持创建: accountName={}, storeId={}", accountName, storeId);
+                        return false;
+                    }
+                }
+                
+                // 创建新的角色关联记录
+                StorePlatformUserRole newUserRole = new StorePlatformUserRole();
+                newUserRole.setUserId(userId);
+                newUserRole.setRoleId(roleId);
+                newUserRole.setStoreId(storeId);
+                newUserRole.setDeleteFlag(0);
+                newUserRole.setAccountName(accountName != null ? accountName : (storeUser.getNickName() != null ? storeUser.getNickName() : storeUser.getPhone()));
+                newUserRole.setCreatedTime(new Date());
+                
+                boolean insertResult = this.save(newUserRole);
+                if (!insertResult) {
+                    log.error("创建角色关联记录失败: userId={}, storeId={}, roleId={}", userId, storeId, roleId);
+                    return false;
+                }
+                log.info("成功创建角色关联记录: userId={}, storeId={}, roleId={}", userId, storeId, roleId);
+            } else {
+                // 如果提供了账号名称,更新账号名称(需要检查是否重复)
+                if (accountName != null && !accountName.trim().isEmpty()) {
+                    // 检查新账号名称是否在当前店铺已存在(排除当前记录)
+                    LambdaQueryWrapper<StorePlatformUserRole> accountNameCheckWrapper = new LambdaQueryWrapper<>();
+                    accountNameCheckWrapper.eq(StorePlatformUserRole::getAccountName, accountName)
+                            .eq(StorePlatformUserRole::getStoreId, storeId)
+                            .eq(StorePlatformUserRole::getDeleteFlag, 0)
+                            .ne(StorePlatformUserRole::getId, existingUserRole.getId());
+                    StorePlatformUserRole existingAccountName = storePlatformUserRoleMapper.selectOne(accountNameCheckWrapper);
+                    
+                    if (existingAccountName != null) {
+                        log.error("该账号名称在当前店铺已存在,不支持更新: accountName={}, storeId={}", accountName, storeId);
+                        return false;
+                    }
+
+                    // 更新账号名称(只更新当前记录)
+                    LambdaUpdateWrapper<StorePlatformUserRole> accountNameUpdateWrapper = new LambdaUpdateWrapper<>();
+                    accountNameUpdateWrapper.eq(StorePlatformUserRole::getId, existingUserRole.getId())
+                            .set(StorePlatformUserRole::getAccountName, accountName);
+                    
+                    int accountNameUpdateResult = storePlatformUserRoleMapper.update(null, accountNameUpdateWrapper);
+                    if (accountNameUpdateResult <= 0) {
+                        log.error("更新账号名称失败: userId={}, accountName={}", userId, accountName);
+                        return false;
+                    }
+                    log.info("成功更新账号名称: userId={}, accountName={}", userId, accountName);
+                }
+
+                // 如果提供了角色ID且与现有不同,更新角色信息
+                if (roleId != null && !roleId.equals(existingUserRole.getRoleId())) {
+                    // 检查新角色是否已存在相同的关联记录
+                    LambdaQueryWrapper<StorePlatformUserRole> roleCheckWrapper = new LambdaQueryWrapper<>();
+                    roleCheckWrapper.eq(StorePlatformUserRole::getUserId, userId)
+                            .eq(StorePlatformUserRole::getStoreId, storeId)
+                            .eq(StorePlatformUserRole::getRoleId, roleId)
+                            .eq(StorePlatformUserRole::getDeleteFlag, 0);
+                    StorePlatformUserRole existingRoleRecord = storePlatformUserRoleMapper.selectOne(roleCheckWrapper);
+                    
+                    if (existingRoleRecord != null && !existingRoleRecord.getId().equals(existingUserRole.getId())) {
+                        log.error("用户在该店铺已存在该角色关联记录: userId={}, storeId={}, roleId={}", userId, storeId, roleId);
+                        return false;
+                    }
+
+                    // 更新角色ID(只更新当前记录)
+                    LambdaUpdateWrapper<StorePlatformUserRole> roleUpdateWrapper = new LambdaUpdateWrapper<>();
+                    roleUpdateWrapper.eq(StorePlatformUserRole::getId, existingUserRole.getId())
+                            .set(StorePlatformUserRole::getRoleId, roleId);
+                    
+                    int roleUpdateResult = storePlatformUserRoleMapper.update(null, roleUpdateWrapper);
+                    if (roleUpdateResult <= 0) {
+                        log.error("更新角色ID失败: userId={}, roleId={}", userId, roleId);
+                        return false;
+                    }
+                    log.info("成功更新角色ID: userId={}, oldRoleId={}, newRoleId={}", userId, existingUserRole.getRoleId(), roleId);
+                }
+            }
+
+            return true;
+        } catch (Exception e) {
+            log.error("更新子账号信息失败: userId={}, phone={}, accountName={}, storeId={}, roleId={}", 
+                    userId, phone, accountName, storeId, roleId, e);
+            return false;
+        }
+    }
 }