wlzboy
19 小时以前 10354e63eb3298beb9ebcc029dd9f48d8936a272
ruoyi-system/src/main/java/com/ruoyi/system/listener/TaskMessageListener.java
@@ -1,8 +1,11 @@
package com.ruoyi.system.listener;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.DeptUtil;
import com.ruoyi.common.utils.LongUtil;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.event.TaskDispatchSyncEvent;
import com.ruoyi.system.service.ISysTaskAssigneeService;
import com.ruoyi.system.event.*;
import com.ruoyi.system.service.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@@ -11,16 +14,11 @@
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.event.TaskCreatedEvent;
import com.ruoyi.system.event.TaskAssignedEvent;
import com.ruoyi.system.event.TaskStatusChangedEvent;
import com.ruoyi.system.mapper.SysMessageMapper;
import com.ruoyi.system.mapper.SysUserMapper;
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.INotifyTaskService;
import com.ruoyi.system.service.INotifyDispatchService;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@@ -78,9 +76,16 @@
               List<SysTaskAssignee> assignees=taskAssigneeService.getAssigneesByTaskId(emergency.getTaskId());
               if(assignees!=null && !assignees.isEmpty()){
                   List<Long> assigneeIds=assignees.stream().map(SysTaskAssignee::getUserId).collect(Collectors.toList());
                   sendDispatchNotify(assigneeIds, task.getCreatorId(), event.getTaskId(), task.getTaskCode(), buildNotifyContent(task, emergency));
                   task.setEmergencyInfo( emergency);
                   sendDispatchNotify(assigneeIds, task.getCreatorId(), event.getTaskId(),task.getShowTaskCode(), buildNotifyContent(task, emergency));
               }
            }
            Long taskId= event.getTaskId();
            Long dispatchOrdId= event.getDispatchOrderId();
            Long serviceOrdId= event.getServiceOrderId();
            Integer oaUserID= event.getOaUserId();
            legacySystemSyncService.syncTaskAttachment(taskId, dispatchOrdId, serviceOrdId, oaUserID);
        }catch (Exception ex){
            log.error("处理任务派发同步事件失败", ex);
@@ -129,6 +134,118 @@
    }
    @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();
    }
    @Autowired
    private ILegacySystemSyncService legacySystemSyncService;
    @Async
    @EventListener
    public void handleTaskUpdateEvent(TaskUpdateEvent event){
        log.info("收到任务更新事件,任务ID:{},任务编号:{}", event.getTaskId(), event.getTaskCode());
        legacySystemSyncService.resyncDispatchOrderToLegacy(event.getTaskId());
    }
    //在这里监听派发的事件
    @Async
    @EventListener
    public void handleTaskServiceOrderSyncEvent(TaskServiceOrderSyncEvent event) {
//        log.info("收到任务服务单同步事件,任务ID:{},任务编号:{},服务单ID:{}", event.getTaskId(), event.getTaskCode(), event.getServiceOrderId());
       Long dispatchOrderId= legacySystemSyncService.syncDispatchOrderToLegacy(event.getTaskId());
//       if(LongUtil.isNotEmpty(dispatchOrderId)){
//           //更新needsync为0
//
//       }
    }
    /**
     * 监听任务分配事件
@@ -161,7 +278,7 @@
            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());
@@ -170,7 +287,7 @@
            // 构建通知内容
            String notifyContent = buildNotifyContent(task, emergency);
            this.sendDispatchNotify(event.getAssigneeIds(), creatorId, event.getTaskId(), event.getTaskCode(), notifyContent);
            this.sendDispatchNotify(event.getAssigneeIds(), creatorId, event.getTaskId(),task.getShowTaskCode(), notifyContent);
            
        } catch (Exception e) {
@@ -237,6 +354,23 @@
                    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;
    }
    /**
     * 构建通知内容
     */
@@ -260,7 +394,7 @@
            departure = task.getDepartureAddress();
        }
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        content.append(",出发时间:").append(df.format(dispatchTime));
        content.append(",出发时间:").append(df.format(dispatchTime));
        // 添加目的地信息
        String destination = null;
@@ -271,7 +405,6 @@
        }
        
        if (departure != null || destination != null) {
            content = new StringBuilder();
            if (departure != null) {
                content.append("出发地:").append(departure);
            }