<?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.SubjectDao">
    <resultMap type="com.yonge.cooleshow.biz.dal.entity.Subject" id="Subject">
        <result column="id_" property="id"/>
        <result column="name_" property="name"/>
        <result column="code_" property="code"/>
        <result column="parent_subject_id_" property="parentSubjectId"/>
        <result column="img_" property="img"/>
        <result column="ai_default_frequency_" property="aiDefaultFrequency"/>
        <result column="desc_" property="desc"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="del_flag_" property="delFlag"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="Subject">
		SELECT * FROM subject WHERE del_flag_ = 0 and id_ = #{id}
	</select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="Subject">
		SELECT * FROM subject where del_flag_ = 0 ORDER BY id_
	</select>

    <select id="subjectSelect" resultType="com.yonge.cooleshow.biz.dal.entity.Subject">
        select t.* from subject t
        join (
            select a.subject_id_ from (
            <trim prefixOverrides="union all">
                <if test="type == null or type =='MUSIC'">
                    union all
                    (select a.music_subject_ as subject_id_ from music_sheet a
                    where a.del_flag_ = 0 and a.state_ = 1
                    GROUP BY a.music_subject_)
                </if>
                <if test="type == null or type =='ALBUM'">
                    union all
                    (SELECT cm.subject_id_ from music_album cm WHERE cm.album_status_ = 1 and cm.del_flag_ = 0 GROUP BY cm.subject_id_)
                </if>
                <if test="type == null or type =='VIDEO'">
                    union all
                    (select a.lesson_subject_ as subject_id_ from video_lesson_group a
                    where a.audit_status_ = 'PASS' and a.shelves_flag_ = 1
                    GROUP BY a.lesson_subject_)
                </if>
                <if test="type == null or type =='PIANO_ROOM'">
                    union all
                    (select a.subject_id_ as subject_id_ from course_group a
                    where a.type_ = 'PIANO_ROOM_CLASS' and a.status_ in ('ING','COMPLETE','APPLY')
                    GROUP BY a.subject_id_)
                </if>
                <if test="type == null or type =='PRACTICE'">
                    union all
                    (select a.subject_id_ as subject_id_ from course_group a
                    where a.type_ = 'PRACTICE' and a.status_ in ('ING','COMPLETE','APPLY')
                    GROUP BY a.subject_id_)
            </if>
            <if test="type == null or type =='LIVE'">
                union all
                (select a.subject_id_ as subject_id_ from course_group a
                    where a.type_ = 'LIVE' and a.status_ in ('APPLY')
                    GROUP BY a.subject_id_)
                </if>
            </trim>
            ) a group by a.subject_id_
        ) a on t.id_ = a.subject_id_
        where t.parent_subject_id_ != 0
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.yonge.cooleshow.biz.dal.entity.Subject" useGeneratedKeys="true"
            keyColumn="id"
            keyProperty="id">
        INSERT INTO subject (id_,name_,code_,parent_subject_id_,img_,create_time_,update_time_,desc_,ai_default_frequency_)
        VALUES(#{id},#{name},#{code},#{parentSubjectId},#{img},now(),now(),#{desc},#{aiDefaultFrequency})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.yonge.cooleshow.biz.dal.entity.Subject">
        UPDATE subject
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag},
            </if>
            <if test="parentSubjectId != null">
                parent_subject_id_ = #{parentSubjectId},
            </if>
            <if test="code != null">
                code_ = #{code},
            </if>
            <if test="img != null">
                img_ = #{img},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="name != null">
                name_ = #{name},
            </if>
            <if test="desc != null">
                desc_ = #{desc},
            </if>
            <if test="aiDefaultFrequency != null">
                ai_default_frequency_ = #{aiDefaultFrequency},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 根据主键删除一条记录 -->
    <update id="delete">
        UPDATE `subject` SET del_flag_ = 1 WHERE id_ = #{id} OR parent_subject_id_ = #{id}
	</update>

    <delete id="deleteById">
		update subject set del_flag_ = 1 where id_ = #{id}
	</delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="Subject" parameterType="map">
        SELECT * FROM subject
        <include refid="querySubPageSql"/>
        ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*) FROM subject
        <include refid="querySubPageSql"/>
    </select>

    <select id="findByParentId" resultMap="Subject">
        SELECT * FROM subject
        where del_flag_ = 0
        <if test="parentId != null">
            AND parent_subject_id_ = #{parentId}
        </if>
    </select>

    <sql id="querySubPageSql">
        where del_flag_ = 0
        <if test="parentId != null">
            AND parent_subject_id_ = #{parentId}
        </if>
        <if test="delFlag != null">
            AND del_flag_ = #{delFlag}
        </if>
        <if test="search != null and search != ''">
            AND (id_ like concat('%',#{search},'%') or name_ like concat('%',#{search},'%'))
        </if>
        <if test="queryType != null and queryType == 'category'">
            and (parent_subject_id_ = 0 or parent_subject_id_ is null)
        </if>
        <if test="queryType != null and queryType == 'list'">
            and parent_subject_id_ > 0
        </if>
    </sql>

    <select id="findBySubjectByIdList" resultMap="Subject">
        SELECT * FROM `subject` WHERE del_flag_ = 0 AND FIND_IN_SET(id_,#{subjectIdList})
    </select>

    <select id="findBySubjectIds" resultMap="Subject">
        SELECT * FROM subject WHERE del_flag_ = 0 and id_ IN
        <foreach collection="subjectIds" item="subjectId" separator="," open="(" close=")">
            #{subjectId}
        </foreach>
    </select>
    
    <select id="selectSubjectById" resultType="com.yonge.cooleshow.biz.dal.entity.Subject"
            parameterType="java.lang.String">
        SELECT * FROM subject WHERE del_flag_ = 0 and id_=#{lessonSubject}
    </select>

    <select id="queryByCode" resultType="com.yonge.cooleshow.biz.dal.entity.Subject"
            parameterType="java.lang.String">
        SELECT * FROM subject WHERE del_flag_ = 0 and code_ = #{code}
    </select>

    <select id="queryByName" resultType="com.yonge.cooleshow.biz.dal.entity.Subject"
            parameterType="java.lang.String">
        SELECT * FROM subject WHERE del_flag_ = 0 and name_ = #{name}
    </select>

    <select id="useSubject" resultType="java.lang.Boolean">
        select
            case when (
                select count(1) from teacher a where find_in_set(t.id_,a.subject_id_)
            )> 0 then 1 when (
                select count(1) from student a where find_in_set(t.id_,a.subject_id_)
            )> 0 then 1 else 0 end
        from `subject` t
        where  t.id_ = #{id}
    </select>

    <!--声部模糊匹配-->
    <select id="getSubjectMatchByName" resultType="com.yonge.cooleshow.biz.dal.entity.Subject">
        SELECT * FROM subject WHERE del_flag_ = 0 and name_ LIKE CONCAT('%', #{name}, '%') LIMIT 1
    </select>
    <!--声部模糊匹配-->

</mapper>
