package com.red.circle.other.app.command.game.luckygift;

import com.red.circle.common.business.dto.cmd.AppExtCommand;
import com.red.circle.other.infra.database.cache.service.other.GameLuckyGiftCacheService;
import com.red.circle.other.infra.database.cache.service.other.GiftCacheService;
import com.red.circle.other.inner.enums.material.GiftTabEnum;
import com.red.circle.other.inner.model.dto.material.GiftConfigDTO;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.List;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

/**
 * <p>
 * 幸运礼物 删除用户礼物赠送次数.
 * </p>
 *
 * @author pengshigang
 * @since 2022-5-27
 */
@Component
@RequiredArgsConstructor
public class LuckyGiftUserIncrDelCmd {

  private final GiftCacheService giftCacheService;
  private final GameLuckyGiftCacheService gameLuckyGiftCacheService;

  public void execute(AppExtCommand cmd) {

    List<GiftConfigDTO> configCaches = giftCacheService.listBySysOrigin(
        cmd.requireReqSysOriginEnum());
    if (CollectionUtils.isEmpty(configCaches)) {
      return;
    }

    configCaches = configCaches.stream().filter(cache -> cache.getGiftTab().
        contains(GiftTabEnum.LUCKY_GIFT.name())).collect(Collectors.toList());
    if (CollectionUtils.isEmpty(configCaches)) {
      return;
    }

    configCaches.forEach(gift -> {
      gameLuckyGiftCacheService.removeGiftsCount(cmd.requiredReqUserId(), gift.getId(), 1);
      gameLuckyGiftCacheService.removeGiftsCount(cmd.requiredReqUserId(), gift.getId(), 7);
      gameLuckyGiftCacheService.removeGiftsCount(cmd.requiredReqUserId(), gift.getId(), 77);
      gameLuckyGiftCacheService.removeGiftsCount(cmd.requiredReqUserId(), gift.getId(), 177);
      gameLuckyGiftCacheService.removeGiftsCount(cmd.requiredReqUserId(), gift.getId(), 777);
    });
  }


}
