package com.jkcredit.invoice.hub.consumer;

import com.jkcredit.invoice.hub.constant.CommonConstant;
import com.jkcredit.invoice.hub.model.dto.realCharge.RealChargeDto;
import com.jkcredit.invoice.hub.model.dto.user.UserDto;
import com.jkcredit.invoice.hub.model.dto.userBalance.UserBalanceDto;
import com.jkcredit.invoice.hub.model.dto.userBalance.UserBalanceInvoiceDto;
import com.jkcredit.invoice.hub.service.realCharge.RealChargeService;
import com.jkcredit.invoice.hub.service.user.UserService;
import com.jkcredit.invoice.hub.service.userBalance.UserBalanceService;
import com.jkcredit.invoice.hub.util.CommonUtil;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @description:
 * @author: xusonglin
 * @create: 2020/1/17 23:43
 * @version: V1.0
 **/
@Slf4j
@Component
public class ChargeConsumer {

    @Autowired
    UserBalanceService userBalanceService;
    @Autowired
    UserService userService;
    @Autowired
    RealChargeService realChargeService;

    @RabbitHandler
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(
                    name = CommonConstant.CHARGE_EXCHANGE,
                    type = "topic",
                    ignoreDeclarationExceptions = "true"
            ),
            value = @Queue(
                    value = CommonConstant.CHARGE_QUEUE
            ),
            key = "charge.*"
    ))
    public void process(@Payload UserBalanceInvoiceDto userBalanceInvoiceDto, Channel channel, Message message) throws IOException {
        try {
            synchronized (this) {
                // todo
//                UserDto user = userService.getUser(userBalanceInvoiceDto.getUserId());
//                UserBalanceDto dto = new UserBalanceDto();
//                dto.setUserId(userBalanceInvoiceDto.getUserId());
//                dto.setBalance(userBalanceInvoiceDto.getBalance().subtract(user.getPrice()));
//
//                //更新余额
//                userBalanceService.updateUserBalance(dto);
//
//                // 实际扣费数据
//                RealChargeDto realChargeDto = new RealChargeDto();
//                realChargeDto.setUserId(userBalanceInvoiceDto.getUserId());
//                realChargeDto.setPrice(user.getPrice());
//                realChargeDto.setNum(userBalanceInvoiceDto.getNum());
//                realChargeDto.setTransactionId(userBalanceInvoiceDto.getTransactionId());
//                realChargeService.saveRealCharge(realChargeDto);
            }
            //告诉服务器收到这条消息 已经被我消费了 可以在队列删掉 这样以后就不会再发了 否则消息服务器以为这条消息没处理掉 后续还会在发
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            log.warn("消息队列日志:{},计费用户id:{}", "扣款", userBalanceInvoiceDto.getUserId());
        } catch (IOException e) {
            e.printStackTrace();
            //丢弃这条消息
            //channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            //ack返回false，并重新回到队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            log.warn("消息队列失败日志:{},计费用户id:{}", "扣款", userBalanceInvoiceDto.getUserId());
            log.error("接受失败");
        }

    }

}
