zhanghua
2023-07-05 8439887c903fa52560af52b40e12a1baa8a123d3
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
package com.ycl.service.ding.impl;
 
import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ycl.entity.depart.UmsDepart;
import com.ycl.entity.user.UmsAdmin;
import com.ycl.entity.user.UmsAdminRoleRelation;
import com.ycl.entity.user.UmsRole;
import com.ycl.service.depart.IDepartManagerService;
import com.ycl.service.depart.UmsDepartService;
import com.ycl.service.ding.BookRemarkService;
import com.ycl.service.ding.DingService;
import com.ycl.service.user.UmsAdminRoleRelationService;
import com.ycl.service.user.UmsAdminService;
import com.ycl.service.user.UmsRoleService;
import com.ycl.vo.AddressBookVO;
import com.ycl.vo.ContactVO;
import com.ycl.vo.NewAddressBookVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
 
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
@Service
@Slf4j
public class DingServiceImpl implements DingService {
 
    @Autowired
    private UmsDepartService departService;
    @Autowired
    private IDepartManagerService departManagerService;
 
    @Autowired
    private UmsAdminService umsAdminService;
 
    @Autowired
    private UmsRoleService umsRoleService;
 
    @Autowired
    private UmsAdminRoleRelationService umsAdminRoleRelationService;
 
    @Autowired
    private BookRemarkService bookRemarkService;
 
    @Override
    public List<AddressBookVO> getAddressBook(Long userId) {
        List<UmsAdmin> users = umsAdminService.list();
        Map<Long, UmsAdmin> userMap = users.stream().collect(Collectors.toMap(UmsAdmin::getId, Function.identity()));
        List<AddressBookVO> addressBookVOS = new ArrayList<>();
 
        List<UmsDepart> list = departService.list(new QueryWrapper<UmsDepart>().lambda().eq(UmsDepart::getStatus, 1).eq(UmsDepart::getIsDeleted, 0));
        //查询备注信息
        Map<Long, String> remarkMap = bookRemarkService.findByUserId(userId);
        list.forEach(item -> {
            //构建部门信息
            extracted(userMap, addressBookVOS, item, remarkMap);
        });
        log.info(JSON.toJSONString(addressBookVOS));
 
        return buildTreeUseStream(addressBookVOS, 0);
    }
 
    @Override
    public List<AddressBookVO> getAddressBookByParentId(Long id, Long userId) {
        List<UmsAdmin> users = umsAdminService.list();
        Map<Long, UmsAdmin> userMap = users.stream().collect(Collectors.toMap(UmsAdmin::getId, Function.identity()));
        List<AddressBookVO> addressBookVOS = new ArrayList<>();
 
        List<UmsDepart> list = departService.list(new QueryWrapper<UmsDepart>().lambda().eq(UmsDepart::getStatus, 1).eq(UmsDepart::getParentId, id).eq(UmsDepart::getIsDeleted, 0));
        //查询备注信息
        Map<Long, String> remarkMap = bookRemarkService.findByUserId(userId);
        list.forEach(item -> {
            //构建部门信息
            extracted(userMap, addressBookVOS, item, remarkMap);
        });
        log.info(JSON.toJSONString(addressBookVOS));
 
        return addressBookVOS;
    }
 
    @Override
    public NewAddressBookVO getAddressBookById(Long id, Long userId) {
        List<UmsAdmin> users = umsAdminService.list();
        Map<Long, UmsAdmin> userMap = users.stream().collect(Collectors.toMap(UmsAdmin::getId, Function.identity()));
        NewAddressBookVO newAddressBookVO = new NewAddressBookVO();
        List<UmsDepart> departs = departService.list(new QueryWrapper<UmsDepart>().lambda().eq(UmsDepart::getStatus, 1).eq(UmsDepart::getParentId, id).eq(UmsDepart::getIsDeleted, 0));
        Set<Long> deptIds = departs.stream().map(UmsDepart::getParentId).collect(Collectors.toSet());
         //部门信息
        newAddressBookVO.setDeparts(departs);
        Map<Long, String> remarkMap = bookRemarkService.findByUserId(userId);
        Collection<Long> userIds = departManagerService.getByDeptId(id);
        ArrayList<ContactVO> contacts = new ArrayList<>();
        //角色信息
        List<UmsAdminRoleRelation> list = umsAdminRoleRelationService.list();
        Map<Long, List<UmsAdminRoleRelation>> rolesMap = list.stream().collect(Collectors.groupingBy(UmsAdminRoleRelation::getAdminId));
        Map<Long, String> roleName = umsRoleService.list().stream().filter(o -> o.getStatus() == 1).collect(Collectors.toMap(UmsRole::getId, UmsRole::getName));
        //构建用户信息
        if (CollUtil.isNotEmpty(userIds)) {
            userIds.forEach(u -> {
                UmsAdmin admin = userMap.get(u);
                if (Objects.nonNull(admin)) {
                    ContactVO contactVO = new ContactVO();
                    contactVO.setUserId(admin.getId());
                    contactVO.setSex(admin.getSex());
                    contactVO.setUsername(admin.getUsername());
                    contactVO.setNickName(admin.getNickName());
                    contactVO.setIcon(admin.getIcon());
                    contactVO.setMobile(admin.getMobile());
                    contactVO.setRemark(remarkMap.get(admin.getId()));
                    //添加用户角色信息
                    List<UmsAdminRoleRelation> roleRelations = rolesMap.get(admin.getId());
                    if (CollUtil.isNotEmpty(roleRelations)) {
                        ArrayList<String> roles = new ArrayList<>();
                        Set<Long> roleId = roleRelations.stream().map(UmsAdminRoleRelation::getRoleId).collect(Collectors.toSet());
                        for (Long o : roleId) {
                            String s = roleName.get(o);
                            if (StringUtils.isNotBlank(s)) {
                                roles.add(s);
                            }
                        }
                        contactVO.setRoles(roles);
                    }
                    contacts.add(contactVO);
                }
            });
            newAddressBookVO.setContacts(contacts);
        }
        return newAddressBookVO;
    }
 
    // 使用stream流转换
    public static List<AddressBookVO> buildTreeUseStream(List<AddressBookVO> treeList, long id) {
        List<AddressBookVO> list = treeList.stream()
                //过滤父节点与传递的id相同的TreeNode对象
                .filter(treeNode -> treeNode.getParentId() == id)
                .map(treeNode -> {
                    //递归设置孩子节点
                    treeNode.setChildren(buildTreeUseStream(treeList, treeNode.getDeptId()));
                    return treeNode;
                })
                .collect(Collectors.toList());
        return list;
    }
 
 
    private void extracted(Map<Long, UmsAdmin> userMap, List<AddressBookVO> addressBookVOS, UmsDepart depart, Map<Long, String> remarkMap) {
 
 
        AddressBookVO books = new AddressBookVO();
        books.setDeptId(depart.getId());
        books.setParentId(depart.getParentId());
        books.setDeptName(depart.getDepartName());
        books.setDeptCode(depart.getCode());
        ArrayList<ContactVO> contacts = new ArrayList<>();
        Collection<Long> userIds = departManagerService.getByDeptId(depart.getId());
        //角色信息
        List<UmsAdminRoleRelation> list = umsAdminRoleRelationService.list();
        Map<Long, List<UmsAdminRoleRelation>> rolesMap = list.stream().collect(Collectors.groupingBy(UmsAdminRoleRelation::getAdminId));
        Map<Long, String> roleName = umsRoleService.list().stream().filter(o -> o.getStatus() == 1).collect(Collectors.toMap(UmsRole::getId, UmsRole::getName));
        //构建用户信息
        if (CollUtil.isNotEmpty(userIds)) {
            userIds.forEach(u -> {
                UmsAdmin admin = userMap.get(u);
                if (Objects.nonNull(admin)) {
                    ContactVO contactVO = new ContactVO();
                    contactVO.setUserId(admin.getId());
                    contactVO.setSex(admin.getSex());
                    contactVO.setUsername(admin.getUsername());
                    contactVO.setNickName(admin.getNickName());
                    contactVO.setIcon(admin.getIcon());
                    contactVO.setMobile(admin.getMobile());
                    contactVO.setRemark(remarkMap.get(admin.getId()));
                    //添加用户角色信息
                    List<UmsAdminRoleRelation> roleRelations = rolesMap.get(admin.getId());
                    if (CollUtil.isNotEmpty(roleRelations)) {
                        ArrayList<String> roles = new ArrayList<>();
                        Set<Long> roleId = roleRelations.stream().map(UmsAdminRoleRelation::getRoleId).collect(Collectors.toSet());
                        for (Long id : roleId) {
                            String s = roleName.get(id);
                            if (StringUtils.isNotBlank(s)) {
                                roles.add(s);
                            }
                        }
                        contactVO.setRoles(roles);
                    }
                    contacts.add(contactVO);
                }
            });
            books.setContacts(contacts);
        }
        addressBookVOS.add(books);
    }
}