whyczh
2021-06-03 195cdca1fae35cc9d7750ec2446412ef6cfb41fe
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
package com.whyc.service;
 
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.whyc.dto.Response;
import com.whyc.mapper.RoleMapper;
import com.whyc.mapper.UserMapper;
import com.whyc.mapper.UserRoleMapper;
import com.whyc.pojo.*;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
 
@Service
public class RoleService {
 
    @Resource
    private RoleMapper mapper;
    @Resource
    private UserRoleMapper userRoleMapper;
 
 
    public List<Role> getAll() {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>();
        queryWrapper.ne("name","superuser");
        return mapper.selectList(queryWrapper);
    }
 
 
    public Response<PageInfo<Role>> getRoleByCondition(int pageNum,int pageSize,Role role){
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>(role);
        queryWrapper.or().like(StringUtils.isNotEmpty(role.getSearch()) ,"label",role.getSearch());
        queryWrapper.or().like(StringUtils.isNotEmpty(role.getSearch()) ,"description",role.getSearch());
        queryWrapper.ne("name","superuser");
        List<Role> list = mapper.selectList(queryWrapper);
        for (Role role1: list) {
            QueryWrapper<UserRole> userRoleQueryWrapper = new QueryWrapper<>();
            userRoleQueryWrapper.eq("role_id",role1.getId());
            int count = userRoleMapper.selectCount(userRoleQueryWrapper);
            role1.setUserCount(count);
        }
        //IPage<Role> roleIPage = mapper.selectPage(new Page<>(pageNum, pageSize), queryWrapper);
        PageInfo<Role> rolePageInfo = new PageInfo<>(list);
        return new Response<PageInfo<Role>>().set(1,rolePageInfo);
    }
 
    public Response add(Role role) {
        QueryWrapper<Role> queryWrapper = new QueryWrapper<>(role);
        try {
            if(mapper.selectOne(queryWrapper)!=null){
                return new Response().setMsg(0,"已有此权限组数据");
            }
        }catch (Exception e){
            return new Response().setMsg(0,"已有此权限组数据");
        }
 
        role.setState(0);
        role.setUpdateTime(new Date());
        mapper.insert(role);
        return new Response().set(1,role,"添加成功");
    }
    public Response updateRole(Role role){
        if(mapper.updateById(role)>0){
            return new Response().setMsg(1,"更新成功");
        }else {
            return new Response().setMsg(0,"更新失败");
        }
 
    }
 
 
 
 
    public boolean addBatch(List<Role> roles) {
        return mapper.insertBatchSomeColumn(roles)==roles.size();
    }
 
 
 
 
    public Response updateRoleState(int roleId,int state){
        Role role = mapper.selectById(roleId);
        if ("superuser".equals(role.getName())){
            return new Response().setMsg(0,"更新失败");
        }
        role.setState(state);
        if(mapper.updateById(role)>0){
            if (state==0){
                return new Response().setMsg(1,"启用成功");
            }else{
                return new Response().setMsg(1,"冻结成功");
            }
        }else {
            return new Response().setMsg(0,"更新失败");
        }
    }
 
    public Response deleteRole(int roleId){
        Role role = mapper.selectById(roleId);
        if ("superuser".equals(role.getName())){
            return new Response().setMsg(0,"删除失败");
        }
        if(mapper.deleteById(roleId)>0){
            return new Response().setMsg(1, "删除成功");
        }else{
            return new Response().setMsg(0, "删除失败");
        }
    }
 
 
}