package common.repository;

import common.model.TkioFlowHistory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

public interface TkioFlowHistoryRepository extends JpaRepository<TkioFlowHistory, Long> {

    @Query(value = "select contract_code from tkio_flow_history where email = ?1 order by ds limit 1", nativeQuery = true)
    String findLastRecodeContract(String email);

    @Query(value = "select sum(flow) from tkio_flow_history where email = ?1 and contract_code = ?2", nativeQuery = true)
    BigDecimal sumAllFowByEmailAndCode(String email, String contractCode);

    @Transactional
    @Modifying
    @Query(value = "delete from tkio_flow_history where email = ?1", nativeQuery = true)
    void deleteByEmail(String email);
}
