bug
jiangqs
2023-09-06 1ca4c6c6c7861c07980ad0dcd8d5df4b0219bfdc
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
package com.ruoyi.system.service.impl.staff;
 
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.system.api.domain.poji.sys.SysDept;
import com.ruoyi.system.api.domain.vo.MgtUserTaskMsgVo;
import com.ruoyi.system.api.service.RemoteShopService;
import com.ruoyi.system.domain.dto.MgtWxCPStaffDto;
import com.ruoyi.system.api.domain.poji.sys.SysStaff;
import com.ruoyi.system.service.staff.SysStaffService;
import com.ruoyi.system.service.staff.SysWxCpService;
import com.ruoyi.system.service.sys.ISysDeptService;
import lombok.extern.log4j.Log4j2;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.cp.api.WxCpDepartmentService;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpDepart;
import me.chanjar.weixin.cp.bean.WxCpUser;
import me.chanjar.weixin.cp.bean.message.WxCpMessage;
import me.chanjar.weixin.cp.bean.message.WxCpMessageSendResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
import java.util.List;
 
/**
 * @ClassName WxCpServiceImpl
 * @Description TODO
 * @Author jqs
 * @Date 2023/8/17 15:25
 * @Version 1.0
 */
@Service
@Log4j2
public class SysWxCpServiceImpl implements SysWxCpService {
 
    @Autowired
    @Qualifier("wxService")
    private WxCpService wxCpService;
 
    @Autowired
    @Qualifier("adWxService")
    private WxCpService adWxCpService;
 
    @Resource
    private RemoteShopService remoteShopService;
 
    @Resource
    private ISysDeptService  sysDeptService;
 
    @Resource
    private SysStaffService sysStaffService;
 
    /**
     * @description
     * @author  jqs
     * @date    2023/8/17 15:26
     * @param
     * @return  String
     */
    @Override
    public String getToken(){
        String accessToken = null;
        try {
            accessToken = wxCpService.getAccessToken();
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return accessToken;
    }
 
    /**
     * @description  同步部门
     * @author  jqs
     * @date    2023/8/17 15:42
     * @param
     * @return  void
     */
    @Override
    public void syncDepartment(){
        WxCpDepartmentService wxCpDepartmentService = wxCpService.getDepartmentService();
        try {
            //从企业微信拉去部门名单
            List<WxCpDepart> wxCpDepartList = wxCpDepartmentService.list(null);
            if(wxCpDepartList!=null&&wxCpDepartList.size()>0){
                for(WxCpDepart wxCpDepart : wxCpDepartList){
                    if(wxCpDepart.getParentId().compareTo(24L)<0){
                        //获取系统内对应部门
                        SysDept sysDept = sysDeptService.getByWxDeptId(wxCpDepart.getId());
                        //判断是否有该部门,没有则新建
                        if(sysDept!=null){
                            //判断是否有变化
                            if(!sysDept.getDeptName().equals(wxCpDepart.getName())||sysDept.getOrderNum()!=wxCpDepart.getOrder().intValue()){
                                sysDept.setDeptName(wxCpDepart.getName());
                                sysDept.setOrderNum(wxCpDepart.getOrder().intValue());
                                sysDept.setUpdateBy("企业微信同步");
                                sysDeptService.updateDept(sysDept);
                                log.info("企业微信同步更新部门:"+wxCpDepart.getName());
                            }
                        }else{
                            sysDept = new SysDept();
                            sysDept.setDelFlag("0");
                            sysDept.setStatus("0");
                            sysDept.setParentId(100L);
                            sysDept.setOrderNum(wxCpDepart.getOrder().intValue());
                            sysDept.setCreateBy("企业微信同步");
                            sysDept.setDeptName(wxCpDepart.getName());
                            sysDept.setWxDeptId(wxCpDepart.getId());
                            sysDeptService.insertDept(sysDept);
                            log.info("企业微信同步新增部门:"+wxCpDepart.getName());
                        }
                    }
                }
            }
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * @description  企业微信消息推送
     * @author  jqs
     * @date    2023/8/29 11:05
     * @return  void
     */
    @Override
    public void sendTaskMessage(){
        //获取员工任务列表
        List<MgtUserTaskMsgVo> userTaskMsgVoList = remoteShopService.getTaskMsgList().getData();
        log.info("定时发送通知任务任务列表---"+userTaskMsgVoList.size());
        WxCpMessage message;
        SysStaff sysStaff;
        if(userTaskMsgVoList!=null&&!userTaskMsgVoList.isEmpty()){
            //循环发送消息
            for(MgtUserTaskMsgVo mgtUserTaskMsgVo : userTaskMsgVoList){
                sysStaff = sysStaffService.getByUserId(mgtUserTaskMsgVo.getUserId());
                if(sysStaff!=null&& StringUtils.isNotBlank(sysStaff.getWxUserId())){
                    message = new WxCpMessage();
                    message.setAgentId(1000024);
                    message.setToUser(sysStaff.getWxUserId());
                    message.setMsgType("text");
                    message.setContent(mgtUserTaskMsgVo.getTaskTitle()+"今日需要任务跟进,请注意!");
                    try {
                        WxCpMessageSendResult result = wxCpService.getMessageService().send(message);
                        System.out.println(result);
                    } catch (WxErrorException e) {
                        throw new RuntimeException(e);
                    }
                }
            }
        }
    }
 
    /**
     * @description  更新企业微信部门
     * @author  jqs
     * @date    2023/9/4 15:19
     * @param wxDeptId
     * @param deptName
     * @return  void
     */
    @Override
    public void updateWxCpDept(Long wxDeptId,String deptName){
        WxCpDepart group = new WxCpDepart();
        group.setId(wxDeptId);
        group.setName(deptName);
        try {
            adWxCpService.getDepartmentService().update(group);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * @description  创建企业微信部门
     * @author  jqs
     * @date    2023/9/4 15:19
     * @param parentId
     * @param deptName
     * @return  void
     */
    @Override
    public Long addWxCpDept(Long parentId,String deptName){
        WxCpDepart depart = new WxCpDepart();
        depart.setParentId(parentId);
        depart.setName(deptName);
        Long departId = null;
        try {
            departId = adWxCpService.getDepartmentService().create(depart);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return departId;
    }
 
    /**
     * @description  删除企业微信部门
     * @author  jqs
     * @date    2023/9/4 15:19
     * @param wxDeptId
     * @return  void
     */
    @Override
    public void deleteWxCpDept(Long wxDeptId){
        try {
            adWxCpService.getDepartmentService().delete(wxDeptId);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
 
    /**
     * @description  通过手机号获取wxUserId
     * @author  jqs
     * @date    2023/9/4 15:44
     * @param mobile
     * @return  String
     */
    private String getWxUserIdByMobile(String mobile){
        String wxUserId = null;
        try {
            wxUserId = adWxCpService.getUserService().getUserId(mobile);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return wxUserId;
    }
 
 
    /**
     * @description  通过wxUserId获取用户
     * @author  jqs
     * @date    2023/9/4 16:35
     * @param wxUserId
     * @return  WxCpUser
     */
    private WxCpUser getWxCpUser(String wxUserId){
        WxCpUser wxCpUser = null;
        try {
            wxCpUser = adWxCpService.getUserService().getById(wxUserId);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
        return wxCpUser;
    }
 
    /**
     * @description  创建企业微信员工
     * @author  jqs
     * @date    2023/9/4 15:19
     * @param mgtWxCPStaffDto
     * @return  void
     */
    @Override
    public String addWxCpStaff(MgtWxCPStaffDto mgtWxCPStaffDto){
        String wxUserId = mgtWxCPStaffDto.getWxUserId();
        String mobile = mgtWxCPStaffDto.getStaffMobile();
        String oldUserId = this.getWxUserIdByMobile(mobile);
        WxCpUser oldWxCpUser = null;
        Boolean sameUserId = false;
        //判断手机号是否有对应企业微信用户 有则更新 无则新建
        if(StringUtils.isNotBlank(oldUserId)){
            oldWxCpUser = this.getWxCpUser(oldUserId);
        }else{
            //当手机号无对应用户时判断wxUserId是否被占用
            oldWxCpUser = this.getWxCpUser(wxUserId);
            if(oldWxCpUser!=null){
                sameUserId = true;
                oldWxCpUser = null;
            }
        }
        //获取企业微信用户是否存在
        if(oldWxCpUser!=null){
            oldWxCpUser.setUserId(mgtWxCPStaffDto.getWxUserId());
            oldWxCpUser.setName(mgtWxCPStaffDto.getStaffName());
            oldWxCpUser.setMobile(mgtWxCPStaffDto.getStaffMobile());
            if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffEmail())){
                oldWxCpUser.setEmail(mgtWxCPStaffDto.getStaffEmail());
            }
            if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffPost())){
                oldWxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost());
            }
            oldWxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost());
            Long[] departIds = new Long[1];
            departIds[0] = mgtWxCPStaffDto.getWxDeptId();
            oldWxCpUser.setDepartIds(departIds);
            if(mgtWxCPStaffDto.getHeadFlag()!=null){
                Integer[] isLeaderInDept = new Integer[1];
                isLeaderInDept[0] = Integer.valueOf(mgtWxCPStaffDto.getHeadFlag());
                oldWxCpUser.setIsLeaderInDept(isLeaderInDept);
            }
            try {
                adWxCpService.getUserService().update(oldWxCpUser);
            } catch (WxErrorException e) {
                throw new RuntimeException(e);
            }
        }else{
            WxCpUser wxCpUser = new WxCpUser();
            //wxUserId被占用重新生成
            if(sameUserId){
                wxUserId = wxUserId + "1";
            }
            wxCpUser.setUserId(wxUserId);
            wxCpUser.setName(mgtWxCPStaffDto.getStaffName());
            wxCpUser.setMobile(mgtWxCPStaffDto.getStaffMobile());
            if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffEmail())){
                wxCpUser.setEmail(mgtWxCPStaffDto.getStaffEmail());
            }
            if(StringUtils.isNotBlank(mgtWxCPStaffDto.getStaffPost())){
                wxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost());
            }
            wxCpUser.setPosition(mgtWxCPStaffDto.getStaffPost());
            Long[] departIds = new Long[1];
            departIds[0] = mgtWxCPStaffDto.getWxDeptId();
            wxCpUser.setDepartIds(departIds);
            if(mgtWxCPStaffDto.getHeadFlag()!=null){
                Integer[] isLeaderInDept = new Integer[1];
                isLeaderInDept[0] = Integer.valueOf(mgtWxCPStaffDto.getHeadFlag());
                wxCpUser.setIsLeaderInDept(isLeaderInDept);
            }
            try {
                adWxCpService.getUserService().create(wxCpUser);
            } catch (WxErrorException e) {
                throw new RuntimeException(e);
            }
        }
        return wxUserId;
    }
 
    /**
     * @description  删除企业微信员工
     * @author  jqs
     * @date    2023/9/4 15:19
     * @param wxUserId
     * @return  void
     */
    @Override
    public void deleteWxCpStaff(String wxUserId){
        try {
            adWxCpService.getUserService().delete(wxUserId);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
}