package com.izouma.nineth.repo.rewardpoints;

import com.izouma.nineth.domain.PointRewardActivity;
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 PointRewardActivityRepo extends JpaRepository<PointRewardActivity, Long>, JpaSpecificationExecutor<PointRewardActivity> {

    /**
     * 获取最近的活动
     * @return
     */
    @Query(value = "select id from point_reward_activity where now() >= " +
            "start_time and now() <= end_time and del = FALSE " +
            "order by created_at desc limit 1", nativeQuery =true)
    Optional<Long> recentActivity();

    @Query("update PointRewardActivity t set t.del = true where t.id = ?1")
    @Modifying
    @Transactional
    void softDel(Long id);
}