package com.erp.mq.producer;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * 库存消息生产者
 * 负责发送库存相关的消息
 */
@Slf4j
@Component
public class InventoryMessageProducer extends BaseRocketMQProducer {

    @Value("${erp.rocketmq.topics.inventory}")
    private String inventoryTopic;

    public static final String TAGS_DEDUCT = "deduct";           // 扣减库存
    public static final String TAGS_RESTORE = "restore";         // 恢复库存
    public static final String TAGS_LOCK = "lock";               // 锁定库存
    public static final String TAGS_UNLOCK = "unlock";           // 解锁库存
    public static final String TAGS_ALERT = "alert";             // 库存预警

    @Override
    protected String getTopic() {
        return inventoryTopic;
    }

    @Override
    protected String getProducerGroup() {
        return "erp-inventory-producer-group";
    }

    /**
     * 发送库存扣减消息
     */
    public void sendInventoryDeduct(InventoryMessage inventoryMessage) {
        log.info("发送库存扣减消息 - SkuId: {}, Quantity: {}",
                inventoryMessage.getSkuId(), inventoryMessage.getQuantity());
        sendMessage(inventoryMessage, TAGS_DEDUCT, inventoryMessage.getSkuId());
    }

    /**
     * 发送库存恢复消息
     */
    public void sendInventoryRestore(InventoryMessage inventoryMessage) {
        log.info("发送库存恢复消息 - SkuId: {}, Quantity: {}",
                inventoryMessage.getSkuId(), inventoryMessage.getQuantity());
        sendMessage(inventoryMessage, TAGS_RESTORE, inventoryMessage.getSkuId());
    }

    /**
     * 发送库存锁定消息
     */
    public void sendInventoryLock(InventoryMessage inventoryMessage) {
        log.info("发送库存锁定消息 - SkuId: {}, Quantity: {}",
                inventoryMessage.getSkuId(), inventoryMessage.getQuantity());
        sendMessage(inventoryMessage, TAGS_LOCK, inventoryMessage.getSkuId());
    }

    /**
     * 发送库存预警消息
     */
    public void sendInventoryAlert(InventoryMessage inventoryMessage) {
        log.info("发送库存预警消息 - SkuId: {}, CurrentStock: {}",
                inventoryMessage.getSkuId(), inventoryMessage.getCurrentStock());
        sendMessage(inventoryMessage, TAGS_ALERT, inventoryMessage.getSkuId());
    }

    @Data
    public static class InventoryMessage {
        private String skuId;
        private String productName;
        private Integer quantity;
        private Integer currentStock;
        private Integer threshold;
        private String warehouseId;
        private String orderId;
        private String operationType;
        private Long timestamp;
    }
}
