jyy
2022-04-15 f57554f7da5e4d05b4b4bab99bf49ac9ca8c2038
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
package com.matrix.system.common.service.impl;
 
import com.matrix.core.constance.MatrixConstance;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.pojo.PaginationVO;
import com.matrix.core.tools.ModelUtils;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.WebUtil;
import com.matrix.system.common.bean.*;
import com.matrix.system.common.dao.SysRoleDao;
import com.matrix.system.common.dao.SysRolePwoerFnDao;
import com.matrix.system.common.service.SysRoleService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
 
/**
 * 角色管理功能
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date Dec 11, 2017
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {
 
    @Autowired
    private SysRoleDao sysRoleDao;
    @Autowired
    private SysRolePwoerFnDao rolePwoerDao;
 
    @Override
    public int add(SysRole sysRole) {
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        sysRole.setCompanyId(user.getCompanyId());
        sysRole.setCreateBy(user.getSuAccount());
        sysRole.setUpdateBy(user.getSuAccount());
        sysRole.setIsDefault(SysRole.NOT_DEFAULT);
        // 新增角色对应的中间表
        int i = sysRoleDao.insert(sysRole);
        addRolePwoer(sysRole);
        return i;
 
    }
 
    /**
     * 新增角色的权限,添加前清空已有的权限
     * 
     * @param sysRole
     */
    private void addRolePwoer(SysRole sysRole) {
        SysRolePwoerFn sysRolePwoerFn = new SysRolePwoerFn();
        sysRolePwoerFn.setRoleId(sysRole.getRoleId());
        rolePwoerDao.deleteByModel(sysRolePwoerFn);
        List<SysFunction> fnList = sysRole.getFnList();
        List<SysRolePwoerFn> rolePwoer = new ArrayList<>();
        if (fnList != null) {
            for (SysFunction sysFunction : fnList) {
                // 如果为空则不在进行添加
                if (sysFunction == null || sysFunction.getFnId() == null) {
                    continue;
                }
                SysRolePwoerFn temp = new SysRolePwoerFn();
                temp.setFnId(sysFunction.getFnId());
                temp.setRoleId(sysRole.getRoleId());
                temp.setCreateBy(sysRole.getCreateBy());
                temp.setUpdateBy(sysRole.getUpdateBy());
                temp.setRpfBtns(getBtns(sysFunction.getSysFnBtnRel()));
                rolePwoer.add(temp);
            }
        }
        if (CollectionUtils.isNotEmpty(rolePwoer)) {
            rolePwoerDao.batchInsert(rolePwoer);
        }
    }
 
    /**
     * 获取功能按钮的字符串值
     * 
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2017年12月6日
     * @param sysFunction
     * @return
     */
    private String getBtns(List<SysFnBtnRel> fnBtnRelList) {
        StringBuilder btns = new StringBuilder();
        if (CollectionUtils.isNotEmpty(fnBtnRelList)) {
            for (SysFnBtnRel fnBtnRel : fnBtnRelList) {
                if (fnBtnRel != null && StringUtils.isNotBlank(fnBtnRel.getBtnValue())) {
                    btns.append(fnBtnRel.getBtnValue() + ",");
                }
            }
        }
        return btns.toString().substring(0, btns.toString().length());
    }
 
    @Override
    public int batchAdd(List<SysRole> sysRoleList) {
        return sysRoleDao.batchInsert(sysRoleList);
    }
 
    @Override
    public int modifyByMap(SysRole oldSysRole, SysRole newSysRole) {
        // 这样写只能保证基本信息不被覆盖
        SysUsers user = WebUtil.getSessionAttribute(MatrixConstance.LOGIN_KEY);
        newSysRole.setCompanyId(user.getCompanyId());
        newSysRole.setCreateBy(user.getSuName());
        newSysRole.setUpdateBy(user.getSuName());
        addRolePwoer(newSysRole);
        Map<String, Object> modifyMap = null;
        try {
            if (!ModelUtils.isModified(oldSysRole, newSysRole)) {
                return MatrixConstance.DML_SUCCESSS;
            }
            modifyMap = ModelUtils.comparePojo2Map(oldSysRole, newSysRole);
        } catch (Exception e) {
            throw new GlobleException(SystemErrorCode.DATA_UPDATE_FAIL, e, newSysRole.getRoleName());
        }
        if (modifyMap.size() > 0) {
            modifyMap.put("roleId", oldSysRole.getRoleId());
            return sysRoleDao.updateByMap(modifyMap);
        }
        return MatrixConstance.DML_SUCCESSS;
    }
 
    @Override
    public int modifyByModel(SysRole sysRole) {
        return sysRoleDao.updateByModel(sysRole);
    }
 
    @Override
    public int remove(List<String> list) {
 
        return sysRoleDao.deleteByIds(list);
 
    }
 
    @Override
    public int removeById(String roleId) {
 
        return sysRoleDao.deleteById(Long.valueOf(roleId));
 
    }
 
    @Override
    public int removeByModel(SysRole sysRole) {
 
        return sysRoleDao.deleteByModel(sysRole);
 
    }
 
    @Override
    public List<SysRole> findInPage(SysRole sysRole, PaginationVO pageVo) {
 
        return sysRoleDao.selectInPage(sysRole, pageVo);
 
    }
 
    @Override
    public List<SysRole> findByModel(SysRole sysRole) {
 
        return sysRoleDao.selectByModel(sysRole);
 
    }
 
    @Override
    public int findTotal(SysRole sysRole) {
 
        return sysRoleDao.selectTotalRecord(sysRole);
 
    }
 
    @Override
    public SysRole findById(String roleId) {
 
        return sysRoleDao.selectById(Long.valueOf(roleId));
 
    }
 
}