<?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.qmth.teachcloud.common.mapper.BasicStudentMapper">

    <select id="listByClass" resultType="com.qmth.teachcloud.common.bean.dto.TaskPrintClassDto">
        SELECT
            s.clazz_id classId,
            clz.clazz_name className,
            COUNT(1) studentCount
        FROM
            basic_student s
                LEFT JOIN
            basic_clazz clz ON s.clazz_id = clz.id and clz.enable = true
        WHERE
            s.school_id = #{schoolId}
            <if test="stringList != null and stringList != ''">
                and s.clazz_id in
                <foreach collection="stringList" item="cla" open="(" close=")" separator=",">
                    #{cla}
                </foreach>
            </if>
        GROUP BY s.clazz_id
    </select>

    <select id="findBasicStudentPage"
            resultType="com.qmth.teachcloud.common.bean.result.BasicStudentResult">
        SELECT
            stu.id,
            student_name AS studentName,
            student_code AS studentCode,
            phone_number AS phoneNumber,
            stu.campus_id AS campusId,
            cam.campus_name AS campusName,
            clazz_id AS clazzId,
            clz.clazz_name AS clazz,
            stu.enable,
            stu.create_id AS createId,
            stu.create_time AS createTime
        FROM
            basic_student stu
                LEFT JOIN
            basic_campus cam ON stu.campus_id = cam.id
                AND cam.enable = TRUE
                LEFT JOIN
            basic_clazz clz ON stu.clazz_id = clz.id AND clz.enable = true
        <where>
            and stu.enable = true
            <if test="queryParams != null and queryParams != ''">
                and (student_name like concat('%',#{queryParams},'%') or student_code like concat('%',#{queryParams},'%'))
            </if>
            <if test="schoolId != null and schoolId > 0">
                and stu.school_id = #{schoolId}
            </if>
            <if test="campusId != null and campusId > 0">
                and stu.campus_Id = #{campusId}
            </if>
            <if test="clazzIdList != null">
                AND stu.clazz_id IN
                <foreach collection="clazzIdList" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="orgIds != null">
                AND stu.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY stu.create_time DESC
    </select>
</mapper>
