@@ -1,60 +1,65 @@
package com.srs.flowable.listener.disciplinary ;
package com.srs.flowable.listener.disciplinary ;
import com.srs.common.utils.WeChatUtil ;
import com.srs.common.utils.WeChatUtil ;
import com.srs.common.utils.spring.SpringUtils ;
import com.srs.flowable.mapper.DisciplinaryMapper ;
import com.srs.flowable.mapper.DisciplinaryMapper ;
import lombok.extern.slf4j.Slf4j ;
import lombok.extern.slf4j.Slf4j ;
import org.flowable.engine.delegate.DelegateExecution ;
import org.flowable.engine.delegate.DelegateExecution ;
import org.flowable.engine.delegate.ExecutionListener ;
import org.flowable.engine.delegate.ExecutionListener ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Component ;
import org.springframework.stereotype.Component ;
import org.springframework.util.CollectionUtils ;
import java.util.List ;
import java.util.List ;
@Component
/**
* 学工领导审批监听器
* 使用 @Component + @Autowired, 兼容 Flowable Delegate Expression
*/
@Component ( " xgldshListener " )
@Slf4j
@Slf4j
public class XGLDSHListener implements ExecutionListener {
public class XGLDSHListener implements ExecutionListener {
@Autowired
private DisciplinaryMapper disciplinaryMapper ;
@Autowired
@Autowired
private WeChatUtil weChatUtil ;
private WeChatUtil weChatUtil ;
@Override
@Override
public void notify ( DelegateExecution delegateE xecution) {
public void notify ( DelegateExecution e xecution) {
DisciplinaryMapper disciplinaryMapper = SpringUtils . getBean ( DisciplinaryMapper . class ) ;
// 固定角色 Key
final String TARGET_ROLE_KEY = " xgldsp " ;
final String TARGET_ROLE_KEY = " xgldsp " ;
String processInstanceId = execution . getProcessInstanceId ( ) ;
// 步骤 1: 查询角色下的所有审批人 userId
log . info ( " 流程实例 [{}]: 触发 XGLDSHListener, 为角色 '{}' 节点准备数据... " , processInstanceId , TARGET_ROLE_KEY ) ;
// 查询角色下审批人
List < Long > userIdList = disciplinaryMapper . getApprovalByRoleKey ( TARGET_ROLE_KEY ) ;
List < Long > userIdList = disciplinaryMapper . getApprovalByRoleKey ( TARGET_ROLE_KEY ) ;
if ( CollectionUtils . isEmpty ( userIdList ) ) {
if ( userIdList = = null | | userIdList . isEmpty ( ) ) {
log . error ( " 流程实例 [{}]: 角色 '{}' 未配置审批人员 " , processInstanceId , TARGET_ROLE_KEY ) ;
throw new RuntimeException ( " 未找到角色 ' " + TARGET_ROLE_KEY + " ' 的审批人员,无法发送通知。 " ) ;
throw new RuntimeException ( " 学工领导审批人员未设置 " ) ;
}
}
// 发送企微通知
sendWeChatNotification ( userIdList , processInstanceId , TARGET_ROLE_KEY ) ;
}
private void sendWeChatNotification ( List < Long > userIdList , String processInstanceId , String roleKey ) {
try {
try {
// 步骤 2: 查询 userName 列表
List < String > userNameList = disciplinaryMapper . getUserNamesByUserIdList ( userIdList ) ;
List < String > userNameList = disciplinaryMapper . getUserNamesByUserIdList ( userIdList ) ;
if ( CollectionUtils . isEmpty ( userNameList ) ) {
if ( userNameList ! = null & & ! userNameList . isEmpty ( ) ) {
log . warn ( " 流程实例 [{}]: 角色 '{}' 审批人存在但无人配置企微账号 " , processInstanceId , roleKey ) ;
String toUser = String . join ( " | " , userNameList ) ;
return ;
WeChatUtil weChatUtil = SpringUtils . getBean ( WeChatUtil . class ) ;
String content = " 您有一条新的学生违纪审批任务待处理, " +
" <a href='http://zhxg.gxsdxy.cn/web/#/pages/Approval/index'>请点击前往处理</a>。 " ;
// 步骤 3: 发送企业微信通知
weChatUtil . sendTextMessage ( toUser , content ) ;
log . info ( " 已成功向角色 '{}' 的成员发送企业微信审批通知。接收人: {} " , TARGET_ROLE_KEY , toUser ) ;
} else {
log . warn ( " 角色 '{}' 下找到 {} 个用户,但无对应的企业微信账号,无法发送通知。 " , TARGET_ROLE_KEY , userIdList . size ( ) ) ;
}
}
String toUser = String . join ( " | " , userNameList ) ;
String content = " 您有一条新的学生违纪审批任务待处理, " +
" <a href='http://zhxg.gxsdxy.cn/web/#/pages/Approval/index'>请点击前往处理</a> " ;
weChatUtil . sendTextMessage ( toUser , content ) ;
log . info ( " 流程实例 [{}]: 已成功向角色 '{}' 成员发送企微通知,接收人: {} " , processInstanceId , roleKey , toUser ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
log . error ( " 向角色 '{}' 发送企业微信通知时出现异常,但流程将继续。错误详情: {} " , TARGET_ROLE_KEY , e . getMessage ( ) , e ) ;
log . error ( " 流程实例 [{}]: 向角色 '{}' 发送企微通知异常 " , processInstanceId , roleKey , e ) ;
}
}
}
}
}
}