package com.iformall.mq;

import com.iformall.domain.po.msg.*;
import com.iformall.enums.*;
import com.iformall.mapper.WxMsgRecordMapper;
import com.iformall.service.msg.impl.*;
import com.iformall.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 消息消费入口
 */
@Service
public class MqBaseConsumer {
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private WxMsgRecordMapper wxMsgRecordMapper;
    @Autowired
    private SendSmsServiceImpl sendSmsService;
    @Autowired
    private SendCallBackSmsServiceImpl sendCallBackSmsService;
    @Autowired
    private SendEmailServiceImpl sendEmailService;
    @Autowired
    private SendSmartAppMsgServiceImpl sendSmartAppMsgService;
    @Autowired
    private SendMpMsgServiceImpl sendMpMsgService;
    @Autowired
    private SendWeappUniformMsgServiceImpl sendWeappUniformMsgService;
    @Autowired
    private FmInsideOrderSuccessMsgServiceImpl fmInsideOrderSuccessMsgService;
    @Autowired
    private FmInsideCouponVerifyMsgServiceImpl fmInsideCouponVerifyMsgService;
    @Autowired
    private MqBaseProducer mqBaseProducer;

    @Autowired
    private FmInsideNotifyPaySuccessMsgServiceImpl fmInsideNotifyPaySuccessMsgService;
    @Autowired
    private FmInsideNotifyRefundSuccessMsgServiceImpl fmInsideNotifyRefundSuccessMsgService;



    public void doMessage(String message) {
        log.info("received message: {}", message);
        BaseMsg baseMsg = null;

        try {
            baseMsg = (BaseMsg)JsonUtil.readValue(message,BaseMsg.class);

            if(EnumMsgRecordType.SMS.getCode().equals(baseMsg.getMsgType())){
                //短信
                WxMsgRecord msg = (WxMsgRecord)JsonUtil.readValue(message,WxMsgRecord.class);
                sendSmsService.send(msg);
            } else if(EnumMsgRecordType.SMS_CALLBACK.getCode().equals(baseMsg.getMsgType())){
                //业务短信
                WxMsg msg = (WxMsg)JsonUtil.readValue(message,WxMsg.class);
                sendCallBackSmsService.send(msg);
            } else if(EnumMsgRecordType.EMAIL.getCode().equals(baseMsg.getMsgType())){
                //邮件
                MailMsg msg = (MailMsg)JsonUtil.readValue(message,MailMsg.class);
                sendEmailService.send(msg);
            } else if(EnumMsgRecordType.SMART_APP.getCode().equals(baseMsg.getMsgType())){
                //微信小程序-模板消息
                SmartAppMsg msg = (SmartAppMsg)JsonUtil.readValue(message,SmartAppMsg.class);
                sendSmartAppMsgService.send(msg);
            } else if(EnumMsgRecordType.PUBLIC.getCode().equals(baseMsg.getMsgType())) {
                //公众号-模板消息
                MpAppMsg msg = (MpAppMsg)JsonUtil.readValue(message,MpAppMsg.class);
                sendMpMsgService.send(msg);
            } else if(EnumMsgRecordType.SMART_APP_UNIFORM.getCode().equals(baseMsg.getMsgType())) {
                //微信小程序-统一消息
                AppUniformMsg msg = (AppUniformMsg)JsonUtil.readValue(message,AppUniformMsg.class);
                sendWeappUniformMsgService.send(msg);
            } else if(EnumMsgRecordType.INSIDE_ORDER_SUCCESS.getCode().equals(baseMsg.getMsgType())) {
                // 内部消息 - 下订单成功
                FmInsideOrderSuccessMsg msg = (FmInsideOrderSuccessMsg)JsonUtil.readValue(message,FmInsideOrderSuccessMsg.class);
                fmInsideOrderSuccessMsgService.send(msg);
            } else if(EnumMsgRecordType.INSIDE_COUPON_VERIFY.getCode().equals(baseMsg.getMsgType())) {
                // 内部消息 - 券核销
                FmInsideCouponVerifyMsg msg = (FmInsideCouponVerifyMsg)JsonUtil.readValue(message,FmInsideCouponVerifyMsg.class);
                fmInsideCouponVerifyMsgService.send(msg);
            } else if(EnumMsgRecordType.INSIDE_C_LOGIN.getCode().equals(baseMsg.getMsgType())) {
                // 内部消息 - c端登录
                FmInsideCLoginMsg msg = (FmInsideCLoginMsg)JsonUtil.readValue(message,FmInsideCLoginMsg.class);
                fmInsideCouponVerifyMsgService.send(msg);
            } else if(EnumMsgRecordType.INSIDE_NOTIFY_PAY_SUCCESS.getCode().equals(baseMsg.getMsgType())) {
                // 内部消息 - 微信支付通知
                FmInsideNotifyPaySuccessMsg msg = (FmInsideNotifyPaySuccessMsg)JsonUtil.readValue(message,FmInsideNotifyPaySuccessMsg.class);
                fmInsideNotifyPaySuccessMsgService.send(msg);
            } else if(EnumMsgRecordType.INSIDE_NOTIFY_REFUND_SUCCESS.getCode().equals(baseMsg.getMsgType())) {
                // 内部消息 - 微信退款通知
                FmInsideNotifyRefundSuccessMsg msg = (FmInsideNotifyRefundSuccessMsg)JsonUtil.readValue(message,FmInsideNotifyRefundSuccessMsg.class);
                fmInsideNotifyRefundSuccessMsgService.send(msg);
            }
            baseMsg.setMsgStatus(EnumMsgRecordStatus.CONSUME_SUCC.getCode());
            wxMsgRecordMapper.updateByBaseMsg(baseMsg);
        }catch (Throwable e){
            try {
                log.error("consum received error: ", e);
                baseMsg.setStatusMessage(e.getMessage());
                baseMsg.setMsgStatus(EnumMsgRecordStatus.CONSUME_FAIL.getCode());
                wxMsgRecordMapper.updateByBaseMsg(baseMsg);

                if(null != baseMsg){
                    if(baseMsg.getDelayTimeLevel() > 0){
                        log.info("消息重试，DelayTimeLevel: "+baseMsg.getDelayTimeLevel());
                        Thread.sleep(5000);
                        baseMsg.setDelayTimeLevel(baseMsg.getDelayTimeLevel()-1);

                        if(EnumMsgRecordType.INSIDE_NOTIFY_REFUND_SUCCESS.getCode().equals(baseMsg.getMsgType())) {
                            // 内部消息 - 微信退款通知
                            FmInsideNotifyRefundSuccessMsg msg = (FmInsideNotifyRefundSuccessMsg)JsonUtil.readValue(message,FmInsideNotifyRefundSuccessMsg.class);
                            mqBaseProducer.sendMessage(msg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
                        }
                    }else{
                        baseMsg.setStatusMessage("重试次数超过3次");
                        baseMsg.setMsgStatus(EnumMsgRecordStatus.CONSUME_FAIL.getCode());
                        wxMsgRecordMapper.updateByBaseMsg(baseMsg);
                    }
                }
            }catch (Throwable e1){
                log.error("更新db状态error: ", e1);
            }
        }

    }
}
