<?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.CourseDetailsDao">

    <!--查找指定短剧的目录  按照顺序数字升序-->
    <select id="findByCourseId" resultType="com.sqx.modules.course.entity.CourseDetails">
        select c.*,
               (select count(*) from course_collect cc where cc.user_id=#{userId} and cc.course_details_id=c.course_details_id and cc.classify=2) as isGood
        from course_details c where c.course_id=#{id}  order by c.sort asc
    </select>

    <select id="selectCoursePageByCourseId" resultType="com.sqx.modules.course.entity.CourseDetails">
        select * from course_details where course_id=#{id}
        <if test="good!=null and good==1">
            and good=#{good}
        </if>
        <if test="good!=null and good==2">
            and (good=#{good} or good is null)
        </if>
        order by sort asc
    </select>

    <select id="findByCourseIdNotUrl" resultType="com.sqx.modules.course.entity.CourseDetails">
        select c.course_details_id as courseDetailsId,c.course_id as courseId,c.wx_course_details_id as wxCourseDetailsId,
               c.course_details_name as courseDetailsName,c.create_time as createTime,
               c.dy_episode_id as dyEpisodeId,c.advertising, c.view_count as viewCount, c.play_complete_count as playCompleteCount,
               (select count(*) from course_collect cc where cc.user_id=#{userId} and cc.course_details_id=c.course_details_id and cc.classify=2) as isGood,
               c.title_img as titleImg,c.content,c.good_num as goodNum,c.price,if(is_price!=1,c.video_url,'') as videoUrl
        from course_details c
        where c.course_id=#{id}  order by c.sort asc
    </select>


    <delete id="deleteCourseDetails" parameterType="String">
        DELETE FROM course_details WHERE course_details_id IN
        <foreach collection="array" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </delete>


    <select id="selectCourseDetailsList" resultType="com.sqx.modules.course.entity.CourseDetails">
        select cd.*,
               MD5( `course_details_id` ) AS `uid`
        from course_details cd
        left join course c on c.course_id=cd.course_id
        where c.course_id is not null and c.is_delete=0 and cd.good=1
        <if test='null != wxShow and wxShow==1'>
            and c.wx_show = #{wxShow}
        </if>
        <if test='null != wxShow and wxShow==2'>
            and (c.wx_show = #{wxShow} or c.wx_show is null)
        </if>
        <if test='null != dyShow and dyShow==1'>
            and c.dy_show = #{dyShow}
        </if>
        <if test='null != dyShow and dyShow==2'>
            and (c.dy_show = #{dyShow} or c.dy_show is null)
        </if>
        order by SUBSTR(uid, #{randomNum}, 6)
    </select>

</mapper>
