package com.izouma.nineth.repo;

import com.izouma.nineth.domain.RockRecord;
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.Collection;

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

    RockRecord findFirstByUserIdOrderByCreatedAtDesc(Long userId);

    @Query(nativeQuery = true, value = "select ifnull(record, 0) from rock_record where user_id = ?1 order by id desc limit 1")
    BigDecimal findRecordByUserIdOrderByCreatedAtDesc(Long userId);

    @Query(nativeQuery = true, value = "select ifnull(sum(record), 0) from rock_record where user_id in ?1 order by created_at desc limit 1")
    BigDecimal findRecordByUserIdOrderByIdInDesc(Collection<Long> userId);
}