ludc
2024-07-09 5acc490fa6f77a9ed7b5976ee6a2e22b070df5bf
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package com.vci.frameworkcore.controller;
 
import com.vci.frameworkcore.compatibility.SmRoleQueryServiceI;
import com.vci.frameworkcore.constant.FrameWorkLangCodeConstant;
import com.vci.frameworkcore.dto.SmRoleDTO;
import com.vci.frameworkcore.pagemodel.SmRoleVO;
import com.vci.starter.web.annotation.controller.VciUnCheckRight;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.*;
import com.vci.starter.web.util.LocalFileUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.starter.web.util.WebThreadLocalUtil;
import com.vci.web.enumpck.UserTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import java.io.File;
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) {
        try {
            if(queryObject == null){
                queryObject = new BaseQueryObject();
            }
            DataGrid<SmRoleVO> roleVODataGrid = roleQueryService.refGridRoles(queryObject.getConditionMap(),queryObject.getPageHelper());
            return BaseResult.dataGrid(roleVODataGrid);
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("查询角色列表时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("查询角色列表时出现错误,原因" + exceptionMessage);
        }
    }
 
    /**
     * 角色的列表查询:用于角色管理列表等查询场景:会根据当前登录的用户类型来决定查询那些角色
     * @param queryObject 查询对象,包含了查询条件,分页,排序等,即允许使用SmRoleVO里的所有属性作为查询条件
     * @return 包含三员的角色 的显示对象列表数据,请获取其中的data属性
     * @throws VciBaseException 查询出错的时候会抛出异常,如果是老的项目里不抛出异常
     */
    @RequestMapping(value = "/gridRoles",method = RequestMethod.GET)
    public BaseResult<SmRoleVO> gridRoles(BaseQueryObject queryObject) {
        try {
            if(queryObject == null){
                queryObject = new BaseQueryObject();
            }
            //根据当前用户来决定能查那些角色
            String usertype = WebThreadLocalUtil.getCurrentUserSessionInfoInThread().getUsertype();
            if(UserTypeEnum.SUPPER_ADMIN.getValue().equals(usertype)){
                queryObject.getConditionMap().put("pltype","1");
            }else {
                queryObject.getConditionMap().put("pltype","2");
            }
            DataGrid<SmRoleVO> roleVOData = roleQueryService.gridRoles(queryObject.getConditionMap(),queryObject.getPageHelper());
            return BaseResult.dataGrid(roleVOData);
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("查询角色列表时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("查询角色列表时出现错误,原因:"+exceptionMessage);
        }
    }
 
    /**
     * 根据用户主键获取关联的角色
     * @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 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("这里返回前端的错误信息");
    }
 
    /**
     * 新增单条角色
     * @param smRoleDTO
     * @return
     */
    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    public BaseResult addRole(@RequestBody SmRoleDTO smRoleDTO){
        try {
            return roleQueryService.addRole(smRoleDTO) ? BaseResult.success("角色添加成功!"):BaseResult.fail("角色添加失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("添加角色时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("添加角色时出现错误,原因:" + exceptionMessage);
        }
    }
 
    /**
     * 修改角色
     * @param smRoleDTO
     * @return
     */
    @RequestMapping(value = "/updateRole",method = RequestMethod.PUT)
    public BaseResult updateRole(@RequestBody SmRoleDTO smRoleDTO){
        try {
            return roleQueryService.updateRole(smRoleDTO) ? BaseResult.success("角色修改成功!"):BaseResult.fail("角色修改失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("修改角色时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("修改角色时出现错误,原因:" + exceptionMessage);
        }
    }
 
    /**
     * 删除角色
     * @param ids
     * @return
     */
    @RequestMapping(value = "/deleteRole",method = RequestMethod.DELETE)
    public BaseResult deleteRole(String[] ids){
        try {
            return roleQueryService.deleteRole(ids) ? BaseResult.success("删除用户成功!"):BaseResult.fail("删除用户失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("添加用户时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("添加用户时出现错误,原因:" + exceptionMessage);
        }
    }
 
    /**
     * 导入角色
     * @param file
     * @return
     * @throws VciBaseException
     */
    @RequestMapping(value = "/importRole",method = RequestMethod.POST)
    public BaseResult importRole(MultipartFile file){
        String excelFileName = LocalFileUtil.getDefaultTempFolder() + File.separator + LocalFileUtil.getFileNameForIE(file.getOriginalFilename());
        File file1 = new File(excelFileName);
        try {
            file.transferTo(new File(excelFileName));
            if (file != null) {
                return roleQueryService.importRole(file1);
            } else {
                return BaseResult.fail(FrameWorkLangCodeConstant.IMPORT_FAIL, new String[]{"无导入的文件"});
            }
        }catch (Throwable e) {
            throw new VciBaseException(e.getMessage(),new String[0],e);
        }finally {
            file1.delete();
        }
    }
 
    /**
     * 成员管理分配角色:保存用户角色关联关系
     * @param userOids
     * @param roleIds
     * @return
     */
    @RequestMapping(value = "/saveRights",method = RequestMethod.POST)
    public BaseResult saveRights(String[] userOids, String[] roleIds){
        try {
            return roleQueryService.saveRights(userOids,roleIds) ? BaseResult.success("角色分配成功!"):BaseResult.fail("角色分配失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("分配角色时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("分配角色时出现错误,原因:" + exceptionMessage);
        }
    }
 
    /**
     * 角色管理分配成员:保存用户角色关联关系
     * @param userOids
     * @param roleId
     * @return
     */
    @RequestMapping(value = "/saveRight",method = RequestMethod.POST)
    public BaseResult saveRight(String[] userOids, String roleId){
        try {
            return roleQueryService.saveRight(userOids,roleId) ? BaseResult.success("成员分配成功!"):BaseResult.fail("成员分配失败!");
        }catch (Exception e){
            e.printStackTrace();
            String exceptionMessage = VciBaseUtil.getExceptionMessage(e);
            log.error("分配用户时出现错误,原因:" + exceptionMessage);
            return BaseResult.fail("分配用户时出现错误,原因:" + exceptionMessage);
        }
    }
 
}