zss
2024-09-13 e05875fa75605595b192fca04e598598cb2f2931
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
package com.yuanchu.mom.service.impl;
 
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yuanchu.mom.common.GetLook;
import com.yuanchu.mom.common.PrintChina;
import com.yuanchu.mom.dto.RolePowerDto;
import com.yuanchu.mom.mapper.PowerMapper;
import com.yuanchu.mom.mapper.RoleMapper;
import com.yuanchu.mom.mapper.UserMapper;
import com.yuanchu.mom.pojo.Menu;
import com.yuanchu.mom.pojo.Power;
import com.yuanchu.mom.pojo.Role;
import com.yuanchu.mom.service.RoleService;
import com.yuanchu.mom.utils.QueryWrappers;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
@Service
@AllArgsConstructor
public class RoleServiceImp implements RoleService {
 
    private RoleMapper roleMapper;
 
    private PowerMapper powerMapper;
 
    private GetLook getLook;
    private UserMapper userMapper;
 
    @Override
    public List<Role> selectList() {
        return roleMapper.selectList(null);
    }
 
    @Override
    public Map<String, Object> selectUserList(IPage<Role> page, Role role) {
        Map<String, Object> map = new HashMap<>();
        map.put("head", PrintChina.printChina(Role.class));
        Map<String, Integer> map1 = getLook.selectPowerByMethodAndUserId("selectRoleLists");
        if(map1.get("look")==1) role.setCreateUser(map1.get("userId"));
        map.put("body", roleMapper.selectPage(page, QueryWrappers.queryWrappers(role)));
        return map;
    }
 
    @Override
    public int delRole(Integer id) {
        return roleMapper.deleteById(id);
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addRole(RolePowerDto rolePowerDto) {
        Role role = new Role();
        role.setName(rolePowerDto.getRoleName());
        int insert = roleMapper.insert(role);
        if (insert == 1){
            rolePowerDto.getPowers().forEach(a->{
                a.setRoleId(role.getId());
                powerMapper.insert(a);
            });
        }
        return 1;
    }
 
    @Override
    public List<Menu> selectMenuList() {
        return roleMapper.selectMenuList();
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int upRole(RolePowerDto rolePowerDto) {
        Role role = new Role();
        role.setName(rolePowerDto.getRoleName());
        role.setCategory(rolePowerDto.getCategory());
        role.setId(rolePowerDto.getRoleId2());
        int up = roleMapper.updateById(role);
        if (up == 1){
            powerMapper.delete(Wrappers.<Power>lambdaUpdate().eq(Power::getRoleId, role.getId()));
            rolePowerDto.getPowers().forEach(a->{
                a.setRoleId(role.getId());
                powerMapper.insert(a);
            });
        }
        return 1;
    }
 
    @Override
    public String getRole() {
        Integer userId = getLook.selectPowerByMethodAndUserId(null).get("userId");
        Integer roleId = userMapper.selectById(userId).getRoleId();
        Role role = roleMapper.selectById(roleId);
        return role.getName();
    }
}