<?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.yida.data.user.mapper.EduStaffMapper">
    <resultMap id="baseResult" type="com.yida.data.common.core.entity.user.EduStaff">
        <id column="id" property="id"/>
        <result column="create_date" property="createDate"/>
        <result column="name" property="name"/>
        <result column="avatar" property="avatar"/>
        <result column="sex" property="sex"/>
        <result column="mobile" property="mobile"/>
        <result column="land_line" property="landLine"/>
        <result column="email" property="email"/>
        <result column="school_id" property="schoolId"/>
        <result column="school_name" property="schoolName"/>
        <result column="position" property="position"/>
        <result column="address" property="address"/>
        <result column="wx_id" property="wxId"/>
        <result column="yida_app_user_id" property="yidaAppUserId"/>
        <result column="wx_public_open_id" property="wxPublicOpenId"/>
        <result column="sys_user_id" property="sysUserId"/>
        <collection property="staffDept" ofType="com.yida.data.common.core.entity.user.EduStaffDept"
                    column="id"
                    select="listStaffDept">
        </collection>
    </resultMap>


    <resultMap id="listTeacherResult" type="com.yida.data.user.vo.TeacherListVO">
        <id column="id" property="id"/>
        <result column="create_date" property="createTime"/>
        <result column="name" property="name"/>
        <result column="mobile" property="phone"/>
        <result column="sys_user_id" property="sysUserId"/>
        <result column="school_id" property="schoolId"/>
        <result column="school_name" property="schoolName"/>
        <result column="create_id" property="createId"/>
        <collection property="deptVOList" ofType="com.yida.data.user.vo.TeacherDeptVO"
                    select="listTeacherDeptByTeacher" column="id">
        </collection>
    </resultMap>

    <resultMap id="detailResult" type="com.yida.data.user.vo.TeacherDetailVO">
        <result property="id" column="id"/>
        <result property="phone" column="mobile"/>
        <result property="name" column="name"/>
        <result property="schoolName" column="school_name"/>
        <result property="avatar" column="avatar"/>
        <collection property="type" ofType="java.lang.Integer">
            <result column="type"/>
        </collection>
        <collection property="gradeClass" ofType="java.lang.String">
            <result column="grade_class"/>
        </collection>
        <collection property="courseName" ofType="java.lang.String">
            <result column="course_name"/>
        </collection>
    </resultMap>

    <resultMap id="staffPageResult" type="com.yida.data.user.vo.StaffPageVO">
        <result property="id" column="id"/>
        <result property="position" column="position"/>
        <result property="name" column="name"/>
        <result property="createDate" column="create_date"/>
        <result property="schoolId" column="school_id"/>
        <result property="createId" column="create_id"/>
        <result property="mobile" column="mobile"/>
        <result property="sex" column="sex"/>
        <result property="sysUserId" column="sys_user_id"/>
        <result property="avatar" column="avatar"/>
        <result property="collectType" column="collect_type"/>
        <collection property="deptList" ofType="com.yida.data.common.core.entity.user.EduStaffDept"
                    select="listStaffDept" column="id">
        </collection>
    </resultMap>

    <resultMap id="voMap" type="com.yida.data.user.vo.TeacherDeptVO">
        <result column="campus_id" property="campusId"/>
        <result column="section_id" property="sectionId"/>
        <result column="sectionName" property="sectionName"/>
        <result column="course_id" property="courseId"/>
        <result column="course_name" property="courseName"/>
        <result column="type" property="type"/>
        <result column="grade_id" property="gradeId"/>
        <collection property="classList" ofType="com.yida.data.user.vo.TeacherClassVO">
            <result property="classId" column="class_id"/>
        </collection>
    </resultMap>


    <select id="listStaffDept" resultType="com.yida.data.common.core.entity.user.EduStaffDept">
        select *
        from edu_staff_dept
        where staff_id = #{id}
    </select>

    <select id="listTeacherDeptByTeacher" resultMap="voMap">
        select etd.course_id,
               ec.course_name,
               etd.type,
               etd.campus_id,
               etd.section_id,
               section.DEPT_NAME sectionName,
               etd.grade_id,
               etd.class_id
        from edu_teacher_dept etd
                 left join edu_course ec on etd.course_id = ec.id
                 left join edu_user_dept section
        on etd.section_id = section.DEPT_ID
        where etd.teacher_id = #{teacherId}
          and etd.del_flag = 0
        order by etd.type, etd.class_id, etd.grade_id, etd.section_id, etd.campus_id
    </select>

    <select id="getTeracherDept" resultType="com.yida.data.user.vo.TeacherDeptVO">
        select dept_id,
               course_id   d_course_id,
               course_name d_course_name,
               type        d_type,
               grade_id    d_grade_id,
               grade_name  d_grade_name,
               class_id    d_class_id,
               class_name  d_class_name
        from edu_teacher_dept
        where teacher_id = #{id}
    </select>

    <select id="listTeacher" resultMap="listTeacherResult">
        select a.id,
        a.create_date,
        a.create_id,
        a.name,
        a.sex,
        a.mobile,
        a.sys_user_id,
        a.school_id,
        a.school_name
        from(
        SELECT et.id,
        et.create_date,
        et.create_id,
        et.name,
        et.sex,
        et.mobile,
        et.sys_user_id,
        et.school_id,
        et.school_name
        FROM edu_staff et
        LEFT JOIN edu_teacher_dept etd ON et.id = etd.teacher_id
        where et.del_flag = 0
        and etd.del_flag = 0
        <if test="dto.schoolId != null">
            and et.school_id = #{dto.schoolId}
        </if>
        <if test="dto.schoolIds != null and dto.schoolIds.size() > 0">
            and et.school_id in
            <foreach collection="dto.schoolIds" close=")" open="(" separator="," item="item">
                #{item}
            </foreach>
        </if>
        <if test="dto.courseId != null">
            and etd.course_id = #{dto.courseId}
        </if>
        <if test="dto.campusId != null">
            and etd.campus_id = #{dto.campusId}
        </if>
        <if test="dto.sectionId != null">
            and etd.section_id = #{dto.sectionId}
        </if>
        <if test="dto.gradeId != null">
            and etd.grade_id = #{dto.gradeId}
        </if>
        <if test="dto.classId != null">
            and etd.class_id = #{dto.classId}
        </if>
        <if test="dto.type != null">
            and etd.type = #{dto.type}
        </if>
        <if test="dto.phone != null and dto.phone != ''">
            and et.mobile like concat('%', #{dto.phone}, '%')
        </if>
        <if test="dto.teacherName != null and dto.teacherName != ''">
            and et.name like concat('%', #{dto.teacherName}, '%')
        </if>
        group by et.id,
        et.create_date,
        et.create_id,
        et.name,
        et.sex,
        et.mobile,
        et.sys_user_id,
        et.school_id,
        et.school_name
        order by max(etd.create_date) desc
        )a
    </select>

    <select id="getTeacherById" resultMap="detailResult">
        SELECT et.id AS                               id,
               et.avatar,
               et.name,
               et.sex,
               et.mobile,
               et.school_name,
               etd.course_name,
               etd.type,
               concat(etd.grade_name, etd.class_name) grade_class
        FROM edu_staff et
                 LEFT JOIN edu_teacher_dept etd ON et.id = etd.teacher_id

        where et.del_flag = 0
          and (etd.del_flag = 0 or etd.del_flag is null)
          and et.id = #{teacherId}
    </select>

    <!--判断该教师手机号是否已经存在-->
    <select id="checkTeacherTelephone" resultType="int">
        SELECT COUNT(1)
        FROM edu_staff
        WHERE mobile = #{mobile}
        AND del_flag = '0'
        and school_id = #{schoolId}
        <if test="id != null">
            AND id != #{id}
        </if>
    </select>

    <select id="listClassByTeacher" resultType="com.yida.data.user.vo.TeacherClassVO">

        SELECT DISTINCT etd.class_id                        classId,
                        concat(td.DEPT_NAME, td2.DEPT_NAME) className
        FROM edu_teacher_dept etd
                 LEFT JOIN edu_user_dept td ON etd.grade_id = td.DEPT_ID
                 LEFT JOIN edu_user_dept td2 ON etd.class_id = td2.DEPT_ID
        WHERE etd.teacher_id = #{teacherId}
          AND etd.type = 3
          AND etd.del_flag = 0
    </select>

    <select id="listDeptByTeacher" resultType="com.yida.data.user.vo.TeacherDeptVO">
        SELECT id, campus_id campusId, section_id sectionId, grade_id gradeId, class_id classId,type
        FROM edu_teacher_dept etd
        WHERE etd.teacher_id = #{teacherId}
          AND etd.type = #{type}
          AND etd.del_flag = 0
    </select>

    <sql id="listStaffSql">
        select distinct es.id,
        es.school_id,
        es.school_name,
        es.name,
        es.sex,
        es.position,
        es.create_date,
        es.create_id,
        es.sys_user_id,
        es.mobile,
        es.avatar,
        es.collect_type
        from edu_staff es
        left join edu_staff_dept esd on es.id = esd.staff_id
        where es.del_flag = 0
        <if test="schoolId != null and schoolId.size() != 0">
            and es.school_id in
            <foreach collection="schoolId" item="item" separator="," close=")" open="(">
                #{item}
            </foreach>
        </if>
        <if test="deptId != null">
            and esd.dept_id = #{deptId}
        </if>
        <if test="isLeader != null">
            and esd.is_leader = #{isLeader}
        </if>
        <if test="mobile != null and mobile != ''">
            and es.mobile = #{mobile}
        </if>
        <if test="name != null and name != ''">
            and es.name like concat('%', #{name}, '%')
        </if>
        <if test="position != null and position != ''">
            and es.position like concat('%', #{position}, '%')
        </if>
        <if test="sex != null">
            and es.sex = #{sex}
        </if>
        <if test="collectType != null and collectType != ''">
            and es.collect_type = #{collectType}
        </if>
        group by es.id, es.school_name, es.name, es.sex, es.position, es.create_date, es.sys_user_id, es.avatar
        order by es.create_date desc
    </sql>

    <select id="listStaffPage" resultMap="staffPageResult">
        <include refid="listStaffSql">
        </include>
    </select>

    <select id="listExportStaff" resultMap="staffPageResult">
        <include refid="listStaffSql">
        </include>
    </select>

    <select id="getStaffById" resultMap="baseResult">
        select a.*, b.pic_path collectBasePic
        from edu_staff a
                 left join (select user_id, pic_path from edu_user_face where user_type = 1) b on
            a.id = b.user_id
        where a.id = #{staffId}
    </select>

    <select id="listStaff" resultType="com.yida.data.common.core.entity.user.EduStaff">
        select distinct a.*
        from edu_staff a
        left join edu_staff_dept b on a.id = b.staff_id
        where del_flag = 0
        <if test="dto.schoolId != null">
            and a.school_id = #{dto.schoolId}
        </if>
        <if test="dto.deptId != null">
            and b.dept_id = #{dto.deptId}
        </if>
        <if test="dto.ids != null and dto.ids.size() > 0">
            and a.id in
            <foreach collection="dto.ids" close=")" item="item" open="(" separator=",">
                #{item}
            </foreach>
        </if>
        <if test="dto.name != null and dto.name != ''">
            and a.name like concat('%', #{dto.name}, '%')
        </if>
        <if test="dto.accurateName != null and dto.accurateName != ''">
            and a.name = #{dto.accurateName}
        </if>
        <if test="dto.mobile != null and dto.mobile != ''">
            and a.mobile = #{dto.mobile}
        </if>
        <if test="dto.wxPath != null">
            and a.wx_path = #{dto.wxPath}
        </if>
        ORDER BY CONVERT(a.name using gbk)
    </select>
    <select id="listStaffByDeptId" resultType="java.lang.Long">
        SELECT DISTINCT a.id
        FROM `edu_staff` a
        RIGHT JOIN edu_staff_dept b ON a.id = b.staff_id
        WHERE 1 = 1
        and del_flag = 0
        <if test="deptId != null">
            and b.dept_id = #{deptId}
        </if>
        <if test="staffIds != null and staffIds.size > 0">
            and a.id in
            <foreach collection="staffIds" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
        </if>
        GROUP BY a.id
    </select>
    <select id="listStaffByNameOrPhone" resultType="java.lang.Long">
        SELECT DISTINCT b.staff_id
        FROM `edu_staff_dept` b
        left JOIN edu_staff a ON a.id = b.staff_id
        WHERE
        a.del_flag = 0
        <if test="schoolId != null">
            and a.school_id = #{schoolId}
        </if>
        <if test="nameOrPhone != null and nameOrPhone != ''">
            and a.`name` LIKE CONCAT('%', #{nameOrPhone}, '%')
            OR a.mobile like CONCAT('%', #{nameOrPhone}, '%')
        </if>
        <if test="deptId != null">
            and b.dept_id = #{deptId}
        </if>
    </select>
    <select id="listClassAndGradeByTeacher" resultType="com.yida.data.user.vo.TeacherClassAndGradeVO">
        SELECT DISTINCT class_id,
                        grade_id
        FROM edu_teacher_dept a

        WHERE teacher_id = #{teacherId}
          AND type IN (3, 4)
          AND del_flag = 0
    </select>
</mapper>