
package cn.novalon.gym.manage.groupcourse.dao;

import cn.novalon.gym.manage.groupcourse.entity.GroupCourseEntity;
import org.springframework.data.domain.Sort;
import org.springframework.data.r2dbc.repository.Modifying;
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;

import java.time.LocalDateTime;

@Repository
public interface GroupCourseDao extends R2dbcRepository<GroupCourseEntity, Long> {

    Mono<GroupCourseEntity> findByIdIsAndDeletedAtIsNull(Long id);

    Flux<GroupCourseEntity> findAll();

    Flux<GroupCourseEntity> findAll(Sort sort);

    Flux<GroupCourseEntity> findAllByDeletedAtIsNull();

    Flux<GroupCourseEntity> findAllByDeletedAtIsNull(Sort sort);

    @Modifying
    @Query("UPDATE group_course SET status = '1', updated_at = :updatedAt WHERE id = :id AND deleted_at IS NULL")
    Mono<Integer> cancelCourse(Long id, LocalDateTime updatedAt);

    @Modifying
    @Query("UPDATE group_course SET current_members = current_members + :delta, updated_at = :updatedAt WHERE id = :id AND deleted_at IS NULL")
    Mono<Integer> updateCurrentMembers(Long id, Integer delta, LocalDateTime updatedAt);

    @Modifying
    @Query("UPDATE group_course SET deleted_at = :deletedAt WHERE id = :id")
    Mono<Integer> softDelete(Long id, LocalDateTime deletedAt);

    Flux<GroupCourseEntity> findByCourseTypeAndDeletedAtIsNull(Long courseType);
}
