Your Name 2 gadus atpakaļ
vecāks
revīzija
9be81a9893

+ 1
- 1
src/main/java/com/yunzhi/inte/controller/ResourcesController.java Parādīt failu

@@ -48,7 +48,7 @@ public class ResourcesController extends BaseController {
48 48
     @ApiOperation("分页查询")
49 49
     @GetMapping("/resources")
50 50
     public ResponseBean list(@ApiParam("资源类型") @RequestParam(value = "type") String type,
51
-                             @ApiParam("角色ID") @RequestParam(value = "roleId") Integer roleId) throws Exception {
51
+                             @ApiParam("角色ID") @RequestParam(value = "roleId", required = false) Integer roleId) throws Exception {
52 52
 
53 53
         List<Resources> result = resourcesService.listBy(type, roleId);
54 54
         

+ 14
- 6
src/main/java/com/yunzhi/inte/controller/RoleResourcesController.java Parādīt failu

@@ -5,6 +5,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage;
5 5
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
6 6
 import com.yunzhi.inte.common.BaseController;
7 7
 import com.yunzhi.inte.common.ResponseBean;
8
+
9
+import java.util.HashMap;
8 10
 import java.util.List;
9 11
 import io.swagger.annotations.Api;
10 12
 import io.swagger.annotations.ApiOperation;
@@ -62,14 +64,20 @@ public class RoleResourcesController extends BaseController {
62 64
     /** 
63 65
      * 新增数据
64 66
      *
65
-     * @param roleResources 实例对象
67
+     * @param roleId 角色ID
68
+     * @param roleResourcesList 授权列表
66 69
      * @return 实例对象
67 70
      */
68
-    @ApiOperation("新增数据")
69
-    @PostMapping("/roleResources")
70
-    public ResponseBean add(@ApiParam("对象实体") @RequestBody RoleResources roleResources) throws Exception {
71
-        roleResourcesService.save(roleResources);
72
-        return ResponseBean.success(roleResources);
71
+    @ApiOperation("新增数据, 包含编辑")
72
+    @PostMapping("/role/{roleId}/Resources")
73
+    public ResponseBean add(@ApiParam("角色ID") @PathVariable Integer roleId,
74
+                            @ApiParam("授权列表") @RequestBody List<RoleResources> roleResourcesList) throws Exception {
75
+        if (null == roleResourcesList) {
76
+            return ResponseBean.error("未找到授权对象");
77
+        }
78
+
79
+        roleResourcesService.mergeData(roleId, roleResourcesList);
80
+        return ResponseBean.success(roleResourcesList);
73 81
     }
74 82
     
75 83
     /** 

+ 1
- 1
src/main/java/com/yunzhi/inte/entity/RoleResources.java Parādīt failu

@@ -24,7 +24,7 @@ import lombok.experimental.Accessors;
24 24
 public class RoleResources implements Serializable,Cloneable{
25 25
     /** ID */
26 26
     @ApiModelProperty(name = "ID",notes = "")
27
-    @TableId(value = "id", type = IdType.INPUT)
27
+    @TableId(value = "id", type = IdType.AUTO)
28 28
     private Integer id ;
29 29
     /** 角色ID */
30 30
     @ApiModelProperty(name = "角色ID",notes = "")

+ 9
- 2
src/main/java/com/yunzhi/inte/mapper/RoleResourcesMapper.java Parādīt failu

@@ -5,12 +5,19 @@ import org.apache.ibatis.annotations.Mapper;
5 5
 import org.apache.ibatis.annotations.Param;
6 6
 import com.yunzhi.inte.entity.RoleResources;
7 7
 
8
- /**
8
+import java.util.List;
9
+
10
+/**
9 11
  * 角色资源;(role_resources)表数据库访问层
10 12
  * @author : http://njyunzhi.com
11 13
  * @date : 2022-10-12
12 14
  */
13 15
 @Mapper
14 16
 public interface RoleResourcesMapper  extends BaseMapper<RoleResources>{
15
-    
17
+
18
+     List<RoleResources> getByRole(@Param("roleId") Integer roleId);
19
+
20
+    RoleResources getByRoleAndResource(@Param("roleId") Integer roleId, @Param("resourceId") Integer resourceId);
21
+
22
+    int deleteNotIn(@Param("roleId") Integer roleId, @Param("idList") List<Integer> idList);
16 23
 }

+ 6
- 4
src/main/java/com/yunzhi/inte/service/RoleResourcesService.java Parādīt failu

@@ -1,13 +1,15 @@
1 1
 package com.yunzhi.inte.service;
2 2
 
3
-import com.baomidou.mybatisplus.extension.service.IService;
4 3
 import com.yunzhi.inte.entity.RoleResources;
5 4
 
6
- /**
5
+import java.util.List;
6
+
7
+/**
7 8
  * 角色资源;(role_resources)表服务接口
8 9
  * @author : http://njyunzhi.com
9 10
  * @date : 2022-10-12
10 11
  */
11 12
 public interface RoleResourcesService extends IBaseService<RoleResources> {
12
-    
13
-}
13
+
14
+     boolean mergeData(Integer roleId, List<RoleResources> roleResourcesList);
15
+ }

+ 38
- 3
src/main/java/com/yunzhi/inte/service/impl/RoleResourcesServiceImpl.java Parādīt failu

@@ -1,16 +1,51 @@
1 1
 package com.yunzhi.inte.service.impl;
2 2
 
3
-import org.springframework.beans.factory.annotation.Autowired;
4 3
 import org.springframework.stereotype.Service;
5 4
 import com.yunzhi.inte.entity.RoleResources;
6 5
 import com.yunzhi.inte.mapper.RoleResourcesMapper;
7 6
 import com.yunzhi.inte.service.RoleResourcesService;
8
- /**
7
+
8
+import java.util.ArrayList;
9
+import java.util.HashMap;
10
+import java.util.List;
11
+
12
+/**
9 13
  * 角色资源;(role_resources)表服务实现类
14
+ *
10 15
  * @author : http://www.chiner.pro
11 16
  * @date : 2022-10-12
12 17
  */
13 18
 @Service
14 19
 public class RoleResourcesServiceImpl extends BaseServiceImpl<RoleResourcesMapper, RoleResources> implements RoleResourcesService {
15
-    
20
+
21
+    @Override
22
+    public boolean mergeData(Integer roleId, List<RoleResources> roleResourcesList) {
23
+        // 如果是空 list, 代表取消授权
24
+        if (roleResourcesList.size() == 0) {
25
+            removeByMap(new HashMap<String, Object>(){{
26
+                put("role_id", roleId);
27
+            }});
28
+            return true;
29
+        }
30
+
31
+        //
32
+        List<Integer> idList = new ArrayList<>();
33
+        for (RoleResources item: roleResourcesList) {
34
+            item.setRoleId(roleId);
35
+
36
+            RoleResources origin = baseMapper.getByRoleAndResource(roleId, item.getResourceId());
37
+            if (null != origin) {
38
+                item.setId(origin.getId());
39
+            } else {
40
+                baseMapper.insert(item);
41
+            }
42
+
43
+            idList.add(item.getId());
44
+        }
45
+
46
+        // 删除多余的数据
47
+        baseMapper.deleteNotIn(roleId, idList);
48
+
49
+        return false;
50
+    }
16 51
 }

+ 29
- 1
src/main/resources/mapper/RoleResourcesMapper.xml Parādīt failu

@@ -2,5 +2,33 @@
2 2
 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
3 3
 
4 4
 <mapper namespace="com.yunzhi.inte.mapper.RoleResourcesMapper">
5
-    
5
+    <delete id="deleteNotIn">
6
+        DELETE
7
+        FROM
8
+            role_resources t
9
+        WHERE
10
+            t.role_id = 1
11
+          AND t.id NOT IN
12
+            <foreach item="item" collection="idList" separator="," open="(" close=")" index="">
13
+                #{item}
14
+            </foreach>
15
+    </delete>
16
+
17
+    <select id="getByRole" resultType="com.yunzhi.inte.entity.RoleResources">
18
+        SELECT
19
+            *
20
+        FROM
21
+            role_resources t
22
+        WHERE
23
+            t.role_id = #{roleId}
24
+    </select>
25
+    <select id="getByRoleAndResource" resultType="com.yunzhi.inte.entity.RoleResources">
26
+        SELECT
27
+            *
28
+        FROM
29
+            role_resources t
30
+        WHERE
31
+            t.role_id = #{roleId}
32
+            AND t.resource_id = #{resourceId}
33
+    </select>
6 34
 </mapper>