<?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.ClassMapper">
    <!--班级学生列表-->
    <select id="findAll" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        select c.classid,c.classname,c.gradeid,c.classnum,tg.groupid,c.classorder,c.schoolyear,c.classtype
        from t_class c left join t_group tg on c.classid = tg.classid
        where tg.grouptype=1
        and c.schoolid=#{tClass.schoolid} and c.year=#{tClass.year}
        <if test="tClass.classname!=null and tClass.classname!=''">
            and c.classname like '%${tClass.classname}%'
        </if>
        <if test="tClass.classstate!=null and tClass.classstate!=0">
            and c.classstate=#{tClass.classstate}
        </if>
        <if test="tClass.classtype!=null and tClass.classtype!=0">
            <if test="tClass.classtype!=11">and c.classtype=#{tClass.classtype}</if>
            <if test="tClass.classtype==11">and c.classtype in(1,2)</if>
        </if>
        <if test="tClass.gradeid!=null and tClass.gradeid!=0">
            and c.gradeid=#{tClass.gradeid}
        </if>
        order by c.gradeid,c.classtype,c.classorder
    </select>
    <!--更新学生数量-->
    <update id="updateNum">
        update t_class c set c.classnum=
        (select count(*) from t_class_student cs where cs.classid=#{classid})
        where c.classid=#{classid}
    </update>
    <!--更新班级、分组学生数量-->
    <update id="updateNum2">
        update t_class c left join t_group g on g.classid=c.classid
        ,(SELECT COUNT(*)num FROM t_class_student cs where cs.classid=#{classid})a
        set c.classnum=a.num,g.groupnum=(SELECT COUNT(*) FROM t_group_student gs WHERE gs.groupid=g.groupid)
        where c.classid=#{classid}
    </update>
    <!--更新全校指定班级、分组学生数量-->
    <update id="updateClassGroupNumByType">
        update t_class c left join t_group g on g.classid=c.classid and g.grouptype=1
        set c.classnum=(SELECT COUNT(*)FROM t_class_student cs where cs.classid=c.classid),
        g.groupnum=(SELECT COUNT(*) FROM t_group_student gs WHERE gs.groupid=g.groupid)
        where c.classid in (${classids})
    </update>
    <!--基本信息-->
    <select id="getById" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        select c.classid,c.classname,c.gradeid,c.classnum,tg.groupid,c.classtype,c.createid,c.schoolid,c.year
        from t_class c left join t_group tg on c.classid = tg.classid
        where c.classid=#{classid} and tg.grouptype=1 limit 1
    </select>

    <!--根据班级名称获取班级信息-->
    <select id="getByName" resultType="com.xhkjedu.suser.model.gradeclass.TClass">
        select c.classid, c.classname, c.gradeid, c.classnum, c.classstate, c.classorder, c.schoolyear, c.year, c.classtype
        , c.createid, c.createtime, c.schoolid
        from t_class c where c.schoolid=#{tClass.schoolid} and c.classname=#{tClass.classname}
        and c.year=#{tClass.year} and c.gradeid=#{tClass.gradeid} and c.classstate=1 and c.classtype=#{tClass.classtype} limit 1
    </select>
    <!--获取任教老师列表-->
    <select id="listTeacher" resultType="com.xhkjedu.suser.vo.gradeclass.ClassTeacherSubjectVo">
        select u.username as teachername,s.subjectname from t_class_teacher ct left join t_user u on ct.teacherid=u.userid
        left join t_teacher_powersb tp on ct.teacherid=tp.teacherid
        left join t_subject_book sb on tp.lsbid=sb.lsbid
        left join t_subject s on sb.subjectid=s.subjectid
        where ct.classid=#{classid} and u.userstate=1 and u.schoolid=#{schoolid}
        group by u.userid order by ifnull(s.subjectorder,9999),convert(u.username using gbk),u.userid
    </select>

    <!--修改单个班级状态-->
    <update id="updateClassstateByClassid">
       update t_class set classstate=#{classstate} where classid=#{classid}
    </update>
    <!--获取班级集合-->
    <select id="listClass" resultType="java.util.Map">
        select c.gradeid,c.classid,c.classname,g.groupid,c.year
        from t_school s left join t_class c on s.schoolid = c.schoolid
        left join t_group g on c.classid=g.classid
        where s.schoolid=#{schoolid} and s.year=c.year
        and c.classtype=#{classtype} and c.classstate=1 and g.grouptype=1
    </select>

    <!--班级下教师，以及教师科目-->
    <select id="listTeacherSubject" resultType="java.util.Map">
        select ct.teacherid,u.username teachername,u.loginname,sb.subjectid from t_class_teacher ct
        left join t_user u on ct.teacherid=u.userid
       left join t_teacher_powersb tp on ct.teacherid=tp.teacherid
        left join t_subject_book sb on tp.lsbid=sb.lsbid
        where ct.classid=#{classid} group by ct.teacherid order by ct.teacherid
    </select>
    <!--获取班级信息-->
    <select id="listByIds" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        select c.classid,c.classname,c.gradeid,c.classnum,tg.groupid,c.classtype,c.createid,c.schoolid,c.year
        from t_class c left join t_group tg on c.classid = tg.classid
        where tg.grouptype=1 and c.classid in
        <foreach collection="classids" item="classid" index="index" open="(" separator="," close=")">
            ${classid}
        </foreach>
        group by c.classid
    </select>

    <resultMap id="schoolClass" 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>
    </resultMap>
    <!--获取伙伴学校及伙伴学校班级-->
    <select id="listSchoolAndClass" resultMap="schoolClass">
        select s.schoolid,s.schoolname
        ,c.classid,c.classname,c.classnum,c.gradeid,c.classtype
        from t_school_friend sf left join t_school s on sf.fschoolid = s.schoolid
        left join t_class c on s.schoolid=c.schoolid
        where sf.schoolid=#{schoolid}
        and c.year=s.year and c.classstate=1 and c.schoolid=s.schoolid
        and c.classtype in(1,2) and c.classnum>0
        group by c.classid
        order by c.schoolid,c.gradeid,c.classtype,c.classorder,c.classid
    </select>

    <!--获取学生所在行政班信息-->
    <select id="getXzClassByUserid" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        select DISTINCT c.classid,c.classname,c.gradeid from t_class_student cs
        left join t_class c on cs.classid=c.classid
        left join t_school s on c.schoolid=s.schoolid
        where  c.classstate=1 and c.year=s.year and c.classtype=1 and cs.studentid=#{userid}
    </select>
</mapper>
