<?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.grouptype,g.groupid desc
    </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>
        <result property="classname" column="classname"></result>
        <result property="gradeid" column="gradeid"></result>
        <result property="classnum" column="classnum"></result>
        <collection property="groups" ofType="com.xhkjedu.suser.vo.gradeclass.GroupVo">
            <result property="classid" column="classid"></result>
            <result property="groupid" column="groupid"></result>
            <result property="groupname" column="groupname"></result>
            <result property="grouptype" column="grouptype"></result>
            <result property="selected" column="selected"></result>
            <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>
        <result property="studentname" column="studentname"></result>
    </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 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.classstate=1
        and c.classtype<![CDATA[ < ]]>5 and (g.createid=#{teacherid} or g.grouptype=1)
        order by c.gradeid,c.classtype,c.classorder,g.grouptype asc, g.groupid desc
    </select>

</mapper>
