<?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.GroupMapper">
    <!--班级分组列表-->
    <select id="findAll" resultType="com.xhkjedu.suser.model.gradeclass.TGroup">
        select g.*,u.username createname from t_group g left join t_user u on g.createid=u.userid
        where g.classid=#{classid}
        <if test="createid!=null">
            and (g.createid=#{createid} or g.grouptype=1)
        </if>
        order by g.groupid
    </select>
    <!--更新分组人数-->
    <update id="updateNum">
        update t_group g set g.groupnum=
        (select count(*) from t_group_student gs where gs.groupid=#{groupid})
        where g.groupid=#{groupid}
    </update>
    <resultMap id="teacherGroup" type="com.xhkjedu.suser.vo.gradeclass.ClassGroupVo">
        <result property="classid" column="classid"/>
        <result property="classname" column="classname"/>
        <result property="gradeid" column="gradeid"/>
        <result property="classnum" column="classnum"/>
        <collection property="groups" ofType="com.xhkjedu.suser.vo.gradeclass.GroupVo">
            <result property="classid" column="classid"/>
            <result property="groupid" column="groupid"/>
            <result property="groupname" column="groupname"/>
            <result property="grouptype" column="grouptype"/>
            <result property="groupnum" column="groupnum"/>
            <!--<collection property="students" ofType="com.xhkjedu.suser.vo.gradeclass.CStudentVo"
                        javaType="java.util.List" select="listStudent" column="{groupid=groupid}">
            </collection>-->
        </collection>
    </resultMap>
    <resultMap id="studentResult" type="com.xhkjedu.suser.vo.gradeclass.CStudentVo">
        <result property="studentid" column="studentid"/>
        <result property="studentname" column="studentname"/>
    </resultMap>
    <select id="listStudent" resultMap="studentResult">
        select gs.studentid,u.username studentname,u.headpic
        from t_group_student gs left join t_user u on gs.studentid=u.userid
        where gs.groupid=#{groupid} order by convert(u.username using gbk)
    </select>
    <!--教师分组列表-->
    <select id="teacherList" resultMap="teacherGroup">
        select c.classid,c.classname,c.classnum,c.gradeid
        ,g.groupid,g.groupname,g.grouptype,g.groupnum
        from t_group g left join t_class_teacher ct on ct.classid=g.classid
        left join t_class c on ct.classid = c.classid
        where ct.teacherid=#{teacherid} and c.year=#{year} and c.schoolid=#{schoolid} and c.classstate=1
        and c.classtype <![CDATA[ < ]]>5 and (g.createid=#{teacherid} or g.grouptype=1)
        order by c.gradeid,c.classtype,c.classorder,g.groupid
    </select>
    <!--伙伴学校班级分组列表-->
    <resultMap id="teacherGroup2" type="java.util.Map">
        <result property="schoolid" column="schoolid"/>
        <result property="schoolname" column="schoolname"/>
        <collection property="classes" ofType="java.util.Map" javaType="java.util.List">
            <result property="schoolid" column="schoolid"/>
            <result property="classid" column="classid"/>
            <result property="classname" column="classname"/>
            <result property="gradeid" column="gradeid"/>
            <result property="classtype" column="classtype"/>
            <collection property="groups" ofType="com.xhkjedu.suser.vo.gradeclass.GroupVo" javaType="java.util.List">
                <result property="classid" column="classid"/>
                <result property="groupid" column="groupid"/>
                <result property="groupname" column="groupname"/>
                <result property="grouptype" column="grouptype"/>
                <result property="groupnum" column="groupnum"/>
            </collection>
        </collection>
    </resultMap>
    <select id="teacherListJtClass" resultMap="teacherGroup2">
        select s.schoolid,s.schoolname
        ,c.classid,c.classname,c.classnum,c.gradeid,c.classtype
        ,g.groupid,g.groupname,g.grouptype,g.groupnum
        from t_school_friend sf left join t_school s on sf.fschoolid = s.schoolid
        left join t_class_teacher ct on s.schoolid = ct.schoolid
        left join t_class c on ct.classid = c.classid
        left join t_group g on c.classid = g.classid
        where sf.schoolid=#{schoolid}
        and (g.grouptype=1 or g.createid=ct.teacherid)
        and c.gradeid=(select c2.gradeid from t_class c2 left join t_class_teacher ct2 on c2.classid=ct2.classid
        where c2.schoolid=#{schoolid} and c2.year=s.year and c2.classtype in(1,2) and ct2.teacherid=#{teacherid} limit 1)
        and c.year=s.year and c.classstate=1 and c.schoolid=s.schoolid
        and c.classtype in(1,2) and c.classnum>0 and g.groupnum>0
        group by g.groupid
        order by c.schoolid,c.gradeid,c.classtype,c.classorder,c.classid
        ,g.groupid
    </select>
</mapper>
