<?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="courseSets" resultType="com.sqx.modules.course.vo.CourseDetailsSetVo">
        select c.course_id                       as courseId
             , c.course_details_id               as courseDetailsId
             , c.course_details_name             as courseDetailsName
             , c.video_url                       as videoUrl
             , c.price                           as price
             , c.sort                            as sort
             , ifnull(#{wholesalePrice}, 0)      as wholesalePrice
             , IF(#{isPrice} = 1, c.is_price, 2) AS isPrice
             , c.title_img                       as titleImg
             , c.good_num                        as goodNum
             , c.sort                            as sort
        from course_details c
        where c.course_id = #{courseId}
        order by c.sort asc
    </select>

    <!--查找指定短剧的目录  按照顺序数字升序-->
    <select id="findByCourseId" resultType="com.sqx.modules.course.entity.CourseDetails">
        select c.*,
               ifnull(cc.isGood, 0) as isGood
        from course_details c
                 left join (select course_details_id, count(*) isGood
                            from course_collect
                            where user_id = #{userId}
                              and classify = 2
                            group by course_details_id) cc on c.course_details_id = c.course_details_id
        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.title_img                        as titleImg,
               c.content,
               c.good_num                         as goodNum,
               c.price,
               if(c.is_price!=1, c.video_url, '') as videoUrl,
               c.dy_episode_id                    as dyEpisodeId,
               c.advertising,
               c.view_count                       as viewCount,
               c.play_complete_count              as playCompleteCount,
               ifnull(cc.isGood, 0)               as isGood
        from course_details c
                 left join (select course_details_id, count(*) as isGood
                            from course_collect
                            where user_id = #{userId}
                              and classify = 2
                            group by course_details_id) cc on cc.course_details_id = c.course_details_id
        where c.course_id = #{id}
        order by c.sort asc
    </select>
    <update id="updateViewCount">
        update course_details
        set
        <if test="state==1">
            view_count = view_count + 1
        </if>
        <if test="state==2">
            play_complete_count = play_complete_count + 1
        </if>
        where
        course_id=#{courseId}
        and course_details_id =#{courseDetailsId}
    </update>

    <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="countCourseByCourseId" resultType="Integer">
        select count(1)
        from course_details
        where course_id = #{courseId}
    </select>


    <select id="countCourse" resultType="Map">
        select ifnull(sum(view_count), 0) as viewCounts,
               ifnull(sum(good_num), 0)   as goodNum,
               ifnull(count(*), 0)        as courseDetailsCount
        from course_details
        where course_id = #{courseId}
    </select>
    <select id="countByCourseId" resultType="com.sqx.modules.course.entity.CourseDetails">
        SELECT course_id as courseId,
               COUNT(*)  AS courseDetailsCount
        FROM course_details
        where course_id in
        <foreach collection="courseIdList" item="ids" open="(" separator="," close=")">
            #{ids}
        </foreach>
    </select>

</mapper>
