WeDepartmentServiceImpl.java
7.49 KB
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
package com.linkwechat.wecom.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.HttpStatus;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.enums.WeExceptionTip;
import com.linkwechat.common.exception.CustomException;
import com.linkwechat.common.exception.wecom.WeComException;
import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.SnowFlakeUtil;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.common.utils.bean.BeanUtils;
import com.linkwechat.common.utils.spring.SpringUtils;
import com.linkwechat.wecom.client.WeDepartMentClient;
import com.linkwechat.wecom.domain.WeDepartment;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.dto.WeDepartMentDto;
import com.linkwechat.wecom.domain.dto.WeResultDto;
import com.linkwechat.wecom.mapper.WeDepartmentMapper;
import com.linkwechat.wecom.service.IWeDepartmentService;
import com.linkwechat.wecom.service.IWeUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
/**
* 企业微信组织架构相关Service业务层处理
*
* @author ruoyi
* @date 2020-09-01
*/
@Service
public class WeDepartmentServiceImpl extends ServiceImpl<WeDepartmentMapper, WeDepartment> implements IWeDepartmentService {
@Autowired
private WeDepartMentClient weDepartMentClient;
/**
* 查询企业微信组织架构相关列表
*
* @return 企业微信组织架构相关
*/
@Override
public List<WeDepartment> getList() {
//校验数据中中是否存在根节点,如果不存在,从微信端获取,同时入库
//暂时弃用
/*WeDepartment weDepartment = this.getOne(new LambdaQueryWrapper<WeDepartment>()
.eq(WeDepartment::getDeptId, WeConstans.WE_ROOT_DEPARMENT_ID));
if (null == weDepartment) {
WeDepartMentDto weDepartMentDto = weDepartMentClient.weDepartMents(WeConstans.WE_ROOT_DEPARMENT_ID);
if (CollectionUtils.isNotEmpty(weDepartMentDto.getDepartment())) {
this.save(WeDepartment.transformWeDepartment(weDepartMentDto.getDepartment().stream()
.filter(item -> item.getId().equals(WeConstans.WE_ROOT_DEPARMENT_ID))
.findFirst().get())
);
}
}*/
return this.list();
}
/**
* 新增企业微信组织架构相关
*
* @param weDepartment 企业微信组织架构相关
* @return 结果
*/
@Override
@Transactional(rollbackFor = {Exception.class,WeComException.class},isolation = Isolation.READ_COMMITTED)
public void insert(WeDepartment weDepartment) {
WeResultDto resultDto = weDepartMentClient.createWeDepartMent(
weDepartment.transformDeartMentDto(weDepartment)
);
if (resultDto.getId() != null) {
weDepartment.setId(resultDto.getId());
this.insert2Data(weDepartment);
}
}
@Override
@Transactional(rollbackFor = {Exception.class,WeComException.class},isolation = Isolation.READ_COMMITTED)
public void insert2Data(WeDepartment weDepartment) {
if(!super.update(weDepartment,new LambdaQueryWrapper<WeDepartment>()
.eq(WeDepartment::getId,weDepartment.getId()))){
super.save(weDepartment);
}
}
/**
* 修改企业微信组织架构相关
*
* @param weDepartment 企业微信组织架构相关
* @return 结果
*/
@Override
@Transactional(rollbackFor = {Exception.class,WeComException.class},isolation = Isolation.READ_COMMITTED)
public void update(WeDepartment weDepartment) {
WeResultDto resultDto = weDepartMentClient.updateWeDepartMent(
weDepartment.transformDeartMentDto(weDepartment)
);
if (resultDto.getErrcode().equals(WeConstans.WE_SUCCESS_CODE)) {
this.updateById(weDepartment);
}
}
/**
* 同步部门
*/
@Override
@Transactional(rollbackFor = {Exception.class,WeComException.class},isolation = Isolation.READ_COMMITTED)
public List<WeDepartment> synchWeDepartment() {
List<WeDepartment> weDepartments = weDepartMentClient.weAllDepartMents().findWeDepartments();
if (CollectionUtil.isNotEmpty(weDepartments)) {
List<List<WeDepartment>> lists = Lists.partition(weDepartments, 500);
for(List<WeDepartment> list : lists){
this.baseMapper.insertBatch(list);
}
}
return weDepartments;
}
/**
* 根据部门id删除部门
*
* @param ids
*/
@Override
@Transactional(rollbackFor = {Exception.class,WeComException.class},isolation = Isolation.READ_COMMITTED)
public void deleteByIds(Long[] ids) {
List<WeDepartment> weDepartmentList = this.list(new LambdaQueryWrapper<WeDepartment>()
.in(WeDepartment::getId, CollectionUtil.toList(ids)));
if(CollectionUtil.isNotEmpty(weDepartmentList)){
//查询当前部门下所有的子部门,如果存在,则不可以删除
List<WeDepartment> weDepartments = this.list(new LambdaQueryWrapper<WeDepartment>()
.in(WeDepartment::getParentId, weDepartmentList.stream().map(WeDepartment::getId).collect(Collectors.toList())));
if (CollectionUtil.isNotEmpty(weDepartments)) {
//抛出异常,请删除此部门下的成员或子部门后,再删除此部门
throw new CustomException(WeExceptionTip.WE_EXCEPTION_TIP_60006.getTipMsg(), WeExceptionTip.WE_EXCEPTION_TIP_60006.getCode());
}
weDepartmentList.forEach(weDepartment -> {
List<String> deptIds = CollectionUtil.newArrayList(String.valueOf(weDepartment.getId()));
List<WeUser> weUsers = SpringUtils.getBean(IWeUserService.class).getList(WeUser.builder()
.department(String.valueOf(deptIds))
//这里多企业的话需要加上企业id
.build());
if (CollectionUtil.isNotEmpty(weUsers)) {
//该部门存在成员无法删除
throw new CustomException(WeExceptionTip.WE_EXCEPTION_TIP_60005.getTipMsg(), WeExceptionTip.WE_EXCEPTION_TIP_60005.getCode());
}
});
//删除数据库中数据
if (this.removeByIds(ListUtil.toList(ids))) {
weDepartmentList.forEach(weDepartment -> {
//移除微信端
weDepartMentClient.deleteWeDepartMent(String.valueOf(weDepartment.getId()));
});
}
}
}
@Override
public WeDepartment getByDeptId(Long deptId) {
return this.getOne(new LambdaQueryWrapper<WeDepartment>().eq(WeDepartment::getId, deptId));
}
}