<?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.slive.mapper.gradeclass.ClassMapper">
    <!--更新学生数量-->
    <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="updateClassGroupNumBatch">
        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.slive.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>

    <!--获取年级下行政班班级id集合-->
    <select id="listClassidsByGradeid" resultType="java.lang.Integer">
        select c.classid from t_class c
        LEFT JOIN t_school s on c.schoolid=s.schoolid and c.year=s.year
        where c.gradeid=#{gradeid} and c.schoolid=#{schoolid} and c.classtype=1 and c.classstate=1
    </select>
    <!--修改单个班级状态-->
    <update id="updateClassstateByClassid">
       update t_class set classstate=#{classstate} where classid=#{classid}
    </update>

    <!--修改班级类型-->
    <update id="updateClasstype">
        update t_class set classtype=#{classtype} where classid=#{classid}
    </update>
    <!--获取班级当前年份-->
    <select id="getYearById" resultType="java.lang.Integer">
        select year from t_class where classid=#{classid}
    </select>

    <!--删除班级学生及分组学生-->
    <delete id="deleteClassstu">
        delete cs.*,gs.* from t_class_student cs
        left join t_class_student cs2 on cs.studentid=cs2.studentid
        left join t_group_student gs on cs.classid=gs.classid and cs.studentid=gs.studentid
        where cs.classid=#{zbclassid} and cs2.classid=#{classid} and cs.xzed=0
    </delete>

    <!--获取年级下所有行政班和提高班-->
    <select id="listClassByXzid" resultType="com.xhkjedu.slive.vo.xz.XzClassVo">
        select DISTINCT c.classid,c.classname,c.classnum,c.classtype,c.gradeid from t_class c
        left join t_school s on c.schoolid=s.schoolid and c.year=s.year
        where s.schoolid=#{schoolid} and c.gradeid=#{gradeid}
        and c.classstate=1 and c.classtype in (1,2) and c.classnum!=0
        order by c.classtype, c.classorder, c.classid desc
    </select>
</mapper>
