<?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.xhkjedu.suser.mapper.gradeclass.ClassTeacherMapper">
    <!--班级教师列表-->
    <select id="findAll" resultType="com.xhkjedu.suser.vo.gradeclass.ClassTeacherVo">
        select u.userid teacherid,u.username teachername,u.usersex,u.loginname,u.usertype,u.createtime,u.usertime,u.userlock
        ,(case when u.userphone='' then userphone else concat(left(u.userphone,3),'****',right(u.userphone,4)) end)userphone
        ,(select s.subjectname from t_teacher_powersb ts left join t_subject_book sb on ts.lsbid=sb.lsbid
        left join t_subject s on sb.subjectid=s.subjectid
        where ts.teacherid=u.userid limit 1)subjectname
        ,u.userstate
        from t_user u
        <if test="teacher.gradeid!=null and teacher.gradeid!=0">
            left join t_class_teacher ct on ct.teacherid=u.userid
            left join t_class c on c.classid=ct.classid
        </if>
        <if test="teacher.subjectid!=null and teacher.subjectid!='0'.toString() and teacher.subjectid!=''">
            left join t_teacher_powersb tp on tp.teacherid=u.userid
            left join t_subject_book sb on sb.lsbid=tp.lsbid
        </if>
        where u.schoolid=#{teacher.schoolid} and u.usertype in(0,1)
        <if test="teacher.userstate!=null and teacher.userstate!=0">
            and u.userstate=#{teacher.userstate}
        </if>
        <if test="teacher.userlock!=null and teacher.userlock!=0">
            and u.userlock=#{teacher.userlock}
        </if>
        <if test="teacher.teachername!=null and teacher.teachername!=''">
            and (u.username like '%${teacher.teachername}%' or binary u.loginname like '%${teacher.teachername}%' or u.userphone like '%${teacher.teachername}%')
        </if>
        <if test="teacher.gradeid!=null and teacher.gradeid!=0">
            and c.gradeid=#{teacher.gradeid} and c.year=#{teacher.year} and c.classstate=1 and c.classtype in (1,2)
        </if>
        <if test="teacher.subjectid!=null and teacher.subjectid!='0'.toString() and teacher.subjectid!=''">
            and sb.subjectid=#{teacher.subjectid}
        </if>
        group by u.userid
        order by ${teacher.fieldname} ${teacher.fieldorder},u.userid desc
    </select>

    <!--获取当前年份下教师任教班级-->
    <select id="listTeacherClass" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        SELECT ct.classid,c.classname,c.gradeid,c.classnum
        FROM t_class_teacher ct LEFT JOIN t_class c ON ct.classid=c.classid
        WHERE ct.teacherid=#{teacherid} AND c.year=#{year} and c.classstate=1 and c.schoolid=#{schoolid}
        <if test="classtype!=null and classtype!=0">
            <if test="classtype!=11">and c.classtype=#{classtype}</if>
            <if test="classtype==11">and c.classtype in(1,2)</if>
        </if>
        <if test="classtype==null or classtype==0">
            and c.classtype!=99
        </if>
        order by c.gradeid,c.classtype,c.classorder
    </select>
    <select id="listTeacherClass2" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        SELECT ct.classid,c.classname,c.gradeid,c.classnum,c.classtype
        FROM t_class_teacher ct LEFT JOIN t_class c ON ct.classid=c.classid
        WHERE ct.teacherid=#{teacherid} AND c.year=#{year} and c.classstate=1
        and c.classtype in(1,2) and c.schoolid=#{schoolid}
        order by c.gradeid,c.classtype,c.classorder
    </select>
    <!--前年份教师任教分组学生-->
    <resultMap id="teacherClass" type="java.util.Map">
        <result property="classid" column="classid"></result>
        <result property="classname" column="classname"></result>
        <result property="gradeid" column="gradeid"></result>
        <result property="classnum" column="classnum"></result>
        <collection property="students" ofType="java.util.Map" javaType="java.util.List">
            <result property="studentid" column="studentid"></result>
            <result property="studentname" column="studentname"></result>
            <result property="headpic" column="headpic"></result>
        </collection>
    </resultMap>
    <!--获取作业学生列表-->
    <select id="listPaperStudent" resultMap="teacherClass">
        select pg.classid,c.classname,c.gradeid,c.classnum,cs.studentid,u.username studentname,u.headpic
        from t_paper_class pg left join t_class c on pg.classid=c.classid
        left join t_class_student cs on c.classid = cs.classid
        left join t_user u on cs.studentid=u.userid
        where pg.paperid=#{paperid} order by c.gradeid,c.classtype,c.classorder,convert(u.username using gbk)
    </select>
    <!--获取作业教师列表-->
    <resultMap id="paperTeacher" type="java.util.Map">
        <result property="classid" column="classid"></result>
        <result property="classname" column="classname"></result>
        <result property="gradeid" column="gradeid"></result>
        <collection property="teachers" ofType="java.util.Map" javaType="java.util.List">
            <result property="teacherid" column="teacherid"></result>
            <result property="teachername" column="teachername"></result>
        </collection>
    </resultMap>
    <select id="listPaperTeacher" resultMap="paperTeacher">
        select pg.classid,c.classname,c.gradeid,ct.teacherid,u.username teachername
        from t_paper_class pg left join t_class c on pg.classid=c.classid
        left join t_class_teacher ct on c.classid=ct.classid
        right join t_teacher_powersb tp on tp.teacherid=ct.teacherid
        right join t_subject_book sb on sb.lsbid=tp.lsbid and sb.subjectid=#{subjectid}
        right join t_user u on ct.teacherid=u.userid and u.userstate=1
        where pg.paperid=#{paperid} order by c.gradeid,c.classtype,c.classorder,convert(u.username using gbk)
    </select>
    <!-- 删除教师任教的行政和提高班 -->
    <delete id="deleteByUserid">
        DELETE ct.* FROM t_class_teacher ct
        LEFT JOIN t_class c ON ct.classid=c.classid
        WHERE ct.teacherid=#{userid} AND c.year=#{year} and c.classtype in(1,2)
    </delete>
    <!-- 删除教师任教的行政和提高班 -->
    <delete id="deleteByUserid2">
        DELETE ct.* FROM t_class_teacher ct
        LEFT JOIN t_class c ON ct.classid=c.classid
        WHERE ct.teacherid in (${teacherids}) AND c.year=#{year} and c.classtype in(1,2)
    </delete>

    <!-- 获取用户当年任教班级id -->
    <select id="listClassesByUserid" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        SELECT ct.classid,c.classname,c.gradeid,c.classtype FROM t_class_teacher ct LEFT JOIN t_class c ON ct.classid=c.classid
         WHERE ct.teacherid=#{userid} AND c.year=#{year} and c.classstate=1 and c.schoolid=#{schoolid}
        <if test="classtype!=null and classtype!=0">
            <if test="classtype!=11">and c.classtype=#{classtype}</if>
            <if test="classtype==11">and c.classtype in(1,2)</if>
        </if>
        <if test="classtype==null or classtype==0">
            and c.classtype!=99
        </if>
         order by c.gradeid,c.classtype,c.classorder
    </select>

    <resultMap id="gradeClass" type="com.xhkjedu.suser.vo.gradeclass.GradeVo">
        <result property="gradeid" column="gradeid"></result>
        <result property="gradename" column="gradename"></result>
        <collection property="classes" ofType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
            <result property="gradeid" column="gradeid"></result>
            <result property="classid" column="classid"></result>
            <result property="classname" column="classname"></result>
            <result property="classnum" column="classnum"></result>
            <result property="classtype" column="classtype"></result>
        </collection>
    </resultMap>

    <!--1月30日修改获取班级-->
    <!--<if test="role.classtype!=null and role.classtype!=0">
            and c.classtype=#{role.classtype}
        </if>-->
    <select id="listGradeClassesByUserid" resultMap="gradeClass">
        select c.gradeid,c.classid,c.classname,c.classnum,c.classtype
        from t_class_teacher ct left join  t_class c on ct.classid=c.classid
        where ct.teacherid=#{role.teacherid} and c.year=#{role.year} and c.classstate=1
        and c.schoolid=#{role.schoolid}
        <if test="role.classtype!=null and role.classtype!=0">
            <if test="role.classtype!=11">and c.classtype=#{role.classtype}</if>
            <if test="role.classtype==11">and c.classtype in(1,2)</if>
        </if>
        <if test="role.classtype==null or role.classtype==0">
            and c.classtype!=99
        </if>
        <if test="role.gradeid!=null and role.gradeid!=0">
            and c.gradeid=#{role.gradeid}
        </if>
        order by c.gradeid,c.classtype,c.classorder,c.classname
    </select>
    <select id="listGradeClassesByUserid2" resultMap="gradeClass">
        select c.gradeid,c.classid,c.classname,c.classnum,c.classtype
        from t_class_teacher ct left join  t_class c on ct.classid=c.classid
        where ct.teacherid=#{role.teacherid} and c.year=#{role.year} and c.classstate=1
        and c.classtype in(1,2) and c.schoolid=#{role.schoolid}
        order by c.gradeid,c.classtype,c.classorder
    </select>

    <!--获取学校年级下教师-->
    <select id="listTeachersByGradeid" resultType="com.xhkjedu.suser.vo.gradeclass.ClassTeacherVo">
        select ct.teacherid,u.username as teachername from t_class_teacher ct
        left join t_class c on ct.classid=c.classid
        left join t_user u on ct.teacherid=u.userid
        where c.schoolid=#{schoolid} and c.gradeid=#{gradeid} and c.year=#{year} and c.classstate=1 and u.usertype=1 and u.userstate=1
        group by ct.teacherid
    </select>

    <!--获取科目年级下教师-->
    <select id="listTeahcersBySubjectGrade" resultType="com.xhkjedu.suser.vo.gradeclass.ClassTeacherVo">
     select ct.teacherid,u.username as teachername,u.loginname,u.usersex from t_class_teacher ct
     left join t_class c on ct.classid=c.classid
     left join t_user u on ct.teacherid=u.userid
    LEFT JOIN t_teacher_powersb tp on tp.teacherid=ct.teacherid
    LEFT JOIN t_subject_book sb on tp.lsbid=sb.lsbid
    where u.schoolid=#{ctp.schoolid} and c.gradeid=#{ctp.gradeid} and c.year=#{ctp.year} and c.classstate=1 and c.classtype in (1,2)
    and sb.subjectid=#{ctp.subjectid} and u.usertype=1 and u.userstate=1
    <if test="ctp.username!=null and ctp.username!=''">
        and (u.username like '%${ctp.username}%' or binary u.loginname like '%${ctp.username}%')
    </if>
    group by ct.teacherid order by convert(u.username using gbk)
    </select>
    <!--获取年级科目下老师(标识是否是本班教师)-->
    <select id="listTeahcersBySubjectGrade2" resultType="java.util.Map">
        select ct.teacherid,u.username as teachername
        ,ifnull((select count(distinct ct.teacherid) from t_class_teacher ct2
        where ct2.classid=#{ctp.classid} and ct2.teacherid=ct.teacherid),0)classteacher
        from t_class_teacher ct
        left join t_class c on ct.classid=c.classid
        left join t_user u on ct.teacherid=u.userid
        LEFT JOIN t_teacher_powersb tp on tp.teacherid=ct.teacherid
        LEFT JOIN t_subject_book sb on tp.lsbid=sb.lsbid
        where u.schoolid=#{ctp.schoolid} and c.gradeid=#{ctp.gradeid} and c.year=#{ctp.year} and c.classstate=1 and c.classtype in (1,2)
        and sb.subjectid=#{ctp.subjectid} and u.usertype=1 and u.userstate=1
        <if test="ctp.username!=null and ctp.username!=''">
            and u.username like '%${ctp.username}%'
        </if>
        group by ct.teacherid
    </select>

    <!--获取教师权限年级下所有班级-->
    <select id="listRoleGradeAllClass" resultMap="gradeClass">
        select c.gradeid,c.classid,c.classname,c.classnum,c.classtype
             ,(case c.gradeid when 1 then '一年级' when 2 then '二年级' when 3 then '三年级' when 4 then '四年级'
                              when 5 then '五年级' when 6 then '六年级' when 7 then '七年级' when 8 then '八年级' when 9 then '九年级'
                              when 10 then '高一' when 11 then '高二' when 12 then '高三' else '' end)gradename
        from t_class c where c.schoolid=#{role.schoolid} and c.year=#{role.year} and c.classstate=1 and c.classtype in (1,2)
        and c.gradeid=(
        select c2.gradeid from t_class c2 left join t_class_teacher ct on c2.classid=ct.classid
        where ct.teacherid=#{role.teacherid} and c2.year=#{role.year} and c2.classstate=1 and c2.classtype=1 limit 1)
        order by c.classtype,c.classorder,c.classname
    </select>
</mapper>
