| | |
| | | package com.ruoyi.system.listener; |
| | | |
| | | import com.ruoyi.common.core.domain.entity.SysDept; |
| | | import com.ruoyi.common.utils.DeptUtil; |
| | | import com.ruoyi.system.domain.*; |
| | | import com.ruoyi.system.event.TaskDispatchSyncEvent; |
| | | import com.ruoyi.system.service.ISysDeptService; |
| | | import com.ruoyi.system.service.ISysTaskAssigneeService; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | |
| | | import org.springframework.stereotype.Component; |
| | | import com.ruoyi.common.utils.DateUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.system.domain.SysMessage; |
| | | import com.ruoyi.system.domain.SysTask; |
| | | import com.ruoyi.system.domain.SysTaskEmergency; |
| | | import com.ruoyi.system.event.TaskCreatedEvent; |
| | | import com.ruoyi.system.event.TaskAssignedEvent; |
| | | import com.ruoyi.system.event.TaskStatusChangedEvent; |
| | |
| | | import com.ruoyi.system.mapper.SysTaskMapper; |
| | | import com.ruoyi.system.mapper.SysTaskEmergencyMapper; |
| | | import com.ruoyi.common.core.domain.entity.SysUser; |
| | | import com.ruoyi.system.service.IWechatTaskNotifyService; |
| | | import com.ruoyi.system.service.INotifyTaskService; |
| | | import com.ruoyi.system.service.INotifyDispatchService; |
| | | |
| | | import java.util.HashMap; |
| | | import java.text.DateFormat; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.ArrayList; |
| | | import java.util.Date; |
| | | import java.util.List; |
| | | import java.util.stream.Collectors; |
| | | |
| | | /** |
| | | * 任务消息监听器 |
| | |
| | | private SysTaskEmergencyMapper sysTaskEmergencyMapper; |
| | | |
| | | @Autowired |
| | | private IWechatTaskNotifyService wechatTaskNotifyService; |
| | | private INotifyTaskService notifyTaskService; |
| | | |
| | | @Autowired |
| | | private INotifyDispatchService notifyDispatchService; |
| | | |
| | | /** 待准备状态 - 可以发送短信通知 */ |
| | | private static final String TASK_STATUS_PENDING = "PENDING"; |
| | | /** 待准备状态 - 可以发送短信通知 */ |
| | | private static final String TASK_STATUS_PREPARING = "PREPARING"; |
| | | |
| | | @Autowired |
| | | private ISysTaskAssigneeService taskAssigneeService; |
| | | |
| | | @Async |
| | | @EventListener |
| | | public void handleTaskDispatchEvent(TaskDispatchSyncEvent event) { |
| | | try{ |
| | | log.info("收到任务派发同步事件,任务ID:{},任务编号:{},派发单ID:{}", event.getTaskId(), event.getTaskCode(), event.getDispatchOrderId()); |
| | | SysTask task=sysTaskMapper.selectSysTaskByTaskId(event.getTaskId()); |
| | | SysTaskEmergency emergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(event.getTaskId()); |
| | | if(emergency != null){ |
| | | List<SysTaskAssignee> assignees=taskAssigneeService.getAssigneesByTaskId(emergency.getTaskId()); |
| | | if(assignees!=null && !assignees.isEmpty()){ |
| | | List<Long> assigneeIds=assignees.stream().map(SysTaskAssignee::getUserId).collect(Collectors.toList()); |
| | | task.setEmergencyInfo( emergency); |
| | | sendDispatchNotify(assigneeIds, task.getCreatorId(), event.getTaskId(),task.getShowTaskCode(), buildNotifyContent(task, emergency)); |
| | | } |
| | | } |
| | | |
| | | }catch (Exception ex){ |
| | | log.error("处理任务派发同步事件失败", ex); |
| | | } |
| | | } |
| | | /** |
| | | * 监听任务创建事件 |
| | | * |
| | |
| | | } |
| | | } |
| | | |
| | | |
| | | @Autowired |
| | | private ISysDeptService sysDeptService; |
| | | /** |
| | | * 只有服务单,且只在广州外的服务单,没有调度单 |
| | | * @param event |
| | | */ |
| | | @Async |
| | | @EventListener |
| | | public void handleTaskOnlyServiceSync(TaskDispatchSyncEvent event){ |
| | | //给负责人发送消息 |
| | | Long taskId= event.getTaskId(); |
| | | SysTask task=sysTaskMapper.selectSysTaskByTaskId(taskId); |
| | | SysTaskEmergency emergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(taskId); |
| | | if(emergency != null){ |
| | | task.setEmergencyInfo( emergency); |
| | | Long deptId=task.getDeptId(); |
| | | if(!deptId.equals(DeptUtil.GUANGZHOU_DEPT_ID)){ |
| | | //找到该部门的负责人 |
| | | SysDept dept= sysDeptService.selectDeptById(deptId); |
| | | if(dept!=null){ |
| | | String serviceOrdClass= dept.getServiceOrderClass(); |
| | | String dispatchOrdClass= dept.getDispatchOrderClass(); |
| | | List<SysUser> user=sysUserMapper.selectUsersByOrderClassAndCanViewAllConsult(serviceOrdClass); |
| | | if(user!=null && !user.isEmpty()){ |
| | | List<NotifyTask> tasks=new ArrayList<>(); |
| | | String buildNotifyContent = buildUnAssignNotifyContent(task, emergency); |
| | | for(SysUser u:user){ |
| | | NotifyTask notifyTask= this.sendTaskUnAssignNotify(u.getUserId(),u.getNickName(),u.getPhonenumber(),taskId,task.getShowTaskCode(),"服务单派发",buildNotifyContent); |
| | | tasks.add(notifyTask); |
| | | } |
| | | if(!tasks.isEmpty()){ |
| | | int successCount = notifyDispatchService.dispatchNotifies(tasks); |
| | | log.info("任务未分配消息已发送,发送成功数量:{}", successCount); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | |
| | | } |
| | | } |
| | | |
| | | private String buildUnAssignNotifyContent(SysTask task, SysTaskEmergency emergency){ |
| | | //派发单号 |
| | | String dispatchCode=emergency.getDispatchCode(); |
| | | String taskCode=task.getTaskCode(); |
| | | String orderCode=dispatchCode; |
| | | if(dispatchCode==null){ |
| | | orderCode=taskCode; |
| | | } |
| | | Date dispatchTime=task.getPlanedStartTime(); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("您有新的服务单,任务单号:"+orderCode); |
| | | // 添加出发地信息 |
| | | String departure = null; |
| | | if (emergency != null && StringUtils.isNotEmpty(emergency.getHospitalOutName())) { |
| | | departure = emergency.getHospitalOutName(); |
| | | } else if (StringUtils.isNotEmpty(task.getDepartureAddress())) { |
| | | departure = task.getDepartureAddress(); |
| | | } |
| | | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| | | content.append(",出发时间:").append(df.format(dispatchTime)); |
| | | |
| | | // 添加目的地信息 |
| | | String destination = null; |
| | | if (emergency != null && StringUtils.isNotEmpty(emergency.getHospitalInName())) { |
| | | destination = emergency.getHospitalInName(); |
| | | } else if (StringUtils.isNotEmpty(task.getDestinationAddress())) { |
| | | destination = task.getDestinationAddress(); |
| | | } |
| | | |
| | | if (departure != null || destination != null) { |
| | | if (departure != null) { |
| | | content.append("出发地:").append(departure); |
| | | } |
| | | if (destination != null) { |
| | | if (content.length() > 0) content.append(","); |
| | | content.append("目的地:").append(destination); |
| | | } |
| | | content.append(",请及时处理。"); |
| | | } |
| | | |
| | | return content.toString(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 监听任务分配事件 |
| | | * 创建通知任务,由通知分发服务决定发送渠道 |
| | | * |
| | | * @param event 任务分配事件 |
| | | */ |
| | |
| | | log.warn("执行人ID列表为空,无法推送消息"); |
| | | return; |
| | | } |
| | | |
| | | // 给每个执行人发送站内消息 |
| | | for (int i = 0; i < event.getAssigneeIds().size(); i++) { |
| | | Long assigneeId = event.getAssigneeIds().get(i); |
| | | |
| | | // 获取执行人信息 |
| | | SysUser assignee = sysUserMapper.selectUserById(assigneeId); |
| | | if (assignee == null) { |
| | | log.warn("找不到执行人信息,用户ID:{}", assigneeId); |
| | | continue; |
| | | } |
| | | |
| | | // 创建站内消息 |
| | | SysMessage message = new SysMessage(); |
| | | message.setMessageType("PUSH"); |
| | | message.setMessageTitle("任务推送"); |
| | | message.setMessageContent("您有新的任务,请及时处理"); |
| | | message.setTaskId(event.getTaskId()); |
| | | message.setTaskCode(event.getTaskCode()); |
| | | message.setReceiverId(assigneeId); |
| | | message.setReceiverName(assignee.getNickName()); |
| | | message.setSenderId(event.getAssignerId()); |
| | | message.setSenderName(event.getAssignerName() != null ? event.getAssignerName() : "系统"); |
| | | message.setIsRead("0"); |
| | | message.setCreateTime(DateUtils.getNowDate()); |
| | | message.setDelFlag("0"); |
| | | |
| | | // 保存消息 |
| | | sysMessageMapper.insertSysMessage(message); |
| | | log.info("任务分配消息已保存,消息ID:{},接收人:{}", message.getMessageId(), assignee.getNickName()); |
| | | |
| | | // 查询任务信息 |
| | | SysTask task = sysTaskMapper.selectSysTaskByTaskId(event.getTaskId()); |
| | | if (task == null) { |
| | | log.warn("任务不存在,taskId={}", event.getTaskId()); |
| | | return; |
| | | } |
| | | |
| | | // 发送微信订阅消息(排除创建人) |
| | | try { |
| | | SysTask task = sysTaskMapper.selectSysTaskByTaskId(event.getTaskId()); |
| | | Long creatorId = task != null ? task.getCreatorId() : null; |
| | | wechatTaskNotifyService.sendTaskNotifyMessage(event.getTaskId(), event.getAssigneeIds(), creatorId); |
| | | } catch (Exception e) { |
| | | log.error("处理任务分配事件时发送微信订阅消息失败", e); |
| | | // 查询急救扩展信息(用于构建通知内容) |
| | | SysTaskEmergency emergency = sysTaskEmergencyMapper.selectSysTaskEmergencyByTaskId(event.getTaskId()); |
| | | |
| | | Long creatorId = task.getCreatorId(); |
| | | String taskStatus = task.getTaskStatus(); |
| | | task.setEmergencyInfo(emergency); |
| | | // 仅在待准备状态下发送通知 |
| | | if (!TASK_STATUS_PENDING.equals(taskStatus) && !TASK_STATUS_PREPARING.equals(taskStatus)) { |
| | | log.info("任务状态({})非待准备状态,跳过通知,taskId={}", taskStatus, event.getTaskId()); |
| | | return; |
| | | } |
| | | |
| | | // 构建通知内容 |
| | | String notifyContent = buildNotifyContent(task, emergency); |
| | | this.sendDispatchNotify(event.getAssigneeIds(), creatorId, event.getTaskId(),task.getShowTaskCode(), notifyContent); |
| | | |
| | | |
| | | } catch (Exception e) { |
| | | log.error("处理任务分配事件失败", e); |
| | |
| | | } |
| | | |
| | | /** |
| | | * 向执行人发送任务分配通知 |
| | | * @param assigneeIds |
| | | * @param creatorId |
| | | * @param taskId |
| | | * @param taskCode |
| | | * |
| | | * @param notifyContent |
| | | */ |
| | | |
| | | private void sendDispatchNotify(List<Long> assigneeIds, Long creatorId, |
| | | Long taskId,String taskCode,String notifyContent) { |
| | | // 收集创建的通知任务 |
| | | |
| | | List<NotifyTask> createdTasks = new ArrayList<>(); |
| | | |
| | | // 为每个执行人创建通知任务 |
| | | for (Long assigneeId : assigneeIds) { |
| | | // 排除创建人 |
| | | if (creatorId != null && creatorId.equals(assigneeId)) { |
| | | log.debug("跳过创建人,不发送任务分配通知,userId={}", assigneeId); |
| | | continue; |
| | | } |
| | | |
| | | // 获取执行人信息 |
| | | SysUser assignee = sysUserMapper.selectUserById(assigneeId); |
| | | if (assignee == null) { |
| | | log.warn("找不到执行人信息,用户ID:{}", assigneeId); |
| | | continue; |
| | | } |
| | | |
| | | // 创建通知任务(带防重) |
| | | NotifyTask notifyTask = new NotifyTask(); |
| | | notifyTask.setTaskId(taskId); |
| | | notifyTask.setTaskCode(taskCode); |
| | | notifyTask.setNotifyType(NotifyTask.NOTIFY_TYPE_TASK_ASSIGN); |
| | | notifyTask.setUserId(assigneeId); |
| | | notifyTask.setUserName(assignee.getNickName()); |
| | | notifyTask.setUserPhone(assignee.getPhonenumber()); |
| | | notifyTask.setTitle("转运单任务派单通知"); |
| | | notifyTask.setContent(notifyContent); |
| | | notifyTask.setCreateBy( "系统"); |
| | | |
| | | NotifyTask created = notifyTaskService.createNotifyTask(notifyTask); |
| | | if (created != null) { |
| | | createdTasks.add(created); |
| | | log.info("创建通知任务成功,id={}, userId={}", created.getId(), assigneeId); |
| | | } else { |
| | | log.info("通知任务已存在,跳过,taskId={}, userId={}", taskId, assigneeId); |
| | | } |
| | | } |
| | | |
| | | // 分发通知任务 |
| | | if (!createdTasks.isEmpty()) { |
| | | int successCount = notifyDispatchService.dispatchNotifies(createdTasks); |
| | | log.info("通知分发完成,taskId={},创建数量={},成功数量={}", |
| | | taskId, createdTasks.size(), successCount); |
| | | } |
| | | } |
| | | |
| | | private NotifyTask sendTaskUnAssignNotify(Long userId,String nickName,String phone, |
| | | Long taskId,String taskCode,String title,String notifyContent) { |
| | | NotifyTask notifyTask = new NotifyTask(); |
| | | notifyTask.setTaskId(taskId); |
| | | notifyTask.setTaskCode(taskCode); |
| | | notifyTask.setNotifyType(NotifyTask.NOTIFY_TASK_UNASSIGN); |
| | | notifyTask.setUserId(userId); |
| | | notifyTask.setUserName(nickName); |
| | | notifyTask.setUserPhone(phone); |
| | | notifyTask.setTitle(title); |
| | | notifyTask.setContent(notifyContent); |
| | | notifyTask.setCreateBy( "系统"); |
| | | |
| | | NotifyTask created = notifyTaskService.createNotifyTask(notifyTask); |
| | | return created; |
| | | } |
| | | /** |
| | | * 构建通知内容 |
| | | */ |
| | | private String buildNotifyContent(SysTask task, SysTaskEmergency emergency) { |
| | | //派发单号 |
| | | String dispatchCode=emergency.getDispatchCode(); |
| | | String taskCode=task.getTaskCode(); |
| | | String orderCode=dispatchCode; |
| | | if(dispatchCode==null){ |
| | | orderCode=taskCode; |
| | | } |
| | | Date dispatchTime=task.getPlanedStartTime(); |
| | | |
| | | StringBuilder content = new StringBuilder(); |
| | | content.append("您有新的转运任务,任务单号:"+orderCode); |
| | | // 添加出发地信息 |
| | | String departure = null; |
| | | if (emergency != null && StringUtils.isNotEmpty(emergency.getHospitalOutName())) { |
| | | departure = emergency.getHospitalOutName(); |
| | | } else if (StringUtils.isNotEmpty(task.getDepartureAddress())) { |
| | | departure = task.getDepartureAddress(); |
| | | } |
| | | DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm"); |
| | | content.append(",出发时间:").append(df.format(dispatchTime)); |
| | | |
| | | // 添加目的地信息 |
| | | String destination = null; |
| | | if (emergency != null && StringUtils.isNotEmpty(emergency.getHospitalInName())) { |
| | | destination = emergency.getHospitalInName(); |
| | | } else if (StringUtils.isNotEmpty(task.getDestinationAddress())) { |
| | | destination = task.getDestinationAddress(); |
| | | } |
| | | |
| | | if (departure != null || destination != null) { |
| | | if (departure != null) { |
| | | content.append("出发地:").append(departure); |
| | | } |
| | | if (destination != null) { |
| | | if (content.length() > 0) content.append(","); |
| | | content.append("目的地:").append(destination); |
| | | } |
| | | content.append(",请及时处理。"); |
| | | } |
| | | |
| | | return content.toString(); |
| | | } |
| | | |
| | | /** |
| | | * 监听任务状态变更事件 |
| | | * |
| | | * @param event 任务状态变更事件 |