package cn.novalon.gym.manage.member.repository;

import cn.novalon.gym.manage.member.entity.RefundApplication;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.r2dbc.repository.R2dbcRepository;
import org.springframework.stereotype.Repository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

/**
 * 退款申请 Repository
 * 
 * @author 付嘉
 * @date 2026-05-27
 */
@Repository
public interface RefundApplicationRepository extends R2dbcRepository<RefundApplication, Long> {

    /**
     * 根据会员卡记录ID查询退款申请
     */
    @Query("SELECT * FROM refund_application WHERE record_id = :recordId AND deleted_at IS NULL LIMIT 1")
    Mono<RefundApplication> findByRecordId(Long recordId);

    /**
     * 根据会员ID查询退款申请列表
     */
    @Query("SELECT * FROM refund_application WHERE member_id = :memberId AND deleted_at IS NULL ORDER BY created_at DESC")
    Flux<RefundApplication> findByMemberId(Long memberId);

    /**
     * 根据状态查询退款申请列表
     */
    @Query("SELECT * FROM refund_application WHERE status = :status AND deleted_at IS NULL ORDER BY created_at DESC")
    Flux<RefundApplication> findByStatus(String status);

    /**
     * 审核退款申请
     */
    @Query("UPDATE refund_application SET status = :status, auditor_id = :auditorId, audit_time = NOW(), audit_remark = :auditRemark, updated_at = NOW() WHERE id = :id")
    Mono<Integer> approve(Long id, String status, Long auditorId, String auditRemark);

    /**
     * 逻辑删除退款申请
     */
    @Query("UPDATE refund_application SET deleted_at = NOW() WHERE id = :id")
    Mono<Integer> logicalDelete(Long id);
}
