package com.zhentao.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zhentao.entity.Course;
import com.zhentao.entity.CourseOrder;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 课程订单Mapper
 */
@Mapper
public interface CourseOrderMapper extends BaseMapper<CourseOrder> {
    
    /**
     * 根据订单号查询订单
     */
    @Select("SELECT * FROM course_orders WHERE order_no = #{orderNo}")
    CourseOrder selectByOrderNo(@Param("orderNo") String orderNo);
    
    /**
     * 查询用户已购买的课程ID列表
     */
    @Select("SELECT cs.* FROM course_orders c left join courses cs on c.course_id = cs.id WHERE c.user_id = #{userId} AND c.status = 1")
    List<Course> selectPurchasedCourseIds(@Param("userId") Long userId);
    
    /**
     * 检查用户是否已购买某课程
     */
    @Select("SELECT COUNT(*) FROM course_orders WHERE user_id = #{userId} AND course_id = #{courseId} AND status = 1")
    int checkPurchased(@Param("userId") Long userId, @Param("courseId") Integer courseId);
}
