package com.lanyuanxiaoyao.leopard.core.repository;

import com.lanyuanxiaoyao.leopard.core.entity.Daily;
import com.lanyuanxiaoyao.service.template.repository.SimpleRepository;
import com.querydsl.core.types.Predicate;
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

@Repository
public interface DailyRepository extends SimpleRepository<Daily> {
  @Query("select distinct daily.tradeDate from Daily daily")
  Set<LocalDate> findDistinctTradeDate();

  @Query("select max(daily.tradeDate) from Daily daily")
  LocalDate findMaxTradeDate();

  @Query("select min(daily.tradeDate) from Daily daily")
  LocalDate findMinTradeDate();

  @Query("from Daily daily where daily.stock.id = ?1 order by daily.tradeDate desc limit 1")
  Optional<Daily> findLatest(Long stockId);

  @EntityGraph(attributePaths = {"stock"})
  @Override
  Optional<Daily> findOne(Predicate predicate);

  @EntityGraph(attributePaths = {"stock"})
  @Override
  List<Daily> findAll(Predicate predicate, Sort sort);
}
