package com.tzy.listener.mq;

import com.rabbitmq.client.Channel;
import com.tzy.common.constant.Constants;
import com.tzy.common.constant.MqConstans;
import com.tzy.sportcard.api.service.MineApiService;
import com.tzy.sportcard.point.domain.AppUserPointRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;


@Slf4j
@Component
public class UserPointConsumer {

    @Autowired
    private MineApiService mineApiService;

    @RabbitHandler
    @RabbitListener(queuesToDeclare = @Queue(MqConstans.QUEUE_USER_POINT))
    public void addPointRecord(AppUserPointRecord pointRecord, Channel channel, Message message) throws IOException {
        userPointConsumer(pointRecord, channel, message);
    }

    private void userPointConsumer(AppUserPointRecord pointRecord, Channel channel, Message message) throws IOException {
        try {
            mineApiService.addUserPoint(pointRecord);
            /*if(Constants.POINT_TYPE_GOODS.equals(pointRecord.getType())){
                pointRecord.setType(Constants.POINT_TYPE_USER_GROWTH);
                mineApiService.addUserPoint(pointRecord);
            }*/
        } catch (Exception e) {
            log.info("积分记录添加异常,参数:{}", pointRecord, e);
        }
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }





}
