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 shopId 店铺id
     */
    public void sendProductInfoChangeMsg(String shopId) {
        sendMsg(RabbitConstants.Queue.PRODUCT_INFO_CHANGE_QUEUE, shopId);
    }

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

    /**
     * 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 sendEntryManagerMsg(String shopId) {
        sendMsg(RabbitConstants.Queue.SHOP_ENTRY_MANAGER, shopId);
    }


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


    /**
     * 其它类型 打印消息
     * STOCK 出入库
     * DAY_REPORT 经营日报
     * DAY_ORDER 日结单
     * PRODUCT_REPORT 商品报表
     * RECHARGE 储值单
     * STOCK_CHECK 库存盘点
     * HANDOVER 交班单
     * CALL 排队取号
     *
     * @param printType {@link com.czg.service.order.print.PrinterHandler.PrintTypeEnum}
     */
    public void sendOtherPrintMsg(Long shopId, Object data, String printType) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("shopId", shopId);
        jsonObject.put("data", data);
        jsonObject.put("printTypeEnum", printType);
        sendMsg(RabbitConstants.Queue.OTHER_PRINT_QUEUE, jsonObject.toString());
    }

    /**
     * 后付费订单打印消息
     *
     * @param orderId  订单id
     * @param before   0后付费/1先付费
     * @param status   订单状态 0未完成/1完成
     * @param placeNum 第几次下单
     */
    public void sendOrderPrintMsg(Long orderId, Integer before, Integer status, Integer placeNum, String source) {
        log.info("订单打印消息, orderId:{},{},{},第:{}次下单,source:{}",
                orderId, before == 0 ? "后付" : "先付", status == 0 ? "未完成" : "完成", placeNum, source);
        String sendOrderId = orderId + "_" + before + "_" + status + "_" + placeNum;
        //厨房单
        sendMsg(RabbitConstants.Queue.ORDER_MACHINE_PRINT_QUEUE, new JSONObject().fluentPut("orderId", sendOrderId).toString());
    }

    /**
     * 本地打印机 订单消息通知
     *
     * @param shopId        店铺Id
     * @param orderId
     * @param placeNum
     * @param printTypeEnum
     * @param data
     */
    public void sendOrderPrintLocalMsg(Long shopId, Long orderId, Integer placeNum, String printTypeEnum, String data, String operator, String amount) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("shopId", shopId);
        jsonObject.put("orderId", orderId);
        jsonObject.put("placeNum", placeNum);
        jsonObject.put("amount", amount);
        jsonObject.put("printType", printTypeEnum);
        jsonObject.put("data", data);
        jsonObject.put("operator", operator);
        sendMsg(RabbitConstants.Queue.ORDER_PRINT_QUEUE, jsonObject.toString());
    }

    /**
     * 其它类型 打印消息
     * STOCK 出入库
     * DAY_REPORT 经营日报
     * DAY_ORDER 日结单
     * PRODUCT_REPORT 商品报表
     * RECHARGE 储值单
     * STOCK_CHECK 库存盘点
     * HANDOVER 交班单
     * CALL 排队取号
     *
     * @param printType {@link com.czg.service.order.print.PrinterHandler.PrintTypeEnum}
     */
    public void sendOtherPrintLocalMsg(Long shopId, String printType, Long dataId) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("shopId", shopId);
        jsonObject.put("dataId", dataId);
        jsonObject.put("printType", printType);
        sendMsg(RabbitConstants.Queue.OTHER_PRINT_LOCAL_QUEUE, jsonObject.toString());
    }


    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);
    }
}
