package com.fzm.mall.server.front.mq;

import com.alibaba.fastjson.JSON;
import com.fzm.mall.server.front.asset.model.po.to.*;
import com.fzm.mall.server.front.asset.model.properties.IntervalProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author tdz
 * @date 2021/02/22 11:26
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MqSend {
    private final AmqpTemplate amqpTemplate;
    private final IntervalProperties intervalProperties;

    public void sendRegister(String uid) {
        this.amqpTemplate.convertAndSend(MqConst.QUEUE_MALL_REGISTER, uid);
    }


    public void sendGoodCain(GoodTO goodTO) throws Exception {
        String jsonStr = JSON.toJSONString(goodTO);
        this.amqpTemplate.convertAndSend(MqConst.QUEUE_MALL_GOOD_CHAIN, jsonStr);
    }

    public void sendPayOrder(PayTO payTO) throws Exception {
        String jsonStr = JSON.toJSONString(payTO);
        this.amqpTemplate.convertAndSend(MqConst.QUEUE_MALL_PAY, jsonStr);
    }

    public void sendOutCoinApply(CoinOutTO coinOutTO) throws Exception {
        String jsonStr = JSON.toJSONString(coinOutTO);
        this.amqpTemplate.convertAndSend(MqConst.QUEUE_MALL_WITHDRAW, jsonStr);
    }
    
    public void sendOrder(String oid) {
        this.amqpTemplate.convertAndSend(MqConst.EXCHANGE_MALL_ORDER_DELAY, MqConst.KEY_MALL_ORDER, oid, message -> {
            message.getMessageProperties().setExpiration(intervalProperties.getOrder());
            return message;
        });
    }

    public void sendDelivery(String oid) {
        this.amqpTemplate.convertAndSend(MqConst.EXCHANGE_MALL_DELIVERY_DELAY, MqConst.KEY_MALL_DELIVERY, oid, message -> {
            message.getMessageProperties().setExpiration(intervalProperties.getDelivery());
            return message;
        });
    }

    public void sendBp(BpTO bpTO) throws Exception {
        String jsonStr = JSON.toJSONString(bpTO);
        this.amqpTemplate.convertAndSend(MqConst.QUEUE_MALL_CREATE_TOKEN, jsonStr);
    }

    public void sendAddToken(CoinTotalTO coinTotalTO) throws Exception {
        String jsonStr = JSON.toJSONString(coinTotalTO);
        this.amqpTemplate.convertAndSend(MqConst.QUEUE_MALL_ADD_TOKEN, jsonStr);
    }

    public void sendOTC(long oid) {
        this.amqpTemplate.convertAndSend(MqConst.EXCHANGE_MALL_OTC_DELAY, MqConst.KEY_MALL_OTC, oid, message -> {
            message.getMessageProperties().setExpiration(intervalProperties.getOtc());
            return message;
        });
    }
}
