package com.red.circle.other.app.command.gift;

import com.red.circle.framework.core.asserts.ResponseAssert;
import com.red.circle.mq.rocket.business.producer.GiftMqMessage;
import com.red.circle.other.app.convertor.material.GiftAppConvertor;
import com.red.circle.other.app.dto.cmd.gift.GiveAwayGiftBackpackCmd;
import com.red.circle.other.infra.database.cache.service.other.GiftCacheService;
import com.red.circle.other.infra.database.rds.service.gift.GiftBackpackService;
import com.red.circle.other.inner.asserts.GiftErrorCode;
import com.red.circle.other.inner.model.dto.material.GiftConfigDTO;
import com.red.circle.tool.core.sequence.IdWorkerUtils;
import com.red.circle.wallet.inner.error.WalletErrorCode;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 赠送礼物背包中的道具.
 *
 * @author pengliang on 2021/3/11
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class GiveAwayGiftBackpackCmdExe {

  private final GiftMqMessage giftMqMessage;
  private final GiftAppConvertor giftAppConvertor;
  private final GiftCacheService giftCacheService;
  private final GiftBackpackService giftBackpackService;

  public void execute(GiveAwayGiftBackpackCmd cmd) {
    ResponseAssert.isTrue(
        WalletErrorCode.INSUFFICIENT_BALANCE,
        giftBackpackService.consumerGifts(cmd.getId(), cmd.getGiftId(), cmd.requiredReqUserId(),
            cmd.getQuantity()));
    GiftConfigDTO giftConfigInfo = giftCacheService.getById(cmd.getGiftId());
    // 未找到礼物
    ResponseAssert.notNull(GiftErrorCode.GIFT_NOT_FOUND, giftConfigInfo);
    giftMqMessage.sendGift(IdWorkerUtils.getIdStr(),
        giftAppConvertor.toBackpackGift(cmd, giftConfigInfo, cmd.getId()));
  }

}
