package com.czg.config;

import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author GYJoker
 */
@Service
@Slf4j
public class RabbitPublisher {

    @Value("${spring.profiles.active}")
    private String activeProfile;

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 订单取消消息
     * 目前 用来回滚库存
     *
     * @param orderId 订单id
     */
    public void sendOrderCancelMsg(String orderId) {
        sendMsg(RabbitConstants.Queue.ORDER_CANCEL_QUEUE, orderId);
    }

    /**
     * 库存损耗消息
     *
     * @param orderId 订单id
     */
    public void sendOrderStockMsg(String orderId) {
        sendMsg(RabbitConstants.Queue.ORDER_STOCK_QUEUE, orderId);
    }

    /**
     * 退款库存回滚消息(未校验商品退款是否退回库存)
     *
     * @param refundMap Map <Long,BigDecimal> 商品Id,退单数量
     */
    public void sendOrderRefundMsg(String refundMap) {
        sendMsg(RabbitConstants.Queue.ORDER_REFUND_QUEUE, refundMap);
    }

    /**
     * 订单打印消息
     *
     * @param orderId    订单id
     * @param printOrder 是否打印结算单
     */
    public void sendOrderPrintMsg(String orderId, boolean printOrder) {
        log.info("开始发送打印mq消息, orderId: {}, printOrder: {}", orderId, printOrder);
        //厨房票
        sendMsg(RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE, new JSONObject().fluentPut("orderId", orderId).fluentPut("printOrder", printOrder).toString());
        //前台票
        if (printOrder) {
            sendMsg(RabbitConstants.Queue.ORDER_PRINT_QUEUE, orderId);
        }
    }

    /**
     * 退款订单打印消息
     *
     * @param orderId 订单id
     */
    public void sendOrderReturnPrintMsg(String orderId) {
        //厨房票
        sendMsg(RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE, new JSONObject().fluentPut("orderId", orderId).fluentPut("printOrder", true).toString());
    }

    /**
     * 交班小票打印消息
     *
     * @param handoverRecordId 交班记录id
     */
    public void sendHandoverPrintMsg(String handoverRecordId) {
        sendMsg(RabbitConstants.Queue.ORDER_HANDOVER_PRINT_QUEUE, handoverRecordId);
    }

    /**
     * 商品信息变动消息
     *
     * @param shopId 店铺id
     */
    public void sendProductInfoChangeMsg(String shopId) {
        sendMsg(RabbitConstants.Queue.PRODUCT_INFO_CHANGE_QUEUE, shopId);
    }

    /**
     * 排队叫号小票打印
     *
     * @param id 叫号队列id
     */
    public void printCallNumTicket(Long id) {
        sendMsg(RabbitConstants.Queue.ORDER_HANDOVER_PRINT_QUEUE, id.toString());
    }

    /**
     * 1,2,applySmsTemp  模版审核   shop_id,sms_shop_template.id,applySmsTemp
     * 1,2,sendMarkSms 发送营销短信    shop_id,sms_push_event.id,sendMarkSms
     * 1,2,sendWechatTemp 发送微信模版消息  shop_id,ac_push_event.id,sendWechatTemp
     * 1,2,groupBuyYes 发送微信模版消息     shop_id,gb_order.id,groupBuyYes
     * @param param 店铺Id,主键Id
     * @param type applySmsTemp,sendMarkSms,sendWechatTemp,groupBuyYes
     */
    public void sendApplySmsMsg(String param, String type) {
        sendMsg(RabbitConstants.Queue.APPLY_SMS_TEMPLATE_QUEUE, param + "," + type);
    }

    /**
     * 发送生日礼品短信队列
     *
     */
    public void sendBirthdayGiftSmsMsg(String param) {
        sendMsg(RabbitConstants.Queue.BIRTHDAY_GIFT_SMS_QUEUE, param);
    }

    /**
     * 订单商品状态消息
     */
    public void sendOrderProductStatusMsg(String qrContent) {
        sendMsg(RabbitConstants.Queue.ORDER_PRODUCT_STATUS_QUEUE, qrContent);
    }


    /**
     * 订单商品状态消息
     */
    public void sendOrderDetailStatusMsg(String shopId, String type) {
        sendMsg(RabbitConstants.Queue.ORDER_DETAIL_STATUS_QUEUE, JSONObject.toJSONString(Map.of(
                "shopId", shopId,
                "type", type
        )));
    }

    private void sendMsg(String queue, String msg) {
        log.info("开始发送mq消息,exchange:{}, queue: {}, msg: {}", activeProfile + "-" + RabbitConstants.Exchange.CASH_EXCHANGE, activeProfile + "-" + queue, msg);
        rabbitTemplate.convertAndSend(activeProfile + "-" + RabbitConstants.Exchange.CASH_EXCHANGE, activeProfile + "-" + queue, msg);
    }

}
