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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.teachcloud.common.entity.BasicCourse">
    <result column="id" property="id" />
        <result column="school_id" property="schoolId" />
        <result column="code" property="code" />
        <result column="name" property="name" />
        <result column="create_id" property="createId" />
        <result column="create_time" property="createTime" />
        <result column="update_id" property="updateId" />
        <result column="update_time" property="updateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        select id, school_id, code, name, create_id, create_time, update_id, update_time from basic_course
    </sql>
    <select id="listCoursesByUserId" resultMap="BaseResultMap">
        SELECT
            a.id, a.code, a.name
        FROM
            basic_course a
                JOIN
            basic_user_course b ON a.id = b.course_id
        WHERE
            b.user_id = #{id}
    </select>
    <select id="listPage" resultMap="BaseResultMap">
        <include refid="Base_Column_List"></include>
        <where>
            <if test="schoolId != null and schoolId != ''">
                and school_id = #{schoolId}
            </if>
            <if test="code != null and code != ''">
                and code like concat('%',#{code} ,'%')
            </if>
            <if test="name != null and name != ''">
                and name like concat('%',#{name} ,'%')
            </if>
        </where>
        order by create_time desc
    </select>
    <select id="findByUserLoginNameAndRealName"
            resultType="com.qmth.teachcloud.common.bean.dto.CourseInfoDto">
        SELECT
            bc.code AS courseCode, bc.name AS courseName
        FROM
            basic_course bc
                INNER JOIN
            basic_user_course buc ON buc.course_id = bc.id
                INNER JOIN
            sys_user su ON su.id = buc.user_id
        WHERE
            su.login_name = #{loginName}
          AND real_name = #{realName};
    </select>
    <select id="findBasicCoursePage" resultType="com.qmth.teachcloud.common.bean.result.BasicCourseResult">
        SELECT
            bc.id,
            MAX(bc.code) AS courseCode,
            MAX(bc.name) AS courseName,
            MAX(teaching_room_id) AS teachingRoomId,
            MAX(org.name) AS teachingRoomName,
            GROUP_CONCAT(clz.id
                         SEPARATOR ',') AS clazzIds,
            MAX(bc.enable) as enable,
            MAX(bc.create_id) AS createId,
            MAX(bc.create_time) AS createTime
        FROM
            basic_course bc
                INNER JOIN
            basic_user_course buc ON bc.id = buc.course_id
                LEFT JOIN
            basic_clazz clz ON buc.clazz_id = clz.id AND clz.enable = TRUE
                LEFT JOIN
            sys_org org ON bc.teaching_room_id = org.id
                AND org.type = 'TEACHING_ROOM'
                AND org.enable = TRUE
        <where>
            <if test="courseName != null and courseName.length > 0">
                AND bc.name LIKE CONCAT('%',#{courseName},'%')
            </if>
            <if test="startCreateTime != null and startCreateTime > 0">
                AND bc.create_time >= #{startCreateTime}
            </if>
            <if test="endCreateTime != null and endCreateTime > 0">
                AND #{endCreateTime} >= bc.create_time
            </if>
            <if test="enable != null">
                AND bc.enable = #{enable}
            </if>
            <if test="schoolId != null and schoolId > 0">
                and bc.school_id = #{schoolId}
            </if>
            <if test="orgIds != null and orgIds.size > 0">
                AND bc.teaching_room_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
        GROUP BY bc.id
        ORDER BY createTime DESC
    </select>
    <select id="findExamTaskByCourseCode" resultType="java.util.Map">
        SELECT
            course_code AS courseCode, course_name AS courseName
        FROM
            exam_task
        <where>
            AND school_id = #{schoolId}
            <if test="courseCodeSet != null">
                AND course_code IN
                <foreach collection="courseCodeSet" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

</mapper>
