郑永安
2023-06-19 7a6abd05683528032687c75e80e0bd2030a3e46c
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
package com.gkhy.safePlatform.account.service.baseService.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gkhy.safePlatform.account.entity.user.RoleInfo;
import com.gkhy.safePlatform.account.entity.user.RoleInfoDO;
import com.gkhy.safePlatform.account.enums.RoleStatusEnum;
import com.gkhy.safePlatform.commons.enums.E;
import com.gkhy.safePlatform.commons.enums.ResultCodes;
import com.gkhy.safePlatform.commons.exception.AusinessException;
import com.gkhy.safePlatform.commons.exception.BusinessException;
import com.gkhy.safePlatform.commons.utils.StringUtils;
import com.gkhy.safePlatform.account.repository.RoleInfoRepository;
import com.gkhy.safePlatform.account.service.baseService.RoleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
 
@Service("roleInfoService")
public class RoleInfoServiceImpl  extends ServiceImpl<RoleInfoRepository, RoleInfo> implements RoleInfoService {
 
    @Autowired
    private RoleInfoRepository roleInfoRepository;
 
 
 
    @Override
    public List<RoleInfo> getRoleInfo(Collection<? extends Serializable> idList) {
        if (idList.size() < 1) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        return this.listByIds(idList);
    }
 
    @Override
    public RoleInfo getRoleInfoByUserId(Long userId) {
        if (userId == null) {
            throw new BusinessException(ResultCodes.CLIENT_PARAM_NULL);
        }
        return roleInfoRepository.selectRoleInfoByUserId(userId);
    }
 
 
 
    @Override
    public List<RoleInfo> getRoleInfoByCodes(List<String> roleCodes) {
        List<RoleInfo> roleInfos = new ArrayList<>();
        for (String code : roleCodes) {
            if (StringUtils.isBlank(code)) {
                throw new AusinessException(E.DATA_PARAM_NULL,"角色code传入为空");
            }
            RoleInfo roleInfo = roleInfoRepository.getRoleInfoByCode(code);
            if (roleInfo == null) {
                // 角色code不存在
                throw new BusinessException(ResultCodes.CLIENT_ROLE_CODE_NOT_EXIST);
            }
            else if (roleInfo.getStatus() != RoleStatusEnum.ENABLED.getCode()){
                // 未启用
                throw new BusinessException(ResultCodes.CLIENT_ROLE_CODE_NOT_ON);
            }
            roleInfos.add(roleInfo);
        }
        return roleInfos;
    }
 
    @Override
    public void saveRoleInfo(RoleInfo roleInfo) {
        int i = roleInfoRepository.insertRoleInfo(roleInfo);
        if (i != 1) {
            throw new BusinessException(ResultCodes.SERVER_ADD_ERROR);
        }
    }
 
    @Override
    public RoleInfo getRoleInfoById(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return roleInfoRepository.getRoleInfoById(id);
    }
 
    @Override
    public RoleInfoDO getRoleInfoDOById(Long id) {
        if (id == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return roleInfoRepository.getRoleInfoDOById(id);
    }
 
    @Override
    public void updateRoleInfo(RoleInfo roleInfo) {
        if (roleInfo.getId() == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        int i = roleInfoRepository.updateRoleInfo(roleInfo);
        if (i == 0) {
            throw new BusinessException(ResultCodes.SERVER_UPDATE_DATA_NO_CHANGE);
        }
        if (i > 1) {
            throw new BusinessException(ResultCodes.SERVER_UPDATE_ERROR);
        }
    }
 
 
    @Override
    public List<RoleInfoDO> getRoleInfoByStatus(RoleStatusEnum status) {
        if (status == null) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        return roleInfoRepository.getRoleInfoByStatus(status.getCode());
    }
 
    @Override
    public RoleInfo getRoleInfoByCode(String code) {
        if (StringUtils.isBlank(code)) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        List<RoleInfo> roleInfos = roleInfoRepository.selectList(new LambdaQueryWrapper<RoleInfo>()
                .eq(RoleInfo::getCode, code)
                .eq(RoleInfo::getStatus, RoleStatusEnum.ENABLED.getCode()));
        if (roleInfos.size() > 1) {
            throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED);
        }
        if (roleInfos.size() == 0) {
            return null;
        }
        return roleInfos.get(0);
    }
 
    @Override
    public RoleInfo getRoleInfoByName(String name) {
        if (StringUtils.isBlank(name)) {
            throw new BusinessException(ResultCodes.SERVER_PARAM_NULL);
        }
        List<RoleInfo> roleInfos = roleInfoRepository.selectList(new LambdaQueryWrapper<RoleInfo>()
                .eq(RoleInfo::getName, name)
                .eq(RoleInfo::getStatus, RoleStatusEnum.ENABLED.getCode()));
        if (roleInfos.size() > 1) {
            throw new BusinessException(ResultCodes.SERVER_DATABASE_DATA_DUPLICATED);
        }
        if (roleInfos.size() == 0) {
            return null;
        }
        return roleInfos.get(0);
    }
 
}