<?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.ym.mec.biz.dal.dao.SubjectDao">

    <resultMap type="com.ym.mec.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="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
        <result column="del_flag_" property="delFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
    </resultMap>

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

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

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

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.Subject">
        UPDATE subject
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </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>
        </set>
        WHERE id_ = #{id}
    </update>

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

    <!-- 分页查询 -->
    <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="findSubByMusicGroupId" resultMap="Subject">
        SELECT s.* FROM music_group_subject_plan mgsp
        LEFT JOIN `subject` s ON mgsp.subject_id_ = s.id_
        WHERE mgsp.music_group_id_ = #{musicGroupId} AND s.del_flag_ = 0
    </select>

    <select id="findSubByChargeType" resultMap="Subject">
        SELECT s.* FROM charge_type_subject_mapper ctsm
        LEFT JOIN `subject` s ON ctsm.subject_id_ = s.id_
        WHERE ctsm.charge_type_id_ = #{chargeTypeId} AND s.del_flag_ = 0
    </select>

    <select id="findByParentId" resultMap="Subject">
        SELECT * FROM subject <include refid="querySubPageSql"/>
    </select>

    <select id="findDefaultSubByChargeTypeId" resultMap="Subject">
        SELECT s.* FROM charge_type_subject_mapper ctsm
        LEFT JOIN `subject` s ON subject_id_ = s.id_
        WHERE ctsm.charge_type_id_ = #{chargeTypeId} AND s.del_flag_ = 0
    </select>

    <resultMap id="subApplyDetail" type="com.ym.mec.biz.dal.dto.SubjectApplyDetailDto">
        <result column="pay_num_" property="payNum"/>
        <result column="name_" property="subjectName"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="expected_student_num_" property="expectedStudentNum"/>
        <result column="music_group_subject_plan_id_" property="musicGroupSubjectPlanId"/>
        <result column="apply_student_num_" property="applyStudentNum"/>
        <result column="not_part_class_num_" property="notPartClassNum"/>
    </resultMap>
    <select id="findSubApplyDetail" resultMap="subApplyDetail">
        SELECT mgsp.subject_id_,s.name_,mgsp.expected_student_num_,mgsp.apply_student_num_,
        mgsp.paid_student_num_ pay_num_,mgsp.id_ music_group_subject_plan_id_
        FROM music_group_subject_plan mgsp
        LEFT JOIN `subject` s ON mgsp.subject_id_ = s.id_
        WHERE mgsp.music_group_id_ = #{musicGroupId} AND s.del_flag_ = 0
    </select>
    <select id="findBySubIds" resultType="string">
        SELECT name_ FROM `subject` WHERE del_flag_ = 0 AND FIND_IN_SET (id_,#{subIds})
    </select>

    <select id="findTeacherSubjects" resultMap="Subject">
        SELECT s.* FROM teacher t
            LEFT JOIN `subject` s ON FIND_IN_SET(s.id_,t.subject_id_)
            WHERE t.id_=#{teacherId} AND s.del_flag_ = 0
    </select>
    <select id="findSubjectConditions" resultType="com.ym.mec.biz.dal.dto.ConditionDto">
      select id_ id,name_ `name` from subject where parent_subject_id_ != 0 AND del_flag_ = 0
    </select>
    <select id="queryNameByIds" resultType="java.util.Map">
        select id_ `key`,name_ `value` FROM `subject` s WHERE FIND_IN_SET(s.id_,#{subjectIds}) AND s.del_flag_ = 0
    </select>
    <select id="findBySubjecIds" resultType="java.util.Map">
        select id_ `key`,name_ `value` FROM `subject` s WHERE FIND_IN_SET(s.id_,#{subjectIds}) AND s.del_flag_ = 0
    </select>
    <select id="findSubSubjects" resultMap="Subject">
        SELECT * FROM `subject`
        <where>
            parent_subject_id_ != 0 AND del_flag_ = 0
            <if test="parentSubjectId != null and parentSubjectId != ''">
                AND parent_subject_id_ = #{parentSubjectId}
            </if>
        </where>
    </select>

    <select id="queryPageByIdOrNameCount" resultType="integer">
        SELECT count(1) FROM `subject`
        <where>
            del_flag_ = 0 AND parent_subject_id_  != 0
            <if test="parentSubjectId != null and parentSubjectId != ''">
                AND parent_subject_id_ = #{parentSubjectId}
            </if>
            <if test="search != null and search != ''">
                AND (id_ = #{search} or name_ like concat('%', #{search}, '%'))
            </if>
        </where>
    </select>
    <select id="queryPageByIdOrName" resultMap="Subject">
        SELECT *,
               (select name_ from `subject` s1 where s.parent_subject_id_ = s1.id_) as parentSubjectName
               FROM `subject` s
        <where>
            s.del_flag_ = 0 AND s.parent_subject_id_  != 0
            <if test="parentSubjectId != null and parentSubjectId != ''">
                AND s.parent_subject_id_ = #{parentSubjectId}
            </if>
            <if test="search != null and search != ''">
                AND (s.id_ = #{search} or s.name_ like concat('%', #{search}, '%'))
            </if>
        </where>
        <include refid="global.limit"/>
    </select>

    <sql id="querySubPageSql">
        <where>
            <if test="parentId != null">
                AND parent_subject_id_ = #{parentId}
            </if>
            <if test="delFlag != null">
                AND del_flag_ = #{delFlag,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
        </where>
    </sql>

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

    <select id="findByMusicGroupUserSubject" resultType="map">
        SELECT
            sr.user_id_ AS `key`,
            s.name_ AS `value`
        FROM
            student_registration sr
            LEFT JOIN `subject` s ON sr.actual_subject_id_=s.id_
            WHERE sr.music_group_id_=#{musicGroupId} AND s.del_flag_ = 0 AND sr.music_group_status_ != 'QUIT'
            AND sr.user_id_ IN
            <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
                #{userId}
            </foreach>
    </select>

    <select id="findSubjectNameCourseMaps" resultType="java.util.Map">
        SELECT
            cs.id_ AS 'key',
            GROUP_CONCAT(s.name_) AS 'value'
        FROM
            course_schedule cs
            LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
            LEFT JOIN `subject` s ON FIND_IN_SET(s.id_,cg.subject_id_list_)
            WHERE cs.id_ IN
            <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
                #{courseScheduleId}
            </foreach>
            AND cg.subject_id_list_ IS NOT NULL
        GROUP BY cs.id_
    </select>
    <select id="findClassGroupSubjectId" resultType="java.util.Map">
        SELECT
            cs.id_ AS 'key',
            cg.subject_id_list_ AS 'value'
        FROM
            course_schedule cs
            LEFT JOIN class_group cg ON cs.class_group_id_=cg.id_
            WHERE cs.id_ IN
            <foreach collection="courseScheduleIds" item="courseScheduleId" open="(" close=")" separator=",">
                #{courseScheduleId}
            </foreach>
            AND cg.subject_id_list_ IS NOT NULL
        GROUP BY cs.id_
    </select>
    <select id="findNames" resultType="java.lang.String">
        SELECT GROUP_CONCAT(DISTINCT s.name_) FROM `subject` s WHERE FIND_IN_SET(s.id_,#{subjectIdList})
    </select>
    <select id="findStudentSubjectNameMaps" resultType="java.util.Map">
        SELECT
            sr.user_id_ as 'key',
            s.name_ as 'value'
        FROM
            student_registration sr
            LEFT JOIN `subject` s ON sr.actual_subject_id_ = s.id_
        WHERE 1=1
        AND
        <foreach collection="studentInfos" item="student" open="(" close=")" separator=" OR ">
            sr.user_id_=#{student.studentId} AND sr.music_group_id_=#{student.musicGroupId}
        </foreach>
    </select>
    <select id="findStudentSubjectIdMaps" resultType="java.util.Map">
        SELECT
            sr.user_id_ as 'key',
            sr.actual_subject_id_ as 'value'
        FROM
            student_registration sr
        WHERE 1=1
        AND
        <foreach collection="studentInfos" item="student" open="(" close=")" separator=" OR ">
            sr.user_id_=#{student.studentId} AND sr.music_group_id_=#{student.musicGroupId}
        </foreach>
    </select>
    <select id="findBySubjectIds" resultMap="Subject">
      SELECT * FROM subject WHERE id_ IN
      <foreach collection="subjectIds" item="subjectId" separator="," open="(" close=")">
          #{subjectId}
      </foreach>
    </select>

    <select id="getRegisterSubject" resultMap="com.ym.mec.biz.dal.dao.StudentRegistrationDao.StudentRegistration">
        SELECT sr.user_id_, s.name_ subject_name_ FROM student_registration sr
        LEFT JOIN subject s ON sr.actual_subject_id_=s.id_
        WHERE sr.music_group_id_=#{musicGroupId} AND s.del_flag_ = 0 AND sr.music_group_status_ != 'QUIT'
        AND sr.user_id_ IN
        <foreach collection="userIds" item="userId" open="(" close=")" separator=",">
            #{userId}
        </foreach>
    </select>
    <select id="list" resultMap="Subject">
        select * from subject where del_flag_ = 0 and parent_subject_id_ != 0
    </select>

</mapper>
