<?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.BasicSemesterMapper">

    <select id="findBasicSemesterPage" resultType="com.qmth.teachcloud.common.bean.result.BasicSemesterResult">
        SELECT
            id,
            name AS semesterName,
            code AS semesterCode,
            start_time AS startTime,
            end_time AS endTime,
            enable
        FROM
            basic_semester bs
        <where>
            <if test="schoolId != null and schoolId != ''">
                AND bs.school_id = #{schoolId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND bs.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND bs.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        ORDER BY bs.create_time DESC
    </select>
    <select id="countBasicExamBySemesterId" resultType="java.lang.Integer">
        SELECT
            COUNT(*)
        FROM
            basic_exam
        <where>
            <if test="semesterId != null and semesterId != ''">
                AND semester_id = #{semesterId}
            </if>
        </where>
    </select>
    <select id="selectByExamId" resultType="com.qmth.teachcloud.common.entity.BasicSemester">
        SELECT
            id, name, code, enable
        FROM
            basic_semester bs
        WHERE
            EXISTS( SELECT
                        1
                    FROM
                        basic_exam be
                    WHERE
                        bs.id = be.semester_id AND be.id = #{examId})
    </select>
    <select id="listBySemesterId" resultType="com.qmth.teachcloud.common.entity.BasicExam">
        SELECT
            id, name, enable
        FROM
            basic_exam
        WHERE
            school_id = #{schoolId} AND semester_id = #{semesterId}
        ORDER BY id
    </select>
</mapper>
