package com.fdkankan.ucenter.mq.consumer;

import com.alibaba.fastjson.JSONObject;
import com.fdkankan.ucenter.mq.vo.LaserSendMail;
import com.fdkankan.ucenter.mq.vo.ScenePayStatusVo;
import com.fdkankan.ucenter.service.IMailTemplateService;
import com.fdkankan.ucenter.service.ISceneProService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 场景封存解封 mq
 */
@Slf4j
@Component
public class LaserSendMailConsumer {


    @Autowired
    IMailTemplateService mailTemplateService;

    @RabbitListener(
            queuesToDeclare = @Queue("${send-cooperation-email:send-cooperation-email}")
            ,concurrency = "1"
    )
    public void consumerQueue(Channel channel, Message message)  {
        try {
            String messageId = message.getMessageProperties().getMessageId();
            String msg = new String(message.getBody(), StandardCharsets.UTF_8);
            log.info("send-cooperation-email-mq--messageId:{},msg:{}",messageId,msg);

            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);

            JSONObject jsonObject = JSONObject.parseObject(msg);
            if(jsonObject == null){
                return;
            }
            List<LaserSendMail> vo = JSONObject.parseArray(jsonObject.getString("data"), LaserSendMail.class);
            if(vo == null || vo.isEmpty()){
                return;
            }
            mailTemplateService.sendSceneCooperation(vo);
        }catch (Exception e){
            log.info("send-cooperation-email----消费失败",e);
        }finally {

        }

    }

}
