<?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.GradeMapper">
    <!--学校年级列表-->
    <select id="findAll" resultType="com.xhkjedu.suser.model.gradeclass.TGrade">
        select g.* from t_grade g
        where g.schoolid=#{schoolid} and g.year=#{year}
        order by g.gradeorder
    </select>
    <!--详情-->
    <select id="findById" resultType="com.xhkjedu.suser.model.gradeclass.TGrade">
        select g.*,u.username createname
        from t_grade g left join t_user u on g.createid=u.userid
        where g.gradeid=#{gradeid} limit 1
    </select>

    <resultMap id="gradeResult" 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="gradeid2"></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>
            <result property="classorder" column="classorder"></result>
            <result property="year" column="year"></result>
            <result property="schoolyear" column="schoolyear"></result>
        </collection>
        <collection property="subjects" ofType="com.xhkjedu.suser.vo.gradeclass.GradeSubjectVo">
            <result property="subjectid" column="subjectid"></result>
            <result property="subjectname" column="subjectname"></result>
            <collection property="teachers" ofType="com.xhkjedu.suser.vo.gradeclass.ClassTeacherVo">
                <result property="teacherid" column="teacherid"></result>
                <result property="teachername" column="teachername"></result>
            </collection>
        </collection>
    </resultMap>
    <!--获取通知班级列表-->
    <select id="listClass" resultMap="gradeResult">
        select c.gradeid,c.gradeid gradeid2,c.classid,c.classname,c.classnum,c.classtype,c.classorder,c.year,c.schoolyear
        ,(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=#{schoolid} and c.year=#{year} and c.classstate=1
        <if test="classtype!=null and classtype!=0">
            and c.classtype=#{classtype}
        </if>
        <if test="classtype==null or classtype==0">
            and c.classtype<![CDATA[ < ]]>5
        </if>
         order by c.gradeid,c.classtype,c.classorder,c.classid desc
    </select>

    <!--教师设置权限班级-->
    <select id="listClassForTeacher" resultMap="gradeResult">
        select c.gradeid,c.gradeid gradeid2,c.classid,c.classname,c.classnum,c.classtype,c.classorder,c.year,c.schoolyear
        ,(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=#{schoolid} and c.year=#{year} and c.classstate=1 and c.classtype between 1 and 2
        order by c.gradeid,c.classtype,c.classorder,c.classid desc
    </select>

    <!--获取通知教师列表-->
    <select id="listTeacher" resultMap="gradeResult">
        select c.gradeid,s.subjectid,s.subjectname,u.userid teacherid,u.username teachername
        ,(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 left join t_teacher_powersb ts on c.schoolid=ts.schoolid
        left join t_subject_book sb on ts.lsbid=sb.lsbid
        left join t_subject s on sb.subjectid=s.subjectid
        left join t_class_teacher ct on c.classid=ct.classid and ts.teacherid=ct.teacherid
        left join t_user u on ct.teacherid=u.userid
        where c.schoolid=#{schoolid} and c.year=#{year} and c.classstate=1 and u.usertype=1 and u.userstate=1 and c.classtype in (1,2)
        order by c.gradeid,c.classtype,c.classorder,s.subjectorder,convert(u.username using gbk)
    </select>
    <!--集团校教师-->
    <resultMap id="jtTeacherResult" type="java.util.Map">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <collection property="grades" ofType="java.util.Map" javaType="java.util.List">
            <result property="gradeid" column="gradeid"/>
            <collection property="subjects" ofType="java.util.Map" javaType="java.util.List">
                <result property="subjectid" column="subjectid"/>
                <result property="subjectname" column="subjectname"/>
                <collection property="teachers" ofType="java.util.Map" javaType="java.util.List">
                    <result property="teacherid" column="teacherid"/>
                    <result property="teachername" column="teachername"/>
                </collection>
            </collection>
        </collection>
    </resultMap>
    <select id="listJtTeacher" resultMap="jtTeacherResult">
        select s1.schoolid,s1.schoolname,c.gradeid,s.subjectid,s.subjectname
        ,u.userid as teacherid,u.username as teachername
        from t_school_friend sf left join t_school s1 on sf.fschoolid=s1.schoolid
        left join t_class c on s1.schoolid=c.schoolid
        left join t_teacher_powersb ts on c.schoolid=ts.schoolid
        left join t_subject s on ts.subjectid=s.subjectid
        left join t_class_teacher ct on c.classid=ct.classid and ts.teacherid=ct.teacherid
        left join t_user u on ct.teacherid=u.userid
        where sf.schoolid=#{schoolid} and c.year=#{year} and c.classstate=1 and u.usertype=1 and u.userstate=1 and c.classtype in (1,2)
        group by u.userid
        order by s.schoolid,c.gradeid,c.classtype,c.classorder,s.subjectorder,convert(u.username using gbk)
    </select>

    <!--集团校班级-->
    <resultMap id="listJtClassMap" type="java.util.Map">
        <result column="schoolid" property="schoolid"></result>
        <result column="schoolname" property="schoolname"></result>
        <collection property="classes" ofType="java.util.Map" javaType="java.util.List">
            <result column="classid" property="classid"></result>
            <result column="classname" property="classname"></result>
            <result column="gradeid" property="gradeid"></result>
            <result column="classtype" property="classtype"></result>
        </collection>
    </resultMap>
    <select id="listJtClasses" resultMap="listJtClassMap">
        select s.schoolid,s.schoolname,c.classid,c.classname,c.gradeid,c.classtype
        from t_class c left join t_school s on c.schoolid=s.schoolid
        left join t_school_friend sf on s.schoolid=sf.fschoolid
        left join t_school sy on sy.schoolid=#{schoolid}
        where sf.schoolid=#{schoolid} and c.classstate=1 and s.schoolstate=1 and c.`year`=sy.`year` and c.classtype in (1,2)
        group by c.classid order by s.schoolid,c.gradeid,c.classtype,c.classorder,c.classid
    </select>

    <!--教师已设置集团校班级-->
    <select id="listTeacherJtClasses" resultMap="listJtClassMap">
        select s.schoolid,s.schoolname,c.classid,c.classname,c.gradeid,c.classtype
        from t_class c left join t_school s on c.schoolid=s.schoolid
        left join t_class_teacher ct on c.classid=ct.classid
        where ct.teacherid=#{teacherid} and c.classstate=1 and s.schoolstate=1 and c.`year`=s.`year`
          and c.year=#{year} and c.classtype in (1,2) and s.schoolid!=#{schoolid}
        group by c.classid
        order by s.createtime desc,c.gradeid asc,c.classorder asc,c.classid desc
    </select>
</mapper>
