package com.izouma.nineth.repo;

import com.izouma.nineth.domain.DestroyRecord;
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.time.LocalDateTime;
import java.util.List;
import java.util.Map;

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

    @Query(nativeQuery = true, value = "SELECT user.avatar avatar,user.nickname nickname,sum( destroy_record.record ) num FROM destroy_record LEFT JOIN user ON destroy_record.user_id = user.id WHERE destroy_record.asset_id IN ( SELECT asset.id FROM asset LEFT JOIN blind_box_item ON asset.collection_id = blind_box_item.collection_id where blind_box_item.blind_box_id = ?1 )" +
            "and destroy_record.name like ?2 and destroy_record.name not like ?3 and destroy_record.created_at > ?4 GROUP BY destroy_record.user_id ORDER BY sum( destroy_record.record ) DESC")
    List<Map<String, String>> destroyRecordRank(Long blindBoxId, String rare, String not, LocalDateTime countDateTime);

    List<DestroyRecord> findByCreatedAtBetween(LocalDateTime start, LocalDateTime end);
}