<?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.distributed.print.business.mapper.ConditionMapper">
    <select id="listCourse" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            bc.code code,
            bc.name name
        FROM
            basic_course bc
        <where>
            EXISTS( SELECT
                        1
                    FROM
                        exam_task et
                            LEFT JOIN
                        basic_exam be ON et.exam_id = be.id
                    <where>
                        bc.school_id = et.school_id
                      AND bc.code = et.course_code
                      <if test="semesterId != null">
                          AND be.semester_id = #{semesterId}
                      </if>
                        <if test="examId != null">
                            AND et.exam_id = #{examId}
                        </if>
                        <if test="dpr != null">
                            <if test="dpr.requestUserId != null">
                                AND et.create_id = #{dpr.requestUserId}
                            </if>
                            <if test="dpr.courseUserId != null">
                                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = et.course_code)
                            </if>
                            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                                AND et.org_id IN
                                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                                    #{item}
                                </foreach>
                            </if>
                        </if>
                    </where>)
            <if test="enable != null">
                and bc.enable = #{enable}
            </if>
        </where>
        order by bc.code
    </select>
    <select id="listPaperNumber" resultType="java.lang.String">
        SELECT
            distinct edc.paper_number
        FROM
            (select ed.id, ed.print_plan_id, ed.exam_id from exam_detail ed
                LEFT JOIN
            basic_exam be ON ed.exam_id = be.id
            <where>
                <if test="semesterId != null">
                    AND be.semester_id = #{semesterId}
                </if>
                AND ed.exam_id = #{examId}
                <if test="printPlanIds != null and printPlanIds != '' and printPlanIds.size > 0">
                    AND ed.print_plan_id IN
                    <foreach collection="printPlanIds" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </where>) a
                left join
            exam_detail_course edc on a.id = edc.exam_detail_id
                left join
            exam_task et on edc.school_id = et.school_id
                    and a.exam_id = et.exam_id
                    and edc.course_code = et.course_code
                    and edc.paper_number = et.paper_number
        <where>
            <if test="courseCode != null and courseCode != ''">
                AND edc.course_code = #{courseCode}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND et.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = et.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND et.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by edc.paper_number
    </select>
    <select id="listCourseForApply" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct et.course_code code,
            et.course_name name
        FROM
            exam_task et
        <where>
            et.enable = true
            <if test="examId != null">
                and et.exam_id = #{examId}
            </if>
            <if test="userId != null">
                and et.user_id = #{userId}
            </if>
        </where>
    </select>
    <select id="listCourseForAudit" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            bc.code code,
            bc.name name
        FROM
            basic_course bc
        <where>
            EXISTS( SELECT
            1
            FROM
                exam_task et
                    LEFT JOIN
                basic_exam be ON et.exam_id = be.id
            <where>
                AND bc.school_id = et.school_id
                AND bc.code = et.course_code
                <if test="semesterId != null">
                    AND be.semester_id = #{semesterId}
                </if>
                <if test="examId != null">
                    AND et.exam_id = #{examId}
                </if>
            </where>)
            <if test="enable != null">
                and bc.enable = #{enable}
            </if>
        </where>
        order by bc.code
    </select>
    <select id="listPaperNumberForApply" resultType="java.lang.String">
        SELECT
            et.paper_number paperNumber
        FROM
            exam_task et
        <where>
            et.enable = true
            <if test="examId != null">
                and et.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and et.course_code = #{courseCode}
            </if>
            <if test="userId != null">
                and et.user_id = #{userId}
            </if>
        </where>
        order by et.create_time desc
    </select>
    <select id="listPaperNumberAudit" resultType="java.lang.String">
        SELECT
            et.paper_number
        FROM
            exam_task et
            LEFT JOIN
            basic_exam be ON et.exam_id = be.id
        <where>
            <if test="semesterId != null">
                AND be.semester_id = #{semesterId}
            </if>
                AND et.exam_id = #{examId}
            <if test="courseCode != null and courseCode != ''">
                AND et.course_code = #{courseCode}
            </if>
        </where>
        order by et.paper_number
    </select>
    <select id="listPrintPlan" resultType="com.qmth.distributed.print.business.bean.result.PrintPlanBrief">
        select a.id, a.name, a.status from exam_print_plan a
            left join basic_exam be on a.exam_id = be.id
            left join sys_user b on a.create_id = b.id
        <where>
            <if test="semesterId != null">
                and be.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND b.id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND b.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listCourseForScore" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT DISTINCT
            mp.course_code code, mp.course_name name
        FROM
            mark_paper mp
                left join sys_user su ON mp.user_id = su.id
                left join basic_exam be ON mp.exam_id = be.id
        <where>
                and mp.status = #{status}
            <if test="semesterId != null">
                AND be.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                AND mp.exam_id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND mp.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = mp.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by mp.course_code
    </select>
    <select id="listClazz" resultType="com.qmth.teachcloud.common.entity.BasicClazz">
        SELECT DISTINCT
            tsess.clazz_id id, tsess.clazz_name clazzName
        FROM
            t_sync_exam_student_score tsess
                JOIN
            exam_task et ON tsess.school_id = et.school_id
                AND tsess.paper_number = et.paper_number
                JOIN
            basic_course bc ON et.school_id = bc.school_id
                AND et.course_code = bc.code
                LEFT JOIN
            teach_course tc ON bc.school_id = tc.school_id
                AND bc.code = tc.course_code
        <where>
            <if test="semesterId != null">
                AND tsess.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                AND et.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND et.course_code = #{courseCode}
            </if>
            <if test="clazzName != null and clazzName != ''">
                AND tsess.clazz_name like concat(#{clazzName}, '%')
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND tc.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND bc.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listPrintPlanForClient"
            resultType="com.qmth.distributed.print.business.bean.result.PrintPlanBrief">
        SELECT
            distinct epp.id, epp.name, epp.status
        FROM
            exam_print_plan epp
                JOIN
            exam_detail ed ON epp.id = ed.print_plan_id
                JOIN
            basic_exam be ON epp.exam_id = be.id
        <where>
            and be.enable = true
            <if test="semesterId != null">
                and be.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                and be.id = #{examId}
            </if>
            AND ed.status not IN
            <foreach collection="examDetailStatus" item="item" index="index" open="(" separator="," close=")">
                #{item}
            </foreach>
        </where>
    </select>
    <select id="listCourseForPlanTask" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct c.course_code code, c.course_name name
        FROM
        exam_print_plan a
        JOIN
        exam_detail b ON a.id = b.print_plan_id
        JOIN
        exam_detail_course c ON b.id = c.exam_detail_id
        left join exam_task et on b.exam_id = et.exam_id and c.course_code = et.course_code and c.paper_number = et.paper_number
        JOIN
        sys_user d ON et.user_id = d.id
        JOIN
        basic_exam e ON a.exam_id = e.id
        <where>
            <if test="semesterId != null and semesterId != ''">
                and e.semester_id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and e.id = #{examId}
            </if>
            <if test="printPlanIds != null and printPlanIds.size >0">
                and a.id in
                <foreach collection="printPlanIds" separator="," open="(" close=")" item="printPlanId">
                    #{printPlanId}
                </foreach>
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND et.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = c.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listPaperNumberPlanTask" resultType="java.lang.String">
        SELECT
            distinct c.paper_number
        FROM
        exam_print_plan a
        JOIN
        exam_detail b ON a.id = b.print_plan_id
        JOIN
        exam_detail_course c ON b.id = c.exam_detail_id
        left join exam_task et on b.exam_id = et.exam_id and c.course_code = et.course_code and c.paper_number = et.paper_number
        JOIN
        sys_user d ON et.user_id = d.id
        JOIN
        basic_exam e ON a.exam_id = e.id
        <where>
            <if test="semesterId != null and semesterId != ''">
                and e.semester_id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and e.id = #{examId}
            </if>
            <if test="printPlanIds != null and printPlanIds.size >0">
                and a.id in
                <foreach collection="printPlanIds" separator="," open="(" close=")" item="printPlanId">
                    #{printPlanId}
                </foreach>
            </if>
            <if test="courseCode != null and courseCode != ''">
                and c.course_code = #{courseCode}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND et.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = c.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listCourseForEntrance" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct mg.course_code code,
            mg.course_name name
        FROM
            mark_user_group mug
        LEFT JOIN
            mark_group mg ON mug.exam_id = mg.exam_id
            AND mug.paper_number = mg.paper_number
            AND mug.group_number = mg.number
        <where>
            mg.exam_id = #{examId}
            and mug.user_id = #{userId} and mg.task_count > 0
            <if test="status != null and status != ''">
                and exists (select 1 from mark_paper mp where mp.exam_id = mg.exam_id and mp.paper_number = mg.paper_number and mp.status = #{status})
            </if>
        </where>
        order by mg.course_code
    </select>
    <select id="listPaperNumberForEntrance" resultType="java.lang.String">
        SELECT
            distinct mg.paper_number paperNumber
        FROM
            mark_user_group mug
                LEFT JOIN
            mark_group mg ON mug.exam_id = mg.exam_id
                AND mug.paper_number = mg.paper_number
                AND mug.group_number = mg.number
        <where>
            mg.exam_id = #{examId}
            <if test="courseCode != null and courseCode != ''">
                and mg.course_code = #{courseCode}
            </if>
            and mug.user_id = #{userId} and mg.task_count > 0
            <if test="status != null and status != ''">
                and exists (select 1 from mark_paper mp where mp.exam_id = mg.exam_id and mp.paper_number = mg.paper_number and mp.status = #{status})
            </if>
        </where>
        order by mg.paper_number
    </select>
    <select id="listCourseFromMarkPaper" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct mp.course_code code,
            mp.course_name name
        FROM
            mark_paper mp
            LEFT JOIN sys_user su ON mp.user_id = su.id
        where mp.exam_id = #{examId}
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND mp.user_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.courseUserId != null">
                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = mp.course_code)
            </if>
            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                AND su.org_id IN
                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
    </select>
    <select id="listPaperNumberFromMarkPaper" resultType="java.lang.String">
        SELECT
            mp.paper_number paperNumber
        FROM
        mark_paper mp
        LEFT JOIN sys_user su ON mp.user_id = su.id
        where mp.exam_id = #{examId}
        <if test="courseCode != null and courseCode != ''">
            AND mp.course_code = #{courseCode}
        </if>
        <if test="dpr != null">
            <if test="dpr.requestUserId != null">
                AND mp.user_id = #{dpr.requestUserId}
            </if>
            <if test="dpr.courseUserId != null">
                AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = mp.course_code)
            </if>
            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                AND su.org_id IN
                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </if>
    </select>
    <select id="listCourseFromExamTaskByCreateId" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct a.course_code code,
            a.course_name name
        FROM
        exam_task a
        LEFT JOIN
        sys_user d ON a.create_id = d.id
        <where>
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND a.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = a.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listCourseFromExamTaskByUserId" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct a.course_code code,
            a.course_name name
        FROM
            exam_task a
                LEFT JOIN
            t_f_flow_approve g ON g.flow_id = a.flow_id
                LEFT JOIN
            sys_user d ON a.user_id = d.id
        <where>
            a.status = 'SUBMIT' AND (a.review = 0 OR (a.review = 1 AND g.status = 'FINISH'))
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND a.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = a.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listPaperNumberFromExamTaskByCreateId" resultType="java.lang.String">
        SELECT
            a.paper_number paperNumber
            FROM
            exam_task a
                LEFT JOIN
            sys_user d ON a.create_id = d.id
        <where>
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND a.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = a.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="listPaperNumberFromExamTaskByUserIdAndPass" resultType="java.lang.String">
        SELECT
            a.paper_number paperNumber
        FROM
            exam_task a
                LEFT JOIN
            t_f_flow_approve g ON g.flow_id = a.flow_id
                LEFT JOIN
            sys_user d ON a.user_id = d.id
        <where>
            a.status = 'SUBMIT' AND (a.review = 0 OR (a.review = 1 AND g.status = 'FINISH'))
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND a.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = a.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="listCourseForQuery" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct a.course_code code,
            a.course_name name
        FROM
            exam_task a
                LEFT JOIN
            t_f_flow_approve g ON g.flow_id = a.flow_id
                LEFT JOIN
            sys_user d ON a.create_id = d.id
        <where>
             a.status = 'SUBMIT' and (a.flow_id is null or (a.flow_id is not null and g.status = 'FINISH'))
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND a.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = a.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listPaperNumberForQuery" resultType="java.lang.String">
        SELECT
            a.paper_number paperNumber
        FROM
            exam_task a
                LEFT JOIN
            t_f_flow_approve g ON g.flow_id = a.flow_id
                LEFT JOIN
            sys_user d ON a.create_id = d.id
        <where>
            a.status = 'SUBMIT' and (a.flow_id is null or (a.flow_id is not null and g.status = 'FINISH'))
            <if test="examId != null">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND a.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = a.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND d.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by IFNULL(g.update_time, a.update_time) desc
    </select>
    <select id="listCourseForApproveHistory" resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            distinct et.course_code code,
            et.course_name name
        FROM
        t_f_flow_approve tffa
        JOIN
        t_f_custom_flow_entity tfcfe ON tfcfe.flow_id = tffa.flow_id
        JOIN
        exam_task et ON et.flow_id = tffa.flow_id
        LEFT JOIN
        basic_exam be ON et.exam_id = be.id
        LEFT JOIN
        basic_semester bs ON be.semester_id = bs.id
        LEFT JOIN
        sys_user su ON et.user_id = su.id
        <where>
            <if test="semesterId != null and semesterId != ''">
                and bs.id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and be.id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND et.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = et.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
            and tffa.enable = 1
            and et.enable = 1
        </where>
    </select>
    <select id="listPaperNumberForApproveHistory" resultType="java.lang.String">
        SELECT
            et.paper_number paperNumber
        FROM
            t_f_flow_approve tffa
            JOIN
            t_f_custom_flow_entity tfcfe ON tfcfe.flow_id = tffa.flow_id
            JOIN
            exam_task et ON et.flow_id = tffa.flow_id
            LEFT JOIN
            basic_exam be ON et.exam_id = be.id
            LEFT JOIN
            basic_semester bs ON be.semester_id = bs.id
            LEFT JOIN
            sys_user su ON et.user_id = su.id
        <where>
            <if test="semesterId != null">
                and bs.id = #{semesterId}
            </if>
            <if test="examId != null">
                and be.id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and et.course_code = #{courseCode}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND et.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = et.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
            and tffa.enable = 1
            and et.enable = 1
        </where>
    </select>
    <select id="listPaperNumberForScore" resultType="java.lang.String">
        SELECT DISTINCT
            mp.paper_number paperNumber
        FROM
        mark_paper mp
        left join sys_user su ON mp.user_id = su.id
        left join basic_exam be ON mp.exam_id = be.id
        <where>
            and mp.status = #{status}
            <if test="semesterId != null">
                AND be.semester_id = #{semesterId}
            </if>
            <if test="examId != null">
                AND mp.exam_id = #{examId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND mp.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.courseUserId != null">
                    AND EXISTS( SELECT 1 FROM (select course_code from teach_course where exam_id = #{examId} and user_id = #{dpr.courseUserId}) tc WHERE tc.course_code = mp.course_code)
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        order by mp.course_code
    </select>
    <select id="listProfessional" resultType="com.qmth.distributed.print.business.entity.BasicProfessional"
            parameterType="com.qmth.teachcloud.common.bean.dto.DataPermissionRule">
        select
            bp.id,
            bp.name,
            so.name orgName
        from basic_professional bp
        left join sys_org so on bp.org_id = so.id
        <where>
            <if test="schoolId != null">
                and bp.school_id = #{schoolId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND bp.create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND bp.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="listCultureProgram" resultType="com.qmth.distributed.print.business.entity.ObeCultureProgram">
        SELECT
            ocp.*
        FROM
            obe_culture_program ocp
        <where>
            <if test="professionalId != null">
                AND ocp.professional_id = #{professionalId}
            </if>
            AND EXISTS( SELECT
                        1
                    FROM
                        obe_course_outline oco
                            LEFT JOIN
                        sys_user su ON oco.user_id = su.id
                    WHERE
                        ocp.id = oco.culture_program_id
                        <if test="schoolId != null">
                            AND oco.school_id = #{schoolId}
                        </if>
                        <if test="dpr != null">
                            <if test="dpr.requestUserId != null">
                                AND oco.user_id = #{dpr.requestUserId}
                            </if>
                            <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                                AND su.org_id IN
                                <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                                    #{item}
                                </foreach>
                            </if>
                        </if>
                )
        </where>
    </select>


    <select id="listOutlineSemester"
            resultType="com.qmth.distributed.print.business.bean.result.DictionaryResult">
        SELECT
            id AS id, name AS name, code AS code
        FROM
            basic_semester
        <where>
            AND EXISTS( SELECT
            1
                FROM
            obe_course_outline oco
                LEFT JOIN
            obe_culture_program ocp ON oco.culture_program_id = ocp.id
                LEFT JOIN
            sys_user su ON oco.user_id = su.id
            WHERE
                oco.semester_id = basic_semester.id
            <if test="schoolId != null">
                AND oco.school_id = #{schoolId}
            </if>
            <if test="professionalId != null">
                AND ocp.professional_id = #{professionalId}
            </if>
            <if test="cultureProgramId != null">
                AND oco.culture_program_id = #{cultureProgramId}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND oco.user_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>

    <select id="listCultureProgramCourse"
            resultType="com.qmth.teachcloud.common.entity.BasicCourse">
        SELECT
            bc.*
        FROM
            basic_course bc
        <where>
             AND EXISTS( SELECT
                1
                    FROM
                obe_course_outline oco
                    LEFT JOIN
                obe_culture_program ocp ON oco.culture_program_id = ocp.id
                    LEFT JOIN
                sys_user su ON oco.user_id = su.id
                WHERE
                    bc.id = oco.course_id
                <if test="schoolId != null">
                    AND oco.school_id = #{schoolId}
                </if>
                <if test="professionalId != null">
                    AND ocp.professional_id = #{professionalId}
                </if>
                <if test="cultureProgramId != null">
                    AND oco.culture_program_id = #{cultureProgramId}
                </if>
                <if test="semesterId != null">
                    AND oco.semester_id = #{semesterId}
                </if>
                <if test="dpr != null">
                    <if test="dpr.requestUserId != null">
                        AND oco.user_id = #{dpr.requestUserId}
                    </if>
                    <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                        AND su.org_id IN
                        <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                            #{item}
                        </foreach>
                    </if>
                </if>
            )
        </where>
    </select>
</mapper>