package com.unis.detectionnum.server.message.utils;

import com.alibaba.fastjson.JSONObject;
import com.unis.detectionnum.server.message.entity.MessageVo;
import com.unis.detectionnum.server.message.service.SMSIdentifyCodeService;
import com.unis.detectionnum.server.utils.RobotCallJobUtils;
import org.apache.commons.httpclient.protocol.Protocol;
import org.apache.commons.httpclient.protocol.ProtocolSocketFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;

import java.util.Map;

/**
 * 文件描述:异步发送消息
 *
 * @author tongxi.xia
 * @date 2020年01月11日 16:06
 */

@Configuration
public class MessageAsyncSendUtils {
    final static Logger logger = LoggerFactory.getLogger(MessageAsyncSendUtils.class);

    @Autowired
    private SMSIdentifyCodeService smsService;

    @Async
    public void asyncSend(MessageVo messageVo) {

        try {
            boolean flag = smsService.sendMessageAndContens(messageVo.getMobile(), messageVo.getCreater(), messageVo.getContens());
            logger.info("消息发送成功");
        } catch (Exception e) {
            logger.info("消息发送失败参数：" + e.getMessage());
            e.printStackTrace();
        }
    }

    @Async
    public void asyncSendPhone(String telephoneNumber,String cfmc) {
        try {
            /**
             * 调用智能报警电话 待完善 获取电话列表id
             */
            RobotCallJobUtils robotCallJobUtils = new RobotCallJobUtils();
//            robotCallJobUtils.getPhoneList();
//创建任务

            String robotCallJobIdA = robotCallJobUtils.createTask();
//获取任务id
            JSONObject Obj = JSONObject.parseObject(robotCallJobIdA);
            Map map = (Map) Obj.get("data");
            String robotCallJobId = map.values().toString().substring(1, 7);
//获取电话列表
            robotCallJobUtils.getPhoneList();
//获取工作列表
            robotCallJobUtils.getJobs();
//获取话术列表
            robotCallJobUtils.getDialogFlowList();
//向任务中导入客户 临时电话18501020163
            robotCallJobUtils.importTaskCustomer(robotCallJobId, telephoneNumber,cfmc);
//        robotCallJobUtils.importTaskCustomer(robotCallJobId, telephoneNumber1);
//开始任务
            robotCallJobUtils.startTask(robotCallJobId);
            logger.info("语音电话通话成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("语音电话通话失败：" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        MessageAsyncSendUtils messageAsyncSendUtils = new MessageAsyncSendUtils();
        messageAsyncSendUtils.asyncSendPhone("18331115505","5号仓");
    }
}
