package com.clx.performance.service.impl.settle;

import com.alibaba.fastjson.JSON;
import com.clx.performance.common.MqDelay;
import com.clx.performance.common.MqWrapper;
import com.clx.performance.constant.RabbitKeySettlementConstants;
import com.clx.performance.service.settle.SettlementMqService;
import com.clx.performance.vo.mq.SettlementDetailAddMqParam;
import com.clx.performance.vo.mq.SettlementDetailInvoiceTypeSyncMqParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class SettlementMqServiceImpl implements SettlementMqService {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void settlementDetailAdd(Integer settlementDriverDetailId, Integer settlementOwnerDetailId) {
        SettlementDetailAddMqParam mq = new SettlementDetailAddMqParam();
        mq.setSettlementDriverDetailId(settlementDriverDetailId);
        mq.setSettlementOwnerDetailId(settlementOwnerDetailId);

        MqDelay<MqWrapper<SettlementDetailAddMqParam>> delay = new MqDelay<>(RabbitKeySettlementConstants.DEFAULT_EXCHANGE, RabbitKeySettlementConstants.SETTLEMENT_DETAIL_ADD_ROUTING_KEY, new MqWrapper<>(mq));
        Message message = MessageBuilder.withBody(JSON.toJSONString(new MqWrapper<>(delay)).getBytes()).build();
        message.getMessageProperties().setExpiration("5000");
        rabbitTemplate.send(RabbitKeySettlementConstants.DEFAULT_EXCHANGE, RabbitKeySettlementConstants.DEFAULT_DELAY_ROUTING_KEY, message);

    }

    @Override
    public void invoiceTypeSync(String childNo, Integer invoiceType) {

        SettlementDetailInvoiceTypeSyncMqParam mq = new SettlementDetailInvoiceTypeSyncMqParam();
        mq.setChildNo(childNo);
        mq.setInvoiceType(invoiceType);

        MqDelay<MqWrapper<SettlementDetailInvoiceTypeSyncMqParam>> delay = new MqDelay<>(RabbitKeySettlementConstants.DEFAULT_EXCHANGE, RabbitKeySettlementConstants.SETTLEMENT_DETAIL_INVOICE_TYPE_SYC_ROUTING_KEY, new MqWrapper<>(mq));
        Message message = MessageBuilder.withBody(JSON.toJSONString(new MqWrapper<>(delay)).getBytes()).build();
        message.getMessageProperties().setExpiration("5000");
        rabbitTemplate.send(RabbitKeySettlementConstants.DEFAULT_EXCHANGE, RabbitKeySettlementConstants.DEFAULT_DELAY_ROUTING_KEY, message);
    }

}
