1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.vci.frameworkcore.controller;
 
import com.vci.frameworkcore.compatibility.SmRoleQueryServiceI;
import com.vci.frameworkcore.pagemodel.SmRoleVO;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.*;
import com.vci.starter.web.util.VciBaseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
 
import java.util.List;
import java.util.Map;
 
/**
 * 角色查询控制器
 * @author weidy
 * @date 2020/3/4
 */
@RestController
@RequestMapping("/roleQueryController")
@Slf4j
public class SmRoleQueryController {
 
    /**
     * 角色的查询服务
     */
    @Autowired
    private SmRoleQueryServiceI roleQueryService;
 
    /**
     * 角色的列表参照
     * @param queryObject 查询对象,包含了查询条件,分页,排序等,即允许使用SmRoleVO里的所有属性作为查询条件
     * @return 普通角色(不包含三员的角色)的显示对象列表数据,请获取其中的data属性
     * @throws VciBaseException 查询出错的时候会抛出异常,如果是老的项目里不抛出异常
     */
    @RequestMapping(value = "/refDataGrid",method = RequestMethod.GET)
    public BaseResult<SmRoleVO> refDataGrid(BaseQueryObject queryObject) throws VciBaseException {
        if(queryObject == null){
            queryObject = new BaseQueryObject();
        }
        DataGrid<SmRoleVO> roleVODataGrid =  roleQueryService.refGridRoles(queryObject.getConditionMap(),queryObject.getPageHelper());
        return BaseResult.dataGrid(roleVODataGrid);
        //如果是老的项目,应该在refGridRoles上添加try,catch,然后catch里应该使用下面的代码
        //BaseResult.fail("这里返回前端的错误信息");
    }
 
    /**
     * 根据用户主键获取关联的角色
     * @param userOid 用户主键
     * @param queryMap 查询条件,如果需要使用用户的属性来查询可以使用pkUser.xxxx
     * @return 角色的显示对象
     */
    @RequestMapping(value = "/listRoleByUserOid",method = RequestMethod.GET)
    public BaseResult<List<SmRoleVO>> listRoleByUserOid(String userOid, Map<String, String> queryMap){
        try {
            return BaseResult.dataList(roleQueryService.listRoleByUserOid(userOid,queryMap));
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("根据用户主键获取,关联的角色时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("根据用户主键获取,关联的角色时出现错误,原因:" + exceptionMessage);
        }
    }
 
    /**
     * 分配角色:保存用户角色关联关系
     * @param userOid
     * @param roleIds
     * @return
     */
    @RequestMapping(value = "/saveRights",method = RequestMethod.POST)
    public BaseResult saveRights(String userOid, String[] roleIds){
        try {
            return roleQueryService.saveRights(userOid,roleIds) ? BaseResult.success("角色分配成功!"):BaseResult.fail("角色分配失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("根据用户主键获取,关联的角色时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("根据用户主键获取,关联的角色时出现错误,原因:" + exceptionMessage);
        }
    }
 
    /**
     * 角色的树形参照
     * @param treeQueryObject 树形数据的查询对象,包括查询条件,上级主键,是否多选等,extandParamsMap中添加"showAllRoleNode"为"true"时,返回结果中会包含“所有角色”这个节点
     * @return 角色的树形参照,无上下级关系
     * @throws VciBaseException 查询出错的时候会抛出异常,如果是老的项目里不抛出异常
     */
    @RequestMapping(value = "/refTree",method = RequestMethod.GET)
    public BaseResult<Tree> refTree(TreeQueryObject treeQueryObject) throws VciBaseException{
        List<Tree> roleTreeList = roleQueryService.refTreeRoles(treeQueryObject);
        return  BaseResult.tree(roleTreeList);
        //老的项目依然是添加try catch,方法里不抛出异常
        //BaseResult.fail("这里返回前端的错误信息");
    }
 
}