Pu Zhibing
2025-04-01 8444084e6aa11efa23287e7f82474ac22378a5c4
ruoyi-modules/ruoyi-system/src/main/java/com/ruoyi/system/service/impl/staff/SysWxCpServiceImpl.java
@@ -1,28 +1,17 @@
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
@@ -35,22 +24,14 @@
@Log4j2
public class SysWxCpServiceImpl implements SysWxCpService {
    @Autowired
    @Resource
    @Qualifier("wxService")
    private WxCpService wxCpService;
    @Autowired
    @Resource
    @Qualifier("adWxService")
    private WxCpService adWxCpService;
    @Resource
    private RemoteShopService remoteShopService;
    @Resource
    private ISysDeptService  sysDeptService;
    @Resource
    private SysStaffService sysStaffService;
    /**
     * @description
@@ -70,87 +51,9 @@
        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  更新企业微信部门
@@ -238,7 +141,7 @@
    private WxCpUser getWxCpUser(String wxUserId){
        WxCpUser wxCpUser = null;
        try {
            wxCpUser = adWxCpService.getUserService().getById(wxUserId);
            wxCpUser = wxCpService.getUserService().getById(wxUserId);
        } catch (WxErrorException e) {
            throw new RuntimeException(e);
        }