liusheng
2024-06-05 01bbc8f0563b33dd5ce6d59f76c3bae9931615dd
smartor/src/main/java/com/smartor/service/impl/IvrTaskServiceImpl.java
@@ -1,16 +1,17 @@
package com.smartor.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.text.SimpleDateFormat;
import java.util.*;
import com.alibaba.fastjson2.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.DtoConversionUtils;
import com.ruoyi.common.utils.StringUtils;
import com.smartor.config.RabbitMqCallPhoneConfig;
import com.smartor.domain.*;
import com.smartor.mapper.*;
import lombok.extern.slf4j.Slf4j;
@@ -43,6 +44,9 @@
    @Autowired
    private IvrLibaTemplateTargetoptionMapper ivrLibaTemplateTargetOptionMapper;
    @Autowired
    private RabbitMqCallPhoneConfig rabbitMqCallPhoneConfig;
    /**
     * 查询语音任务
@@ -180,4 +184,99 @@
        return ivrLibaTemplateScriptVOS;
    }
    /**
     * 任务发送
     *
     * @return
     */
    @Override
    public int taskSend(SendTaskVO sendTaskVO) {
        if (sendTaskVO.getTaskId() == null) throw new BaseException("任务ID不能为空,请检查后,再进行执行");
        IvrTask ivrTask = selectIvrTaskByTaskid(sendTaskVO.getTaskId());
        if (sendTaskVO.getSendState() != null && sendTaskVO.getSendState() == 3 || sendTaskVO.getSendState() != null && sendTaskVO.getSendState() == 4) {
            //任务暂停或终止
            IvrTask ivrTask3 = new IvrTask();
            ivrTask3.setTaskid(sendTaskVO.getTaskId());
            ivrTask3.setStopState(ivrTask.getStopState() + 1);
            ivrTask3.setSendState(sendTaskVO.getSendState());
            updateIvrTask(ivrTask3);
            return 0;
        }
        //判断发送状态是否为空
        if (org.apache.commons.lang3.StringUtils.isEmpty(sendTaskVO.getSendType())) {
            sendTaskVO.setSendType(ivrTask.getSendType().toString());
        }
        Long ss = ivrTask.getStopState() + 1;
        //判断任务是否是立即发送
        if (org.apache.commons.lang3.StringUtils.isNotEmpty(sendTaskVO.getSendType()) && sendTaskVO.getSendType().equals("2")) {
            IvrTaskcallMQ ivrTaskcallMQ = new IvrTaskcallMQ();
            ivrTaskcallMQ.setTaskid(sendTaskVO.getTaskId());
            ivrTaskcallMQ.setSendType(sendTaskVO.getSendType());
            ivrTaskcallMQ.setTemplateid(ivrTask.getTemplateid().toString());
            ivrTaskcallMQ.setStopState(ss);
            ivrTaskcallMQ.setPreachform(ivrTask.getPreachform());
            String ivrTaskcallMQJson = JSON.toJSONString(ivrTaskcallMQ);
            ivrTaskcallMQJson = ivrTaskcallMQJson.substring(1, ivrTaskcallMQJson.length() - 1);
            //立即发送
            rabbitMqCallPhoneConfig.sendMessage("phone_exchange", "phone.123", ivrTaskcallMQJson, 0L);
            //将任务状态修改成执行中
            IvrTask ivrTask2 = new IvrTask();
            ivrTask2.setTaskid(ivrTask.getTaskid());
            ivrTask2.setSendState(2);
            ivrTask2.setStopState(ss);
            updateIvrTask(ivrTask2);
        } else if (org.apache.commons.lang3.StringUtils.isNotEmpty(sendTaskVO.getSendType()) && sendTaskVO.getSendType().equals("1")) {
            //时间段发送
            ObjectMapper objectMapper = new ObjectMapper();
            if (org.apache.commons.lang3.StringUtils.isNotEmpty(ivrTask.getSendTimeSlot())) {
                List<TaskSendTimeVO> taskSendTimeVOList = null;
                try {
                    taskSendTimeVOList = objectMapper.readValue(ivrTask.getSendTimeSlot(), new TypeReference<List<TaskSendTimeVO>>() {
                    });
                } catch (JsonProcessingException e) {
                    log.error("JsonProcessingException报错了:{}", e.getMessage());
                }
                for (TaskSendTimeVO taskSendTimeVO : taskSendTimeVOList) {
                    //去SendTimeslot中,获取所有的时间段
                    List<TaskSendTimeVO> list = new ArrayList<>();
                    list.add(taskSendTimeVO);
                    //获取开始发送时间
                    String begantime = taskSendTimeVO.getBegantime();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    //记录目前到发送时间的毫秒值 (发送时间的毫秒值 - 当前时间的毫秒值)
                    Long milliseconds = 1000L;
                    try {
                        Date date = sdf.parse(begantime);
                        milliseconds = date.getTime() - System.currentTimeMillis();
                        System.out.println("日期毫秒数:" + milliseconds);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    IvrTaskcallMQ ivrTaskcallMQ = new IvrTaskcallMQ();
                    ivrTaskcallMQ.setTaskid(sendTaskVO.getTaskId());
                    ivrTaskcallMQ.setSendType("1");
                    ivrTaskcallMQ.setTemplateid(ivrTask.getTemplateid().toString());
                    ivrTaskcallMQ.setSendTimeslot(list);
                    ivrTaskcallMQ.setStopState(ivrTask.getStopState());
                    Long finalMilliseconds = milliseconds;
                    String ivrTaskcallMQJson = JSON.toJSONString(ivrTaskcallMQ);
                    ivrTaskcallMQJson = ivrTaskcallMQJson.substring(1, ivrTaskcallMQJson.length() - 1);
                    rabbitMqCallPhoneConfig.sendMessage("phone_exchange", "phone.123", ivrTaskcallMQJson, milliseconds);
                }
                //将任务状态修改成执行中
                IvrTask ivrTask2 = new IvrTask();
                ivrTask2.setTaskid(ivrTask.getTaskid());
                ivrTask2.setSendState(2);
                ivrTask2.setStopState(ss);
                updateIvrTask(ivrTask2);
            }
        }
        return 1;
    }
}