package com.izouma.nineth.repo;

import com.izouma.nineth.domain.AirDrop;
import com.izouma.nineth.enums.AirDropType;
import org.springframework.cache.annotation.CachePut;
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.annotation.Nonnull;
import javax.transaction.Transactional;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

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


    @Query(nativeQuery = true, value = "select * from air_drop where air_drop.user_ids like ?1 and name like ?2 ")
    List<AirDrop> findAllByUserIdsAndName(String userIds, String name);

    /**
     * 统计出售的空投的数量
     *
     * @param mintId
     * @return
     */
    @Query(nativeQuery = true, value = "select sum(drop_count) from air_drop ad join collection_info ci " +
            "   on ad.collection_id = ci.id where ci.minter_id = ?1 ")
    Integer countDropCount(Long mintId);

    @Nonnull
    @CachePut(value = "airDrop", key = "#airDrop.id")
    AirDrop save(@Nonnull AirDrop airDrop);



}