gdg
2021-02-25 ced11a778b9bf4deee59445a41e3ede298d8c963
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package com.nanometer.smartlab.service;
 
import com.nanometer.smartlab.dao.BaseRoleDao;
import com.nanometer.smartlab.entity.BaseRole;
import com.nanometer.smartlab.entity.BaseRolePage;
import com.nanometer.smartlab.exception.AlarmCode;
import com.nanometer.smartlab.exception.AlarmException;
import com.nanometer.smartlab.exception.BusinessException;
import com.nanometer.smartlab.exception.ExceptionEnumCode;
import com.nanometer.smartlab.util.IDUtils;
import com.nanometer.smartlab.util.MessageUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * Created by johnny on 17/11/18.
 */
@Service("baseRoleService")
public class BaseRoleServiceImpl implements BaseRoleService {
 
    private static Logger logger = Logger.getLogger(BaseRoleService.class);
 
    @Resource(name = "baseRoleDao")
    BaseRoleDao baseRoleDao;
 
    @Resource
    private BaseRolePageService baseRolePageService;
 
    @Transactional(propagation = Propagation.REQUIRED)
    public List<BaseRole> getBaseRoleList(String name, String memo, Integer first, Integer pageSize) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            if (StringUtils.isNotBlank(memo)) {
                params.put("memo", "%" + memo + "%");
            }
            params.put("first", first);
            params.put("pageSize", pageSize);
            return this.baseRoleDao.getBaseRoleList(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
    @Transactional(propagation = Propagation.REQUIRED)
    public List<BaseRole> getBaseRoleList() {
        return this.getBaseRoleList(null, null, null, null);
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public int getBaseRoleTotalCount(String name, String memo) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(name)) {
                params.put("name", "%" + name + "%");
            }
            if (StringUtils.isNotBlank(memo)) {
                params.put("memo", "%" + memo + "%");
            }
            return this.baseRoleDao.getBaseRoleTotalCount(params);
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public BaseRole getBaseRole(String id) {
        try {
            BaseRole baseRole = this.baseRoleDao.getBaseRole(id);
            List<BaseRolePage> baseRolePageList = this.baseRolePageService.getBaseRolePageList(id, null);
            if (baseRolePageList != null && baseRolePageList.size() > 0) {
                List<String> pageIdList = new ArrayList<String>();
                for (BaseRolePage baseRolePage : baseRolePageList) {
                    pageIdList.add(baseRolePage.getPageId());
                }
                baseRole.setPageIdList(pageIdList);
            }
 
            return baseRole;
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean isBaseRoleExist(String name, String editId) {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            if (StringUtils.isNotBlank(name)) {
                params.put("name", name);
            }
            if (StringUtils.isNotBlank(editId)) {
                params.put("editId", editId);
            }
            int count = this.baseRoleDao.getBaseRoleTotalCount(params);
            return count > 0;
        } catch (DataAccessException e) {
            logger.error(e.getMessage(), e);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), e);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public BaseRole insertBaseRole(BaseRole baseRole) {
        try {
            if (baseRole.getId() == null) {
                baseRole.setId(IDUtils.uuid());
            }
            this.baseRoleDao.insertBaseRole(baseRole);
            if (baseRole.getPageIdList() != null) {
                BaseRolePage baseRolePage = null;
                for (String pageId : baseRole.getPageIdList()) {
                    baseRolePage = new BaseRolePage();
                    baseRolePage.setId(IDUtils.uuid());
                    baseRolePage.setRoleId(baseRole.getId());
                    baseRolePage.setPageId(pageId);
                    this.baseRolePageService.insertBaseRolePage(baseRolePage);
                }
            }
 
            return baseRole;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean updateBaseRole(BaseRole baseRole) {
        try {
            int row = this.baseRoleDao.updateBaseRole(baseRole);
            this.baseRolePageService.deleteBaseRolePage(baseRole.getId());
            if (baseRole.getPageIdList() != null) {
                BaseRolePage baseRolePage = null;
                for (String pageId : baseRole.getPageIdList()) {
                    baseRolePage = new BaseRolePage();
                    baseRolePage.setId(IDUtils.uuid());
                    baseRolePage.setRoleId(baseRole.getId());
                    baseRolePage.setPageId(pageId);
                    this.baseRolePageService.insertBaseRolePage(baseRolePage);
                }
            }
 
            if (row == 0) {
                return false;
            }
            return true;
        } catch (DuplicateKeyException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_DUPLICATE, MessageUtil.getMessage(AlarmCode.DATA_DUPLICATE.getCode()));
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean deleteBaseRole(List<BaseRole> baseRoleList) {
        try {
            if (baseRoleList == null || baseRoleList.size() == 0) {
                return false;
            }
 
            List<String> ids = new ArrayList<String>();
            for (BaseRole baseRole : baseRoleList) {
                ids.add(baseRole.getId());
            }
 
            int row = this.baseRoleDao.deleteBaseRoles(ids);
            if (row == 0) {
                return false;
            }
 
            return true;
        } catch (DataIntegrityViolationException ex) {
            logger.warn(ex.getMessage(), ex);
            throw new AlarmException(AlarmCode.DATA_CONFICT, MessageUtil.getMessage(AlarmCode.DATA_CONFICT.getCode()));
        } catch (DataAccessException ex) {
            logger.error(ex.getMessage(), ex);
            throw new BusinessException(ExceptionEnumCode.DB_ERR, MessageUtil.getMessageByCode(ExceptionEnumCode.DB_ERR.getCode()), ex);
        }
    }
 
    @Override
    public void updateUserMngPageBtn(BaseRole baseRole) {
 
        if (baseRole.getBtnPermissions() != null && baseRole.getBtnPermissions().size() > 0) {
            List<String> btnPermissions = baseRole.getBtnPermissions();
            for (String btnPermission : btnPermissions) {
                if (btnPermissions.indexOf(btnPermission) == 0) {
                    baseRole.setBtnPermission(btnPermission);
                } else {
                    baseRole.setBtnPermission(baseRole.getBtnPermission() + "," + btnPermission);
                }
            }
        }
 
        //更新操作可见
        baseRoleDao.updateBaseRoleInfo(baseRole);
 
 
    }
 
    @Override
    public void updateUserMngPageUser(BaseRole baseRole) {
        //更新人员可见
        baseRoleDao.updateBaseRoleInfo(baseRole);
    }
 
    @Override
    public BaseRole getRoleByUserId(String id) {
        return baseRoleDao.getBaseRoleByUser(id);
    }
}