package com.bizzan.bitrade.dao;

import com.bizzan.bitrade.dao.base.BaseDao;
import com.bizzan.bitrade.entity.ExperienceGiftRiskAlert;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Repository
public interface ExperienceGiftRiskAlertDao extends BaseDao<ExperienceGiftRiskAlert>, ExperienceGiftRiskAlertDaoCustom {

    /** 检查某用户在指定时间之后是否已有状态为0的告警（防重复生成） */
    boolean existsByMemberIdAndCreatedAtAfterAndStatus(Long memberId, Date createdAt, Integer status);

    /** 取消单条告警 */
    @Transactional
    @Modifying
    @Query("UPDATE ExperienceGiftRiskAlert a SET a.status = 1, a.handledAt = :now, a.handledBy = :operatorId " +
           "WHERE a.id = :id AND a.status = 0")
    int cancelById(@Param("id") Long id, @Param("operatorId") Long operatorId, @Param("now") Date now);

    /** 一键取消所有告警 */
    @Transactional
    @Modifying
    @Query("UPDATE ExperienceGiftRiskAlert a SET a.status = 1, a.handledAt = :now, a.handledBy = :operatorId " +
           "WHERE a.status = 0")
    int cancelAll(@Param("operatorId") Long operatorId, @Param("now") Date now);
}
