ny
23 小时以前 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
383
384
385
386
387
388
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 com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.JoinWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import jnpf.base.UserInfo;
import jnpf.base.service.SuperServiceImpl;
import jnpf.constant.MsgCode;
import jnpf.constant.PermissionConst;
import jnpf.permission.entity.OrganizeEntity;
import jnpf.permission.entity.PositionEntity;
import jnpf.permission.entity.UserEntity;
import jnpf.permission.entity.UserRelationEntity;
import jnpf.permission.mapper.*;
import jnpf.permission.model.position.PosistionCurrentModel;
import jnpf.permission.model.user.page.UserPagination;
import jnpf.permission.model.user.vo.UserListVO;
import jnpf.permission.model.userrelation.UserRelationForm;
import jnpf.permission.service.UserRelationService;
import jnpf.permission.service.UserService;
import jnpf.permission.util.UserUtil;
import jnpf.util.RandomUtil;
import jnpf.util.StringUtil;
import jnpf.util.UserProvider;
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 UserRelationServiceImpl extends SuperServiceImpl<UserRelationMapper, UserRelationEntity> implements UserRelationService {
 
    @Autowired
    private UserUtil userUtil;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private OrganizeMapper organizeMapper;
    @Autowired
    private RoleRelationMapper roleRelationMapper;
 
    @Override
    public List<UserListVO> getListPage(UserPagination pagination) {
        String objectType = StringUtil.isNotEmpty(pagination.getPositionId()) ? PermissionConst.POSITION : PermissionConst.ORGANIZE;
        String objectId = StringUtil.isNotEmpty(pagination.getPositionId()) ? pagination.getPositionId() : pagination.getOrganizeId();
        //是否显示子孙组织用户
        List<String> orgIds = new ArrayList<>();
        if (Objects.equals(pagination.getShowSubOrganize(), 1)) {
            List<OrganizeEntity> allChild = organizeMapper.getAllChild(pagination.getOrganizeId());
            orgIds.addAll(allChild.stream().map(OrganizeEntity::getId).collect(Collectors.toList()));
        }
 
        MPJLambdaWrapper<UserRelationEntity> queryWrapper = JoinWrappers.lambda(UserRelationEntity.class);
        queryWrapper.selectAs(UserEntity::getId, UserListVO::getId);
        queryWrapper.selectAs(UserEntity::getAccount, UserListVO::getAccount);
        queryWrapper.selectAs(UserEntity::getRealName, UserListVO::getRealName);
        queryWrapper.selectAs(UserEntity::getGender, UserListVO::getGender);
        queryWrapper.selectAs(UserEntity::getMobilePhone, UserListVO::getMobilePhone);
        queryWrapper.selectAs(UserEntity::getEnabledMark, UserListVO::getEnabledMark);
        queryWrapper.selectAs(UserEntity::getUnlockTime, UserListVO::getUnlockTime);
        queryWrapper.selectMax(UserRelationEntity::getId, UserListVO::getMaxRelationId);
 
        queryWrapper.leftJoin(UserEntity.class, UserEntity::getId, UserRelationEntity::getUserId);
        if (!StringUtil.isEmpty(pagination.getKeyword())) {
            queryWrapper.and(
                    t -> t.like(UserEntity::getAccount, pagination.getKeyword())
                            .or().like(UserEntity::getRealName, pagination.getKeyword())
                            .or().like(UserEntity::getMobilePhone, pagination.getKeyword())
            );
        }
        if (pagination.getEnabledMark() != null) {
            queryWrapper.eq(UserEntity::getEnabledMark, pagination.getEnabledMark());
        }
 
        if (orgIds.size() > 0) {
            queryWrapper.in(UserRelationEntity::getObjectId, orgIds);
        } else {
            queryWrapper.eq(UserRelationEntity::getObjectId, objectId);
        }
 
        queryWrapper.eq(UserRelationEntity::getObjectType, objectType);
 
        queryWrapper.orderByDesc("maxRelationId");
 
        queryWrapper.groupBy(UserEntity::getId, UserEntity::getAccount, UserEntity::getRealName, UserEntity::getGender, UserEntity::getMobilePhone,
                UserEntity::getEnabledMark, UserEntity::getUnlockTime);
        Page<UserListVO> page = new Page<>(pagination.getCurrentPage(), pagination.getPageSize());
        IPage<UserListVO> data = this.selectJoinListPage(page, UserListVO.class, queryWrapper);
        return pagination.setData(data.getRecords(), page.getTotal());
    }
 
    @Override
    public List<UserRelationEntity> getListByUserId(String userId) {
        return this.baseMapper.getListByUserId(userId);
    }
 
    @Override
    public List<UserRelationEntity> getListByUserIdAndObjType(String userId, String objectType) {
        return this.baseMapper.getListByUserIdAndObjType(userId, objectType);
    }
 
    @Override
    public List<UserRelationEntity> getListByUserIdAll(List<String> userId) {
        return this.baseMapper.getListByUserIdAll(userId);
    }
 
    @Override
    public List<UserRelationEntity> getListByObjectId(String objectId) {
        return this.baseMapper.getListByObjectId(objectId);
    }
 
    @Override
    public List<UserRelationEntity> getListByObjectType(String objectType) {
        return this.baseMapper.getListByObjectType(objectType);
    }
 
    @Override
    public List<UserRelationEntity> getListByObjectId(String objectId, String objectType) {
        return this.baseMapper.getListByObjectId(objectId, objectType);
    }
 
    @Override
    public List<UserRelationEntity> getListByObjectIdAll(List<String> objectId) {
        return this.baseMapper.getListByObjectIdAll(objectId);
    }
 
    @Override
    public void deleteAllByObjId(String objId) {
        this.baseMapper.deleteAllByObjId(objId);
    }
 
    @Override
    public void deleteAllByUserId(List<String> userId) {
        this.baseMapper.deleteAllByUserId(userId);
    }
 
 
    @Override
    public UserRelationEntity getInfo(String id) {
        return this.baseMapper.getInfo(id);
    }
 
    @Override
    public void save(String objectId, List<UserRelationEntity> entitys) {
        this.baseMapper.save(objectId, entitys);
    }
 
    @Override
    public void save(List<UserRelationEntity> list) {
        this.baseMapper.save(list);
    }
 
    @Override
    @DSTransactional
    public void delete(UserRelationForm form) {
        List<String> userIds = form.getUserIds();
        String type = form.getObjectType();
        if (CollectionUtil.isEmpty(userIds)) {
            return;
        }
        QueryWrapper<UserRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserRelationEntity::getObjectId, form.getObjectId());
        queryWrapper.lambda().eq(UserRelationEntity::getObjectType, type);
        queryWrapper.lambda().in(UserRelationEntity::getUserId, userIds);
        List<UserRelationEntity> list = this.list(queryWrapper);
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
 
        if (PermissionConst.POSITION.equals(type)) {
            PositionEntity info = positionMapper.getInfo(form.getObjectId());
            //如果责任人被移除则清空责任人
            if (StringUtil.isNotEmpty(info.getDutyUser()) && userIds.contains(info.getDutyUser())) {
                info.setDutyUser(null);
                positionMapper.updateById(info);
            }
            //岗位时顺便移除组织关系
            for (String userId : userIds) {
                queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(UserRelationEntity::getObjectId, info.getOrganizeId());
                queryWrapper.lambda().eq(UserRelationEntity::getObjectType, PermissionConst.ORGANIZE);
                queryWrapper.lambda().in(UserRelationEntity::getUserId, userId);
                List<UserRelationEntity> orgList = this.list(queryWrapper);
                if (CollectionUtil.isNotEmpty(orgList)) {
                    list.add(orgList.get(0));
                }
            }
        }
        for (UserRelationEntity item : list) {
            this.removeById(item);
        }
 
        if (PermissionConst.POSITION.equals(type)) {
            userUtil.delCurUser(MsgCode.PS010.get(), form.getUserIds());
        }
    }
 
    @Override
    public List<UserRelationEntity> getRelationByUserIds(List<String> userIds) {
        return this.baseMapper.getRelationByUserIds(userIds);
    }
 
    @Override
    public List<UserRelationEntity> getListByObjectType(String userId, String objectType) {
        return this.baseMapper.getListByObjectType(userId, objectType);
    }
 
    @Override
    public List<UserRelationEntity> getAllOrgRelationByUserId(String userId) {
        return this.getListByObjectType(userId, PermissionConst.ORGANIZE);
    }
 
    @Override
    public List<PosistionCurrentModel> getObjectVoList() {
        UserInfo user = UserProvider.getUser();
        String userId = user.getUserId();
        String majorPosId = user.getPositionId();
        // 岗位遵循一对多关系
        List<String> ids = new ArrayList<>();
        this.getListByObjectType(userId, PermissionConst.POSITION).forEach(r -> {
            ids.add(r.getObjectId());
        });
        List<PositionEntity> positionList = positionMapper.getListByIds(ids);
        if (positionList.size() > 0) {
            List<PosistionCurrentModel> voList = new ArrayList<>();
            for (PositionEntity p : positionList) {
                PosistionCurrentModel model = new PosistionCurrentModel();
                OrganizeEntity orgInfo = organizeMapper.getInfo(p.getOrganizeId());
                model.setId(p.getId());
                model.setPositionId(p.getId());
                model.setFullName(p.getFullName());
                model.setOrganizeId(orgInfo.getId());
                model.setOrgTreeName(orgInfo.getOrgNameTree());
 
                //上级岗位
                if (StringUtil.isNotEmpty(p.getParentId())) {
                    PositionEntity pPosition = positionMapper.getInfo(p.getParentId());
                    if (pPosition != null) {
                        model.setParentId(pPosition.getId());
                        model.setParentName(pPosition.getFullName());
                        UserEntity dutyUser = userMapper.getInfo(pPosition.getDutyUser());
                        if (dutyUser != null) {
                            model.setManagerId(dutyUser.getId());
                            model.setManagerName(dutyUser.getRealName());
                        }
                    }
                } else {
                    //没有上级岗位的时候--岗位为空,上级责任人去组织上级责任岗位的责任人
                    OrganizeEntity pOrgInfo = organizeMapper.getInfo(orgInfo.getParentId());
                    if (pOrgInfo != null) {
                        PositionEntity dutyPos = positionMapper.getInfo(pOrgInfo.getDutyPosition());
                        if (dutyPos != null) {
                            UserEntity dutyUser = userMapper.getInfo(dutyPos.getDutyUser());
                            if (dutyUser != null) {
                                model.setManagerId(dutyUser.getId());
                                model.setManagerName(dutyUser.getRealName());
                            }
                        }
                    }
                }
                model.setManagerId(p.getId());
 
                if (p.getId().equals(majorPosId)) {
                    model.setIsDefault(true);
                } else {
                    model.setIsDefault(false);
                }
                voList.add(model);
            }
            return voList;
        }
        return Collections.EMPTY_LIST;
    }
 
    @Override
    public Boolean existByObj(String objectType, String objectId) {
        return this.baseMapper.existByObj(objectType, objectId);
    }
 
    @Override
    public Boolean existByObj(String objectType, List<String> objectId) {
        return this.baseMapper.existByObj(objectType, objectId);
    }
 
    @Override
    public List<UserRelationEntity> getListByRoleId(String roleId) {
        List<UserRelationEntity> list = new ArrayList<>();
        roleRelationMapper.getListByRoleId(roleId, PermissionConst.ORGANIZE).forEach(o -> {
            QueryWrapper<UserRelationEntity> query = new QueryWrapper<>();
            query.lambda()
                    .eq(UserRelationEntity::getObjectType, PermissionConst.ORGANIZE)
                    .eq(UserRelationEntity::getObjectId, o.getObjectId());
            list.addAll(this.baseMapper.selectList(query));
        });
        return list;
    }
 
    @Override
    public List<UserRelationEntity> getListByUserId(String userId, String objectType) {
        return this.baseMapper.getListByUserId(userId, objectType);
    }
 
    @Override
    public List<UserRelationEntity> getListByOrgId(List<String> orgIdList) {
        return this.baseMapper.getListByOrgId(orgIdList);
    }
 
    @Override
    public List<UserEntity> getUserProgeny(List<String> idList, String enableMark) {
        return userUtil.getUserProgeny(idList, enableMark);
    }
 
    @Override
    public List<UserEntity> getUserAndSub(List<String> idList, String enableMark) {
        return userUtil.getUserAndSub(idList, enableMark);
    }
 
    @Override
    public void updateOrgToNew(List<String> positionIds, String oldOrgId, String newOrgId) {
        List<UserRelationEntity> userPos = this.baseMapper.getListByObjectIdAll(positionIds);
        Map<String, List<UserRelationEntity>> userPosMap = userPos.stream().collect(Collectors.groupingBy(UserRelationEntity::getUserId));
        List<UserRelationEntity> userOrg = this.baseMapper.getListByObjectId(oldOrgId, PermissionConst.ORGANIZE);
        Map<String, List<UserRelationEntity>> userOrgMap = userOrg.stream().collect(Collectors.groupingBy(UserRelationEntity::getUserId));
        for (String userId : userPosMap.keySet()) {
            List<UserRelationEntity> upos = userPosMap.get(userId);
            List<UserRelationEntity> uorg = userOrgMap.get(userId);
            List<UserRelationEntity> updateList = new ArrayList<>();
            List<UserRelationEntity> insertList = new ArrayList<>();
            if (userOrgMap.get(userId) != null) {
                if (uorg.size() >= upos.size()) {
                    updateList.addAll(uorg.subList(0, upos.size()));
                } else {
                    updateList.addAll(uorg);
                    int n = upos.size() - uorg.size();
                    for (int i = 0; i < n; i++) {
                        UserRelationEntity urel = new UserRelationEntity();
                        urel.setId(RandomUtil.uuId());
                        urel.setUserId(userId);
                        urel.setObjectId(newOrgId);
                        urel.setObjectType(PermissionConst.ORGANIZE);
                        insertList.add(urel);
                    }
                }
            } else {
                UserRelationEntity urel = new UserRelationEntity();
                urel.setId(RandomUtil.uuId());
                urel.setUserId(userId);
                urel.setObjectId(newOrgId);
                urel.setObjectType(PermissionConst.ORGANIZE);
                insertList.add(urel);
            }
            for (UserRelationEntity item : updateList) {
                item.setObjectId(newOrgId);
                this.baseMapper.updateById(item);
            }
            for (UserRelationEntity item : insertList) {
                this.baseMapper.insert(item);
            }
        }
    }
 
    @Override
    public void removeOrgRelation(List<UserRelationEntity> userRelationEntities, String userId) {
        this.removeBatchByIds(userRelationEntities.stream()
                .map(UserRelationEntity::getId).collect(Collectors.toList()));
        List<String> collect = userRelationEntities.stream()
                .map(UserRelationEntity::getObjectId).collect(Collectors.toList());
 
        List<PositionEntity> listByOrganizeId = positionMapper.getListByOrganizeId(collect, false);
        this.baseMapper.deleteByPosIdAndUserId(listByOrganizeId.stream()
                .map(PositionEntity::getId).collect(Collectors.toList()),userId);
    }
}