bug
jiangqs
2023-08-18 0b413f3fd67110cfd7752f27eb171bde06edc4b4
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
package com.ruoyi.system.service.impl.staff;
 
import com.ruoyi.system.api.domain.poji.sys.SysDept;
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 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 {
 
    @Resource
    private WxCpService wxCpService;
 
    @Resource
    private ISysDeptService  sysDeptService;
    /**
     * @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);
        }
    }
}