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.common.utils.DateUtils;
import com.tzy.sportcard.api.service.GroupApiService;
import com.tzy.sportcard.group.service.AppActService;
import com.tzy.sportcard.group.service.ICardGroupGoodsService;
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.stereotype.Component;
import org.teasoft.bee.osql.SuidRich;

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


@Slf4j
@Component
public class GroupOrderActConsumer {

	@Resource
	private AppActService appActService;
	@Resource
	private SuidRich suidRich;
	@Resource
	private GroupApiService groupApiService;
	@Resource
	private ICardGroupGoodsService cardGroupGoodsService;

	@RabbitHandler
	@RabbitListener(queues = {MqConstans.QUEUE_GROUP_ORDER_ACT})
	public void actConsumer(String param, Channel channel, Message message) throws IOException {
		log.info("活动消费记录:{}", param);
		try {
			GroupActRecord actRecord = JSON.parseObject(param, GroupActRecord.class);
			if(appActService.isActSend(actRecord.getType(),actRecord.getRefId(),actRecord.getActType())){
				log.info("活动已存在记录:{}",param);
				return;
			}
			sendActPrizeByType(actRecord);
			actRecord.setCreateTime(DateUtils.getTimestampNow());
			suidRich.insert(actRecord);
		} catch (Exception e) {
			log.error("活动消费异常,参数:{}", param, e);
		}
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

	private void sendActPrizeByType(GroupActRecord actRecord) {
		//String type = actRecord.getType();
		//String actType = actRecord.getActType();
		//Integer refId = actRecord.getRefId();
	}


}
