package com.iformall.controller;

import com.iformall.common.ResultData;
import com.iformall.domain.po.*;
import com.iformall.enums.*;
import com.iformall.mq.MqBaseProducer;
import com.iformall.service.WxMsgRecordService;
import com.iformall.utils.JsonUtil;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Api(description = "消息记录")
@RequestMapping(value = "msgrecord")
public class WxMsgRecordController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

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

    /**
     * 重新发送失败的消息
     * @param x
     * @return
     */
    @GetMapping(value = "/resendMsg")
    public ResultData resendMsg(String x) {
        if("xll1dxx1314".equals(x)){
            WxMsgRecord msgRecord = new WxMsgRecord();
            msgRecord.setMsgStatus(EnumMsgRecordStatus.CONSUME_FAIL.getCode());
            List<WxMsgRecord> recordList = wxMsgRecordService.findList(msgRecord);
            for (WxMsgRecord wxMsgRecord:recordList) {
                if(EnumMsgRecordType.SMS.getCode().equals(wxMsgRecord.getMsgType())){
                    //短信
                    mqBaseProducer.sendMessage((WxMsgRecord) JsonUtil.readValue(wxMsgRecord.getMsgJson(),WxMsgRecord.class), EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.SMS_CALLBACK.getCode().equals(wxMsgRecord.getMsgType())){
                    //业务短信
                    mqBaseProducer.sendMessage((WxMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),WxMsg.class), EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.EMAIL.getCode().equals(wxMsgRecord.getMsgType())){
                    //邮件
                    mqBaseProducer.sendMessage((MailMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),MailMsg.class), EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }else if(EnumMsgRecordType.SMART_APP.getCode().equals(wxMsgRecord.getMsgType())){
                    //微信小程序
                    mqBaseProducer.sendMessage((SmartAppMsg) JsonUtil.readValue(wxMsgRecord.getMsgJson(),SmartAppMsg.class), EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                }
            }
            return new ResultData();
        }else{
            return new ResultData("fail");
        }
    }





}