mitao
2025-01-17 afa0dbb4f54e7244835dd67ec33c3e545f122f71
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/sys/SysDeptServiceImpl.java
@@ -15,10 +15,16 @@
import com.ruoyi.system.mapper.sys.SysDeptMapper;
import com.ruoyi.system.mapper.sys.SysDeptMenuMapper;
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;
@@ -32,14 +38,22 @@
@Service
public class SysDeptServiceImpl implements ISysDeptService
{
    @Autowired
    @Resource
    private SysDeptMapper deptMapper;
    @Autowired
    @Resource
    private SysRoleMapper roleMapper;
    @Autowired
    @Resource
    private SysDeptMenuMapper deptMenuMapper;
    @Resource
    private SysWxCpService sysWxCpService;
    @Resource
    @Qualifier("wxService")
    private WxCpService wxCpService;
    /**
     * 查询部门管理数据
@@ -224,6 +238,8 @@
            throw new ServiceException("部门停用,不允许新增");
        }
        dept.setAncestors(info.getAncestors() + "," + dept.getParentId());
        Long wxDeptId = sysWxCpService.addWxCpDept(1L,dept.getDeptName());
        dept.setWxDeptId(wxDeptId);
        //新增部门
        int i = deptMapper.insertDept(dept);
        if(dept.getMenuIds()!=null||dept.getStaffMenuIds()!=null){
@@ -283,6 +299,9 @@
            String oldAncestors = oldDept.getAncestors();
            dept.setAncestors(newAncestors);
            updateDeptChildren(dept.getDeptId(), newAncestors, oldAncestors);
        }
        if(oldDept.getWxDeptId()!=null&&!oldDept.getDeptName().equals(dept.getDeptName())){
            sysWxCpService.updateWxCpDept(oldDept.getWxDeptId(),dept.getDeptName());
        }
        int result = deptMapper.updateDept(dept);
        if (UserConstants.DEPT_NORMAL.equals(dept.getStatus()) && StringUtils.isNotEmpty(dept.getAncestors())
@@ -383,4 +402,63 @@
    {
        return getChildList(list, t).size() > 0 ? true : false;
    }
    /**
     * @description  通过微信部门id获取部门
     * @author  jqs
     * @date    2023/8/17 16:21
     * @param wxDeptId
     * @return  SysDept
     */
    @Override
    public SysDept getByWxDeptId(Long wxDeptId){
        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);
        }
    }
}