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

    <resultMap type="com.ym.mec.biz.dal.entity.Teacher" id="Teacher">
        <result column="id_" property="id"/>
        <result column="organ_id_" property="organId"/>
        <result column="organ_name_" property="organName"/>
        <result column="job_type_" property="jobType"/>
        <result column="job_nature_" property="jobNature"/>
        <result column="is_probation_period_" property="isProbationPeriod"
                typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="education_background_" property="educationBackground"/>
        <result column="graduate_school_" property="graduateSchool"/>
        <result column="technical_titles_" property="technicalTitles"/>
        <result column="work_unit_" property="workUnit"/>
        <result column="subject_id_" property="subjectId"/>
        <result column="entry_date_" property="entryDate"/>
        <result column="demission_date_" property="demissionDate"/>
        <result column="certificate_type_" property="certificateType"/>
        <result column="certificate_num_" property="certificateNum"/>
        <result column="flow_organ_range_" property="flowOrganRange"/>
        <result column="introduction_" property="introduction"/>
        <result column="update_time_" property="updateTime"/>
        <result column="create_time_" property="createTime"/>

        <result column="username_" property="username"/>
        <result column="password_" property="password"/>
        <result column="real_name_" property="realName"/>
        <result column="salt_" property="salt"/>
        <result column="phone_" property="phone"/>
        <result column="avatar_" property="avatar"/>
        <result column="lock_flag_" property="lockFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="del_flag_" property="delFlag" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="wx_openid_" property="wxOpenid"/>
        <result column="qq_openid_" property="qqOpenid"/>
        <result column="user_type_" property="userType" typeHandler="com.ym.mec.common.dal.CustomEnumTypeHandler"/>
        <result column="gender_" property="gender"/>
        <result column="nation_" property="nation"/>
        <result column="birthdate_" property="birthdate"/>
        <result column="email_" property="email"/>
        <result column="im_token_" property="imToken"/>
        <collection property="subjectName" ofType="string" column="subject_name_"/>
    </resultMap>

    <!-- 根据主键查询一条记录 -->
    <select id="get" resultMap="Teacher">
        SELECT t.*,su.username_,su.password_,su.salt_,su.phone_,su.avatar_,
        su.lock_flag_,su.del_flag_,su.wx_openid_,su.qq_openid_,su.user_type_,
        su.gender_,su.nation_,su.birthdate_,su.email_,su.im_token_
        FROM teacher t
        LEFT JOIN sys_user su ON t.id_ = su.id_
        WHERE t.id_ = #{userId} AND su.del_flag_ = 0
    </select>

    <!-- 全查询 -->
    <select id="findAll" resultMap="Teacher">
        SELECT *
        FROM teacher
        ORDER BY user_id_
    </select>

    <!-- 向数据库增加一条记录 -->
    <insert id="insert" parameterType="com.ym.mec.biz.dal.entity.Teacher" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO teacher
        (id_,organ_id_,job_type_,job_nature_,is_probation_period_,education_background_,graduate_school_,technical_titles_,work_unit_,subject_id_,entry_date_,demission_date_,certificate_type_,certificate_num_,flow_organ_range_,introduction_,update_time_,create_time_)
        VALUES(#{id},#{organId},#{jobType},#{jobNature},#{isProbationPeriod,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},#{educationBackground},#{graduateSchool},#{technicalTitles},#{workUnit},#{subjectId},#{entryDate},#{demissionDate},#{certificateType},#{certificateNum},#{flowOrganRange},#{introduction},now(),now())
    </insert>

    <insert id="addSysUser" parameterType="com.ym.mec.auth.api.entity.SysUser" useGeneratedKeys="true" keyColumn="id"
            keyProperty="id">
        INSERT INTO sys_user
        (im_token_,id_,username_,salt_,phone_,avatar_,organ_id_,create_time_,update_time_,wx_openid_,qq_openid_,user_type_,gender_,nation_,birthdate_,email_,id_card_no_,esign_id_,wechat_id_,real_name_,password_)
        VALUES(#{imToken},#{id},#{username},#{salt},#{phone},#{avatar},#{organId},now(),now(),#{wxOpenid},#{qqOpenid},#{userType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
        #{gender},#{nation},#{birthdate},#{email},#{idCardNo},#{esignId},#{wechatId},#{realName},#{password})
    </insert>

    <!-- 根据主键查询一条记录 -->
    <update id="update" parameterType="com.ym.mec.biz.dal.entity.Teacher">
        UPDATE teacher
        <set>
            <if test="graduateSchool != null">
                graduate_school_ = #{graduateSchool},
            </if>
            <if test="subjectId != null">
                subject_id_ = #{subjectId},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="technicalTitles != null">
                technical_titles_ = #{technicalTitles},
            </if>
            <if test="entryDate != null">
                entry_date_ = #{entryDate},
            </if>
            <if test="demissionDate != null">
                demission_date_ = #{demissionDate},
            </if>
            <if test="jobType != null">
                job_type_ = #{jobType},
            </if>
            <if test="flowOrganRange != null">
                flow_organ_range_ = #{flowOrganRange},
            </if>
            <if test="certificateType != null">
                certificate_type_ = #{certificateType},
            </if>
            <if test="jobNature != null">
                job_nature_ = #{jobNature},
            </if>
            <if test="isProbationPeriod != null">
                is_probation_period_ = #{isProbationPeriod,typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            <if test="educationBackground != null">
                education_background_ = #{educationBackground},
            </if>
            <if test="certificateNum != null">
                certificate_num_ = #{certificateNum},
            </if>
            <if test="workUnit != null">
                work_unit_ = #{workUnit},
            </if>
            <if test="introduction != null">
                introduction_ = #{introduction},
            </if>
        </set>
        WHERE user_id_ = #{userId}
    </update>

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

    <update id="updateUser" parameterType="com.ym.mec.auth.api.entity.SysUser">
        UPDATE sys_user
        <set>
            <if test="delFlag != null">
                del_flag_ = #{delFlag, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="wxOpenid != null">
                wx_openid_ = #{wxOpenid},
            </if>
            <if test="avatar != null">
                avatar_ = #{avatar},
            </if>
            <if test="email != null">
                email_ = #{email},
            </if>
            <if test="gender != null">
                gender_ = #{gender},
            </if>
            <if test="salt != null">
                salt_ = #{salt},
            </if>
            <if test="username != null">
                username_ = #{username},
            </if>
            <if test="userType != null">
                user_type_ = #{userType, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="updateTime != null">
                update_time_ = #{updateTime},
            </if>
            <if test="lockFlag != null">
                lock_flag_ = #{lockFlag, typeHandler=com.ym.mec.common.dal.CustomEnumTypeHandler},
            </if>
            <if test="birthdate != null">
                birthdate_ = #{birthdate},
            </if>
            <if test="phone != null">
                phone_ = #{phone},
            </if>
            <if test="qqOpenid != null">
                qq_openid_ = #{qqOpenid},
            </if>
            <if test="nation != null">
                nation_ = #{nation},
            </if>
            <if test="organId != null">
                organ_id_ = #{organId},
            </if>
            <if test="imToken != null">
                im_token_ = #{imToken},
            </if>
        </set>
        WHERE id_ = #{id}
    </update>

    <!-- 分页查询 -->
    <select id="queryPage" resultMap="Teacher" parameterType="map">
        SELECT t.id_,su.real_name_,su.lock_flag_,t.subject_id_,su.phone_,su.organ_id_,t.job_nature_,t.is_probation_period_
        FROM teacher t LEFT JOIN sys_user su ON t.id_ = su.id_
        WHERE su.user_type_ LIKE '%TEACHER%' AND su.del_flag_ = 0
        <include refid="queryPageMap"/>
        <include refid="global.limit"/>
    </select>

    <sql id="queryPageMap">
        <if test="lockFlag != null">
            AND su.lock_flag_ = #{lockFlag}
        </if>
        <if test="subjectId != null">
            AND t.subject_id_ LIKE CONCAT('%',#{subjectId},'%')
        </if>
        <if test="organId != null">
            AND su.organ_id_ = #{organId}
        </if>
        <if test="jobNature != null">
            AND t.job_nature_ = #{jobNature}
        </if>
        <if test="isProbationPeriod != null">
            AND t.is_probation_period_ = #{isProbationPeriod}
        </if>
    </sql>

    <!-- 查询当前表的总记录数 -->
    <select id="queryCount" resultType="int">
        SELECT COUNT(t.id_)
        FROM teacher t LEFT JOIN sys_user su ON t.id_ = su.id_
        WHERE su.user_type_ LIKE '%TEACHER%' AND su.del_flag_ = 0
        <include refid="queryPageMap"/>
    </select>

    <!-- 根据证件号查询老师 -->
    <select id="findByCertificateNum" resultMap="Teacher">
        SELECT * FROM teacher WHERE certificate_num_ = #{certificateNum}
    </select>
    <select id="queryGroupStudents" resultMap="com.ym.mec.biz.dal.dao.MusicGroupDao.BasicUserDto">
        SELECT su.username_,su.id_ user_id_,su.avatar_ head_url_,su.gender_,s.name_ FROM class_group_teacher_mapper cgtm
        LEFT JOIN class_group_student_mapper cgsm ON cgtm.class_group_id_ = cgsm.class_group_id_
        LEFT JOIN sys_user su ON cgsm.user_id_ = su.id_
        LEFT JOIN student_registration sr ON sr.user_id_ = su.id_
        LEFT JOIN `subject` s ON s.id_ = sr.actual_subject_id_
        WHERE cgtm.user_id_ = #{teacherId}
        <if test="search != null">
            AND su.username_ LIKE CONCAT('%',#{search},'%')
        </if>
    </select>
    <select id="queryNameByIds" resultType="java.util.Map" parameterType="list">
        select id_ `key`,real_name_ `value` FROM sys_user s WHERE id_ IN
        <foreach item="item" index="index" collection="userIds" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="findTeachers" resultMap="Teacher">
        SELECT su.* FROM teacher t
        LEFT JOIN sys_user su on t.id_ = su.id_ WHERE 1=1
        <if test="organId != null and organId !=0">
            AND t.organ_id_ = #{organId}
        </if>
    </select>
</mapper>
