<?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.ruoyi.info.management.mapper.ManagementTeacherMapper">

    <resultMap type="com.ruoyi.info.management.domain.ManagementTeacher" id="ManagementTeacherResult">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <result property="phoneNumber" column="phone_number"/>
        <result property="sex" column="sex"/>
        <result property="age" column="age"/>
        <result property="workYears" column="work_years"/>
        <result property="idCard" column="id_card"/>
        <result property="enableStatus" column="enable_status"/>
        <result property="sortOrder" column="sort_order"/>
        <result property="photographUrl" column="photograph_url"/>
        <result property="manageNursingFlag" column="manage_nursing_flag"/>
        <result property="delFlag" column="del_flag"/>
        <result property="createTime" column="create_time"/>
        <result property="createBy" column="create_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="updateBy" column="update_by"/>
    </resultMap>

    <select id="queryDelById" resultType="com.ruoyi.info.management.domain.vo.ManagementTeacherVo">
         select * from tb_management_teacher where id = #{id}
    </select>

    <select id="selectVoListPage" resultType="com.ruoyi.info.management.domain.vo.ManagementTeacherVo">
        <include refid="queryList"/>
    </select>

    <select id="selectVoAllList" resultType="com.ruoyi.info.management.domain.vo.ManagementTeacherVo">
        <include refid="queryList"/>
    </select>

    <sql id="queryList">
        select
            mt.*
        from tb_management_teacher mt
        <where>
            <if test="bo.name != null and bo.name != ''">
                and mt.name like concat('%', #{bo.name}, '%')
            </if>
            <if test="bo.phoneNumber != null and bo.phoneNumber != ''">
                and mt.phone_number like concat('%', #{bo.phoneNumber}, '%')
            </if>
            <if test="bo.sex != null and bo.sex != ''">
                 and mt.name = #{bo.sex}
            </if>
            <if test="bo.enableStatus != null and bo.enableStatus != ''">
                and mt.enable_status = #{bo.enableStatus}
            </if>
            <if test="bo.createTimeStart != null">
                and <![CDATA[ mt.create_time >= #{bo.createTimeStart} ]]>
            </if>
            <if test="bo.createTimeEnd != null">
                and <![CDATA[ mt.create_time <= #{bo.createTimeEnd} ]]>
            </if>
            and mt.del_flag = 0
            <if test="bo.hospitalId != null or bo.departmentId != null">
                AND EXISTS (
                    SELECT 1
                    FROM tb_teacher_department_rel ttdr
                    WHERE ttdr.teacher_id = mt.id
                    <if test="bo.hospitalId != null">
                        and ttdr.hospital_id = #{bo.hospitalId}
                    </if>
                    <if test="bo.departmentId != null">
                        and ttdr.department_id = #{bo.departmentId}
                    </if>
                    and ttdr.del_flag = 0
                   )
            </if>
        </where>
    </sql>

</mapper>
