package com.tzy.listener.mq;

import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.tzy.common.constant.Constants;
import com.tzy.common.constant.MqConstans;
import com.tzy.sportcard.api.bean.act.UserListActParam;
import com.tzy.sportcard.api.service.GroupApiService;
import com.tzy.sportcard.group.service.AppActService;
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.stereotype.Component;

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


@Slf4j
@Component
public class UserListActConsumer {
	@Resource
	private GroupApiService groupApiService;
	@Resource
	private AppActService appActService;

	@RabbitHandler
	@RabbitListener(queuesToDeclare = @Queue(MqConstans.QUEUE_USER_LIST_ACT))
	public void userListActConsumer(String data, Channel channel, Message message) throws IOException {
		log.info("panini_list收集活动开始:{}", data);
		try {
			UserListActParam listActParam = JSON.parseObject(data, UserListActParam.class);
			if(appActService.isActSend(Constants.ORDER_TYPE_GROUP,listActParam.getGroupInfoId().intValue(), Constants.COLLECT_PANINI_ACT)){
				log.info("活动已存在记录:{}",data);
				return;
			}
			groupApiService.buildUserListActRecord(listActParam);
			appActService.addActRecord(Constants.ORDER_TYPE_GROUP,listActParam.getGroupInfoId().intValue(), Constants.COLLECT_PANINI_ACT);
		} catch (Exception e) {
			log.error("panini list活动消费异常:{}", data, e);
		}
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}



}
