zxl
7 天以前 0fb6b9d8d414822668c401a2b507df1fe6d1fa2d
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
package cn.lili.modules.member.serviceimpl;
 
import cn.lili.cache.Cache;
import cn.lili.cache.CachePrefix;
import cn.lili.common.enums.ResultCode;
import cn.lili.common.exception.ServiceException;
import cn.lili.common.security.context.UserContext;
import cn.lili.common.security.enums.UserEnums;
import cn.lili.modules.member.entity.dos.StoreRole;
import cn.lili.modules.member.mapper.StoreRoleMapper;
import cn.lili.modules.member.service.StoreClerkRoleService;
import cn.lili.modules.member.service.StoreDepartmentRoleService;
import cn.lili.modules.member.service.StoreMenuRoleService;
import cn.lili.modules.member.service.StoreRoleService;
import cn.lili.mybatis.util.PageUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.util.List;
 
/**
 * 角色业务层实现
 *
 * @author Chopper
 * @since 2020/11/17 3:50 下午
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class StoreRoleServiceImpl extends ServiceImpl<StoreRoleMapper, StoreRole> implements StoreRoleService {
    @Override
    public List<StoreRole> getSelectList() {
 
        String storeId = UserContext.getCurrentUser().getStoreId();
//        System.out.println(UserContext.getCurrentUser().getId());
//        System.out.println(storeId);
//        String storeId = "1376433565247471616";
        if ( storeId == null) {
            return null;
        }
        StoreRole storeRole = new StoreRole();
        storeRole.setStoreId(storeId);
 
        return  this.list(PageUtil.initWrapper(storeRole));
    }
    /**
     * 部门角色
     */
    @Autowired
    private StoreDepartmentRoleService storeDepartmentRoleService;
    /**
     * 用户权限
     */
    @Autowired
    private StoreClerkRoleService storeClerkRoleService;
 
    @Autowired
    private StoreMenuRoleService storeMenuRoleService;
 
    @Autowired
    private Cache cache;
 
    @Override
    public List<StoreRole> findByDefaultRole(Boolean defaultRole) {
        QueryWrapper<StoreRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("default_role", true);
        return baseMapper.selectList(queryWrapper);
    }
 
    @Override
    public void deleteRoles(List<String> roleIds) {
        //校验是否为当前店铺
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", roleIds);
        List<StoreRole> roles = this.baseMapper.selectList(queryWrapper);
        roles.forEach(role -> {
            if (!role.getStoreId().equals(UserContext.getCurrentUser().getStoreId())) {
                throw new ServiceException(ResultCode.USER_AUTHORITY_ERROR);
            }
        });
        queryWrapper = new QueryWrapper<>();
        queryWrapper.in("role_id", roleIds);
        //校验是否绑定店铺部门
        if (storeDepartmentRoleService.count(queryWrapper) > 0) {
            throw new ServiceException(ResultCode.PERMISSION_DEPARTMENT_ROLE_ERROR);
        }
        //校验是否绑定店员
        if (storeClerkRoleService.count(queryWrapper) > 0) {
            throw new ServiceException(ResultCode.PERMISSION_USER_ROLE_ERROR);
        }
        //删除角色
        this.removeByIds(roleIds);
        //删除角色与菜单关联
        storeMenuRoleService.remove(queryWrapper);
        cache.vagueDel(CachePrefix.PERMISSION_LIST.getPrefix(UserEnums.STORE));
        cache.vagueDel(CachePrefix.STORE_USER_MENU.getPrefix());
    }
 
    @Override
    public Boolean update(StoreRole storeRole) {
        StoreRole storeRoleTemp = this.getById(storeRole.getId());
        //校验店铺角色是否存在
        if (storeRoleTemp == null) {
            throw new ServiceException(ResultCode.PERMISSION_ROLE_NOT_FOUND_ERROR);
        }
        //校验店铺角色是否属于当前店铺
        if (!storeRoleTemp.getStoreId().equals(UserContext.getCurrentUser().getStoreId())) {
            throw new ServiceException(ResultCode.PERMISSION_ROLE_NOT_FOUND_ERROR);
        }
        cache.vagueDel(CachePrefix.PERMISSION_LIST.getPrefix(UserEnums.STORE));
        cache.vagueDel(CachePrefix.STORE_USER_MENU.getPrefix());
        return updateById(storeRole);
    }
 
 
    @Override
    public Boolean saveStoreRole(StoreRole storeRole) {
        storeRole.setStoreId(UserContext.getCurrentUser().getStoreId());
        return save(storeRole);
    }
 
    @Override
    public List<StoreRole> list(List<String> ids) {
        QueryWrapper<StoreRole> queryWrapper = new QueryWrapper<>();
        String storeId = UserContext.getCurrentUser().getStoreId();
        queryWrapper.eq("store_id", storeId);
        queryWrapper.in("id", ids);
        return this.baseMapper.selectList(queryWrapper);
    }
}