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.GameFruitPrizeDAO;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitPrize;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitPrizeService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 摩天轮游戏奖品 服务实现类.
 * </p>
 *
 * @author lisizhe on 2023-11-30 19:18
 */
@Service
public class GameFruitPrizeServiceImpl extends
    BaseServiceImpl<GameFruitPrizeDAO, GameFruitPrize> implements GameFruitPrizeService {

  @Override
  public List<GameFruitPrize> listPrize(String sysOrigin) {
    return query()
        .eq(Objects.nonNull(sysOrigin), GameFruitPrize::getSysOrigin, sysOrigin)
        .orderByAsc(GameFruitPrize::getSort)
        .last(PageConstant.formatLimit(8)).list();
  }

  @Override
  public Map<Long, GameFruitPrize> mapByType(Integer type, String sysOrigin) {

    return Optional.ofNullable(
            query().eq(GameFruitPrize::getType, type).orderByAsc(GameFruitPrize::getSort)
                .eq(GameFruitPrize::getSysOrigin, sysOrigin).last(PageConstant.formatLimit(4)).list())
        .map(prizes -> prizes.stream()
            .collect(Collectors.toMap(GameFruitPrize::getId, v -> v))).orElse(new HashMap<>());
  }

  @Override
  public Map<Long, GameFruitPrize> mapPrize(String sysOrigin) {
    return Optional.ofNullable(
            query().eq(GameFruitPrize::getSysOrigin, sysOrigin).orderByAsc(GameFruitPrize::getSort)
                .last(PageConstant.formatLimit(8)).list())
        .map(prizes -> prizes.stream()
            .collect(Collectors.toMap(GameFruitPrize::getId, v -> v))).orElse(new HashMap<>());
  }

  @Override
  public void add(GameFruitPrize gameFruitPrize) {
    save(gameFruitPrize);
  }

  @Override
  public void update(GameFruitPrize gameFruitPrize) {
    updateSelectiveById(gameFruitPrize);
  }
}
