package com.seamew.lottery.application.mq.producer;

import com.alibaba.fastjson2.JSON;
import com.seamew.lottery.domain.activity.model.vo.ActivityPartakeRecordVO;
import com.seamew.lottery.domain.activity.model.vo.InvoiceVO;
import com.seamew.lottery.domain.strategy.model.vo.AwardDeductStockVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.support.SendResult;
import org.springframework.stereotype.Component;
import org.springframework.util.concurrent.ListenableFuture;

import javax.annotation.Resource;

/**
 * @Author: seamew
 * @Title: KafkaProducer
 * @CreateTime: 2023年02月27日 11:06:00
 * @Description: 消息生产者
 * @Version: 1.0
 */
@Component
@Slf4j
public class KafkaProducer {
    @Resource
    private KafkaTemplate<String, Object> kafkaTemplate;

    /**
     * MQ主题：中奖发货单
     */
    public static final String TOPIC_INVOICE = "lottery_invoice";

    /**
     * MQ主题：活动领取记录
     */
    public static final String TOPIC_ACTIVITY_PARTAKE = "lottery_activity_partake";

    /**
     * MQ主题：奖品剩余库存记录
     */
    public static final String TOPIC_AWARD_SURPLUS_COUNT = "lottery_award_surplus_count";

    public static final String GROUP_NAME = "lottery";

    /**
     * 发送中奖物品发货单消息
     *
     * @param invoice 发货单
     */
    public ListenableFuture<SendResult<String, Object>> sendLotteryInvoice(InvoiceVO invoice) {
        String objJson = JSON.toJSONString(invoice);
        log.info("发送MQ消息 topic：{} bizId：{} message：{}", TOPIC_INVOICE, invoice.getUId(), objJson);
        return kafkaTemplate.send(TOPIC_INVOICE, objJson);
    }

    /**
     * 发送领取活动记录MQ
     *
     * @param activityPartakeRecord 领取活动记录
     */
    public void sendLotteryActivityPartakeRecord(ActivityPartakeRecordVO activityPartakeRecord) {
        String objJson = JSON.toJSONString(activityPartakeRecord);
        log.info("发送MQ消息(领取活动记录) topic：{} bizId：{} message：{}", TOPIC_ACTIVITY_PARTAKE, activityPartakeRecord.getUId(), objJson);
        kafkaTemplate.send(TOPIC_ACTIVITY_PARTAKE, objJson);
    }

    /**
     * 发送削减库存记录MQ
     *
     * @param awardDeductStock 削减库存记录
     */
    public void sendLotteryAwardDeductStock(AwardDeductStockVO awardDeductStock) {
        String objJson = JSON.toJSONString(awardDeductStock);
        log.info("发送MQ消息(领取活动记录) topic：{} message：{}", TOPIC_AWARD_SURPLUS_COUNT, objJson);
        kafkaTemplate.send(TOPIC_AWARD_SURPLUS_COUNT, objJson);
    }
}
