package com.izouma.nineth.repo;

import com.izouma.nineth.domain.Invite;
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.util.Optional;

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

    Optional<Invite> findFirstByCode(String code);

    @Modifying
    @Transactional
    @Query("update Invite i set i.inviteNum = i.inviteNum + 1 where i.id = ?1")
    void increaseNum(Long id);

    @Modifying
    @Query(value = "update user set invite_num =0,invite_air_drop =0 WHERE invite_num>0 or invite_air_drop>0" , nativeQuery = true)
    void cleanInvite();
}