package com.iformall.schedule;

import com.iformall.domain.po.msg.*;
import com.iformall.enums.*;
import com.iformall.mq.MqBaseProducer;
import com.iformall.service.WxMsgRecordService;
import com.iformall.utils.JsonUtil;
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;

/**
 * 消息重发
 */
@Component
public class MsgReSendSchedule {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxMsgRecordService wxMsgRecordService;
    @Autowired
    private MqBaseProducer mqBaseProducer;

    //@Scheduled(cron = "0 */1 * * * *?") // 每5分钟检查一次
    @Scheduled(cron = "*/10 * * * * ?") // 测试10秒中一次
    public void msgReSend() {
        logger.info("消息重发开始...");
        //消息重发
        WxMsgRecord msgRecord = new WxMsgRecord();
        msgRecord.setMsgStatus(EnumMsgRecordStatus.SEND_FAIL.getCode());
        List<WxMsgRecord> recordList = wxMsgRecordService.findList(msgRecord);
        for (WxMsgRecord wxMsgRecord:recordList) {
            logger.info("消息重发:{}",wxMsgRecord.getMsgJson());
            try {
                if(EnumMsgRecordType.INSIDE_ORDER_SUCCESS.getCode().equals(wxMsgRecord.getMsgType())){
                    //内部消息 - 下订单成功
                    FmInsideOrderSuccessMsg msg = (FmInsideOrderSuccessMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),FmInsideOrderSuccessMsg.class);
                    msg.setMsgType(wxMsgRecord.getMsgType());
                    msg.setReceiver(wxMsgRecord.getReceiver());
                    msg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.INSIDE_COUPON_VERIFY.getCode().equals(wxMsgRecord.getMsgType())){
                    FmInsideCouponVerifyMsg msg = (FmInsideCouponVerifyMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),FmInsideCouponVerifyMsg.class);
                    msg.setMsgType(wxMsgRecord.getMsgType());
                    msg.setReceiver(wxMsgRecord.getReceiver());
                    msg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.INSIDE_C_LOGIN.getCode().equals(wxMsgRecord.getMsgType())){
                    FmInsideCLoginMsg msg = (FmInsideCLoginMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),FmInsideCLoginMsg.class);
                    msg.setMsgType(wxMsgRecord.getMsgType());
                    msg.setReceiver(wxMsgRecord.getReceiver());
                    msg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.INSIDE_NOTIFY_PAY_SUCCESS.getCode().equals(wxMsgRecord.getMsgType())){
                    FmInsideNotifyPaySuccessMsg msg = (FmInsideNotifyPaySuccessMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),FmInsideNotifyPaySuccessMsg.class);
                    msg.setMsgType(wxMsgRecord.getMsgType());
                    msg.setReceiver(wxMsgRecord.getReceiver());
                    msg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.INSIDE_NOTIFY_REFUND_SUCCESS.getCode().equals(wxMsgRecord.getMsgType())){
                    FmInsideNotifyRefundSuccessMsg msg = (FmInsideNotifyRefundSuccessMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),FmInsideNotifyRefundSuccessMsg.class);
                    msg.setMsgType(wxMsgRecord.getMsgType());
                    msg.setReceiver(wxMsgRecord.getReceiver());
                    msg.setTenantId(wxMsgRecord.getTenantId());
                    mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }
            } catch (Exception e) {
                e.printStackTrace();
                logger.error("消息重发错误:" + e.getMessage());
            }
        }
        logger.info("消息重发结束...");
    }


}