From afa0dbb4f54e7244835dd67ec33c3e545f122f71 Mon Sep 17 00:00:00 2001
From: mitao <2763622819@qq.com>
Date: 星期五, 17 一月 2025 16:40:43 +0800
Subject: [PATCH] bug修改

---
 ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/sys/SysDeptServiceImpl.java |   73 ++++++++++++++++++++++++++++++++++--
 1 files changed, 69 insertions(+), 4 deletions(-)

diff --git a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/sys/SysDeptServiceImpl.java b/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/sys/SysDeptServiceImpl.java
index 9e21529..b7a26ed 100644
--- a/ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/sys/SysDeptServiceImpl.java
+++ b/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())
@@ -396,4 +415,50 @@
     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);
+        }
+    }
 }

--
Gitblit v1.7.1