package com.tofly.xrtygis.handle.plan;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tofly.xrtygis.callCenterMapper.BusiAutocallData1000Mapper;
import com.tofly.xrtygis.custom.entity.dto.WisdomDisplayDto;
import com.tofly.xrtygis.custom.entity.message.SendMessage;
import com.tofly.xrtygis.custom.service.messge.SendMessageService;
import com.tofly.xrtygis.entity.*;
import com.tofly.xrtygis.entity.dto.TfMsgPushManageDto;
import com.tofly.xrtygis.entity.vo.TfMsgPersonManageVo;
import com.tofly.xrtygis.service.TfMsgPersonManageService;
import com.tofly.xrtygis.service.TfMsgPushManageService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 定时发送消息
 *
 * @author JF
 */
@Component
public class MsgPushHandler {

    @Autowired
    private TfMsgPersonManageService msgPersonManageService;
    @Autowired
    private TfMsgPushManageService msgPushManageService;
    @Autowired
    private SendMessageService sendMessageService;
    @Autowired
    private BusiAutocallData1000Mapper busiAutocallData1000Mapper;

    @XxlJob("msgPushHandler")
    public ReturnT<String> automaticallySignBackHandler(String param) {
        TfMsgPushManageDto dto = new TfMsgPushManageDto();
        dto.setSendStatus("0");
        List<TfMsgPersonManageVo> msgPersonManageVos = msgPersonManageService.getMsgPersonInfoList(dto);
        for(TfMsgPersonManageVo one : msgPersonManageVos){
            if(one.getSendTime() == null || one.getPhone() == null || one.getContent() == null || one.getMsgType() == null){
                continue;
            }
            int sign = one.getSendTime().compareTo(new Date());
            if(sign>0){
                if(one.getMsgType().equals("2")){
                    SendMessage msg = new SendMessage();
                    msg.setLxdh(one.getPhone());
                    msg.setTxnr(one.getContent());
                    msg.setTxlx(one.getMsgType());
                    sendMessageService.sendMessage(msg);
                }else if(one.getMsgType().equals("1")){
                    Map<String,Object> callInfo = busiAutocallData1000Mapper.selectCallInfo(one.getPhone(),null);
                    if(callInfo == null){
                        busiAutocallData1000Mapper.insertBusiAutocallData(one.getPhone(),one.getContent());
                    }else {
                        busiAutocallData1000Mapper.updateBusiAutocallData(one.getPhone(),one.getContent());
                    }
                }
                if(one.getSendStatus().equals("0")){
                    TfMsgPushManage msgPushManage = new TfMsgPushManage();
                    msgPushManage.setId(one.getId());
                    msgPushManage.setSendStatus("1");
                    msgPushManageService.updateById(msgPushManage);
                }
            }
        }

        //获得呼叫结果
        TfMsgPushManageDto dtoCallResult = new TfMsgPushManageDto();
        dtoCallResult.setMsgType("1");
        dto.setSendStatus("1");
        dtoCallResult.setHasCallResult("0");
        List<TfMsgPersonManageVo> callResultVos = msgPersonManageService.getMsgPersonInfoList(dto);
        if(callResultVos.size() == 0){
            return ReturnT.SUCCESS;
        }
        TfMsgPersonManage msgPersonManage = new TfMsgPersonManage();
        for (TfMsgPersonManageVo callResultVo : callResultVos){
            Map<String,Object> callInfo = busiAutocallData1000Mapper.selectCallInfo(callResultVo.getPhone(),2);
            if(callInfo == null || callInfo.get("callResult") == null){
                continue;
            }
            msgPersonManage.setId(callResultVo.getMsgPersonId());
            msgPersonManage.setCallResult(String.valueOf(callInfo.get("callResult")));
            msgPersonManageService.updateById(msgPersonManage);
        }
        return ReturnT.SUCCESS;
    }
}
