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 ProjectSettleCostRepository extends JpaRepository<ProjectSettleCost, Integer> {

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

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

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

    @Query(value = "select sum(cost_tax_exclude) from project_settle_cost where project_id = ? and type = ?", nativeQuery = true)
    BigDecimal costTaxExcludeSum(int projectId, int type);

    List<ProjectSettleCost> findAllByProjectId(int id);

    List<ProjectSettleCost> findAllByTime(String time);

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

    @Query(value = "select sum(cost_tax_exclude) from project_settle_cost where time = ? and type = ?", nativeQuery = true)
    BigDecimal costTaxExcludeSumByTime(String time, int type);

    @Query(value = "select sum(cost_tax_exclude) from project_settle_cost where type = ?", nativeQuery = true)
    BigDecimal costTaxExcludeSumByType(int type);

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