<?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.ClassGroupTeacherMapperDao">

    <resultMap type="com.ym.mec.biz.dal.entity.ClassGroupTeacherMapper"
               id="ClassGroupTeacherMapper">
        <result column="id_" property="id"/>
        <result column="group_type_" property="groupType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="music_group_id_" property="musicGroupId"/>
        <result column="class_group_id_" property="classGroupId"/>
        <result column="teacher_role_" property="teacherRole"/>
        <result column="real_name_" property="realName"/>
        <result column="user_id_" property="userId"/>
        <result column="salary_" property="salary"/>
        <result column="create_time_" property="createTime"/>
        <result column="update_time_" property="updateTime"/>
		<result column="tenant_id_" property="tenantId" />
    </resultMap>

    <resultMap id="ClassGroupTeacherMapDto" type="com.ym.mec.biz.dal.dto.ClassGroupTeacherMapDto" extends="ClassGroupTeacherMapper">
        <result column="username_" property="userName"/>
    </resultMap>

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

    <!-- 全查询 -->
    <select id="findAll" resultMap="ClassGroupTeacherMapper">
        SELECT *
        FROM class_group_teacher_mapper where tenant_id_ = #{tenantId}
        ORDER BY id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.ClassGroupTeacherMapper"
            useGeneratedKeys="true" keyColumn="id" keyProperty="id">
        INSERT INTO class_group_teacher_mapper
        (group_type_,music_group_id_,class_group_id_,teacher_role_,user_id_,salary_,create_time_,update_time_,tenant_id_)
        VALUES(#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{musicGroupId},
               #{classGroupId},#{teacherRole},#{userId},#{salary},NOW(),NOW(),#{tenantId})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.ClassGroupTeacherMapper">
        UPDATE class_group_teacher_mapper
        <set>
            <if test="userId != null">
                user_id_ = #{userId},
            </if>
            <if test="groupType != null">
                group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="classGroupId != null">
                class_group_id_ = #{classGroupId},
            </if>
            <if test="salary != null">
                salary_ = #{salary},
            </if>
            <if test="updateTime != null">
                update_time_ = NOW(),
            </if>
            <if test="teacherRole != null">
                teacher_role_ = #{teacherRole},
            </if>
            <if test="musicGroupId != null">
                music_group_id_ = #{musicGroupId},
            </if>
        </set>
        WHERE id_ = #{id} and tenant_id_ = #{tenantId}
    </update>

    <update id="updateTeacher">
        UPDATE class_group_teacher_mapper SET user_id_ = #{teacherId},update_time_ = NOW()
        WHERE teacher_role_ = 'BISHOP' AND music_group_id_ = #{groupId} AND group_type_ = #{groupType}
    </update>
    <update id="batchUpdateTeacherByClassId">
      UPDATE class_group_teacher_mapper SET user_id_ = #{newTeacherId},update_time_ = NOW() WHERE user_id_ = #{oldTeacherId} AND class_group_id_ IN
        <foreach collection="classGroupIds" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
    </update>

    <!-- 根据主键删除一条记录 -->
    <delete id="delete">
        DELETE FROM class_group_teacher_mapper WHERE
        id_ = #{id}
    </delete>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="ClassGroupTeacherMapper"
            parameterType="map">
        SELECT * FROM class_group_teacher_mapper where tenant_id_ = #{tenantId} ORDER BY id_
        <include refid="global.limit"/>
    </select>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(*)
        FROM class_group_teacher_mapper where tenant_id_ = #{tenantId}
    </select>

    <!-- 批量插入班级老师 -->
    <insert id="classGroupTeachersInsert" parameterType="java.util.List">
        INSERT INTO class_group_teacher_mapper
        (id_,group_type_,music_group_id_,class_group_id_,teacher_role_,user_id_,salary_,create_time_,update_time_,tenant_id_)
        VALUE
        <foreach collection="classGroupTeacherMapperList" item="item" index="index" separator=",">
            (#{item.id},#{item.groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{item.musicGroupId},#{item.classGroupId},#{item.teacherRole},#{item.userId},#{item.salary},NOW(),NOW(),#{item.tenantId})
        </foreach>
    </insert>

    <!-- 获取乐团班级的老师 -->
    <select id="findClassGroupTeachers" resultMap="ClassGroupTeacherMapper">
        SELECT u.real_name_, ct.*
        FROM sys_user u,
        class_group_teacher_mapper ct
        WHERE u.id_=ct.user_id_ AND FIND_IN_SET(ct.class_group_id_,#{classGroupIds})
    </select>
    <select id="findByClassGroupAndRole" resultMap="ClassGroupTeacherMapper">
        SELECT u.real_name_, ct.*
        FROM sys_user u,
        class_group_teacher_mapper ct
        WHERE ct.class_group_id_ = #{classGroupId}
        AND ct.user_id_=u.id_
        AND teacher_role_=#{teacherRole,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </select>
    <select id="findByMusicGroup" resultMap="ClassGroupTeacherMapper">
        SELECT u.real_name_, ct.*
        FROM sys_user u,
        class_group_teacher_mapper ct
        WHERE ct.music_group_id_ = #{musicGroupId}
        AND ct.user_id_=u.id_
        AND ct.teacher_role_ = 'BISHOP'
    </select>

    <select id="findAllByMusicGroup" resultMap="ClassGroupTeacherMapper">
        SELECT u.real_name_, ct.*
        FROM sys_user u,
        class_group_teacher_mapper ct
        <where>
            ct.user_id_ = u.id_
            <if test="groupType!=null">
                AND ct.group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="teacherRole != null">
                AND ct.teacher_role_ = #{teacherRole}
            </if>
            <if test="musicGroupId != null">
                AND ct.music_group_id_=#{musicGroupId}
            </if>
            <if test="tenantId != null">
                AND ct.tenant_id_=#{tenantId}
            </if>
        </where>
    </select>

    <!-- 删除对应班级老师关联关系 -->
    <delete id="delMusicGroupTeacherMapper">
        DELETE FROM class_group_teacher_mapper WHERE class_group_id_ IN
        <foreach collection="classGroupTeacherMapperList" item="item" index="index" open="(" close=")" separator=",">
            #{item.classGroupId}
        </foreach>
    </delete>

    <!-- 获取班级老师关联关系 -->
    <select id="getClassGroupTeacherMapper" resultMap="ClassGroupTeacherMapper">
        SELECT * FROM class_group_teacher_mapper WHERE class_group_id_ IN
        <foreach collection="classGroupTeacherMapperList" item="item" index="index" open="(" close=")" separator=",">
            #{item.classGroupId}
        </foreach>
        AND group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </select>

    <select id="queryTeachingNames" resultType="java.lang.String">
        SELECT GROUP_CONCAT(su.real_name_) FROM class_group_teacher_mapper cgtm
        LEFT JOIN sys_user su ON cgtm.user_id_ = su.id_
        WHERE cgtm.teacher_role_ = 'TEACHING' AND cgtm.class_group_id_ = #{classGroupId}
    </select>
    <select id="findByClassGroup" resultMap="ClassGroupTeacherMapper">
        SELECT * FROM class_group_teacher_mapper WHERE class_group_id_=#{classGroupId}
    </select>
    <select id="findByClassGroupAndTeacher" resultMap="ClassGroupTeacherMapper">
        SELECT * FROM class_group_teacher_mapper WHERE class_group_id_=#{classGroupId} AND user_id_=#{userId}
    </select>
    <select id="findByClassGroupIds" resultMap="ClassGroupTeacherMapDto">
        SELECT
            cgtm.*,
            su.real_name_ username_
        FROM
            class_group_teacher_mapper cgtm
            LEFT JOIN sys_user su ON cgtm.user_id_ = su.id_
        WHERE cgtm.class_group_id_ IN
        <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
            #{classGroupId}
        </foreach>
    </select>

    <!-- 删除对应班级老师关联关系 -->
    <delete id="delClassGroupTeacherMapper">
        DELETE FROM class_group_teacher_mapper WHERE class_group_id_ IN
        <foreach collection="classGroupIds" item="classGroupId" index="index" open="(" close=")" separator=",">
            #{classGroupId}
        </foreach>
    </delete>
    <delete id="deleteByGroup">
        DELETE FROM class_group_teacher_mapper WHERE music_group_id_=#{groupId} AND group_type_=#{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
    </delete>

    <select id="findByClassGroupIdsAndType" resultMap="ClassGroupTeacherMapper">
        SELECT * FROM class_group_teacher_mapper WHERE class_group_id_ IN
        <foreach collection="classGroupIds" item="classGroupId" open="(" close=")" separator=",">
            #{classGroupId}
        </foreach>
        <if test="teacherRole != null ">
            AND teacher_role_ = #{teacherRole}
        </if>
    </select>
    <select id="getClassGroupTeachersRole" resultMap="ClassGroupTeacherMapper">
        SELECT u.real_name_, ct.*
        FROM sys_user u,
        class_group_teacher_mapper ct
        WHERE u.id_ = ct.user_id_ AND ct.class_group_id_ = #{classGroupId} AND ct.teacher_role_ = #{teacherRole} LIMIT 1
    </select>
    <select id="queryTeacherIdsByClassGroupId" resultType="java.lang.Integer">
        SELECT DISTINCT user_id_ FROM class_group_teacher_mapper
        <include refid="queryTeacherIdsByClassGroupIdSql"/>
        UNION ALL
        SELECT DISTINCT user_id_ FROM course_schedule_teacher_salary
        <include refid="queryTeacherIdsByClassGroupIdSql"/>
    </select>
    <sql id="queryTeacherIdsByClassGroupIdSql">
        <where>
            class_group_id_ = #{classGroupId}
            <if test="groupType != null">
                AND group_type_ = #{groupType,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler}
            </if>
            <if test="musicGroupId != null">
                AND music_group_id_ = #{musicGroupId}
            </if>
        </where>
    </sql>
</mapper>
