package cn.palmte.work.model;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author Yuanping Zhang
 * @date 2021/11/11
 */
public interface ProjectSettleCashFlowRepository extends JpaRepository<ProjectSettleCashFlow, Integer> {

    List<ProjectSettleCashFlow> findAllByProjectIdAndTime(int id, String time);

    @Query(value = "select * from project_settle_cash_flow where project_id = ?1 and time < ?2", nativeQuery = true)
    List<ProjectSettleCashFlow> findAllByProjectIdBefore(int id, String time);

    @Query(value = "select * from project_settle_cash_flow where project_id = ?1 and time <= ?2", nativeQuery = true)
    List<ProjectSettleCashFlow> findAllByProjectIdAndTimeBeforeAndEquals(int id, String time);

    List<ProjectSettleCashFlow> findAllByProjectId(int id);

    List<ProjectSettleCashFlow> findAllByTime(String time);

    @Query(value = "select sum(amount) from project_settle_cash_flow where project_id = ? and type = ?", nativeQuery = true)
    BigDecimal costAmountSum(int projectId, int type);

    @Query(value = "select * from project_settle_cash_flow where project_id in ?1 and time = ?2", nativeQuery = true)
    List<ProjectSettleCashFlow> findAllByProjectsAndTime(List<Integer> ids, String time);

    @Query(value = "SELECT time FROM project_settle_cash_flow GROUP BY time ORDER BY time", nativeQuery = true)
    List<String> getProjectTime();

    @Query(value = "select * from project_settle_cash_flow where project_id in ?1", nativeQuery = true)
    List<ProjectSettleCashFlow> findAllByProjectIds(List<Integer> projectIds);
}
