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.application.dto.enums.ClaimStatusEnum;
import com.red.circle.game.fruit.infrastructure.database.dao.GameFruitAwardRecordDAO;
import com.red.circle.game.fruit.infrastructure.database.entity.GameFruitAwardRecord;
import com.red.circle.game.fruit.infrastructure.database.repository.GameFruitAwardRecordService;
import com.red.circle.tool.core.date.DateUtils;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 游戏每轮中奖名单 服务实现类.
 * </p>
 *
 * @author lisizhe on 2023-11-30 19:18
 */
@Service
public class GameFruitAwardRecordServiceImpl extends
    BaseServiceImpl<GameFruitAwardRecordDAO, GameFruitAwardRecord> implements
    GameFruitAwardRecordService {

  @Override
  public void sendWaitingPrize(List<GameFruitAwardRecord> gameFruitAwardRecords) {
    saveBatch(gameFruitAwardRecords);
  }

  @Override
  public List<GameFruitAwardRecord> listWaitingUser(Long gameId, String claimStatus) {
    return query().eq(GameFruitAwardRecord::getGameId, gameId)
        .eq(GameFruitAwardRecord::getClaimStatus, claimStatus).list();
  }

  @Override
  public void updateClaimStatus(Long gameId) {
    update().set(GameFruitAwardRecord::getClaimStatus, ClaimStatusEnum.RECEIVED.name())
        .eq(GameFruitAwardRecord::getGameId, gameId)
        .eq(GameFruitAwardRecord::getClaimStatus, ClaimStatusEnum.WAITING.name())
        .execute();
  }

  @Override
  public long countWeekWinRounds(Long userId) {
    Date startDate = DateUtils.setDateTime(DateUtils.nowMonday(), 0, 0, 0);
    Date endDate = DateUtils.setDateTime(DateUtils.nowSunday(), 23, 59, 59);
    return Optional.of(query().eq(GameFruitAwardRecord::getUserId, userId)
        .ge(GameFruitAwardRecord::getCreateTime,
            startDate.getTime())
        .lt(GameFruitAwardRecord::getCreateTime,
            endDate.getTime())
        .count()).orElse(0L);
  }

}
