<?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.ClassStudentMapper">
    <!--班级学生列表-->
    <select id="findAll" resultType="com.xhkjedu.suser.vo.gradeclass.ClassStudentVo">
        select cs.csid,cs.classid,c.classname,c.gradeid,u.username studentname,u.studentno,u.usersex,u.userid studentid,u.loginname,cs.createtime
        ,u.bindmac,u.mac,u.headpic,u.usertime,u.userlock,u.examno
        ,(case when u.userphone='' then userphone else concat(left(u.userphone,3),'****',right(u.userphone,4)) end)userphone
        from t_class_student cs left join t_user u on cs.studentid = u.userid
        left join t_class c on cs.classid=c.classid
        where cs.classid=#{student.classid}
        <if test="student.userlock!=null and student.userlock!=0">
            and u.userlock=#{student.userlock}
        </if>
        <if test="student.studentname!=null and student.studentname!=''">
            and (u.username like '%${student.studentname}%' or u.studentno like '%${student.studentname}%' or u.userphone like '%${student.studentname}%')
        </if>
        <if test="student.fieldname!=null and student.fieldname!=''">
          order by ${student.fieldname} ${student.fieldorder},cs.csid desc
        </if>
    </select>
    <!--删除班级、分组对应学生-->
    <delete id="deleteById">
        delete cs.*,gs.*
        from t_class_student cs left join t_group g on g.classid=cs.classid
        left join t_group_student gs on g.groupid = gs.groupid AND gs.studentid=cs.studentid
        where cs.classid=#{classid} and cs.studentid=#{studentid}
    </delete>

    <!--批量删除班级、分组对应学生-->
    <delete id="deleteBatchStudent">
        <foreach collection="list" item="item" separator=";">
            delete cs.*,gs.*
            from t_class_student cs left join t_group g on g.classid=cs.classid
            left join t_group_student gs on g.groupid = gs.groupid AND gs.studentid=cs.studentid
            where cs.classid=#{item} and cs.studentid in(${studentids})
        </foreach>
    </delete>

    <!--班级学生数量-->
    <select id="getNumById" resultType="com.xhkjedu.suser.vo.gradeclass.ImportStudentVo">
        <choose>
            <when test="studentid!=0">
                select count(distinct cs.classid)totalnum
                ,count(distinct case when c.classid=#{classid} then c.classid else null end)classnum
                ,count(distinct case when c.classid!=#{classid} and c.classtype=1 and c.classstate=1 then c.classid else
                null end)xznum
                from t_class_student cs left join t_class c on cs.classid=c.classid
                where cs.studentid=#{studentid} and c.year=#{year}
            </when>
            <otherwise>
                select count(*)classnum from t_class_student cs where cs.classid=#{classid}
            </otherwise>
        </choose>
    </select>
    <!--获取学生行政班id-->
    <select id="getClassIdByStuId" resultType="java.lang.Integer">
        select c.classid
        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 and c.year=s.year
        where cs.studentid=#{studentid} and c.classtype=1 and c.classstate=1 order by c.classid desc limit 1
    </select>
    <!--根据班级id和学生id获取班级学生信息-->
    <select id="getByClassIdStudentId" resultType="java.lang.Integer">
        select csid from t_class_student where classid=#{classid} and studentid=#{studentid} limit 1
    </select>

    <!-- 获取学生所在班级 -->
    <select id="listClassByStudentId" resultType="com.xhkjedu.suser.vo.gradeclass.ClassVo">
        SELECT c.classid,c.classname,c.gradeid,c.classnum,c.classtype FROM t_class c
        LEFT JOIN t_class_student cs ON c.classid=cs.classid
        WHERE c.year=#{year} and c.classstate=1 AND cs.studentid=#{studentid} and (c.classtype=1 or c.classtype=2)
        GROUP BY c.classid ORDER BY c.classtype,c.classorder

    </select>

    <select id="listNoClassStu" resultType="com.xhkjedu.suser.vo.gradeclass.ClassStudentVo">
        select u.username studentname,u.studentno,u.usersex,u.userid studentid,u.loginname,u.createtime,
        u.bindmac,u.mac,u.userstate,u.usertime,u.headpic,u.userlock,u.examno
        ,(case when u.userphone='' then userphone else concat(left(u.userphone,3),'****',right(u.userphone,4)) end)userphone
        from t_user u left join
        (select cs1.studentid,cs1.createtime from t_class_student cs1
        left join t_class c on cs1.classid=c.classid where c.schoolid=#{student.schoolid} and c.year=#{student.year}
        and c.classtype=1 and c.classstate=1) cs on u.userid=cs.studentid
        where u.schoolid=#{student.schoolid} and u.usertype=2 and cs.studentid is null
        <if test="student.userlock!=null and student.userlock!=0">
            and u.userlock=#{student.userlock}
        </if>
        <if test="student.userstate!=null and student.userstate!=0">
            and u.userstate=#{student.userstate}
        </if>
        <if test="student.studentname!=null and student.studentname!=''">
            and (u.username like '%${student.studentname}%' or u.studentno like '%${student.studentname}%' or u.userphone like '%${student.studentname}%')
        </if>
        <if test="student.fieldname!=null and student.fieldname!=''">
            order by ${student.fieldname} ${student.fieldorder},u.userid desc
        </if>
    </select>

    <!--获取学生所在年级列表-->
    <select id="listGrade" resultType="java.lang.Integer">
        SELECT DISTINCT c.gradeid FROM t_class_student cs LEFT JOIN t_class c ON cs.classid=c.classid
        WHERE cs.studentid=#{studentid} AND c.year=#{year} and c.classtype=1 and c.classstate=1
    </select>

    <!--获取全校学生-->
    <select id="listAllStudents" resultType="com.xhkjedu.suser.vo.gradeclass.ClassStudentVo">
        select u.username studentname,u.studentno,
        u.usersex,u.userid studentid,u.loginname,cs.createtime,u.userlock,u.examno,
        u.bindmac,u.mac,u.headpic,u.usertime,c.gradeid,c.classname,c.classid
        ,(case when u.userphone='' then userphone else concat(left(u.userphone,3),'****',right(u.userphone,4)) end)userphone
        from t_user u left join t_class_student cs on u.userid=cs.studentid
        left join t_class c on cs.classid=c.classid
        where u.schoolid=#{student.schoolid} and u.usertype=2 and u.userstate=1
        and c.classstate=1 and c.year=#{student.year} and c.classtype=#{student.classtype}
        <if test="student.userlock!=null and student.userlock!=0">
            and u.userlock=#{student.userlock}
        </if>
        <if test="student.studentname!=null and student.studentname!=''">
            and (u.username like '%${student.studentname}%' or u.studentno like '%${student.studentname}%' or u.userphone like '%${student.studentname}%')
        </if>
        group by u.userid
        <if test="student.classtype==1">
            ,c.classid
        </if>
        order by u.studentno
    </select>
    <!--获取班级学生id列表-->
    <select id="listStudentIdByClassId" resultType="java.lang.Integer">
        select studentid from t_class_student where classid=#{classid}
    </select>
    <!--添加-->
    <insert id="save">
        insert ignore into t_class_student (classid,studentid,createid,createtime,schoolid)
        values (#{student.classid},#{student.studentid},#{student.createid},#{student.createtime},#{student.schoolid})
    </insert>
    <!--获取学生所有提高班-->
    <select id="listTgClassid" resultType="java.lang.Integer">
        SELECT c.classid 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 cs.studentid=#{studentid} AND c.year=s.year AND c.classtype=2
    </select>

    <!--批量保存班级学生-->
    <insert id="saveBatchClassStudent">
        insert ignore into t_class_student (classid,studentid,createid,createtime,schoolid) values
        <foreach collection="list" item="student" index="index" separator=",">
            (#{student.classid},#{student.studentid},#{student.createid},#{student.createtime},#{student.schoolid})
        </foreach>
    </insert>
    <!--获取学生除某个班外的行政班和提高班id-->
    <select id="getClassIdsByCidAndSid" resultType="java.lang.String">
        select group_concat(c.classid) from t_class tc left join t_class c on c.schoolid=tc.schoolid and c.`year`=tc.`year`
        left join t_class_student cs on c.classid=cs.classid
        where tc.classid=#{classid} and c.classid!=#{classid} and c.classtype in(1,2) and cs.studentid=#{studentid}
    </select>
    <!--获取学生除某个班外的行政班和提高班id-->
    <select id="listClassIdsByCidAndSid" resultType="java.util.Map">
        select group_concat(c.classid)classids,cs.studentid
        from t_class tc left join t_class c on c.schoolid=tc.schoolid and c.`year`=tc.`year`
        left join t_class_student cs on c.classid=cs.classid
        where c.classtype in(1,2) and
        <foreach collection="students" item="student" index="index" open="(" separator="or" close=")">
            (tc.classid=${student.classid} and c.classid!=${student.classid} and cs.studentid=${student.studentid})
        </foreach>
        group by cs.studentid
    </select>
    <!--批量删除班级、分组对应学生-->
    <delete id="deleteBatchStudent2">
        delete cs.*,gs.*
        from t_class_student cs left join t_group g on g.classid=cs.classid
        left join t_group_student gs on g.groupid = gs.groupid AND gs.studentid=cs.studentid
        where
        <foreach collection="students" item="student" separator="or">
            (cs.classid=${student.classid} and cs.studentid=${student.studentid})
        </foreach>
    </delete>
</mapper>
