<?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.xjrsoft.module.base.mapper.BaseClassCourseMapper">
    <select id="getPage" parameterType="com.xjrsoft.module.base.dto.BaseClassCoursePageDto" resultType="com.xjrsoft.module.base.vo.BaseClassCoursePageVo">
        SELECT
        t.id                                          AS class_id,
        t.name                                        AS class_name,
        t1.name                                       AS teacher_name,
        t3.name                                       AS major_name,
        t4.name                                       AS dept_name,
        (SELECT GROUP_CONCAT(DISTINCT t6.name SEPARATOR '、')
        FROM base_class_course t5
        LEFT JOIN base_course_subject t6 ON t6.id = t5.course_id
        WHERE t5.class_id = t.id
        AND t5.delete_mark = 0
        <if test="dto.semester != null">
        AND t5.base_semester_id = #{dto.semester}
        </if>
        ) AS course_name,
        (SELECT GROUP_CONCAT(DISTINCT t7.book_name SEPARATOR '、')
        FROM base_class_course t5
        LEFT JOIN textbook t7 ON t7.id = t5.textbook_id
        WHERE t5.class_id = t.id
        AND t5.delete_mark = 0
        <if test="dto.semester != null">
        AND t5.base_semester_id = #{dto.semester}
        </if>
        ) AS textbook_name
        FROM base_class t
        LEFT JOIN xjr_user t1 ON t1.id = t.teacher_id
        LEFT JOIN base_class_major_set t2 ON t2.class_id = t.id
        LEFT JOIN base_major_set t3 ON t3.id = t2.major_set_id
        LEFT JOIN xjr_department t4 ON t4.id = t.org_id
        WHERE t.delete_mark = 0
        <if test="dto.className != null and dto.className != ''">
            and t.name like concat('%', #{dto.className}, '%')
        </if>
        <if test="dto.deptId != null">
            and t4.id = #{dto.deptId}
        </if>
        GROUP BY t.id, t.name, t1.name, t3.name, t4.name
        <if test="dto.courseSet == 1">
            HAVING LENGTH(course_name) > 0
        </if>
        <if test="dto.courseSet == 2">
            HAVING COALESCE(LENGTH(course_name), 0) = 0
        </if>
    </select>


    <select id="getAllCourseBook" resultType="com.xjrsoft.module.base.entity.CourseBookInfo">
        SELECT t.course_subject_id                    AS courseId,
        t1.name                  AS courseName,
        t.book_name              AS bookName,
        t.id                     AS bookId,
        concat(t.course_subject_id, '_', t.id) AS id,
        t.issn,
        t.editor_in_chief,
        t.version
        FROM textbook t
        LEFT JOIN base_course_subject t1 ON t.course_subject_id = t1.id
        WHERE t.delete_mark = 0
        AND t.course_subject_id IS NOT NULL
        <if test="subjectGroupId != null">
            AND t.subject_group_id = #{subjectGroupId}
        </if>
        <if test="semester != null">
            AND EXISTS (
            SELECT 1
            FROM textbook tb
            WHERE tb.id = t.id
            AND tb.base_semester_id = #{semester}
            )
        </if>
    </select>

    <select id="getSelectedCourseBook" resultType="com.xjrsoft.module.base.entity.CourseBookInfo">
        select
            t.course_id as courseId,
            t2.name as courseName,
            t1.book_name as bookName,
            t.textbook_id as bookId,
            concat(t.course_id, '_', t.textbook_id) AS id,
            t1.issn,
            t1.editor_in_chief,
            t1.version
        from base_class_course t
        left join textbook t1 on t1.id = t.textbook_id
        left join base_course_subject t2 on t.course_id = t2.id
        where t.delete_mark = 0
        <if test="classIds != null">
            AND t.class_id in
            <foreach item="classIds" collection="classIds" open="(" separator="," close=")">
                #{classIds}
            </foreach>
        </if>
        <if test="semester != null">
            and t.base_semester_id = #{semester}
        </if>
<!--        <if test="subjectGroupId != null">-->
<!--            AND-->
<!--        </if>-->
    </select>

    <insert id="updateAddClassCourseTextbooks">
        INSERT INTO base_class_course (class_id, course_id, textbook_id, create_date, delete_mark, enabled_mark)
        VALUES (#{classId}, #{courseId}, #{textbookId},now(),0,1)
            ON DUPLICATE KEY UPDATE
                                 class_id = #{classId},
                                 course_id = #{courseId},
                                 textbook_id = #{textbookId}

    </insert>

    <update id="updateRemoveClassCourseTextbooks">
        UPDATE base_class_course
        SET delete_mark = 1
        WHERE class_id = #{classId}
          AND course_id = #{courseId}
          AND textbook_id = #{textbookId}
    </update>

    <update id="markExistingRecordsAsDeleted" parameterType="map">
        UPDATE base_class_course
        SET delete_mark = 1
        WHERE class_id = #{newClassId} AND (course_id IS NULL OR textbook_id IS NULL)
    </update>

    <insert id="insertClassCourseTextbookCombinations" parameterType="map">
        INSERT INTO base_class_course (class_id, course_id, textbook_id, create_date, delete_mark, enabled_mark)
        SELECT #{newClassId}, t.course_id, t.textbook_id, NOW(), 0, 1
        FROM (SELECT course_id, textbook_id FROM base_class_course WHERE class_id = #{sourceClassId} AND delete_mark = 0
            <if test="semester != null">
                AND EXISTS (
                SELECT 1
                FROM textbook tb
                WHERE tb.id = textbook_id
                AND tb.base_semester_id = #{semester}
                )
            </if>
        GROUP BY
            course_id, textbook_id
        ) t
        ON DUPLICATE KEY UPDATE
        delete_mark = 0,
        enabled_mark = 1,
        create_date = NOW()
    </insert>

    <select id="getClassIdByName" resultType="java.lang.Long">
        select t.id
        from base_class t
        where t.delete_mark = 0
        <if test="name != null">
            AND t.name = #{name}
        </if>
    </select>

    <select id="getCourseIdByName" resultType="java.lang.Long">
        select t.id
        from base_course_subject t
        where t.delete_mark = 0
        <if test="name != null">
            AND t.name = #{name}
        </if>
    </select>

    <select id="getBookIdByName" resultType="java.lang.Long">
        select t.id
        from textbook t
        where t.delete_mark = 0
        <if test="name != null">
            AND t.book_name = #{name}
        </if>
    </select>

    <select id="getBaseSemesterIdByName" resultType="java.lang.Long">
        select t.id
        from base_semester t
        where t.delete_mark = 0
        <if test="name != null">
            AND t.name = #{name}
        </if>
    </select>

    <select id="checkExits" resultType="java.lang.Boolean">
        SELECT COUNT(1)
        from base_class_course
        WHERE delete_mark = 0 and class_id = #{classId}
          AND course_id = #{courseId}
          AND textbook_id = #{textbookId}
    </select>
    <select id="checkExitsWithoutTextbook" resultType="java.lang.Boolean">
        SELECT COUNT(1)
        from base_class_course
        WHERE delete_mark = 0 and class_id = #{classId}
          AND course_id = #{courseId}
    </select>
</mapper>