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;

    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())){
                //短信
                sendSmsService.send((WxMsgRecord)JsonUtil.readValue(message,WxMsgRecord.class));
            } else if(EnumMsgRecordType.SMS_CALLBACK.getCode().equals(baseMsg.getMsgType())){
                //业务短信
                sendCallBackSmsService.send((WxMsg)JsonUtil.readValue(message,WxMsg.class));
            } else if(EnumMsgRecordType.EMAIL.getCode().equals(baseMsg.getMsgType())){
                //邮件
                sendEmailService.send((MailMsg)JsonUtil.readValue(message,MailMsg.class));
            } else if(EnumMsgRecordType.SMART_APP.getCode().equals(baseMsg.getMsgType())){
                //微信小程序-模板消息
                sendSmartAppMsgService.send((SmartAppMsg)JsonUtil.readValue(message,SmartAppMsg.class));
            } else if(EnumMsgRecordType.PUBLIC.getCode().equals(baseMsg.getMsgType())) {
                //公众号-模板消息
                sendMpMsgService.send((MpAppMsg)JsonUtil.readValue(message,MpAppMsg.class));
            } else if(EnumMsgRecordType.SMART_APP_UNIFORM.getCode().equals(baseMsg.getMsgType())) {
                //微信小程序-统一消息
                sendWeappUniformMsgService.send((AppUniformMsg)JsonUtil.readValue(message,AppUniformMsg.class));
            }
            baseMsg.setMsgStatus(EnumMsgRecordStatus.CONSUME_SUCC.getCode());
            wxMsgRecordMapper.update(baseMsg);
        }catch (Throwable e){
            try {
                log.error("consum received error: ", e);
                baseMsg.setStatusMessage(e.getMessage());
                baseMsg.setMsgStatus(EnumMsgRecordStatus.CONSUME_FAIL.getCode());
                wxMsgRecordMapper.update(baseMsg);
            }catch (Throwable e1){
                log.error("更新db状态error: ", e1);
            }

//            if(null != baseMsg){
//                if(baseMsg.getDelayTimeLevel() < 3){
//                    baseMsg.setDelayTimeLevel(3); //10s后重试
//                    mqBaseProducer.sendMessage(baseMsg, EnumMsgMqTopic.DEFAULT.getCode(), EnumMsgMqTag.DEFAULT.getCode(), EnumMsgMqKey.DEFAULT.getCode());
//                }else{
//                    baseMsg.setStatusMessage(e.getMessage());
//                    baseMsg.setMsgStatus(EnumMsgRecordStatus.CONSUME_FAIL.getCode());
//                    wxMsgRecordMapper.update(baseMsg);
//                }
//            }
        }

    }
}
