package com.red.circle.game.fruit.infrastructure.database.repository.impl;

import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.game.fruit.infrastructure.database.dao.GameFruitPrizeBetDAO;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitPrizeBet;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitPrizeBetService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 奖品每轮下注记录 服务实现类.
 * </p>
 *
 * @author lisizhe on 2023-11-30 19:12
 */
@Service
public class GameFruitPrizeBetServiceImpl extends
    BaseServiceImpl<GameFruitPrizeBetDAO, GameFruitPrizeBet> implements GameFruitPrizeBetService {

  @Override
  public void updateOrAddBetAmount(GameFruitPrizeBet gameFruitPrizeBet) {
    GameFruitPrizeBet gameFruitUserBet = baseDAO.getByGamePrizeIdForUpdate(
        gameFruitPrizeBet.getGameId(), gameFruitPrizeBet.getPrizeId());
    if (Objects.nonNull(gameFruitUserBet)) {
      BigDecimal betAmount = gameFruitPrizeBet.getBetAmount().add(gameFruitUserBet.getBetAmount());
      update().set(GameFruitPrizeBet::getBetAmount,
              betAmount)
          .eq(GameFruitPrizeBet::getGameId, gameFruitPrizeBet.getGameId())
          .eq(GameFruitPrizeBet::getPrizeId, gameFruitPrizeBet.getPrizeId()).execute();
      return;
    }
    save(gameFruitPrizeBet);
  }

  @Override
  public void addBetAmount(List<GameFruitPrizeBet> gameFruitPrizeBets) {
    saveBatch(gameFruitPrizeBets);
  }

  @Override
  public List<GameFruitPrizeBet> listBetAmount(Long gameId, Set<Long> prizeIds) {
    if (CollectionUtils.isEmpty(prizeIds)) {
      return CollectionUtils.newArrayList();
    }
    return query()
        .eq(GameFruitPrizeBet::getGameId, gameId)
        .in(GameFruitPrizeBet::getPrizeId, prizeIds)
        .list();
  }
}
