ny
昨天 b6f169fe43a2b13f351aefc152374fc7f0bc8cb7
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package jnpf.permission.mapper;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.google.common.collect.Lists;
import jnpf.base.Pagination;
import jnpf.base.mapper.SuperMapper;
import jnpf.permission.entity.RoleEntity;
import jnpf.permission.model.position.PosConModel;
import jnpf.permission.model.role.RolePagination;
import jnpf.util.JsonUtil;
import jnpf.util.RandomUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
import org.apache.ibatis.annotations.Param;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 系统角色
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月26日 上午9:18
 */
public interface RoleMapper extends SuperMapper<RoleEntity> {
 
    /**
     * 通过组织id获取用户信息
     *
     * @param orgIdList
     * @return
     */
    List<String> query(@Param("orgIdList") List<String> orgIdList, @Param("keyword") String keyword, @Param("globalMark") Integer globalMark, @Param("enabledMark") Integer enabledMark);
 
    /**
     * 通过组织id获取用户信息
     *
     * @param
     * @param orgIdList
     * @return
     */
    Long count(@Param("orgIdList") List<String> orgIdList, @Param("keyword") String keyword, @Param("globalMark") Integer globalMark, @Param("enabledMark") Integer enabledMark);
 
 
    default List<RoleEntity> getList(RolePagination pagination) {
        boolean flag = false;
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(pagination.getKeyword())) {
            flag = true;
            queryWrapper.lambda().and(
                    t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
                            .or().like(RoleEntity::getEnCode, pagination.getKeyword())
            );
        }
        if (StringUtil.isNotEmpty(pagination.getType())) {
            queryWrapper.lambda().eq(RoleEntity::getType, pagination.getType());
        }
        if (pagination.getEnabledMark() != null) {
            queryWrapper.lambda().eq(RoleEntity::getEnabledMark, pagination.getEnabledMark());
        }
        //排序
        queryWrapper.lambda().orderByAsc(RoleEntity::getGlobalMark).orderByAsc(RoleEntity::getSortCode).orderByAsc(RoleEntity::getCreatorTime);
        if (flag) {
            queryWrapper.lambda().orderByDesc(RoleEntity::getLastModifyTime);
        }
 
        //不分页
        if (Objects.equals(pagination.getDataType(), 1)) {
            return this.selectList(queryWrapper);
        }
        //分页
        long count = this.selectCount(queryWrapper);
        Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize(), count, false);
        page.setOptimizeCountSql(false);
        IPage<RoleEntity> iPage = this.selectPage(page, queryWrapper);
        return pagination.setData(iPage.getRecords(), page.getTotal());
    }
 
    default Boolean isExistByFullName(String fullName, String id, String type) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RoleEntity::getFullName, fullName);
        queryWrapper.lambda().eq(RoleEntity::getType, type);
        if (!StringUtil.isEmpty(id)) {
            queryWrapper.lambda().ne(RoleEntity::getId, id);
        }
        return this.selectCount(queryWrapper) > 0 ? true : false;
    }
 
    default Boolean isExistByEnCode(String enCode, String id) {
        if (StringUtil.isEmpty(enCode)) return false;
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RoleEntity::getEnCode, enCode);
        if (!StringUtil.isEmpty(id)) {
            queryWrapper.lambda().ne(RoleEntity::getId, id);
        }
        return this.selectCount(queryWrapper) > 0 ? true : false;
    }
 
    default void create(RoleEntity entity) {
        if (StringUtil.isEmpty(entity.getId())) {
            entity.setId(RandomUtil.uuId());
        }
        entity.setGlobalMark(2);
        entity.setEnabledMark(1);
        entity.setCreatorTime(new Date());
        entity.setCreatorUserId(UserProvider.getUser().getUserId());
        this.insert(entity);
    }
 
    default Boolean update(String id, RoleEntity entity) {
        entity.setId(id);
        entity.setEnabledMark(1);
        entity.setLastModifyTime(new Date());
        entity.setLastModifyUserId(UserProvider.getUser().getUserId());
        return SqlHelper.retBool(this.updateById(entity));
    }
 
    default RoleEntity getInfo(String id) {
        return this.selectById(id);
    }
 
    default RoleEntity getByEnCode(String enCode) {
        if (StringUtil.isEmpty(enCode)) return null;
        QueryWrapper<RoleEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(RoleEntity::getEnCode, enCode);
        return this.selectOne(wrapper);
    }
 
    default List<RoleEntity> getList(boolean filterEnabledMark, String type, Integer isSystem) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        if (filterEnabledMark) {
            queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
        }
        if (StringUtil.isNotEmpty(type)) {
            queryWrapper.lambda().eq(RoleEntity::getType, type);
        }
        if (Objects.nonNull(isSystem)) {
            if (Objects.equals(isSystem, 1)) {
                queryWrapper.lambda().eq(RoleEntity::getGlobalMark, 1);
            } else {
                queryWrapper.lambda().ne(RoleEntity::getGlobalMark, 1);
            }
 
        }
        queryWrapper.lambda().orderByAsc(RoleEntity::getSortCode).orderByAsc(RoleEntity::getCreatorTime);
        return this.selectList(queryWrapper);
    }
 
    default List<RoleEntity> getListByIds(List<String> id, String keyword, boolean filterEnabledMark) {
        if (CollectionUtil.isEmpty(id)) return Collections.EMPTY_LIST;
        List<RoleEntity> roleList = new ArrayList<>();
        if (id.size() > 0) {
            QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(RoleEntity::getId, id);
            if (filterEnabledMark) {
                queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
            }
            if (StringUtil.isNotEmpty(keyword)) {
                queryWrapper.lambda().and(
                        t -> t.like(RoleEntity::getFullName, keyword)
                                .or().like(RoleEntity::getEnCode, keyword)
                );
            }
            roleList = this.selectList(queryWrapper);
        }
        return roleList;
    }
 
    default List<RoleEntity> getListByIds(List<String> idList) {
        if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST;
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        List<List<String>> lists = Lists.partition(idList, 1000);
        for (List<String> list : lists) {
            queryWrapper.lambda().in(RoleEntity::getId, list);
        }
        return this.selectList(queryWrapper);
    }
 
    default List<RoleEntity> getListByIds(Pagination pagination, List<String> idList) {
        if (CollectionUtil.isEmpty(idList)) return Collections.EMPTY_LIST;
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        List<List<String>> lists = Lists.partition(idList, 1000);
        for (List<String> list : lists) {
            queryWrapper.lambda().in(RoleEntity::getId, list);
        }
        if (StringUtil.isNotEmpty(pagination.getKeyword())) {
            if (StringUtil.isNotEmpty(pagination.getKeyword())) {
                queryWrapper.lambda().and(
                        t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
                                .or().like(RoleEntity::getEnCode, pagination.getKeyword())
                );
            }
        }
        Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<RoleEntity> iPage = this.selectPage(page, queryWrapper);
        return pagination.setData(iPage.getRecords(), page.getTotal());
    }
 
    default Map<String, Object> getRoleMap() {
        QueryWrapper<RoleEntity> roleWrapper = new QueryWrapper<>();
        roleWrapper.lambda().select(RoleEntity::getFullName, RoleEntity::getId);
        List<RoleEntity> list = this.selectList(roleWrapper);
        return list.stream().collect(Collectors.toMap(RoleEntity::getId, RoleEntity::getFullName));
    }
 
    default Map<String, Object> getRoleNameAndIdMap() {
        return getRoleNameAndIdMap(false);
    }
 
    default Map<String, Object> getRoleNameAndIdMap(boolean enabledMark) {
        QueryWrapper<RoleEntity> roleWrapper = new QueryWrapper<>();
        if (enabledMark) {
            roleWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
        }
        List<RoleEntity> list = this.selectList(roleWrapper);
        Map<String, Object> roleNameMap = new HashMap<>();
        list.stream().forEach(role -> roleNameMap.put(role.getFullName() + "/" + role.getEnCode(), role.getId()));
        return roleNameMap;
    }
 
    default RoleEntity getInfoByFullName(String fullName) {
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(RoleEntity::getFullName, fullName);
        return this.selectOne(queryWrapper);
    }
 
    default List<RoleEntity> getList(List<String> idList, Pagination pagination, boolean filterEnabledMark) {
        if (idList.size() > 0) {
            QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().in(RoleEntity::getId, idList);
            if (filterEnabledMark) {
                queryWrapper.lambda().eq(RoleEntity::getEnabledMark, 1);
            }
            if (StringUtil.isNotEmpty(pagination.getKeyword())) {
                queryWrapper.lambda().and(
                        t -> t.like(RoleEntity::getFullName, pagination.getKeyword())
                                .or().like(RoleEntity::getEnCode, pagination.getKeyword())
                );
            }
            Page<RoleEntity> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
            IPage<RoleEntity> iPage = this.selectPage(page, queryWrapper);
            return pagination.setData(iPage.getRecords(), iPage.getTotal());
        }
        return Collections.emptyList();
    }
 
    default void linkUpdate(String id, PosConModel posConModel) {
        //联动修改互斥对象
        List<String> muEList = new ArrayList<>();
        if (posConModel.getMutualExclusionFlag()) {
            muEList.addAll(posConModel.getMutualExclusion());
        }
        //muEList 互斥对象。除了这个列表外其他角色里不能包含该互斥
        QueryWrapper<RoleEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().like(RoleEntity::getConditionJson, id);
        if (CollectionUtil.isNotEmpty(muEList)) {
            queryWrapper.lambda().or().in(RoleEntity::getId, muEList);
        }
 
        List<RoleEntity> list = this.selectList(queryWrapper);
        for (RoleEntity item : list) {
            if (muEList.contains(item.getId())) {
                //添加
                item.setIsCondition(1);
                PosConModel psModel = StringUtil.isEmpty(item.getConditionJson()) ? new PosConModel() : JsonUtil.getJsonToBean(item.getConditionJson(), PosConModel.class);
                List<Integer> constraintType = psModel.getConstraintType() == null ? new ArrayList<>() : psModel.getConstraintType();
                if (!constraintType.contains(0)) {
                    constraintType.add(0);
                    psModel.setConstraintType(constraintType);
                }
                List<String> mutualExclusion = psModel.getMutualExclusion() == null ? new ArrayList<>() : psModel.getMutualExclusion();
                if (!mutualExclusion.contains(id)) {
                    mutualExclusion.add(id);
                    psModel.setMutualExclusion(mutualExclusion);
                    item.setConditionJson(JsonUtil.getObjectToString(psModel));
                }
                this.update(item.getId(), item);
            } else {
                //移除
                if (Objects.equals(item.getIsCondition(), 1)) {
                    PosConModel psModel = JsonUtil.getJsonToBean(item.getConditionJson(), PosConModel.class);
                    psModel.init();
                    if (psModel.getMutualExclusionFlag()) {
                        List<String> mutualExclusion = psModel.getMutualExclusion();
                        if (mutualExclusion.contains(id)) {
                            mutualExclusion.remove(id);
                            if (mutualExclusion.size() == 0) {
                                List<Integer> constraintType = psModel.getConstraintType();
                                constraintType.remove(Integer.valueOf(0));
                                psModel.setConstraintType(constraintType);
                                if (constraintType.size() == 0) {
                                    item.setIsCondition(0);
                                }
                            }
                            item.setConditionJson(JsonUtil.getObjectToString(psModel));
                            this.update(item.getId(), item);
                        }
                    }
                }
            }
        }
    }
}