<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.sqx.modules.course.dao.CourseCollectDao">



    <select id="selectCourseByCollect" resultType="com.sqx.modules.course.entity.Course">
        SELECT MAX(u.update_time)         AS produceEndTime,
               c.*,
               d.course_details_name      AS courseDetailsName,
               d.course_details_id        AS courseDetailsId,
               COUNT(d.course_details_id) AS courseDetailsCount
        FROM course_collect u
                 LEFT JOIN
             course c ON u.course_id = c.course_id
                 LEFT JOIN
             course_details d ON u.course_details_id = d.course_details_id
                 LEFT JOIN
             course_collect cc ON u.course_id = cc.course_id AND cc.user_id = #{userId} AND cc.classify = 3
        WHERE u.user_id = #{userId}
          AND u.classify = #{classify}
          AND c.course_id IS NOT NULL
        GROUP BY u.course_id, c.course_id, d.course_details_name, d.course_details_id
        ORDER BY produceEndTime DESC
    </select>
    <select id="selectSummary" resultType="java.util.Map">
        SELECT
            COUNT(CASE WHEN classify = 1 THEN 1 END) AS collectCount,
            COUNT(CASE WHEN classify = 2 THEN 1 END) AS likeCount
        FROM course_collect
        WHERE user_id = #{userId};
    </select>
    <select id="selectClassify1In3" resultType="com.sqx.modules.course.entity.Course">
        SELECT
            c1.course_id as courseId,
            c1.course_details_id as courseDetailsId
        FROM
            `course_collect` c1
                INNER JOIN course_collect c2 ON c1.course_id = c2.course_id AND c2.user_id = #{userId} AND c2.classify = 1
        WHERE
            c1.classify = 3
          AND c1.user_id = #{userId}
        order by c1.update_time desc
    </select>

    <select id="selectClassify1In2" resultType="com.sqx.modules.course.entity.Course">
        SELECT
            c1.course_id as courseId,
            c1.course_details_id as courseDetailsId
        FROM
            `course_collect` c1
        WHERE
            c1.classify = 2
          AND c1.user_id = #{userId}
        order by c1.update_time desc
    </select>

    <select id="selectClassify1ByUserId" resultType="com.sqx.modules.course.entity.Course">
        SELECT c1.course_id         as courseId,
               c1.course_details_id as courseDetailsId
        FROM `course_collect` c1
        WHERE c1.classify = 3
          AND c1.user_id = #{userId}
        order by c1.update_time desc
    </select>
</mapper>
