package com.kxs.stat.biz.mq;

import com.alibaba.fastjson.JSON;
import com.kxs.common.core.constant.SecurityConstants;
import com.kxs.stat.api.amqp.rabbit.RabbitKxsActQueueMQ;
import com.kxs.system.api.dto.kxsapp.camp.CampOpenNumStatDTO;
import com.kxs.system.api.feign.RemoteSysService;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 客小爽pos 广电 来客吧激活推送队列
 *
 * @author Pota1ovO
 * @date 2024-05-07
 */
@Component
@Slf4j
@RequiredArgsConstructor
public class RabbitKxsActQueueListener {

    private final RemoteSysService remoteSysService;

    /**
     * 监听 客小爽激活队列 队列的处理器
     *
     * @param message 消息
     */
    @RabbitListener(queues = RabbitKxsActQueueMQ.QUEUE_NAME, ackMode = "MANUAL")
    @RabbitHandler
    public void onActMessage(String msg, Message message, Channel channel) {
        try {
            RabbitKxsActQueueMQ.MsgEntity parse = RabbitKxsActQueueMQ.parse(msg);
            log.info("客小爽激活队列消费端Payload: " + JSON.toJSONString(parse));
//            Integer tradeMonth = TotalDateUtil.getMonthNumber(LocalDateTimeUtil.parseDate(parse.getActTime(), DatePattern.UTC_SIMPLE_PATTERN));

            CampOpenNumStatDTO campOpenNumStatDTO = new CampOpenNumStatDTO();
            BeanUtils.copyProperties(parse, campOpenNumStatDTO);
            remoteSysService.campOpenNumStat(campOpenNumStatDTO, SecurityConstants.FROM_IN);

        } catch (Exception e) {
            log.error("客小爽激活队列消费失败:{}", JSON.toJSONString(msg), e);
            try {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } catch (IOException ex) {
                log.error("客小爽激活队列手动确认失败" + ex.getMessage(), ex);
            }
        }
    }


}