<?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.MusicSheetDao">
    <resultMap id="BaseResultMap" type="com.yonge.cooleshow.biz.dal.entity.MusicSheet">
        <id column="id_" jdbcType="BIGINT" property="id"/>
        <result column="music_sheet_name_" jdbcType="VARCHAR" property="musicSheetName"/>
        <result column="user_id_" jdbcType="BIGINT" property="userId"/>
        <result column="composer_" jdbcType="VARCHAR" property="composer"/>
        <result column="music_subject_" jdbcType="VARCHAR" property="musicSubject"/>
        <result column="audio_type_" jdbcType="VARCHAR" property="audioType"/>
        <result column="music_tag_" jdbcType="VARCHAR" property="musicTag"/>
        <result column="play_speed_" jdbcType="INTEGER" property="playSpeed"/>
        <result column="favorite_count_" jdbcType="INTEGER" property="favoriteCount"/>
        <result column="virtual_number_" property="virtualNumber" />
        <result column="first_pass_audit_time_" jdbcType="TIMESTAMP" property="firstPassAuditTime"/>
        <result column="can_evaluate_" jdbcType="TINYINT" property="canEvaluate"/>
        <result column="show_fingering_" jdbcType="TINYINT" property="showFingering"/>
        <result column="charge_type_" jdbcType="TINYINT" property="chargeType"/>
        <result column="audit_status_" jdbcType="TINYINT" property="auditStatus"/>
        <result column="sort_number_" jdbcType="INTEGER" property="sortNumber"/>
        <result column="top_flag_" jdbcType="TINYINT" property="topFlag"/>
        <result column="hot_flag_" jdbcType="TINYINT" property="hotFlag"/>
        <result column="exquisite_flag_" jdbcType="TINYINT" property="exquisiteFlag"/>
        <result column="music_price_" jdbcType="DECIMAL" property="musicPrice"/>
        <result column="audio_file_url_" jdbcType="VARCHAR" property="audioFileUrl"/>
        <result column="xml_file_url_" jdbcType="VARCHAR" property="xmlFileUrl"/>
        <result column="has_beat_" jdbcType="TINYINT" property="hasBeat"/>
        <result column="create_time_" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="create_by_" jdbcType="BIGINT" property="createBy"/>
        <result column="update_time_" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="update_by_" jdbcType="BIGINT" property="updateBy"/>
        <result column="remark_" jdbcType="VARCHAR" property="remark"/>
        <result column="notation_" jdbcType="VARCHAR" property="notation"/>
        <result column="accompaniment_type_" jdbcType="VARCHAR" property="accompanimentType"/>
        <result column="title_img_" jdbcType="VARCHAR" property="titleImg"/>
        <result column="music_img_" jdbcType="VARCHAR" property="musicImg"/>
        <result column="first_tone_" jdbcType="VARCHAR" property="firstTone"/>
        <result column="fixed_tone_" jdbcType="VARCHAR" property="fixedTone"/>
        <result column="music_sheet_type_" jdbcType="VARCHAR" property="musicSheetType"/>
        <result column="provider_type_" jdbcType="VARCHAR" property="providerType"/>
    </resultMap>

    <sql id="Base_Column_List">
        t.id_ as id ,
        t.music_sheet_name_ as musicSheetName,
        t.user_id_ as userId,
        t.composer_ as composer,
        t.music_subject_ as musicSubject,
        t.audio_type_ as audioType,
        t.music_tag_ as musicTag,
        t.play_speed_ as playSpeed,
        t.favorite_count_ as favoriteCount,
        t.virtual_number_ as virtualNumber,
        t.first_pass_audit_time_ as firstPassAuditTime,
        t.can_evaluate_ as canEvaluate,
        t.show_fingering_ as showFingering,
        t.charge_type_ as chargeType,
        t.payment_type_ as paymentType,
        t.provider_type_ as providerType,
        t.state_ as state,
        t.audit_status_ as auditStatus,
        t.sort_number_ as sortNumber,
        t.top_flag_ as topFlag,
        t.exquisite_flag_ as exquisiteFlag,
        t.hot_flag_ as hotFlag,
        t.music_price_ as musicPrice,
        t.audio_file_url_ as audioFileUrl,
        t.xml_file_url_ as xmlFileUrl,
        t.has_beat_ as hasBeat,
        t.mp3_type_ as mp3Type,
        t.ext_config_json_ as extConfigJson,
        t.create_time_ as createTime,
        t.create_by_ as createBy,
        t.update_time_ as updateTime,
        t.update_by_ as updateBy,
        t.del_flag_ as delFlag,
        t.audit_version_ as auditVersion,
        t.source_type_ as sourceType,
        t.submit_audit_time_ as submitAuditTime,
        t.accompaniment_type_ as accompanimentType,
        t.remark_ as remark,
        t.first_tone_ as firstTone,
        t.fixed_tone_ as fixedTone,
        t.title_img_ as titleImg,
        t.reason_ as reason,
        t.music_img_ as musicImg,
        t.notation_ as notation,
        t.music_json_ as musicJSON,
        t.music_svg_ as musicSvg,
        t.music_jian_svg_ as musicJianSvg,
        t.music_first_svg_ as musicFirstSvg,
        t.music_sheet_type_ as musicSheetType
    </sql>

    <select id="selectPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetVo">
        select <include refid="Base_Column_List"/>
        ,su.username_ as addName
        ,su.avatar_ as addUserAvatar
        ,(select group_concat(mt.name_) from music_tag mt
            where find_in_set(mt.id_,t.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
            where find_in_set(s.id_,t.music_subject_) and s.del_flag_ = 0 ) as subjectNames
        from music_sheet t
        left join sys_user su on t.create_by_ = su.id_
        <where>
            su.del_flag_ = 0
            <include refid="QueryInfo"/>
            <if test="param.auditVersion != null ">
                and #{param.auditVersion} = t.audit_version_
            </if>
            <if test="param.dataRecovery != null">
                AND (t.music_svg_ = '' OR t.music_svg_ IS NULL OR t.music_first_svg_ = '' OR t.music_first_svg_ IS NULL OR t.music_jian_svg_ = '' OR t.music_jian_svg_ IS NULL)
            </if>
            <if test="param.notation">
                AND t.notation_ = #{param.notation}
            </if>
            <if test="param.DataCorrect != null">
                AND t.correct_time_ &lt;= '2023-02-07 15:00:00'
            </if>
        </where>
        order by
        <if test="param.myself == null or param.myself == false ">
            t.top_flag_ desc,t.sort_number_ desc,
        </if>
         t.id_ desc
    </select>

    <sql id="QueryInfo">
        <if test="param.musicSheetType != null">
            and t.music_sheet_type_ = #{param.musicSheetType}
        </if>
        <if test="param.idAndName != null and param.idAndName != ''">
            and (t.id_ like concat('%',#{param.idAndName},'%') or
            t.music_sheet_name_ like concat('%',#{param.idAndName},'%') or
            t.composer_ like concat ('%',#{param.idAndName},'%') or
            su.username_ like concat ('%',#{param.idAndName},'%') )
        </if>
        <if test="param.providerType != null">
            and t.provider_type_ = #{param.providerType}
        </if>
        <if test="param.musicTagIds != null and param.musicTagIds != ''">
            and
            <foreach collection="param.musicTagIdList" open="(" close=")" separator="or" item="item">
                find_in_set(#{item},t.music_tag_)
            </foreach>
        </if>

        <if test="param.subjectIdList != null and param.subjectIdList.size() != 0">
            and(
            <foreach collection="param.subjectIdList" separator="or" item="item" open="(" close=")">
                find_in_set(#{item},t.music_subject_)
            </foreach>
            or t.music_subject_ is null or t.music_subject_ = ''
            )
        </if>
        <if test="param.musicSheetIdlist != null and param.musicSheetIdlist.size() != 0">
            and t.id_ in
            <foreach collection="param.musicSheetIdlist" separator="," item="item" open="(" close=")">
                 #{item}
            </foreach>
        </if>
        <if test="param.state != null">
            and t.state_ = #{param.state}
        </if>
        <if test="param.auditStatus != null">
            and t.audit_status_ = #{param.auditStatus}
        </if>
        <if test="param.createBy != null">
            and t.create_by_ = #{param.createBy}
        </if>
        <if test="param.delFlag != null">
            and t.del_flag_ = #{param.delFlag}
        </if>
        <if test="param.sourceType != null">
            and t.source_type_ = #{param.sourceType}
        </if>
        <if test="param.chargeType != null">
            and (FIND_IN_SET(#{param.chargeType}, t.payment_type_))
        </if>
        <if test="param.topFlag != null">
            and t.top_flag_ = #{param.topFlag}
        </if>
        <if test="param.exquisiteFlag != null">
            and t.exquisite_flag_ = #{param.exquisiteFlag}
        </if>
        <if test="param.accompanimentType != null">
            and t.accompaniment_type_ = #{param.accompanimentType}
        </if>
        <if test="param.audioType != null">
            and t.audio_type_ = #{param.audioType}
        </if>

    </sql>

    <select id="selectAlbumDetailPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetVo">
        select distinct <include refid="Base_Column_List"/>
        ,su.username_ as addName
        ,su.avatar_ as addUserAvatar
        ,(select group_concat(mt.name_) from music_tag mt
            where find_in_set(mt.id_,t.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
            where find_in_set(s.id_,t.music_subject_) and s.del_flag_ = 0 ) as subjectNames
        ,amr.sort_number_ as albumSortNumber
        from music_sheet t
        left join album_music_relate amr on t.id_ = amr.music_sheet_id_
        left join sys_user su on t.create_by_ = su.id_
        <where>
            <if test="param.composer != null">
                AND t.composer_ LIKE '%${param.composer}%'
            </if>
            <if test="param.providerType != null">
                and t.provider_type_ = #{param.providerType}
            </if>
            <if test="param.idAndName != null and param.idAndName != ''">
                and (t.id_ like concat('%',#{param.idAndName},'%') or
                t.music_sheet_name_ like concat('%',#{param.idAndName},'%'))
            </if>
            <if test="param.chargeType != null">
                and t.charge_type_ = #{param.chargeType}
            </if>
            <if test="param.musicTagIds != null and param.musicTagIds != ''">
                and
                <foreach collection="param.musicTagIdList" separator="or" item="item"  open="(" close=")" >
                    find_in_set(#{item},t.music_tag_)
                </foreach>
            </if>
            <if test="param.subjectIdList != null and param.subjectIdList.size() != 0">
                and
                <foreach collection="param.subjectIdList" separator="or" item="item"  open="(" close=")" >
                    find_in_set(#{item},t.music_subject_)
                </foreach>
            </if>
            <if test="param.musicSheetType != null">
                and t.music_sheet_type_ = #{param.musicSheetType}
            </if>
            <if test="param.state != null">
                and t.state_ = #{param.state}
            </if>
            <if test="param.delFlag != null">
                and t.del_flag_ = #{param.delFlag}
            </if>
            <if test="param.sourceType != null">
                AND t.source_type_ = #{param.sourceType}
            </if>
            <if test="param.chargeType != null">
                AND (t.payment_type_ = 'FREE' OR FIND_IN_SET(#{param.chargeType}, t.payment_type_))
            </if>
            <if test="param.paymentTypes != null">
                AND t.payment_type_ IN (<foreach collection="param.paymentTypes" separator="," item="item">#{item}</foreach>)
            </if>
            <if test="param.id != null">
                <if test="param.type == 2">
                    and amr.album_id_ = #{param.id}
                    order by amr.sort_number_ desc, t.id_ desc
                </if>
                <if test="param.type == 1">
                    and t.id_ not in(select amr2.music_sheet_id_  from album_music_relate amr2
                        where amr2.album_id_ = #{param.id})
                    order by t.id_ desc
                </if>
            </if>
        </where>
    </select>

    <select id="detail" resultMap="DetailResultMap">
        SELECT
            <include refid="Base_Column_List"/>
            ,t.url_ as url
            ,t.midi_url_ as midiUrl
            ,t.metronome_url_ as metronomeUrl
            ,msa.id_ as accompanimentId
            ,msa.music_sheet_id_ as accompanimentMusicSheetId
            ,msa.music_subject_ as accompanimentMusicSubject
            ,msa.audio_file_url_ as accompanimentAudioFileUrl
            ,msa.sort_number_ as accompanimentSortNumber
            ,msa.create_time_ as accompanimentCreateTime
            ,msa.metronome_url_ as accompanimentMetronomeUrl
            ,msa.track_ as track
            ,su.username_ as userName
            ,su.avatar_ as userAvatar
            ,(
                select group_concat(mt.name_) from music_tag mt
                where find_in_set(mt.id_,t.music_tag_)
                and mt.del_flag_ = 0  and mt.state_ = 1
            ) as musicTagNames
            ,s2.name_ as subjectNames
            ,s2.code_ as code
        ,su.real_name_ as realName
            ,s2.ai_default_frequency_ as aiDefaultFrequency
        FROM music_sheet t
        left join music_sheet_accompaniment msa on msa.music_sheet_id_ = t.id_
        left join sys_user su on t.create_by_ = su.id_
        left join subject s2 on t.music_subject_ = s2.id_
        where t.id_ = #{id}
         order by  msa.sort_number_
    </select>


    <resultMap id="DetailResultMap" type="com.yonge.cooleshow.biz.dal.vo.MusicSheetDetailVo">
        <id column="id" jdbcType="BIGINT" property="id"/>
        <result column="musicSheetName" jdbcType="VARCHAR" property="musicSheetName"/>
        <result column="userId" jdbcType="BIGINT" property="userId"/>
        <result column="composer" jdbcType="VARCHAR" property="composer"/>
        <result column="titleImg" jdbcType="VARCHAR" property="titleImg"/>
        <result column="musicSubject" jdbcType="VARCHAR" property="musicSubject"/>
        <result column="audioType" jdbcType="VARCHAR" property="audioType"/>
        <result column="providerType" jdbcType="VARCHAR" property="providerType"/>
        <result column="musicTag" jdbcType="VARCHAR" property="musicTag"/>
        <result column="playSpeed" jdbcType="INTEGER" property="playSpeed"/>
        <result column="canEvaluate" jdbcType="TINYINT" property="canEvaluate"/>
        <result column="showFingering" jdbcType="TINYINT" property="showFingering"/>
        <result column="chargeType" jdbcType="TINYINT" property="chargeType"/>
        <result column="auditStatus" property="auditStatus"/>
        <result column="state" jdbcType="TINYINT" property="state"/>
        <result column="sortNumber" jdbcType="INTEGER" property="sortNumber"/>
        <result column="topFlag" jdbcType="TINYINT" property="topFlag"/>
        <result column="hotFlag" jdbcType="TINYINT" property="hotFlag"/>
        <result column="musicPrice" jdbcType="DECIMAL" property="musicPrice"/>
        <result column="audioFileUrl" jdbcType="VARCHAR" property="audioFileUrl"/>
        <result column="musicTagNames" jdbcType="VARCHAR" property="musicTagNames"/>
        <result column="subjectNames" jdbcType="VARCHAR" property="subjectNames"/>
        <result column="xmlFileUrl" jdbcType="VARCHAR" property="xmlFileUrl"/>
        <result column="hasBeat" jdbcType="TINYINT" property="hasBeat"/>
        <result column="createTime" jdbcType="TIMESTAMP" property="createTime"/>
        <result column="createBy" jdbcType="BIGINT" property="createBy"/>
        <result column="updateTime" jdbcType="TIMESTAMP" property="updateTime"/>
        <result column="updateBy" jdbcType="BIGINT" property="updateBy"/>
        <result column="delFlag" jdbcType="BOOLEAN" property="delFlag"/>
        <result column="url" jdbcType="BOOLEAN" property="url"/>
        <result column="metronomeUrl" jdbcType="BOOLEAN" property="metronomeUrl"/>
        <result column="midiUrl" jdbcType="BOOLEAN" property="midiUrl"/>
        <result column="mp3Type" jdbcType="VARCHAR" property="mp3Type"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="userName" jdbcType="VARCHAR" property="userName"/>
        <result column="auditVersion" jdbcType="VARCHAR" property="auditVersion"/>
        <result column="code" jdbcType="VARCHAR" property="code"/>
        <result column="reason" jdbcType="VARCHAR" property="reason"/>
        <result column="accompanimentType" jdbcType="VARCHAR" property="accompanimentType"/>
        <result column="notation" jdbcType="VARCHAR" property="notation"/>
	    <result column="musicImg" jdbcType="VARCHAR" property="musicImg"/>
	    <result column="favoriteCount" jdbcType="VARCHAR" property="favoriteCount"/>
	    <result column="firstPassAuditTime" jdbcType="VARCHAR" property="firstPassAuditTime"/>
        <result column="realName" jdbcType="VARCHAR" property="realName"/>
        <result column="auditTime" jdbcType="VARCHAR" property="auditTime"/>
        <result column="auditName" jdbcType="VARCHAR" property="auditName"/>
        <result column="auditReason" jdbcType="VARCHAR" property="auditReason"/>
        <result column="aiDefaultFrequency" jdbcType="VARCHAR" property="aiDefaultFrequency"/>
        <result column="musicJSON" jdbcType="VARCHAR" property="musicJSON"/>
        <result column="musicSvg" jdbcType="VARCHAR" property="musicSvg"/>
        <result column="musicJianSvg" jdbcType="VARCHAR" property="musicJianSvg"/>
        <result column="musicFirstSvg" jdbcType="VARCHAR" property="musicFirstSvg"/>
        <result column="musicSheetType" jdbcType="VARCHAR" property="musicSheetType"/>
        <collection property="background" ofType="com.yonge.cooleshow.biz.dal.entity.MusicSheetAccompaniment">
            <id column="accompanimentId" jdbcType="BIGINT" property="id"/>
            <result column="accompanimentMusicSheetId" jdbcType="BIGINT" property="musicSheetId"/>
            <result column="accompanimentMusicSubject" jdbcType="VARCHAR" property="musicSubjectId"/>
            <result column="accompanimentAudioFileUrl" jdbcType="VARCHAR" property="audioFileUrl"/>
            <result column="accompanimentSortNumber" jdbcType="TINYINT" property="sortNumber"/>
            <result column="accompanimentCreateTime" jdbcType="TIMESTAMP" property="createTime"/>
            <result column="accompanimentMetronomeUrl" jdbcType="VARCHAR" property="metronomeUrl"/>
            <result column="track" jdbcType="VARCHAR" property="track"/>
        </collection>
    </resultMap>

    <select id="selectStudentMusicPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetVo">
        select <include refid="Base_Column_List"/>
        ,su.username_ as addName
        ,su.avatar_ as addUserAvatar
        from music_sheet t
        left join sys_user su on t.create_by_ = su.id_
        <where>
            su.del_flag_ = 0
            <include refid="QueryInfo"/>
            <if test="param.auditVersion != null ">
                and #{param.auditVersion} = t.audit_version_
            </if>

        </where>
        order by
        <choose>
            <when test="param.musicSortType != null and param.musicSortType.code == 'TOP'">
                t.top_flag_ desc, t.sort_number_ desc,
            </when>
            <when test="param.musicSortType != null and param.musicSortType.code == 'HOT'">
                t.favorite_count_ desc,
            </when>
            <when test="param.musicSortType != null and param.musicSortType.code == 'NEW'">
                t.first_pass_audit_time_ desc,
            </when>
            <otherwise>
                <if test="param.myself == null or param.myself == false ">
                    t.top_flag_ desc, t.sort_number_ desc, t.favorite_count_ desc,
                </if>
            </otherwise>
        </choose>

        t.id_ desc
    </select>

    <select id="selectMyMusicPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetVo">
        select distinct <include refid="Base_Column_List"/>
        ,su.username_ as addName
        ,su.avatar_ as addUserAvatar
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,t.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
        where find_in_set(s.id_,t.music_subject_) and s.del_flag_ = 0 ) as subjectNames
        ,if(mf.id_ is not null,1,0) as favorite
        from music_sheet t
        left join sys_user su on t.create_by_ = su.id_
        left join music_sheet_purchase_record mspr on mspr.music_sheet_id_ = t.id_
        left join music_favorite mf on t.id_ = mf.music_sheet_id_ and mspr.student_id_ = mf.user_id_
        <where>
            <include refid="QueryInfo"/>
            <if test="param.studentId != null">
                and mspr.student_id_ = #{param.studentId}
                and mspr.order_status_ = 'PAID'
            </if>
            <if test="param.userType != null">
                and mspr.client_type_ = #{param.userType}
            </if>
            <if test="param.purchaseType != null">
                AND mspr.purchase_type_ = #{param.purchaseType}
            </if>
            <if test="param.courseGift != null">
                AND mspr.course_music_album_id_ <choose><when test="param.courseGift == 0"> = </when><otherwise> > </otherwise> </choose> 0
            </if>
        </where>
        order by  mspr.id_ desc
    </select>

    <select id="selectFavoriteMusicPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetVo">
        select <include refid="Base_Column_List"/>
        ,su.username_ as addName
        ,su.avatar_ as addUserAvatar
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,t.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s
        where find_in_set(s.id_,t.music_subject_) and s.del_flag_ = 0 ) as subjectNames
        ,if(mf.id_ is not null,1,0) as favorite
        from music_sheet t
        left join sys_user su on t.create_by_ = su.id_
        left join music_favorite mf on t.id_ = mf.music_sheet_id_
        <where>
            su.del_flag_ = 0
            <include refid="QueryInfo"/>
            <if test="clientType != null">
                AND mf.client_type_ = #{clientType}
            </if>
            <if test="param.studentId != null">
                and mf.user_id_ = #{param.studentId}
            </if>
        </where>
        order by mf.id_ desc
    </select>

    <select id="selectPracticeMusicPage" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetVo">
        select <include refid="Base_Column_List"/>
        ,su.username_ as addName
        ,su.avatar_ as addUserAvatar
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,t.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,(select group_concat(s.name_) from subject s where find_in_set(s.id_,t.music_subject_) ) as subjectNames
        ,if(mf.id_ is not null,1,0) as favorite
        from sys_music_compare_record mspr
        left join music_sheet t on mspr.music_sheet_id_ = t.id_
        left join sys_user su on t.create_by_ = su.id_
        left join music_favorite mf on (t.id_ = mf.music_sheet_id_ and mspr.user_id_ = mf.user_id_ <if test="param.clientType != null"> AND mf.client_type_ = #{param.clientType}</if> )
        <where>
            su.del_flag_ = 0
            <include refid="QueryInfo"/>
            <if test="practiceMusicIdList != null and practiceMusicIdList.size() != 0">
                and mspr.id_ in
                <foreach collection="practiceMusicIdList" item="item" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        <if test="practiceMusicIdList != null and practiceMusicIdList.size() != 0">
            order by field(mspr.id_,
            <foreach collection="practiceMusicIdList" item="item" separator=",">
                #{item}
            </foreach>
        </if>
        )
    </select>

    <select id="selectStudentOrderPage" resultType="com.yonge.cooleshow.biz.dal.vo.StudentMusicSheetOrderVo">
        select distinct mspr.music_sheet_id_ as musicSheetId
        ,ms.music_sheet_name_ as musicSheetName
        ,ms.composer_ as composer
        ,ms.title_img_ as titleImg
        ,ms.music_tag_ as musicTag
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,ms.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,mspr.purchase_price_ as purchasePrice
        ,mspr.order_no_ as orderNo
        ,mspr.purchase_time_ as purchaseTime
        from music_sheet_purchase_record mspr
        left join music_sheet ms on mspr.music_sheet_id_ = ms.id_
        <where>
            mspr.order_status_ = 'PAID' and mspr.purchase_type_ = 'MUSIC'
            <if test="param.idAndName != null and param.idAndName != ''">
                and (ms.id_ like concat('%',#{param.idAndName} ,'%')
                    or ms.music_sheet_name_ like concat('%',#{param.idAndName},'%'))
            </if>
            <if test="param.studentId != null">
                and mspr.student_id_ = #{param.studentId}
            </if>
            <if test="param.orderNo != null and param.orderNo != ''">
                and mspr.order_no_ = #{param.orderNo}
            </if>
            <if test="param.musicTagList != null and param.musicTagList.size() != 0">
                and
                <foreach collection="param.musicTagList" separator="or" item="item"  open="(" close=")" >
                    find_in_set(#{item},ms.music_tag_)
                </foreach>
            </if>
            <if test="param.startTime != null">
                and mspr.purchase_time_ &gt; #{param.startTime}
            </if>
            <if test="param.endTime != null">
                and mspr.purchase_time_ &lt; #{param.endTime}
            </if>
        </where>
        order by mspr.id_ desc
    </select>

    <select id="selectTeacherPage" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherMusicSheetVo">
        select ms.id_ as musicSheetId
        ,ms.music_sheet_name_ as musicSheetName
        ,ms.composer_ as composer
        ,ms.title_img_ as titleImg
        ,ms.accompaniment_type_ as accompanimentType
        ,ms.music_tag_ as musicTag
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,ms.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,ms.play_speed_ as playSpeed
        ,ms.music_price_ as musicPrice
        ,ms.create_time_ as createTime
        ,ms.audit_status_ as auditStatus
        ,su.username_ as auditName
        from music_sheet ms
        left join music_sheet_auth_record msar on ms.id_ = msar.music_sheet_id_
        left join sys_user su on msar.verify_user_id_ = su.id_
        <where>
            <if test="param.idAndName != null and param.idAndName != ''">
                and (ms.id_ like concat('%',#{param.idAndName} ,'%')
                or ms.music_sheet_name_ like concat('%',#{param.idAndName},'%')
                or ms.composer_ like concat('%',#{param.idAndName},'%'))
            </if>
            <if test="param.teacherId != null">
                and ms.create_by_ = #{param.teacherId} and ms.source_type_ = 'TEACHER'
            </if>
            <if test="param.startTime != null">
                and ms.create_time_ &gt; #{param.startTime}
            </if>
            <if test="param.endTime != null">
                and ms.create_time_ &lt; #{param.endTime}
            </if>
            <if test="param.delFlag != null">
                and ms.del_flag_ = #{param.delFlag}
            </if>
        </where>
        order by  ms.id_ desc
    </select>

    <select id="selectAuditPage" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherMusicSheetVo">
        select
        msar.id_ as authMusicSheetId,
        ms.create_by_ as teacherId
        ,ms.exquisite_flag_ as exquisiteFlag
        ,ms.title_img_ as titleImg
        ,ms.accompaniment_type_ as accompanimentType
        ,su.username_ as username
        ,su.real_name_ as realName
        ,su.phone_ as phone
        ,ms.id_ as musicSheetId
        ,ms.music_sheet_name_ as musicSheetName
        ,ms.composer_ as composer
        ,ms.music_tag_ as musicTag
        ,(select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,ms.music_tag_) and mt.del_flag_ = 0  and mt.state_ = 1) as musicTagNames
        ,ms.play_speed_ as playSpeed
        ,ms.music_price_ as musicPrice
        ,ms.update_time_ as createTime
        ,if(msar.audit_state_ is null,ms.audit_status_,msar.audit_state_) as auditStatus
        ,su2.username_ as auditName
        ,msar.first_auth_ as firstAuth
        from music_sheet_auth_record  msar
        left join  music_sheet ms on ms.id_ = msar.music_sheet_id_
        left join sys_user su on su.id_ = ms.create_by_
        left join sys_user su2 on msar.verify_user_id_ = su2.id_
        <where>
            su.del_flag_ = 0 and
            ms.source_type_  = 'TEACHER'
            <if test="param.idAndName != null and param.idAndName != ''">
                and (su.id_ like concat('%',#{param.idAndName} ,'%')
                or su.username_ like concat('%',#{param.idAndName},'%')
                or ms.music_sheet_name_ like concat('%',#{param.idAndName},'%')
                or su.phone_ like concat('%',#{param.idAndName},'%'))
            </if>
            <if test="param.auditName != null and param.auditName != ''">
                and su2.username_ like concat('%',#{param.auditName},'%')
            </if>
            <if test="param.authStatus != null">
                and msar.audit_state_ = #{param.authStatus}
            </if>
            <if test="param.startTime != null">
                and ms.create_time_ &gt; #{param.startTime}
            </if>
            <if test="param.endTime != null">
                and ms.create_time_ &lt; #{param.endTime}
            </if>
            <if test="param.chargeType != null">
                and ms.charge_type_ = #{param.chargeType}
            </if>
            <if test="param.firstAuth != null">
                and msar.first_auth_ = #{param.firstAuth}
            </if>
	        <if test="param.exquisiteFlag != null">
	            and ms.exquisite_flag_ = #{param.exquisiteFlag}
	        </if>
        </where>
        order by field(ms.audit_status_,'DOING') desc,  ms.submit_audit_time_ desc
    </select>

    <select id="selectTeacherMusicSheetCount" resultType="java.lang.Integer">
        select count(1)
        from music_sheet
        where user_id_ = #{userId}
        and state_ = 1 and audit_status_ = 'PASS' and del_flag_ = 0 and source_type_ = 'TEACHER'
    </select>

    <select id="selectMusicNum" resultType="com.yonge.cooleshow.biz.dal.vo.HomeMusicSheetVo">
        select
            sum(if(ms.source_type_ = 'PLATFORM',1,0)) as musicSheetPlatformCount,
            sum(if(ms.source_type_ = 'TEACHER',1,0)) as musicSheetTeacherCount,
            count(1) as musicSheetCount
        from music_sheet ms
        where ms.state_ = 1 and ms.del_flag_ = 0 and ms.audit_status_ = 'PASS' and find_in_set('CHARGE',ms.payment_type_)
    </select>

    <select id="selectPayMusicSheet" resultType="com.yonge.cooleshow.biz.dal.vo.HomeMusicSheetVo$MusicSheetSmall">

        SELECT
            ms.id_ as musicSheetId,
            ms.music_sheet_name_ as musicSheetName,
            count(DISTINCT pr.student_id_) as num
        FROM
            music_sheet ms
                LEFT JOIN music_sheet_purchase_record pr ON ms.id_ = pr.music_sheet_id_
        WHERE
            ms.del_flag_=0
          AND ms.charge_type_='CHARGE'
          AND ms.state_=1
          AND pr.order_status_='PAID'
        GROUP BY ms.id_
        ORDER BY count(DISTINCT pr.student_id_)  DESC,ms.id_ desc
        limit 5
    </select>

    <select id="selectTeacherCount" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherAuditMusicSheetVo">
        select
            sum(if(t.audit_status_ = 'DOING',1,0)) as doing,
            sum(if(t.audit_status_ = 'PASS'
                       and t.state_ = 1,1,0)) as pass,
            sum(if(t.audit_status_ = 'UNPASS',1,0)) as upPass
        from
            music_sheet t
        where t.user_id_ = #{userId}
    </select>

    <select id="queryStudentTotal" resultType="com.yonge.cooleshow.biz.dal.vo.StudentTotalVo">
        select
            t.user_id_ as userId,
            a.musicAlbumNum,
            b.musicSheetNum
        from student t
        left join (
            select
                user_id_, count(1) as musicAlbumNum
            from album_favorite
            <where>
                <if test="null != userId">
                    and user_id_ = #{userId}
                </if>
            </where>
            group by user_id_
        ) a on t.user_id_ = a.user_id_
        left join (
            select
                user_id_, count(1) as musicSheetNum
            from music_favorite
            <where>
                <if test="null != userId">
                    and user_id_ = #{userId}
                </if>
            </where>
            group by user_id_
        ) b on t.user_id_ = b.user_id_
        <where>
            <if test="null != userId">
                and t.user_id_ = #{userId}
            </if>
        </where>
    </select>

    <select id="queryTeacherTotal" resultType="com.yonge.cooleshow.biz.dal.vo.TeacherTotalVo">
        select
            t.user_id_ as userId,
            a.musicSheetNum as musicSheetNum
        from teacher t
        left join (
            select
                user_id_, count(1) as musicSheetNum
            from music_sheet
            where source_type_ = 'TEACHER' and state_ = 1 and audit_status_ = 'PASS' and del_flag_ = 0
            <if test="null != userId">
                and user_id_ = #{userId}
            </if>
            group by user_id_
        ) a on t.user_id_ = a.user_id_
        <where>
            <if test="null != userId">
                and t.user_id_ = #{userId}
            </if>
        </where>
    </select>

    <select id="auditDetailList" resultMap="DetailResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        ,t.url_ as url
        ,t.midi_url_ as midiUrl
        ,t.metronome_url_ as metronomeUrl
        ,msa.id_ as accompanimentId
        ,msa.music_sheet_id_ as accompanimentMusicSheetId
        ,msa.music_subject_ as accompanimentMusicSubject
        ,msa.audio_file_url_ as accompanimentAudioFileUrl
        ,msa.sort_number_ as accompanimentSortNumber
        ,msa.create_time_ as accompanimentCreateTime
        ,msa.metronome_url_ as accompanimentMetronomeUrl
        ,msa.track_ as track
        ,su.username_ as userName
        ,su.avatar_ as userAvatar
        ,(
        select group_concat(mt.name_) from music_tag mt
        where find_in_set(mt.id_,t.music_tag_)
        and mt.del_flag_ = 0  and mt.state_ = 1
        ) as musicTagNames
        ,s2.name_ as subjectNames
        ,s2.code_ as code
        ,s2.ai_default_frequency_ as aiDefaultFrequency

        ,su.real_name_ as realName
        ,msar.update_time_ as auditTime
        ,su2.username_ as auditName
        ,msar.reason_ as auditReason
        from music_sheet_auth_record  msar
        left join  music_sheet t on t.id_ = msar.music_sheet_id_
        left join sys_user su on su.id_ = t.create_by_
        left join sys_user su2 on msar.verify_user_id_ = su2.id_
        left join music_sheet_accompaniment msa on msa.music_sheet_id_ = t.id_
        left join subject s2 on t.music_subject_ = s2.id_
        <where>
            <if test="originalMusicSheetId != null">
                and #{originalMusicSheetId} = msar.original_music_sheet_id_
            </if>
            <if test="authMusicSheetId != null">
                and #{authMusicSheetId} &gt; msar.id_
            </if>
        </where>

        order by msar.id_ desc
    </select>

    <!--单曲专辑数量统计-->
    <select id="selectMusicAlbumStatInfo" resultType="com.yonge.cooleshow.biz.dal.wrapper.StatGroupWrapper">
        SELECT t1.music_sheet_id_ AS id, COUNT(DISTINCT t1.id_) AS total
        FROM album_music_relate t1 JOIN music_sheet t2 ON (t1.music_sheet_id_ = t2.id_)
        <where>
            <if test="musicIds != null">
                AND t1.music_sheet_id_ IN (<foreach collection="musicIds" separator="," item="item">#{item}</foreach>)
            </if>
        </where>
        GROUP BY t1.music_sheet_id_
    </select>

    <update id="updateFavoriteCount">
        update music_sheet set favorite_count_ =
        <choose>
            <when test="state.code == 0">
                favorite_count_ - 1
            </when>
            <when test="state.code == 1">
                favorite_count_ + 1
            </when>
            <otherwise>
                favorite_count_
            </otherwise>
        </choose>
        where id_ = #{musicSheetId}
    </update>

    <select id="countByUser" resultType="com.yonge.cooleshow.biz.dal.vo.MusicSheetUploadCountVo">
        select user_id_ userId, count(id_) count
        from music_sheet
        where del_flag_ = false and state_ =1 and source_type_ = 'TEACHER' and user_id_ in
        <foreach collection="userIds" open="(" close=")" separator="," item="item">
            #{item}
        </foreach>
        group by user_id_
    </select>

    <select id="searchMusic" resultType="java.lang.Long">
        select t.id_ from music_sheet  t
        <if test="param.tenantAlbumIds != null and param.tenantAlbumIds.size() != 0">
            left join tenant_album_music tam on t.id_ = tam.music_sheet_id_
        </if>
            <where>
                t.state_=1 and t.audit_version_ = 0 and t.del_flag_ = 0 and t.audit_status_ = 'PASS'
                <if test="param.subjectId != null">
                    and ( t.music_subject_ = #{param.subjectId} or t.music_subject_ is null or t.music_subject_ = '')
                </if>
                <if test="param.name != null and param.name != ''">
                    and t.music_sheet_name_ like concat('%',#{param.name},'%')
                </if>
                <if test="param.tenantAlbumIds != null and param.tenantAlbumIds.size() != 0">
                    and tam.tenant_album_id_ in
                    <foreach collection="param.tenantAlbumIds" item="item" open="(" close=")" separator=",">
                        #{item}
                    </foreach>
                    and tam.del_flag_=0
                </if>
                <if test="param.providerType != null">
                    and t.provider_type_ = #{param.providerType}
                </if>
            </where>
        order by t.top_flag_ desc, t.sort_number_ desc, t.favorite_count_ desc

    </select>

    <select id="searchAlbum" resultType="java.lang.Long">
        select id_ from music_album t
        <where>
            t.album_status_=1 and del_flag_=0 and audit_version_ = 0
            <if test="param.subjectId != null">
                and ( t.subject_id_ = #{param.subjectId} or t.subject_id_ is null or t.subject_id_ = '')
            </if>
            <if test="param.name != null and param.name != ''">
                and t.album_name_ like concat('%',#{param.name},'%')
            </if>
        </where>
        order by  t.sort_number_ desc
    </select>
    <!--单曲专辑数量统计-->
</mapper>