<?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.sstudy.mapper.resource.GuideGroupMapper">
    <resultMap id="GuideGroup" type="com.xhkjedu.sstudy.vo.resource.GuideGroupVo">
        <result property="groupnum" column="groupnum"></result>
        <result property="classname" column="classname"></result>
        <result property="gradename" column="gradename"></result>
        <collection property="students" ofType="com.xhkjedu.sstudy.vo.resource.GuideGroupStudentVo"
                    javaType="java.util.List" select="listStudents" column="{guideid=guideid,classid=classid}">
        </collection>
    </resultMap>
    <resultMap id="studentResult" type="com.xhkjedu.sstudy.vo.resource.GuideGroupStudentVo">
        <result property="studentname" column="studentname"></result>
        <result property="headpic" column="headpic"></result>
        <result property="viewnum" column="viewnum"></result>
        <result property="costtime" column="costtime"></result>
        <result property="readtime" column="readtime"></result>
        <result property="classid" column="classid"></result>
    </resultMap>
    <select id="listStudents" resultMap="studentResult">
        SELECT u.username studentname,u.headpic,COUNT(*)viewnum,SUM(ggs.costtime)costtime,MAX(ggs.readtime)readtime
        ,ggs.classid
        FROM t_guide_group_student ggs LEFT JOIN t_user u ON ggs.studentid = u.userid
        WHERE ggs.guideid=#{guideid} and ggs.classid=#{classid}
        GROUP BY ggs.studentid
        order by sum(ggs.costtime) desc,convert(u.username using gbk)
    </select>
    <!--导学分析-->
    <select id="findAll" resultMap="GuideGroup">
        SELECT gg.classid,count(distinct gg.studentid)groupnum,c.classname,gg.guideid
        ,(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_guide_group_student gg left join t_class c on gg.classid=c.classid
        WHERE gg.guideid=#{group.guideid}
        <if test="group.classid!=null and group.classid!=0">
            and gg.classid=#{group.classid}
        </if>
        GROUP BY c.classid ORDER BY c.gradeid,c.classtype,c.classorder
    </select>
</mapper>
