package com.zhentao.mapper;

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

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 红娘课程Mapper接口
 */
@Mapper
public interface MatchmakerCourseMapper extends BaseMapper<MatchmakerCourse> {
    
    /**
     * 插入课程兑换记录
     */
    @Insert("INSERT INTO matchmaker_course_exchange (maker_id, course_id, points_used, exchange_time, create_time) " +
            "VALUES (#{makerId}, #{courseId}, #{points}, #{exchangeTime}, NOW())")
    void insertCourseExchange(@Param("makerId") Integer makerId, 
                              @Param("courseId") Integer courseId, 
                              @Param("points") Integer points,
                              @Param("exchangeTime") Date exchangeTime);
    
    /**
     * 查询红娘已兑换的课程列表
     */
    @Select("SELECT ce.course_id, ce.points_used, ce.exchange_time, c.name as course_name, " +
            "c.cover_image, c.instructor, c.duration, c.rating, c.description " +
            "FROM matchmaker_course_exchange ce " +
            "LEFT JOIN matchmaker_courses c ON ce.course_id = c.id " +
            "WHERE ce.maker_id = #{makerId} " +
            "ORDER BY ce.exchange_time DESC")
    List<Map<String, Object>> selectPurchasedCourses(@Param("makerId") Integer makerId);
    
    /**
     * 检查红娘是否已兑换某课程
     */
    @Select("SELECT COUNT(*) FROM matchmaker_course_exchange WHERE maker_id = #{makerId} AND course_id = #{courseId}")
    int checkExchanged(@Param("makerId") Integer makerId, @Param("courseId") Integer courseId);
}
