1
hao
2025-05-20 8e24c6fea30d9b179375ee2893710cdec2443b13
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.dev.service.internal;
 
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import com.app.base.data.ApiResponseResult;
import com.app.base.data.DataGrid;
import com.dev.service.ApiCodeService;
import com.system.permission.dao.SysPermissionDao;
import com.system.permission.entity.SysPermission;
import com.system.role.dao.RolePermissionMapDao;
import com.system.role.dao.SysRoleDao;
import com.system.role.entity.RolePermissionMap;
import com.system.role.entity.SysRole;
import com.system.user.dao.UserRoleMapDao;
import com.system.user.entity.SysUser;
import com.utils.BaseService;
import com.utils.SearchFilter;
import com.utils.UserUtil;
import com.utils.enumeration.BasicStateEnum;
 
/**
 * 角色
 *
 */
@Service(value = "apiCodeService")
@Transactional(propagation = Propagation.REQUIRED)
public class ApiCodeImpl implements ApiCodeService {
 
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private RolePermissionMapDao rolePermissionMapDao;
    @Autowired
    private UserRoleMapDao userRoleMapDao;
    @Autowired
    private SysPermissionDao sysPermissionDao;
 
    /**
     * 新增角色
     * @param sysRole
     * @return
     * @author fyx
     * @serialData 2018-11-21
     * @throws Exception
     */
    @Override
    @Transactional
    public ApiResponseResult add(SysRole sysRole) throws Exception{
        if(sysRole == null){
            return ApiResponseResult.failure("角色不能为空!");
        }
        if(StringUtils.isEmpty(sysRole.getBsCode())){
            return ApiResponseResult.failure("角色编号不能为空!");
        }
        if(StringUtils.isEmpty(sysRole.getBsName())){
            return ApiResponseResult.failure("角色名称不能为空!");
        }
        int count = sysRoleDao.countByIsDelAndBsCode(0, sysRole.getBsCode());
        if(count > 0){
            return ApiResponseResult.failure("该角色已存在,请填写其他角色编号!");
        }
 
        sysRole.setCreatedTime(new Date());
        sysRoleDao.save(sysRole);
 
        return ApiResponseResult.success("角色添加成功!").data(sysRole);
    }
 
    @Override
    @Transactional
    public ApiResponseResult edit(SysRole sysRole) throws Exception {
        if(sysRole == null){
            return ApiResponseResult.failure("角色不能为空!");
        }
        if(sysRole.getId() == null){
            return ApiResponseResult.failure("角色ID不能为空!");
        }
        if(StringUtils.isEmpty(sysRole.getBsCode())){
            return ApiResponseResult.failure("角色编号不能为空!");
        }
        if(StringUtils.isEmpty(sysRole.getBsName())){
            return ApiResponseResult.failure("角色名称不能为空!");
        }
        SysRole o = sysRoleDao.findById((long) sysRole.getId());
        if(o == null){
            return ApiResponseResult.failure("该角色不存在!");
        }
 
        //判断角色编号是否有变化,有则修改;没有则不修改
        String originalCode = o.getBsCode();
        if(o.getBsCode().equals(sysRole.getBsCode())){
        }else{
            int count = sysRoleDao.countByIsDelAndBsCode(0, sysRole.getBsCode());
            if(count > 0){
                return ApiResponseResult.failure("角色编号已存在,请填写其他角色编号!");
            }
            o.setBsCode(sysRole.getBsCode().trim());
        }
        o.setModifiedTime(new Date());
        o.setBsName(sysRole.getBsName());
        o.setDescpt(sysRole.getDescpt());
        sysRoleDao.save(o);
 
        return ApiResponseResult.success("编辑成功!");
    }
 
    @Override
    @Transactional
    public ApiResponseResult delete(Long id) throws Exception{
        if(id == null){
            return ApiResponseResult.failure("角色ID不能为空!");
        }
        SysRole o  = sysRoleDao.findById((long) id);
        if(o == null){
            return ApiResponseResult.failure("该角色不存在!");
        }
 
        o.setModifiedTime(new Date());
        o.setIsDel(1);
        sysRoleDao.save(o);
 
        return ApiResponseResult.success("删除成功!");
    }
 
    @Override
    @Transactional
    public ApiResponseResult getList(String keyword, String bsCode,String bsName, Date createdTimeStart, Date createdTimeEnd, PageRequest pageRequest) throws Exception {
        //查询条件1
        List<SearchFilter> filters =new ArrayList<>();
        filters.add(new SearchFilter("isDel", SearchFilter.Operator.EQ, BasicStateEnum.FALSE.intValue()));
        if(StringUtils.isNotEmpty(bsCode)){
            filters.add(new SearchFilter("bsCode", SearchFilter.Operator.LIKE, bsCode));
        }
        if(StringUtils.isNotEmpty(bsName)){
            filters.add(new SearchFilter("bsName", SearchFilter.Operator.LIKE, bsName));
        }
        if(createdTimeStart != null){
            filters.add(new SearchFilter("createdTime", SearchFilter.Operator.GTE, createdTimeStart));
        }
        if(createdTimeEnd != null){
            filters.add(new SearchFilter("createdTime", SearchFilter.Operator.LTE, createdTimeEnd));
        }
        //查询2
        List<SearchFilter> filters1 =new ArrayList<>();
        if(StringUtils.isNotEmpty(keyword)){
            filters1.add(new SearchFilter("bsCode", SearchFilter.Operator.LIKE, keyword));
            filters1.add(new SearchFilter("bsName", SearchFilter.Operator.LIKE, keyword));
        }
        Specification<SysRole> spec = Specification.where(BaseService.and(filters, SysRole.class));
        Specification<SysRole> spec1 =  spec.and(BaseService.or(filters1, SysRole.class));
        Page<SysRole> page = sysRoleDao.findAll(spec1, pageRequest);
 
        return ApiResponseResult.success().data(DataGrid.create(page.getContent(), (int) page.getTotalElements(), pageRequest.getPageNumber() + 1, pageRequest.getPageSize()));
    }
 
    @Override
    public ApiResponseResult getCheckedRoles(long userId) throws Exception {
        Map map = new HashMap();
        return ApiResponseResult.success().data(map);
    }
    @Override
    @Transactional
    public ApiResponseResult saveUserRoles(long userId,String roles) throws Exception {
        return ApiResponseResult.success("修改成功!");
    }
 
    @Override
    public ApiResponseResult addRouter(String rolecode, String roles) throws Exception {
        return ApiResponseResult.success();
    }
 
    @Override
    public ApiResponseResult getRouter(String rolecode) throws Exception {
        return null;
    }
 
    /**
     * 获取当前角色的操作权限
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(readOnly = true)
    public ApiResponseResult getPermission() throws Exception{
        SysUser currUser = UserUtil.getCurrUser();  //获取当前用户
//        if(currUser == null || currUser.getId() == null){
//            return ApiResponseResult.failure("当前用户不存在!");
//        }
        if(currUser == null || currUser.getFid() == null){
            return ApiResponseResult.failure("当前用户不存在!");
        }
 
        //1.初始化
        int isSuper = 0;
        String perm = "";
 
        //2.判断是否为管理员用户,是则无需获取操作权限;否则获取操作权限
//        if(currUser.getUserIsSuper() == 1){
//            //2.1
//            isSuper = 1;
//        }else{
//            //2.2获取当前用户所属角色
//            List<UserRolesMap> mapList = userRolesMapDao.findByIsDelAndUserId(0, currUser.getId());
//            if(mapList != null && mapList.size() > 0){
//                for(UserRolesMap map : mapList){
//                    if(map != null && map.getUserId() != null){
//                        //获取角色
//                        ApiCode role = sysRoleDao.findById((long) map.getRoleId());
//                        if(role != null && StringUtils.isNotEmpty(role.getBsPermission())){
//                            perm = StringUtils.isNotEmpty(perm) ? (perm+","+role.getBsPermission()) : role.getBsPermission();
//                        }
//                    }
//                }
//            }
//        }
 
        //3.封装数据
        Map<String, Object> map = new HashMap<>();
        map.put("isSuper", isSuper);
        map.put("perm", perm);
 
        return ApiResponseResult.success().data(map);
    }
 
    /**
     * 根据ID获取角色
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ApiResponseResult getRole(Long id) throws Exception{
        if(id == null){
            return ApiResponseResult.failure("角色ID不能为空!");
        }
        SysRole o = sysRoleDao.findById((long) id);
        if(o == null){
            return ApiResponseResult.failure("该角色不存在!");
        }
        return ApiResponseResult.success().data(o);
    }
 
    /**
     * 获取所有角色
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(readOnly = true)
    public ApiResponseResult getRoles() throws Exception{
        List<SearchFilter> filters = new ArrayList<>();
        Specification<SysRole> spec = Specification.where(BaseService.and(filters, SysRole.class));
        List<SysRole> list = sysRoleDao.findAll(spec);
        return ApiResponseResult.success().data(list);
    }
 
    /**
     * 根据角色ID获取权限信息
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ApiResponseResult getRolePerm(Long id) throws Exception{
        if(id == null){
            return ApiResponseResult.failure("角色ID不能为空!");
        }
        SysRole o = sysRoleDao.findById((long) id);
        if(o == null){
            return ApiResponseResult.failure("该角色不存在!");
        }
 
        //获取当前角色下角色权限关联信息
        List<RolePermissionMap> list = rolePermissionMapDao.findByIsDelAndAndRoleId(0, id);
 
        //获取所有权限信息
        List<SysPermission> list2 = sysPermissionDao.findByIsDel(0);
 
        List<Map<String, Object>> mapList = new ArrayList<>();
        for(SysPermission permItem : list2) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", permItem.getId()!=null ? permItem.getId().toString() : "");
            map.put("code", permItem.getBsCode()!=null ? permItem.getBsCode().toString() : "");
            map.put("name", permItem.getBsName()!=null ? permItem.getBsName().toString() : "");
            map.put("pId", permItem.getParentId()!=null ? permItem.getParentId().toString() : "");
            map.put("zindex", permItem.getZindex()!=null ? permItem.getZindex().toString() : "");
            map.put("istype", permItem.getIstype()!=null ? permItem.getIstype().toString() : "");
            map.put("descpt", permItem.getDescpt()!=null ? permItem.getDescpt().toString() : "");
            map.put("icon", permItem.getBsIcon()!=null ? permItem.getBsIcon().toString() : "");
            map.put("page", permItem.getPageUrl()!=null ? permItem.getPageUrl().toString() : "");
            map.put("checked", false);
            map.put("open", true);
            //判断是否有权限
            for(RolePermissionMap rolePermItem : list){
                if(rolePermItem.getPermitId()!=null && rolePermItem.getPermitId().equals(permItem.getId())){
                    map.put("checked", true);
                }
            }
            mapList.add(map);
        }
 
        return ApiResponseResult.success().data(mapList);
    }
 
    /**
     * 设置权限
     * @param roleId
     * @param permIds
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public ApiResponseResult doRolePerm(Long roleId, String permIds) throws Exception{
        if(roleId == null){
            return ApiResponseResult.failure("角色ID不能为空!");
        }
        //转换
        String[] permIdArray = permIds.split(",");
        List<Long> permIdList = new ArrayList<Long>();
        for(int i = 0; i < permIdArray.length; i++){
            if(StringUtils.isNotEmpty(permIdArray[i])) {
                permIdList.add(Long.parseLong(permIdArray[i]));
            }
        }
 
        //1.删除角色原权限信息
        List<RolePermissionMap> listOld = rolePermissionMapDao.findByIsDelAndAndRoleId(0, roleId);
        if(listOld.size() > 0){
            for(RolePermissionMap item : listOld){
                item.setModifiedTime(new Date());
                item.setIsDel(1);
            }
            rolePermissionMapDao.saveAll(listOld);
        }
 
        //2.添加角色新权限信息
        List<RolePermissionMap> listNew = new ArrayList<>();
        if(permIdList.size() > 0){
            for(Long permId : permIdList){
                RolePermissionMap item = new RolePermissionMap();
                item.setCreatedTime(new Date());
                item.setRoleId(roleId);
                item.setPermitId(permId);
                listNew.add(item);
            }
            rolePermissionMapDao.saveAll(listNew);
        }
 
        return ApiResponseResult.success("设置权限成功!");
    }
}