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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package jnpf.permission.service.impl;
 
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import jnpf.base.Pagination;
import jnpf.base.SysConfigApi;
import jnpf.base.service.SuperServiceImpl;
import jnpf.constant.CodeConst;
import jnpf.constant.PermissionConst;
import jnpf.emnus.SysParamEnum;
import jnpf.model.BaseSystemInfo;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.entity.PositionEntity;
import jnpf.permission.entity.UserRelationEntity;
import jnpf.permission.mapper.*;
import jnpf.permission.model.permission.PermissionModel;
import jnpf.permission.model.position.PosConModel;
import jnpf.permission.model.position.PositionListVO;
import jnpf.permission.model.position.PositionPagination;
import jnpf.permission.service.CodeNumService;
import jnpf.permission.service.PositionService;
import jnpf.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.stream.Collectors;
 
/**
 * 岗位信息
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2019年9月26日 上午9:18
 */
@Service
public class PositionServiceImpl extends SuperServiceImpl<PositionMapper, PositionEntity> implements PositionService {
 
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private CacheKeyUtil cacheKeyUtil;
    @Autowired
    private SysConfigApi sysconfigApi;
    @Autowired
    private CodeNumService codeNumService;
 
    @Autowired
    private AuthorizeMapper authorizeMapper;
    @Autowired
    private UserRelationMapper userRelationMapper;
    @Autowired
    private RoleRelationMapper roleRelationMapper;
    @Autowired
    private OrganizeMapper organizeMapper;
 
    @Override
    public List<PositionEntity> getList(PositionPagination pagination) {
        return this.getBaseMapper().getList(pagination);
    }
 
 
    @Override
    public boolean isExistByFullName(PositionEntity entity, boolean isFilter) {
        return this.getBaseMapper().isExistByFullName(entity, isFilter);
    }
 
    @Override
    public Boolean isExistByEnCode(String enCode, String id) {
        return this.getBaseMapper().isExistByEnCode(enCode, id);
    }
 
    @Override
    public void create(PositionEntity entity) {
        if (StringUtil.isEmpty(entity.getEnCode())) {
            entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.GW), code -> this.isExistByEnCode(code, null)));
        }
        this.baseMapper.create(entity);
        redisUtil.remove(cacheKeyUtil.getPositionList());
        redisUtil.remove(TenantHolder.getDatasourceId() + CacheKeyUtil.SYS_POS);
    }
 
    @Override
    public boolean update(String id, PositionEntity entity) {
        if (StringUtil.isEmpty(entity.getEnCode())) {
            entity.setEnCode(codeNumService.getCodeFunction(() -> codeNumService.getCodeOnce(CodeConst.GW), code -> this.isExistByEnCode(code, id)));
        }
        if (Objects.equals(entity.getIsCondition(), 0)) {
            entity.setConditionJson("");
        }
        redisUtil.remove(cacheKeyUtil.getPositionList());
        redisUtil.remove(TenantHolder.getDatasourceId() + CacheKeyUtil.SYS_POS);
        return this.baseMapper.update(id, entity);
    }
 
    @Override
    public PositionEntity getInfo(String id) {
        return this.baseMapper.selectById(id);
    }
 
    @Override
    @DSTransactional
    public void delete(PositionEntity entity) {
        this.removeById(entity.getId());
        userRelationMapper.deleteAllByObjId(entity.getId());
        authorizeMapper.deleteByObjIds(Arrays.asList(entity.getId()));
        roleRelationMapper.deleteAllByObjId(Arrays.asList(entity.getId()));
        redisUtil.remove(cacheKeyUtil.getPositionList());
        redisUtil.remove(TenantHolder.getDatasourceId() + CacheKeyUtil.SYS_POS);
    }
 
    @Override
    public void deleteByOrgId(String orgId) {
        this.baseMapper.deleteByOrgId(orgId);
    }
 
    @Override
    public boolean checkLevel(PositionEntity entity) {
        BaseSystemInfo sysInfo = sysconfigApi.getSysInfo(UserProvider.getUser().getTenantId());
        Integer positionLevel = sysInfo.getPositionLevel();
        PositionEntity pEntity = this.getInfo(entity.getParentId());
        Integer thisLevel = 1;
        if (pEntity != null) {
            String[] parents = pEntity.getPositionIdTree().split(",");
            //创建修改不同判断
            if (StringUtil.isEmpty(entity.getId())) {
                thisLevel += parents.length;
            } else {
                Integer childMax = 1;
                List<PositionEntity> allChild = this.getAllChild(entity.getId());
                for (PositionEntity item : allChild) {
                    String[] trees = item.getPositionIdTree().split(entity.getId() + ",");
                    if (trees.length >= 2) {
                        String[] childs = trees[1].split(",");
                        Integer length = childs.length + 1;
                        if (length > childMax) {
                            childMax = length;
                        }
                    }
                }
                thisLevel = parents.length + childMax;
            }
        }
        if (thisLevel <= positionLevel) {
            return true;
        }
        return false;
    }
 
 
    @Override
    public List<PositionEntity> getParentList(String parentId) {
        return this.baseMapper.getParentList(parentId);
    }
 
    @Override
    public List<PositionEntity> getByParentId(String parentId) {
        return this.baseMapper.getByParentId(parentId);
    }
 
    @Override
    public List<PositionEntity> getListByOrgIds(List<String> orgIds) {
        return this.baseMapper.getListByOrgIds(orgIds);
    }
 
    @Override
    public List<PositionEntity> getListByOrgIdOneLevel(List<String> orgIds) {
        return this.baseMapper.getListByOrgIdOneLevel(orgIds);
    }
 
    @Override
    public List<PositionEntity> getList(boolean filterEnabledMark) {
        return this.baseMapper.getList(filterEnabledMark);
    }
 
    @Override
    public List<PositionEntity> getAllChild(String id) {
        return this.baseMapper.getAllChild(id);
    }
 
    @Override
    public List<PositionEntity> getPosList(List<String> idList) {
        return this.baseMapper.getPosList(idList);
    }
 
    @Override
    public List<PositionEntity> getListByIds(List<String> idList) {
        return this.baseMapper.getListByIds(idList);
    }
 
    @Override
    public List<PositionEntity> getListByIds(Pagination pagination, List<String> idList) {
        return this.baseMapper.getListByIds(pagination, idList);
    }
 
    @Override
    public Map<String, String> getPosMap() {
        return this.baseMapper.getPosMap();
    }
 
    @Override
    public Map<String, String> getPosFullNameMap() {
        if (redisUtil.exists(cacheKeyUtil.getPositionList())) {
            return new HashMap<>(redisUtil.getMap(cacheKeyUtil.getPositionList()));
        } else {
            QueryWrapper<PositionEntity> queryWrapper = new QueryWrapper<>();
            queryWrapper.lambda().select(PositionEntity::getId, PositionEntity::getFullName, PositionEntity::getOrganizeId);
            List<PositionEntity> list = this.list(queryWrapper);
            Map<String, String> infoMap = new HashMap<>(list.size());
            Map<String, Object> allOrgsTreeName = organizeMapper.getAllOrgsTreeName();
            for (PositionEntity t : list) {
                String fullName = t.getFullName();
                if (Objects.nonNull(allOrgsTreeName.get(t.getOrganizeId()))) {
                    fullName = allOrgsTreeName.get(t.getOrganizeId()) + "/" + t.getFullName();
                }
                infoMap.put(t.getId(), fullName);
            }
            redisUtil.insert(cacheKeyUtil.getPositionList(), infoMap);
            return infoMap;
        }
    }
 
 
    @Override
    public Map<String, Object> getPosEncodeAndName() {
        return getPosEncodeAndName(false);
    }
 
    @Override
    public Map<String, Object> getPosEncodeAndName(boolean enabledMark) {
        return this.baseMapper.getPosEncodeAndName(enabledMark);
    }
 
    @Override
    @DSTransactional
    public boolean first(String id) {
        return this.baseMapper.first(id);
    }
 
    @Override
    @DSTransactional
    public boolean next(String id) {
        return this.baseMapper.next(id);
    }
 
    @Override
    public List<PositionEntity> getPositionName(List<String> id, boolean filterEnabledMark) {
        return this.baseMapper.getPositionName(id, filterEnabledMark);
    }
 
    @Override
    public List<PositionEntity> getPositionName(List<String> id, String keyword) {
        return this.baseMapper.getPositionName(id, keyword);
    }
 
    @Override
    public List<PositionEntity> getListByOrganizeId(List<String> organizeIds, boolean enabledMark) {
        return this.baseMapper.getPositionName(organizeIds, enabledMark);
    }
 
    @Override
    public List<PositionEntity> getListByOrgIdAndUserId(String organizeId, String userId) {
        // 用户绑定的所有岗位
        List<String> positionIds = userRelationMapper.getListByUserIdAndObjType(userId, PermissionConst.POSITION).stream()
                .map(UserRelationEntity::getObjectId).collect(Collectors.toList());
        if (!positionIds.isEmpty()) {
            List<PositionEntity> positionEntities = this.listByIds(positionIds);
            return positionEntities.stream().filter(p -> p.getOrganizeId().equals(organizeId)).collect(Collectors.toList());
        } else {
            return new ArrayList<>();
        }
    }
 
    @Override
    public PositionEntity getByFullName(String fullName) {
        return this.baseMapper.getByFullName(fullName);
    }
 
    @Override
    public List<PositionEntity> getListByFullName(String fullName, String enCode) {
        return this.baseMapper.getListByFullName(fullName, enCode);
    }
 
    @Override
    public List<PermissionModel> getListByOrganizeIds(List<String> organizeIds, boolean needCode, boolean enabledMark) {
        List<PermissionModel> permissionList = new LinkedList<>();
        for (String organizeId : organizeIds) {
            OrganizeEntity info = organizeMapper.getInfo(organizeId);
            if (info != null) {
                PermissionModel parentModel = new PermissionModel();
                List<PositionEntity> list = this.getListByOrganizeId(Collections.singletonList(organizeId), enabledMark);
                list.forEach(t -> {
                    if (needCode) {
                        t.setFullName(t.getFullName() + "/" + t.getEnCode());
                    }
                });
                List<PermissionModel> positionModels = JsonUtil.getJsonToList(list, PermissionModel.class);
                parentModel.setChildren(positionModels);
                parentModel.setHasChildren(true);
                parentModel.setFullName(info.getFullName());
                parentModel.setId(info.getId());
                permissionList.add(parentModel);
            }
        }
        return permissionList;
    }
 
    @Override
    public List<PositionListVO> selectedList(List<String> idStrList) {
        if (CollectionUtil.isEmpty(idStrList)) return Collections.EMPTY_LIST;
        List<String> idList = new ArrayList<>();
 
        for (String idStr : idStrList) {
            String[] split = idStr.split("--");
            idList.add(split[0]);
        }
        Map<String, Object> allOrgsTreeName = organizeMapper.getAllOrgsTreeName();
 
        List<PositionEntity> listByIds = this.getListByIds(idList);
        List<PositionListVO> listVo = new ArrayList<>();
        for (String idStr : idStrList) {
            String[] split = idStr.split("--");
            String id = split[0];
            String type = split.length > 1 ? split[1] : "";
            SysParamEnum sysParamEnum = SysParamEnum.get(type);
            String suffix = sysParamEnum != null ? sysParamEnum.getSuffix() : "";
            PositionEntity positionEntity = listByIds.stream().filter(t -> t.getId().equals(id)).findFirst().orElse(null);
            if (positionEntity != null) {
                PositionListVO vo = JsonUtil.getJsonToBean(positionEntity, PositionListVO.class);
                vo.setId(idStr);
                vo.setOrgNameTree(allOrgsTreeName.get(positionEntity.getOrganizeId()) + "/" + positionEntity.getFullName() + suffix);
                listVo.add(vo);
            }
        }
        return listVo;
    }
 
    @Override
    public List<PositionEntity> positionCondition(List<String> idStrList) {
        return this.baseMapper.positionCondition(idStrList);
    }
 
    @Override
    public List<PositionEntity> getListByDutyUser(String userId) {
        return this.baseMapper.getListByDutyUser(userId);
    }
 
    @Override
    public List<PositionEntity> getListByParentIds(List<String> idList) {
        return this.baseMapper.getListByParentIds(idList);
    }
 
    @Override
    public void linkUpdate(String id, PosConModel posConModel) {
        this.baseMapper.linkUpdate(id, posConModel);
    }
 
    @Override
    public String getNameByIdStr(String idStr) {
        StringJoiner sj = new StringJoiner(",");
        if (StringUtil.isNotEmpty(idStr)) {
            try {
                List<String> ids = JsonUtil.getJsonToList(idStr, String.class);
                List<PositionEntity> listByIds = this.getListByIds(ids);
                for (PositionEntity item : listByIds) {
                    OrganizeEntity orgInfo = organizeMapper.getInfo(item.getOrganizeId());
                    sj.add(orgInfo.getOrgNameTree() + "/" + item.getFullName());
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return sj.toString();
    }
 
    @Override
    public List<PositionEntity> getProgeny(List<String> idList, Integer enabledMark) {
        return this.baseMapper.getProgeny(idList, enabledMark);
    }
}