mitao
2025-01-17 afa0dbb4f54e7244835dd67ec33c3e545f122f71
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/sys/SysDeptServiceImpl.java
@@ -17,9 +17,14 @@
import com.ruoyi.system.mapper.sys.SysRoleMapper;
import com.ruoyi.system.service.staff.SysWxCpService;
import com.ruoyi.system.service.sys.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
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.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -33,17 +38,22 @@
@Service
public class SysDeptServiceImpl implements ISysDeptService
{
    @Autowired
    @Resource
    private SysDeptMapper deptMapper;
    @Autowired
    @Resource
    private SysRoleMapper roleMapper;
    @Autowired
    @Resource
    private SysDeptMenuMapper deptMenuMapper;
    @Autowired
    @Resource
    private SysWxCpService sysWxCpService;
    @Resource
    @Qualifier("wxService")
    private WxCpService wxCpService;
    /**
     * 查询部门管理数据
@@ -406,5 +416,49 @@
        return deptMapper.getByWxDeptId(wxDeptId);
    }
    /**
     * @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 = this.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("企业微信同步");
                                this.updateDept(sysDept);
                            }
                        }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());
                            this.insertDept(sysDept);
                        }
                    }
                }
            }
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }
    }
}