package com.izouma.jiashanxia.repo;

import com.izouma.jiashanxia.domain.CommissionRecord;
import com.izouma.jiashanxia.enums.TransactionType;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.util.List;

public interface CommissionRecordRepo extends JpaRepository<CommissionRecord, Long>, JpaSpecificationExecutor<CommissionRecord> {
    @Query("update CommissionRecord t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDelete(Long id);

    List<CommissionRecord> findAllByUserId(Long userId);

    @Query("select sum(c.amount) from CommissionRecord c where c.userId = ?1 and c.transactionType = ?2")
    BigDecimal sumByUserId(Long userId, TransactionType type);
}