wlzboy
3 天以前 40a8157440e3b906da8f52e07d939d78c3f4c313
ruoyi-system/src/main/java/com/ruoyi/system/service/impl/NotifyDispatchServiceImpl.java
@@ -1,6 +1,7 @@
package com.ruoyi.system.service.impl;
import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.config.WechatConfig;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.*;
@@ -46,6 +47,16 @@
    @Autowired
    private ISmsService smsService;
    @Autowired
    private IQyWechatService qyWechatService;
    @Autowired
    private WechatConfig wechatConfig;
    @Autowired
    private ISysEmergencyTaskService sysEmergencyTaskService;
    /**
     * 获取指定通知类型启用的渠道列表
     */
@@ -75,8 +86,8 @@
            return 0;
        }
        log.info("开始分发通知任务,id={}, taskId={}, userId={}, notifyType={}",
                notifyTask.getId(), notifyTask.getTaskId(), notifyTask.getUserId(), notifyTask.getNotifyType());
//        log.info("开始分发通知任务,id={}, taskId={}, userId={}, notifyType={}",
//                notifyTask.getId(), notifyTask.getTaskId(), notifyTask.getUserId(), notifyTask.getNotifyType());
        // 更新状态为处理中
        notifyTaskService.markProcessing(notifyTask.getId());
@@ -108,6 +119,9 @@
                        break;
                    case NotifyChannelConfig.CHANNEL_SMS:
                        success = sendSmsMessage(notifyTask);
                        break;
                    case NotifyChannelConfig.CHANNEL_QY_WECHAT:
                        success = sendQyWechatMessage(notifyTask);
                        break;
                    default:
                        log.warn("不支持的渠道类型:{}", channel);
@@ -175,7 +189,9 @@
            sendLog.setChannel(channel);
            sendLog.setSendStatus(success ? NotifySendLog.SEND_STATUS_SUCCESS : NotifySendLog.SEND_STATUS_FAILED);
            sendLog.setSendTime(DateUtils.getNowDate());
            sendLog.setSendContent(notifyTask.getContent());
            sendLog.setResponseMsg(errorMsg);
            sendLog.setSendResult(success ? "发送成功" : ("发送失败: " + (errorMsg != null ? errorMsg : "未知原因")));
            
            notifySendLogService.insertNotifySendLog(sendLog);
        } catch (Exception e) {
@@ -318,4 +334,42 @@
            return false;
        }
    }
    @Autowired
    private ISysConfigService sysConfigService;
    /**
     * 发送企业微信消息
     */
    @Override
    public boolean sendQyWechatMessage(NotifyTask notifyTask) {
        // 检查企业微信服务是否启用
        if (!qyWechatService.isEnabled()) {
            throw new RuntimeException("企业微信服务未启用");
        }
        Long taskId = notifyTask.getTaskId();
        SysTaskEmergency emergency = this.sysEmergencyTaskService.selectSysTaskEmergencyByTaskId(taskId);
        if (emergency == null) {
            throw new RuntimeException("找不到对应的急救任务信息,taskId=" + taskId);
        }
        // 检查用户是否绑定企业微信
        Long userId = notifyTask.getUserId();
        String qyUserId = qyWechatService.getQyUserIdByUserId(userId);
        if (qyUserId == null || qyUserId.isEmpty()) {
            throw new RuntimeException("用户未绑定企业微信ID,userId=" + userId);
        }
        String appId = wechatConfig.getAppId();
        String pathPage = "/pagesTask/detail?id=" + taskId;
        // 发送企业微信消息
        boolean success = qyWechatService.sendNotifyMessage(
                userId,
                notifyTask.getTitle(),
                notifyTask.getContent(), appId, pathPage
        );
        if (success) {
            log.info("企业微信消息发送成功,userId={}", userId);
        } else {
            throw new RuntimeException("企业微信API返回失败,userId=" + userId);
        }
        return true;
    }
}