<?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.yonge.cooleshow.biz.dal.dao.CourseCoursewareDao">
	<resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.CourseCourseware">
            <result column="id_" property="id" />
	        <result column="user_id_" property="userId" />
	        <result column="client_type_" property="clientType" />
	        <result column="music_sheet_id_" property="musicSheetId" />
	        <result column="create_time_" property="createTime" />
	        <result column="update_time_" property="updateTime" />
		</resultMap>  
    
    <!-- 表字段 -->
    <sql id="baseColumns">
         t.id_ as id
        , t.user_id_ as userId
        , t.client_type_ as clientType
         ,t.music_sheet_id_ as musicSheetId
        , t.create_time_ as createTime
        , t.update_time_ as updateTime
        </sql>
    
    <select id="detail" resultType="com.yonge.cooleshow.biz.dal.vo.CourseCoursewareVo">
        SELECT
            <include refid="baseColumns"/>
        FROM course_courseware t
        where t.id_ = #{id}
    </select>
    
    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.CourseCoursewareVo">
		SELECT         
        	<include refid="baseColumns" />
		,ms.cbs_music_sheet_id_ as cbsMusicSheetId
		FROM course_courseware t
        left join music_sheet ms on t.music_sheet_id_ = ms.id_
        <where>
            t.del_flag_ = 0 AND ms.cbs_music_sheet_id_ IS NOT NULL and find_in_set('PLATFORM',ms.provider_type_)
            <if test="param.userId != null">
                and t.user_id_ = #{param.userId}
            </if>
            <if test="param.clientType != null">
                and t.client_type_ = #{param.clientType}
            </if>
            <if test="param.idAndName != null and param.idAndName != ''">
                and  (ms.music_sheet_name_ LIKE CONCAT('%',#{param.idAndName},'%') or ms.cbs_music_sheet_id_ = #{param.idAndName})
            </if>
            <if test="param.musicTagIdList != null and param.musicTagIdList.size() != 0">
                and
                <foreach collection="param.musicTagIdList" open="(" close=")" separator="or" item="item">
                    find_in_set(#{item},ms.music_tag_)
                </foreach>
            </if>
            <if test="param.subjectIdList != null and param.subjectIdList.size() != 0">
                and (ms.music_subject_ is null or ms.music_subject_ = '' OR ms.is_all_subject_ OR
                <foreach collection="param.subjectIdList" separator="or" item="item" open="(" close=")">
                    find_in_set(#{item},ms.music_subject_)
                </foreach>)
            </if>
            <if test="param.status != null and param.status.code == 1">
                 and (ms.payment_type_ like '%FREE%'
                or EXISTS (select * from music_sheet_purchase_record mspr where mspr.music_sheet_id_ = ms.id_ and mspr.student_id_ = t.user_id_ and mspr.order_status_ = 'PAID' and mspr.purchase_type_ = 'MUSIC' and mspr.client_type_ = 'TEACHER')

                or  EXISTS (select mspr.music_sheet_id_ from music_sheet_purchase_record mspr
                left join album_music_relate amr on amr.album_id_ = mspr.music_sheet_id_
                where mspr.student_id_ = t.user_id_ and mspr.order_status_ = 'PAID' and mspr.purchase_type_ = 'ALBUM' and amr.music_sheet_id_ = ms.id_ and mspr.client_type_ = 'TEACHER' )
                or (ms.payment_type_ like '%VIP%' and  EXISTS(select 1 from teacher t1 where t1.user_id_ = t.user_id_ and t1.membership_end_time_ > now()))
                )
            </if>
        </where>
	</select>

	<select id="selectPayMusic" resultType="java.lang.Long">
        select cc.id_ from course_courseware cc

        where
            (EXISTS (select * from music_sheet_purchase_record mspr
                       where mspr.music_sheet_id_ = cc.music_sheet_id_
                         and mspr.student_id_ = cc.user_id_
                         and mspr.order_status_ = 'PAID'
                         and mspr.purchase_type_ = 'MUSIC'
                         and mspr.client_type_ = 'TEACHER')

           or  EXISTS (select mspr.music_sheet_id_ from music_sheet_purchase_record mspr
                       left join album_music_relate amr on amr.album_id_ = mspr.music_sheet_id_
                       where mspr.student_id_ = cc.user_id_
                         and mspr.order_status_ = 'PAID'
                         and mspr.purchase_type_ = 'ALBUM'
                         and amr.music_sheet_id_ = cc.music_sheet_id_
                         and mspr.client_type_ = 'TEACHER'))

        <if test="coursewareIdList != null and coursewareIdList.size() != 0">
            and cc.id_ in
            <foreach collection="coursewareIdList" separator="," item="item" open="(" close=")">
                #{item}
            </foreach>
        </if>
    </select>
</mapper>