<?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.EduTeacherDeptMapper">
  <resultMap id="voMap" type="com.yida.data.user.vo.TeacherDeptVO">
    <result column="campus_id" property="campusId"/>
    <result column="section_id" property="sectionId"/>
    <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="listPermissionDeptByTeacher" resultType="com.yida.data.common.core.entity.system.Dept">
    select distinct b.dept_id deptId, b.dept_type deptType
    from edu_staff a
    left join edu_teacher_dept b on a.id = b.teacher_id
    where a.sys_user_id = #{teacherSysId}
    and b.del_flag = 0
    and a.del_flag = 0
  </select>

  <select id="listTeacherByClass" resultType="com.yida.data.common.core.entity.user.EduStaff">
    select a.*, b.type teacherType
    from edu_staff a
    inner join edu_teacher_dept b on a.id = b.teacher_id
    where a.del_flag = 0
    and b.del_flag = 0
    and b.class_id in
    <foreach collection="classIds" item="item" close=")" open="(" separator=",">
      #{item}
    </foreach>
    <if test="teacherName != null and teacherName != ''">
      and a.name like concat('%', #{teacherName}, '%')
    </if>
    and b.type in
    (3, 4)
    order by a.name
  </select>

  <select id="listTeacherBySchool" resultType="com.yida.data.common.core.entity.user.EduStaff">
    select distinct a.*
    from edu_staff a
    left join edu_teacher_dept b on a.id = b.teacher_id
    <where>
      a.del_flag = 0
      and b.del_flag = 0
      and b.id is not null
      and a.school_id = #{schoolId}
      <if test="name != null and name != ''">
        and name like concat('%', #{name}, '%')
      </if>
    </where>
    order by a.name
  </select>
  <select id="findType" resultType="java.lang.Integer">
    SELECT DISTINCT a.type
    FROM edu_teacher_dept a
    WHERE a.teacher_id = #{teacherId}
    and a.del_flag = 0
  </select>

  <select id="findGroupDeptType" resultType="com.yida.data.common.core.entity.system.Dept">
    SELECT DISTINCT a.DEPT_ID,
    a.DEPT_NAME
    FROM t_dept a,
    (SELECT dept_id FROM `edu_teacher_dept` WHERE del_flag = 0 AND teacher_id = #{teacherId} AND type = 5
    ) b
    WHERE a.PARENT_ID = b.dept_id
  </select>

  <select id="selectDeptList" resultType="com.yida.data.common.core.entity.user.EduUserDept">
    SELECT DISTINCT grade_id as DEPT_ID,
    grade_name as DEPT_NAME
    FROM `edu_teacher_dept`
    WHERE del_flag = 0
    AND teacher_id = #{teacherId}
    AND type IN (3, 4)
  </select>

  <select id="selectCoursetList" resultType="java.lang.Long">
    SELECT DISTINCT course_id
    FROM edu_teacher_dept
    WHERE del_flag = 0
    <if test="teacherId != null">
      AND teacher_id = #{teacherId}
    </if>
    <if test="schoolId != null">
      and school_id = #{schoolId}
    </if>
    <if test="classId != null">
      and class_id = #{classId}
    </if>
    AND course_id IS NOT null
  </select>
  <select id="findClassId" resultType="java.lang.Long">
    SELECT DISTINCT class_id
    FROM edu_teacher_dept
    WHERE del_flag = 0
    AND teacher_id = #{teacherId}
    AND type = 3
    AND course_id IS NOT null
  </select>

  <select id="listTeacherCourseByGrade" resultType="com.yida.data.user.vo.TeacherCourseInfoVO">
    select a.teacher_id, b.name teacherName, a.class_id, a.class_name, c.id courseId, c.course_name
    from edu_teacher_dept a
    left join edu_staff b on a.teacher_id = b.id
    left join (SELECT * FROM edu_course WHERE del_flag = 0) c on a.course_id = c.id
    where a.del_flag = 0
    and b.del_flag = 0
    <if test="gradeId != null">
      and a.grade_id = #{gradeId}
    </if>
    <if test="classId != null">
      and a.class_id = #{classId}
    </if>
    order by a.create_date desc
  </select>

  <select id="listTeacherByClassAndName"
    resultType="com.yida.data.common.core.entity.user.EduStaff">
    select b.*
    from edu_teacher_dept a
    left join edu_staff b on a.teacher_id = b.id
    where a.del_flag = 0
    and b.del_flag = 0
    and a.class_id = #{classId}
    and b.name = #{name}
  </select>

  <select id="listTeacherClass" resultType="com.yida.data.user.vo.TeacherClassVO">
    select distinct class_id, grade_id
    from edu_teacher_dept
    where teacher_id = #{teacherId}
    and type in (3, 4)
  </select>

  <select id="listTeacherDept" resultType="com.yida.data.common.core.entity.user.EduTeacherDept">
    select etd.*,es.wx_id teacherWxId
    from edu_teacher_dept etd left join edu_staff es on etd.teacher_id=es.id
    where es.del_flag=0 and etd.del_flag=0 and etd.teacher_id in
    <foreach collection="list" close=")" item="item" open="(" separator=",">
      #{item}
    </foreach>
  </select>
</mapper>