package com.tzy.listener.mq;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.rabbitmq.client.Channel;
import com.tzy.app.dto.UserGiftCardDTO;
import com.tzy.common.constant.Constants;
import com.tzy.common.constant.MqConstans;
import com.tzy.common.utils.DateUtils;
import com.tzy.framework.util.RedisUtils;
import com.tzy.sportcard.api.service.MineApiService;
import com.tzy.sportcard.point.domain.AppUserPointRecord;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 java.io.IOException;
import java.util.Date;
import java.util.List;


@Slf4j
@Component
public class InitUserGiftCardConsumer {
	@Autowired
	private MineApiService mineApiService;
	@Autowired
	private RedisUtils redisUtils;

	private final static String START_TIME_DEFAULT="2023-11-01 00:00:00";
	private final static String END_TIME_DEFAULT="2023-11-29 00:00:00";

	@RabbitHandler
	@RabbitListener(queues = {MqConstans.QUEUE_INIT_USER_GIFT_CARD})
	public void initUserGiftCardConsumer(String param, Channel channel, Message message) throws IOException {
		log.info("用户精美卡片初始化:{}", param);
		try {
			JSONObject paramJson = JSON.parseObject(param);
			String userId = paramJson.getString("userId");
			String startTime = paramJson.getString("startTime");
			String endTime = paramJson.getString("endTime");
			initUserGiftCard(Integer.valueOf(userId),startTime,endTime);
		} catch (Exception e) {
			log.error("用户精美卡片初始化异常,用户id:{}", param, e);
		}
		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
	}

	private void initUserGiftCard(Integer userId,String startTime,String endTime) {
		if (userId == null) {
			return;
		}
		startTime = StringUtils.isEmpty(startTime) ? START_TIME_DEFAULT : startTime;
		endTime = StringUtils.isEmpty(endTime) ? END_TIME_DEFAULT : endTime;
		List<UserGiftCardDTO> userWaitCards = mineApiService.getUserInitWaitGiftCards(userId, DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, startTime),
				DateUtils.dateTime(DateUtils.YYYY_MM_DD_HH_MM_SS, endTime));
		userWaitCards.forEach(c -> {
			AppUserPointRecord pointRecord = new AppUserPointRecord();
			pointRecord.setType(Constants.POINT_TYPE_GIFT_CARD_PRE + c.getCardId());
			pointRecord.setChangePoint(c.getWaitNum().longValue());
			pointRecord.setCurrPoint(c.getCardId().longValue());
			pointRecord.setUserId(userId.longValue());
			pointRecord.setOrderId(c.getInfoId().longValue());
			pointRecord.setMerchantId(c.getMerchantId().longValue());
			pointRecord.setOrderNo(c.getInfoId().toString());
			pointRecord.setCreateTime(new Date());
			//增加积分明细记录
			mineApiService.addUserPoint(pointRecord);
		});
		redisUtils.hset("init_user_gift_card", userId.toString(), 1);
	}
}
