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

import com.red.circle.framework.mybatis.constant.PageConstant;
import com.red.circle.framework.mybatis.service.impl.BaseServiceImpl;
import com.red.circle.game.fruit.infrastructure.database.dao.GameFruitLotteryRecordDAO;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitLotteryRecord;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitLotteryRecordService;
import com.red.circle.tool.core.collection.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 摩天轮开奖记录 服务实现类.
 * </p>
 *
 * @author lisizhe on 2023-11-30 19:18
 */
@Service
public class GameFruitLotteryRecordServiceImpl extends
    BaseServiceImpl<GameFruitLotteryRecordDAO, GameFruitLotteryRecord> implements
    GameFruitLotteryRecordService {


  @Override
  public List<GameFruitLotteryRecord> getRoundLotteryPrize(Long gameId) {
    return query().eq(GameFruitLotteryRecord::getGameId, gameId).last(PageConstant.formatLimit(8))
        .list();
  }

  @Override
  public void lotteryDraw(List<GameFruitLotteryRecord> gameFruitLotteryRecords) {
    saveBatch(gameFruitLotteryRecords);
  }

  @Override
  public Map<Long, List<GameFruitLotteryRecord>> listLotteryHistoryByGameIds(Set<Long> gameIds) {
    if (CollectionUtils.isEmpty(gameIds)) {
      return CollectionUtils.newHashMap();
    }
    return Optional.ofNullable(query().in(GameFruitLotteryRecord::getGameId, gameIds)
            .orderByDesc(GameFruitLotteryRecord::getId).list())
        .map(
            list -> list.stream().collect(Collectors.groupingBy(GameFruitLotteryRecord::getGameId)))
        .orElse(new HashMap<>());
  }
}
