Răsfoiți Sursa

0813 分配角色和批量封禁功能

Qing 8 luni în urmă
părinte
comite
a60b11ad57

+ 20 - 1
consumer-service-demo/admin-demo/src/main/java/com/sf/admin/controller/UmsAdminController.java

@@ -75,6 +75,10 @@ public class UmsAdminController {
         // 获取用户对应的部门信息
         // ums_admin表中的dept_id 对应 ums_dept表中的id
         UmsDept umsDept = umsDeptService.getById(umsAdmin.getDeptId());
+        String deptName = "";
+        if (umsDept != null) {
+            deptName = umsDept.getDeptName();
+        }
         // 获取用户对应的角色信息
         List<UmsRole> roleList = adminRoleRelationService.getRolesByUserId(umsAdmin.getId());
         String[] roles = new String[roleList.size()];
@@ -104,7 +108,7 @@ public class UmsAdminController {
                 .exNum(umsAdmin.getExNum())
                 .nickName(umsAdmin.getNickName())
                 .headImg(umsAdmin.getHeadImg())
-                .deptName(umsDept.getDeptName())
+                .deptName(deptName)
                 .roles(roles)
                 .menus(menus)
                 .build();
@@ -191,4 +195,19 @@ public class UmsAdminController {
         return CommonResult.failed("用户添加失败");
     }
 
+
+    // 批量封禁
+    // http://localhost:8099/admin-api/admin/batchBan?ids=11&ids=22
+    @PostMapping("/admin/batchBan")
+    public CommonResult<Long> batchBan(@RequestParam("ids") List<Long> ids) {
+        // 将传过来的用户状态修改为0
+        for (Long id : ids) {
+            LambdaUpdateWrapper<UmsAdmin> updateWrapper = new LambdaUpdateWrapper<>();
+            updateWrapper.eq(UmsAdmin::getId, id);
+            updateWrapper.set(UmsAdmin::getStatus, 0);
+            umsAdminService.update(updateWrapper);
+        }
+        return CommonResult.success(1L);
+    }
+
 }

+ 29 - 4
consumer-service-demo/admin-demo/src/main/java/com/sf/admin/controller/UmsRoleController.java

@@ -2,13 +2,13 @@ package com.sf.admin.controller;
 
 import com.sf.admin.dto.CommonResult;
 import com.sf.admin.entity.UmsRole;
+import com.sf.admin.service.IUmsAdminRoleRelationService;
 import com.sf.admin.service.IUmsRoleService;
 import lombok.RequiredArgsConstructor;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.*;
 import org.springframework.stereotype.Controller;
-import org.springframework.web.bind.annotation.RestController;
 
+import java.util.Arrays;
 import java.util.List;
 
 /**
@@ -24,11 +24,36 @@ import java.util.List;
 public class UmsRoleController {
 
     private final IUmsRoleService roleService;
+    private final IUmsAdminRoleRelationService adminRoleRelationService;
 
+    // 角色列表
     // http://localhost:8099/admin-api/role/listAll
     @GetMapping("/role/listAll")
-    public CommonResult<List<UmsRole>> listAll(){
+    public CommonResult<List<UmsRole>> listAll() {
         List<UmsRole> umsRoles = roleService.list();
         return CommonResult.success(umsRoles);
     }
+
+
+    // 根据用户id获取角色
+    // http://localhost:8099/admin-api/admin/role/3
+    @GetMapping("/admin/role/{id}")
+    public CommonResult<List<UmsRole>> getRolesByAdminId(@PathVariable("id") Long adminId) {
+        List<UmsRole> roles = adminRoleRelationService.getRolesByUserId(adminId);
+        return CommonResult.success(roles);
+    }
+
+    // 分配角色
+    // http://localhost:8099/admin-api/admin/role/update
+    @PostMapping("/admin/role/update")
+    public CommonResult updateRole(
+            @RequestParam("adminId") Long adminId, @RequestParam("roleIds") String roleIds) {
+        String[] roleIdArr = roleIds.split(",");
+        List<Long> roleIdList = Arrays.stream(roleIdArr).map(
+                roleId -> Long.parseLong(roleId)).toList();
+        // 将用户和多个角色相关联
+        adminRoleRelationService.updateRole(adminId, roleIdList);
+        return CommonResult.success(null);
+    }
+
 }

+ 2 - 0
consumer-service-demo/admin-demo/src/main/java/com/sf/admin/service/IUmsAdminRoleRelationService.java

@@ -18,4 +18,6 @@ public interface IUmsAdminRoleRelationService extends IService<UmsAdminRoleRelat
 
     // 根据用户id找到角色id 可以有多个
     List<UmsRole> getRolesByUserId(Long adminId);
+
+    void updateRole(Long adminId, List<Long> roleIds);
 }

+ 21 - 0
consumer-service-demo/admin-demo/src/main/java/com/sf/admin/service/impl/UmsAdminRoleRelationServiceImpl.java

@@ -36,6 +36,10 @@ public class UmsAdminRoleRelationServiceImpl extends ServiceImpl<UmsAdminRoleRel
         List<UmsAdminRoleRelation> umsAdminRoleRelations =
                 umsAdminRoleRelationMapper.selectList(queryWrapper);
 
+        if(umsAdminRoleRelations.size() == 0){
+            return new ArrayList<>();
+        }
+
         List<Long> roleIds = new ArrayList<>();
         for (UmsAdminRoleRelation adminRoleRelation : umsAdminRoleRelations) {
             roleIds.add(adminRoleRelation.getRoleId());
@@ -46,4 +50,21 @@ public class UmsAdminRoleRelationServiceImpl extends ServiceImpl<UmsAdminRoleRel
         List<UmsRole> umsRoles = umsRoleMapper.selectBatchIds(roleIds);
         return umsRoles;
     }
+
+
+    @Override
+    public void updateRole(Long adminId, List<Long> roleIds) {
+        // 要先删除原来的关联关系 再增加新的
+        // delete * from ums_admin_role_relation where admin_id = ''
+        LambdaQueryWrapper<UmsAdminRoleRelation> queryWrapper = new LambdaQueryWrapper<>();
+        queryWrapper.eq(UmsAdminRoleRelation::getAdminId, adminId);
+        umsAdminRoleRelationMapper.delete(queryWrapper);
+
+        for (Long roleId : roleIds) {
+            UmsAdminRoleRelation adminRoleRelation = new UmsAdminRoleRelation();
+            adminRoleRelation.setRoleId(roleId);
+            adminRoleRelation.setAdminId(adminId);
+            umsAdminRoleRelationMapper.insert(adminRoleRelation);
+        }
+    }
 }