package com.tzy.listener.mq;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.tzy.common.constant.MqConstans;
import com.tzy.common.dto.group.GroupActRecord;
import com.tzy.sportcard.api.service.GroupApiService;
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.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;


@Slf4j
@Component
public class GiftCardConsumer {


	@Resource
	private GroupApiService groupApiService;
	@Autowired
	private MineApiService mineApiService;

	@RabbitHandler
	@RabbitListener(queues = {MqConstans.QUEUE_GROUP_GIFT_CARD})
	public void giftCardConsumer(String param, Channel channel, Message message) throws IOException {
		log.info("精美卡片新增记录:{}", param);
		try {
			GroupActRecord actRecord = JSON.parseObject(param, GroupActRecord.class);
			List<AppUserPointRecord> records = groupApiService.getUserGroupGiftCard(actRecord.getRefId());
			records.forEach(r -> mineApiService.addUserPoint(r));
		} catch (Exception e) {
			log.error("精美卡片新增异常,参数:{}", param, e);
		}
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

}
