package com.iformall.schedule;

import com.alibaba.fastjson.JSONArray;
import com.iformall.domain.po.msg.WxMsg;
import com.iformall.enums.*;
import com.iformall.mapper.WxMsgConfigMapper;
import com.iformall.mapper.WxMsgMapper;
import com.iformall.mapper.WxMsgModelMapper;
import com.iformall.mq.MqBaseProducer;
import com.iformall.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * @author gongbiao
 */
@Component
public class MsgSendingSchedule {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMsgMapper wxMsgMapper;

    @Autowired
    private WxMsgConfigMapper wxMsgConfigMapper;

    @Autowired
    private WxMsgModelMapper wxMsgModelMapper;

    @Autowired
    private MqBaseProducer mqBaseProducer;

    /**
     * 每小时第一分钟执行
     */
    @Scheduled(cron = "0 1 * * * ?")
    public void sendmsgschedule() {
        logger.info("sendmsg定时任务启动");
        String systemTime = DateUtils.getSystemTime("yyyy-MM-dd HH:00:00");
        WxMsg wxMsg = new WxMsg();
        wxMsg.setIsright(0);
        wxMsg.setSendtime(systemTime);
        List<WxMsg> list = wxMsgMapper.findList(wxMsg);
        logger.info("将要发送的短信列表:" + JSONArray.toJSONString(list));
        for (WxMsg msg : list) {
            //sendmsg(msg);
            msg.setMsgType(EnumMsgRecordType.SMS_CALLBACK.getCode());
            msg.setReceiver(msg.getPhones());
            mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(),null);
        }
        logger.info("sendmsg定时任务结束");
    }

//    @Deprecated
//    public void sendmsg(WxMsg wxMsg) {
//        logger.info("发送短信开始----------");
//        //从短信配置中查询密钥 bid 等信息
//        WxMsgConfig wxMsgConfig = new WxMsgConfig();
//        wxMsgConfig.setTenantId(wxMsg.getTenantId());
//        List<WxMsgConfig> wxMsgConfigs = wxMsgConfigMapper.findList(wxMsgConfig);
//        if (wxMsgConfigs.size() == 0) {
//            logger.info("短信相关配置不存在");
//            return;
//        }
//        wxMsgConfig = wxMsgConfigs.get(0);
//        if (wxMsgConfig.getRemains() == 0) {
//            logger.info("短信数量为0");
//            return;
//        }
//        if (wxMsgConfig.getRemains() < wxMsg.getExpectSendNumber()) {
//            logger.info("短信数量不足");
//            return;
//        }
//        String secret = wxMsgConfig.getSecret();
//        String bid = wxMsgConfig.getBid();
//        String publickey = wxMsgConfig.getPublickey();
//        String phone = wxMsg.getPhones();
//        String signature = wxMsg.getSignature();
//        String msg = wxMsg.getMsg();
//        String notifyUrl = wxMsgConfig.getNotifyurl();
//        Integer modelId = null;
//        Long modelIdParam = wxMsg.getModelId();
//        if (modelIdParam != null) {
//            WxMsgModel wxMsgModel = wxMsgModelMapper.selectById(modelIdParam);
//            modelId = wxMsgModel.getModelId();
//        }
//        String result = WiwideUtil.sendMsg(secret, bid, publickey, phone, signature, msg, notifyUrl, EnumVerifyCode.NO.getCode().toString(), modelId);
//        logger.info("短信返回结果:" + result);
//        JSONObject jsonObjectResult = JSONObject.parseObject(result);
//        String ret = jsonObjectResult.get("ret").toString();
//        if (ret.equals("1")) {
//            wxMsg.setSendstatus(EnumMsgSendStatus.MSG_SEND_SUCCESS.getCode());
//        } else {
//            wxMsg.setSendstatus(EnumMsgSendStatus.MSG_SEND_FAIL.getCode());
//        }
//        wxMsg.setStatus(EnumMsgStatus.MSG_STATUS_SENDED.getCode());
//        wxMsgMapper.updateById(wxMsg);
//        logger.info("短信发送结束:" + jsonObjectResult.toJSONString());
//    }

}