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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.qmth.distributed.print.business.entity.ExamTask">
        <result column="id" property="id"/>
        <result column="school_id" property="schoolId"/>
        <result column="course_code" property="courseCode"/>
        <result column="course_name" property="courseName"/>
        <result column="paper_number" property="paperNumber"/>
        <result column="sequence" property="sequence"/>
        <result column="specialty" property="specialty"/>
        <result column="start_time" property="startTime"/>
        <result column="end_time" property="endTime"/>
        <result column="card_rule_id" property="cardRuleId"/>
        <result column="user_id" property="userId"/>
        <result column="status" property="status"/>
        <result column="review_status" property="reviewStatus"/>
        <result column="enable" property="enable"/>
        <result column="batch_no" property="batchNo"/>
        <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">
        id, school_id, course_code, course_name, specialty, paper_number,`sequence`, start_time, end_time, card_rule_id, user_id, status, review_status, enable, batch_no, create_id, create_time, update_id, update_time
    </sql>
    <select id="listPage" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDto">
        SELECT
        a.id,
        a.school_id schoolId,
        a.course_code courseCode,
        a.course_name courseName,
        a.sequence,
        a.specialty,
        a.paper_number paperNumber,
        a.card_rule_id cardRuleId,
        case a.card_rule_id when -1 then '全部通卡' else b.name end cardRuleName,
        a.user_id userId,
        f.id as examId,
        f.name as examName,
        g.id as semesterId,
        g.name as semesterName,
        c.real_name userName,
        d.real_name createName,
        a.start_time startTime,
        a.end_time endTime,
        CASE
        WHEN
        a.status = 'SUBMIT'
        AND (e.status = 'START'
        OR e.status = 'AUDITING')
        THEN
        'AUDITING'
        WHEN
        a.status = 'SUBMIT'
        AND e.status = 'REJECT'
        THEN
        'REJECT'
        WHEN
        a.status = 'SUBMIT'
        AND (e.status = 'FINISH'
        OR e.status is null)
        THEN
        'FINISH'
        ELSE
        a.status
        END status,
        a.enable,
        a.create_id createId,
        a.create_time createTime
        FROM
        exam_task a
        LEFT JOIN
        basic_card_rule b ON a.card_rule_id = b.id
        LEFT JOIN
        sys_user c ON a.user_id = c.id
        LEFT JOIN
        sys_user d ON a.create_id = d.id
        LEFT JOIN
        t_f_flow_approve e ON a.flow_id = e.flow_id
        LEFT JOIN
        basic_exam f ON a.exam_id = f.id
        LEFT JOIN
        basic_semester g ON f.semester_id = g.id
        <where>
            and f.enable = true
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="examId != null and examId != ''">
                and f.id = #{examId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and g.id = #{semesterId}
            </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>
            <if test="enable != null">
                and a.enable = #{enable}
            </if>
            <if test="status != null and status != ''">
                <choose>
                    <when test="status == 'AUDITING'">
                        and a.status = 'SUBMIT' AND (e.status = 'START' OR e.status = 'AUDITING')
                    </when>
                    <when test="status == 'REJECT'">
                        and a.status = 'SUBMIT' AND e.status = 'REJECT'
                    </when>
                    <when test="status == 'FINISH'">
                        and a.status = 'SUBMIT' AND (e.status = 'FINISH' OR e.status is null)
                    </when>
                    <otherwise>
                        and a.status = #{status}
                    </otherwise>
                </choose>
            </if>
            <if test="cardRuleId != null and cardRuleId != ''">
                and a.card_rule_id = #{cardRuleId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and a.paper_number = #{paperNumber}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.start_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.start_time &lt;= #{endTime}
            </if>
            <if test="userName != null and userName != ''">
                and c.real_name like concat(#{userName},'%')
            </if>
            <if test="createName != null and createName != ''">
                and d.real_name like concat(#{createName},'%')
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="listUser" resultType="com.qmth.teachcloud.common.bean.dto.BlurryUserDto">
        SELECT
        distinct a.user_id id, b.real_name name
        FROM
        exam_task a
        LEFT JOIN
        sys_user b ON a.user_id = b.id
        <where>
            b.enable = true
            <if test="schoolId != null and schoolId != ''">
                and b.school_id = #{schoolId}
            </if>
            <if test="param != null and param != ''">
                and b.real_name like concat('%', #{param}, '%')
            </if>
            <if test="orgIds != null and orgIds != '' and orgIds.size > 0">
                AND a.org_id IN
                <foreach collection="orgIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>
    <select id="listTaskApply" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDto">
        SELECT
            a.id,
            a.school_id schoolId,
            a.course_code courseCode,
            a.course_name courseName,
            a.specialty,
            a.paper_number paperNumber,
            a.card_rule_id cardRuleId,
            CASE a.card_rule_id
            WHEN - 1 THEN '全部通卡'
            ELSE b.name
            END cardRuleName,
            a.user_id userId,
            c.real_name userName,
            a.start_time startTime,
            a.end_time endTime,
            a.status,
            a.teacher_name teacherName,
            a.lecturer_name lecturerName,
            d.name teachingRoomName,
            e.status  auditStatus,
            a.enable,
            a.review,
            a.create_id createId,
            a.create_time createTime,
            e.setup,
            a.flow_id flowId,
            a.exam_id examId,
            f.name examName,
            f.semester_id semesterId,
            f.exam_model examModel,
            g.name semesterName,
            (select count(1) from t_f_flow_log tffl where tffl.flow_id = e.flow_id and tffl.approve_operation <![CDATA[ <> ]]> 'EXCHANGE') as approveCount,
            tffj.type as flowType
        FROM
            exam_task a
        LEFT JOIN
            basic_card_rule b ON a.card_rule_id = b.id
        LEFT JOIN
            sys_user c ON a.user_id = c.id
        LEFT JOIN
            sys_org d ON a.teaching_room_id = d.id
        LEFT JOIN
            t_f_flow_approve e ON a.flow_id = e.flow_id
        LEFT JOIN
            basic_exam f on a.school_id = f.school_id and a.exam_id = f.id
        LEFT JOIN
            basic_semester g on f.school_id = g.school_id and f.semester_id = g.id
        LEFT JOIN t_f_flow_join tffj on tffj.flow_id = e.flow_id
        <where>
            a.enable = true
            and f.enable = true
            <if test="userId != null and userId != ''">
                and a.user_id = #{userId}
            </if>
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and g.id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and f.id = #{examId}
            </if>
            <if test="auditStatus != null">
                and e.status = #{auditStatus}
            </if>
            <if test="cardRuleId != null and cardRuleId != ''">
                and a.card_rule_id = #{cardRuleId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and a.paper_number = #{paperNumber}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.start_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.start_time &lt;= #{endTime}
            </if>
            <if test="userName != null and userName != ''">
                and c.real_name like concat('%',#{userName},'%')
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="listTaskReviewUnaudited" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDto">
        select
        t.*
        from
        (SELECT distinct
        a.id,
        a.school_id schoolId,
        a.course_code courseCode,
        a.course_name courseName,
        a.specialty,
        a.paper_number paperNumber,
        a.card_rule_id cardRuleId,
        case a.card_rule_id when -1 then '全部通卡' else b.name end cardRuleName,
        a.user_id userId,
        c.real_name userName,
        d.real_name createName,
        a.start_time startTime,
        a.end_time endTime,
        a.enable,
        a.create_id createId,
        a.create_time createTime,
        a.status,
        art.PROC_INST_ID_ flowId,
        f.setup,
        f.status as auditStatus,
        f.status as flowStatus,
        a.teacher_name teacherName,
        a.lecturer_name lecturerName,
        so.name teachingRoomName,
        a.exam_id examId,
        be.name examName,
        bs.id semesterId,
        bs.name semesterName,
        true as myself
        FROM ACT_RU_TASK art
        left join t_f_flow_join tffj on
        tffj.flow_id = art.PROC_INST_ID_
        left join exam_task a on
            a.id = tffj.object_id
        left join act_ru_identitylink ari on
            ari.TASK_ID_ = art.ID_
        LEFT JOIN
        basic_card_rule b ON a.card_rule_id = b.id
        LEFT JOIN
        sys_user c ON a.user_id = c.id
        LEFT JOIN
        sys_user d ON a.create_id = d.id
        LEFT JOIN
        exam_task_paper_log e ON a.id = e.exam_task_id AND e.review = false
        LEFT JOIN
        t_f_flow_approve f ON a.school_id = f.school_id and a.flow_id = f.flow_id
        LEFT JOIN
        sys_org so ON a.teaching_room_id = so.id
        LEFT JOIN
            basic_exam be on a.school_id = be.school_id and a.exam_id = be.id
        LEFT JOIN
            basic_semester bs on be.school_id = bs.school_id and be.semester_id = bs.id
        <where>
            a.enable = true
            AND a.review = true
            and EXISTS(
            select * from
            (<include refid="findFlowIdToMySelfSql" />) t where t.flowId = art.PROC_INST_ID_)
            and f.status <![CDATA[ <> ]]> 'START' and f.setup > 1
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and bs.id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and be.id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and a.paper_number = #{paperNumber}
            </if>
            <if test="userId != null and userId != ''">
                and a.user_id = #{userId}
            </if>
            <if test="cardRuleId != null and cardRuleId != ''">
                and a.card_rule_id = #{cardRuleId}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.start_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.start_time &lt;= #{endTime}
            </if>
            <if test="startCreateTime != null and startCreateTime != ''">
                and a.create_time >= #{startCreateTime}
            </if>
            <if test="endCreateTime != null and endCreateTime != ''">
                and #{endCreateTime} >= a.create_time
            </if>
            <if test="createName != null and createName != ''">
                and d.real_name like concat('%',#{createName},'%')
            </if>
        </where>
        ) t
        order by t.id desc
    </select>
    <select id="listTaskReviewAudited" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDto">
        SELECT
        distinct a.id,
        a.school_id schoolId,
        a.course_code courseCode,
        a.course_name courseName,
        a.specialty,
        a.paper_number paperNumber,
        a.card_rule_id cardRuleId,
        case a.card_rule_id when -1 then '全部通卡' else b.name end cardRuleName,
        a.user_id userId,
        c.real_name userName,
        d.real_name createName,
        a.start_time startTime,
        a.end_time endTime,
        f.status auditStatus,
        a.review_status reviewStatus,
        a.enable,
        a.create_id createId,
        a.create_time createTime,
        a.status,
        a.flow_id flowId,
        f.setup,
        f.status as flowStatus,
        a.teacher_name teacherName,
        a.lecturer_name lecturerName,
        e.name teachingRoomName,
        a.exam_id examId,
        be.name examName,
        bs.id semesterId,
        bs.name semesterName
        FROM
        exam_task a
        left join t_f_flow_join tffj on
            a.id = tffj.object_id
        LEFT JOIN
        basic_card_rule b ON a.card_rule_id = b.id
        LEFT JOIN
        sys_user c ON a.user_id = c.id
        LEFT JOIN
        sys_user d ON a.create_id = d.id
        LEFT JOIN t_f_flow_approve f ON
            a.school_id = f.school_id and a.flow_id = f.flow_id
        LEFT JOIN
            sys_org e ON a.teaching_room_id = e.id
        LEFT JOIN
            basic_exam be on a.school_id = be.school_id and a.exam_id = be.id
        LEFT JOIN
            basic_semester bs on be.school_id = bs.school_id and be.semester_id = bs.id
        <where>
            a.enable = true and a.review = true and be.enable = true
            and (f.status = 'AUDITING' or f.status = 'REJECT' or f.status = 'FINISH')
            and EXISTS (
            select * from(select
            aht.PROC_INST_ID_
            from
            ACT_HI_TASKINST aht
            where
            aht.TASK_DEF_KEY_ <![CDATA[ <> ]]> 'usertask_default'
            and aht.ASSIGNEE_ = cast(#{currentUserId} as char)
            and aht.END_TIME_ is not null
            union all
            select
            t.PROC_INST_ID_
            from
            (
            select
            ahi.*
            from
            ACT_HI_IDENTITYLINK ahi
            where
            ahi.TYPE_ = 'candidate'
            and exists(
            select
            distinct aht.id_
            from
            ACT_HI_TASKINST aht
            where
            aht.TASK_DEF_KEY_ <![CDATA[ <> ]]> 'usertask_default'
            and aht.ASSIGNEE_ is null
            and aht.END_TIME_ is not null
            and ahi.TASK_ID_ = aht.id_)
            and ahi.USER_ID_ = cast(#{currentUserId} as char)) temp
            join ACT_HI_TASKINST t on
                t.ID_ = temp.TASK_ID_
            join t_f_flow_join tffj on
                tffj.flow_id = t.PROC_INST_ID_) temp where temp.PROC_INST_ID_ = tffj.flow_id)
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and bs.id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and a.exam_id = #{examId}
            </if>
            <if test="reviewStatus != null and reviewStatus != ''">
                and a.review_status = #{reviewStatus}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and a.paper_number = #{paperNumber}
            </if>
            <if test="userId != null and userId != ''">
                and a.user_id = #{userId}
            </if>
            <if test="cardRuleId != null and cardRuleId != ''">
                and a.card_rule_id = #{cardRuleId}
            </if>
            <if test="startTime != null and startTime != ''">
                and a.start_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.start_time &lt;= #{endTime}
            </if>
            <if test="startCreateTime != null and startCreateTime != ''">
                and a.create_time >= #{startCreateTime}
            </if>
            <if test="endCreateTime != null and endCreateTime != ''">
                and #{endCreateTime} >= a.create_time
            </if>
            <if test="createName != null and createName != ''">
                and d.real_name like concat('%',#{createName},'%')
            </if>
        </where>
        order by a.create_time desc
    </select>
    <select id="listTaskPaper" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDetailDto">
        SELECT
        a.id,
        a.school_id schoolId,
        be.name examName,
        bs.name semesterName,
        a.course_code courseCode,
        a.course_name courseName,
        a.specialty,
        a.paper_number paperNumber,
        a.user_id userId,
        a.card_rule_id cardRuleId,
        c.real_name userName,
        d.real_name createName,
        a.start_time startTime,
        a.end_time endTime,
        a.create_id createId,
        IFNULL(g.update_time, a.update_time) createTime,
        b.enable,
        b.paper_type paperType,
        b.exposed_paper_type exposedPaperType,
        b.unexposed_paper_type unexposedPaperType,
        CASE a.card_rule_id
        WHEN - 1 THEN '全部通卡'
        ELSE e.name
        END cardRuleName,
        -- f.make_method makeMethod,
        g.setup
        FROM
        exam_task a
        LEFT JOIN
        exam_task_detail b ON a.id = b.exam_task_id
        LEFT JOIN
        t_f_flow_approve g ON g.flow_id = a.flow_id
        LEFT JOIN
        sys_user c ON a.user_id = c.id
        LEFT JOIN
        sys_user d ON a.create_id = d.id
        LEFT JOIN
        basic_card_rule e ON a.card_rule_id = e.id
        -- JOIN exam_card f ON FIND_IN_SET(f.id, REPLACE(REPLACE(substring_index(substring_index(CONVERT((b.paper_attachment_ids ->>'$[*].cardId') USING utf8), ']', 1), '[',-1),' "','"'),'"',''))
        LEFT JOIN
        basic_exam be ON a.exam_id = be.id
        LEFT JOIN
        basic_semester bs ON be.semester_id = bs.id
        <where>
            and be.enable = true and (a.status = 'SUBMIT' and (a.flow_id is null or (a.flow_id is not null and g.status = 'FINISH')))
            <if test="schoolId != null and schoolId != ''">
                and a.school_id = #{schoolId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and be.semester_id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and a.paper_number = #{paperNumber}
            </if>
            <if test="startTime != null and startTime != ''">
                AND if(g.update_time != '',g.update_time >= #{startTime},a.update_time >= #{startTime})
            </if>
            <if test="endTime != null and endTime != ''">
                AND if(g.update_time != '',#{endTime} >= g.update_time,#{endTime} >= a.update_time)
            </if>
            <if test="containsQuestionTeacher">
                and a.user_id = #{userId}
            </if>
            <if test="cardRuleId != null and cardRuleId != ''">
                and a.card_rule_id = #{cardRuleId}
            </if>
            <if test="makeMethod != null">
                and f.make_method = #{makeMethod}
            </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 a.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, a.paper_number desc
    </select>
    <select id="listPaperNumbers" resultType="com.qmth.distributed.print.business.bean.dto.RelatePaperDto">
        SELECT
        id,
        school_id schoolId,
        course_code courseCode,
        course_name courseName,
        paper_number paperNumber
        FROM
        exam_task
        <where>
            school_id = #{schoolId} and course_code = #{courseCode} and status = #{status} and enable = true
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND create_id = #{dpr.requestUserId}
                </if>
                <if test="dpr.orgIdSet != null and dpr.orgIdSet != '' and dpr.orgIdSet.size > 0">
                    AND org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
    </select>
    <select id="applyGetOne" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDetailCardDto">
        SELECT
            e.id examTaskId,
            e.exam_id examId,
            e.status taskStatus,
            e.cancel_remark cancelRemark,
            g.name examName,
            g.exam_model examModel,
            g.category,
            h.name semesterName,
            case #{source} when 'REVIEW' then ifnull(c.paper_type, a.paper_type) else a.paper_type end paperType,
            case #{source} when 'REVIEW' then ifnull(c.paper_attachment_ids, a.paper_attachment_ids) else a.paper_attachment_ids end paperAttachmentIds,
            a.paper_confirm_attachment_ids paperConfirmAttachmentIds,
            a.remark,
            a.exposed_paper_type exposedPaperType,
            ifnull(d.make_method, a.make_method) makeMethod,
            a.draw_count drawCount,
            e.flow_id flowId,
            f.setup,
            f.status as flowStatus
        FROM
            exam_task e
                left JOIN
            exam_task_detail a on a.exam_task_id = e.id
                left JOIN
            exam_task_paper_log c ON a.exam_task_id = c.exam_task_id and c.review = false
                left join
            exam_card d on c.card_id = d.id
                left join
            t_f_flow_approve f on e.flow_id = f.flow_id
                left join
            basic_exam g on e.exam_id = g.id
                left join
            basic_semester h on g.semester_id = h.id
        where e.id = #{examTaskId}
    </select>

    <select id="listExamTaskExpire" resultMap="BaseResultMap">
        select * from exam_task a
        <where>
            and a.enable = true
            <if test="status != null and status != ''">
                and a.status in
                <foreach item="item" collection="status" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
            <if test="startTime != null and startTime != ''">
                and a.end_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.end_time &lt; #{endTime}
            </if>
        </where>
    </select>
    <select id="listExamTaskAuditExpire" resultMap="BaseResultMap">
        SELECT
        *
        FROM
        exam_task a
        LEFT JOIN
        exam_task_paper_log b ON a.id = b.exam_task_id
        AND b.review = FALSE
        <where>
            and a.enable = true
            and a.review = true
            and ((a.status = 'SUBMIT' and a.review_status is null) or (a.status = 'FINISH' and b.id is not null ))
            <if test="startTime != null and startTime != ''">
                and a.end_time &gt;= #{startTime}
            </if>
            <if test="endTime != null and endTime != ''">
                and a.end_time &lt; #{endTime}
            </if>
        </where>
    </select>

    <sql id="findAllFlowId">
        (select
			DISTINCT t.PARENT_ID_
		from
			ACT_RU_EXECUTION t
		where
			t.ROOT_PROC_INST_ID_ = et.flow_id
			and
			t.PARENT_ID_ is not null
		)
    </sql>

    <sql id="findAllFlowIdByParam">
        (select
			DISTINCT t.PARENT_ID_
		from
			ACT_RU_EXECUTION t
		where
			t.ROOT_PROC_INST_ID_ = #{flowId}
			and
			t.PARENT_ID_ is not null
		)
    </sql>

    <select id="getFlowTaskReadyList" resultType="com.qmth.distributed.print.business.bean.result.WorkResult">
          select t.* from(select
                et.id,
                et.school_id schoolId,
                et.org_id as orgId,
                et.course_code courseCode,
                et.course_name courseName,
                et.specialty,
                et.paper_number paperNumber,
                et.card_rule_id cardRuleId,
                case
                    et.card_rule_id when -1 then '全部通卡'
                    else b.name
                end cardRuleName,
                cast(art.ASSIGNEE_ as Decimal(24)) as userId,
                c.real_name userName,
                et.start_time startTime,
                et.end_time endTime,
                tffa.status,
                tffa.status auditStatus,
                et.enable,
                et.create_time as createTime,
                et.update_time as updateTime,
                art.EXECUTION_ID_ as executionId,
                art.PROC_INST_ID_ as flowId,
                art.NAME_ as taskName,
                art.TASK_DEF_KEY_ as taskDefKey,
                art.ID_ as taskId,
                tffa.setup,
                et.teacher_name teacherName,
                et.lecturer_name lecturerName,
                d.real_name as propositionName,
                so.name teachingRoomName,
                et.exam_id as examId
            from
                ACT_RU_TASK art
            left join t_f_flow_join tffj on
                tffj.flow_id = art.PROC_INST_ID_
            left join exam_task et on
                et.id = tffj.object_id
            left join basic_card_rule b ON
                et.card_rule_id = b.id
            left join t_f_flow_approve tffa on
                tffa.school_id = et.school_id and tffa.flow_id = et.flow_id
            left join sys_user c ON
                art.ASSIGNEE_ = c.id
            LEFT JOIN sys_user d ON
                et.user_id= d.id
            LEFT JOIN
                sys_org so ON et.teaching_room_id = so.id
            LEFT JOIN exam_task_detail det ON
                det.exam_task_id = et.id
            LEFT JOIN
                basic_exam be ON et.exam_id = be.id
            LEFT JOIN
                basic_semester bs ON be.semester_id = bs.id
            where
                et.enable = true and be.enable = true and bs.enable = true
                and art.ASSIGNEE_ = cast(#{userId} as char)
                and et.school_id = #{schoolId}
            UNION all
                    select
                et.id,
                et.school_id schoolId,
                et.org_id as orgId,
                et.course_code courseCode,
                et.course_name courseName,
                et.specialty,
                et.paper_number paperNumber,
                et.card_rule_id cardRuleId,
                case
                    et.card_rule_id when -1 then '全部通卡'
                    else b.name
                end cardRuleName,
                cast(ari.USER_ID_ as Decimal(24)) as userId,
                c.real_name userName,
                et.start_time startTime,
                et.end_time endTime,
                tffa.status,
                tffa.status auditStatus,
                et.enable,
                et.create_time as createTime,
                et.update_time as updateTime,
                art.EXECUTION_ID_ as executionId,
                art.PROC_INST_ID_ as flowId,
                art.NAME_ as taskName,
                art.TASK_DEF_KEY_ as taskDefKey,
                art.ID_ as taskId,
                tffa.setup,
                et.teacher_name teacherName,
                et.lecturer_name lecturerName,
                d.real_name as propositionName,
                so.name teachingRoomName,
                et.exam_id as examId
            from
                ACT_RU_TASK art
            left join t_f_flow_join tffj on
                tffj.flow_id = art.PROC_INST_ID_
            left join exam_task et on
                et.id = tffj.object_id
            left join act_ru_identitylink ari on
                ari.TASK_ID_ = art.ID_
            left join basic_card_rule b ON
                et.card_rule_id = b.id
            left join t_f_flow_approve tffa on
                tffa.school_id = et.school_id and tffa.flow_id = et.flow_id
            left join sys_user c ON
                ari.USER_ID_ = c.id
            LEFT JOIN sys_user d ON
                    et.user_id= d.id
            LEFT JOIN
                sys_org so ON et.teaching_room_id = so.id
            LEFT JOIN exam_task_detail det ON
                    det.exam_task_id = et.id
            LEFT JOIN
                basic_exam be ON et.exam_id = be.id
            LEFT JOIN
                basic_semester bs ON be.semester_id = bs.id
            where
                  et.enable = true and be.enable = true and bs.enable = true
              and art.ASSIGNEE_ is null
                and ari.USER_ID_ = cast(#{userId} as char)
                and ari.TYPE_ = 'candidate'
                and et.school_id = #{schoolId}
              UNION all
          select
              et.id,
              et.school_id schoolId,
              et.org_id as orgId,
              et.course_code courseCode,
              et.course_name courseName,
              et.specialty,
              et.paper_number paperNumber,
              et.card_rule_id cardRuleId,
              case
                  et.card_rule_id when -1 then '全部通卡'
                                  else b.name
                  end cardRuleName,
              null as userId,
              null userName,
              et.start_time startTime,
              et.end_time endTime,
              null status,
              null auditStatus,
              et.enable,
              et.create_time as createTime,
              et.update_time as updateTime,
              null as executionId,
              null as flowId,
              null as taskName,
              null as taskDefKey,
              null as taskId,
              null setup,
              et.teacher_name teacherName,
              et.lecturer_name lecturerName,
              d.real_name as propositionName,
              so.name teachingRoomName,
              et.exam_id as examId
            from
                 exam_task et
                  left join basic_card_rule b ON
                  et.card_rule_id = b.id
                  LEFT JOIN sys_user d ON
                  et.user_id= d.id
                  LEFT JOIN
                  sys_org so ON et.teaching_room_id = so.id
                  LEFT JOIN exam_task_detail det ON
                  det.exam_task_id = et.id
                  LEFT JOIN
                 basic_exam be ON et.exam_id = be.id
                  LEFT JOIN
                 basic_semester bs ON be.semester_id = bs.id
          where
              et.enable = true and be.enable = true and bs.enable = true
            AND et.status = 'STAGE' AND et.flow_id is null
            AND et.user_id = #{userId}
            and et.school_id = #{schoolId} ) t  order by t.updateTime desc
    </select>

    <sql id="findFlowIdToMySelfSql">
        select cast(art.PROC_INST_ID_ as Decimal(24)) as flowId
        from
            ACT_RU_TASK art
        where
             art.ASSIGNEE_ = cast(#{currentUserId} as char)
        UNION all
          select
            art.PROC_INST_ID_ as flowId
        from
            ACT_RU_TASK art
        left join act_ru_identitylink ari on
            ari.TASK_ID_ = art.ID_
        where
            art.ASSIGNEE_ is null
            and ari.USER_ID_ = cast(#{currentUserId} as char)
            and ari.TYPE_ = 'candidate'
    </sql>

    <select id="findExamTaskByFlowStatus" resultType="com.qmth.distributed.print.business.entity.ExamTask">
        select
        et.*
        from
        exam_task et
        left join t_f_flow_approve tffa on
        tffa.flow_id = et.flow_id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and et.school_id = #{schoolId}
            </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="paperNumber != null and paperNumber != ''">
                and et.paper_number = #{paperNumber}
            </if>
            <if test="flowStatus != null and flowStatus != ''">
                and tffa.status = #{flowStatus}
            </if>
        </where>
        order by et.paper_number asc
    </select>
    <select id="findExamPackageDetailByPaperNumber"
            resultType="com.qmth.distributed.print.business.bean.dto.approvalForm.ExamPackageDetail">
        SELECT
            ed.id AS examDetailId,
            ed.exam_start_time AS examStartTime,
            ed.exam_end_time AS examEndTime,
            ed.exam_place AS examPlace,
            ed.exam_room AS examRoom,
            ed.package_code AS packageCode,
            ed.status AS examDetailStatus,
            IFNULL((select t.name from sys_org t where t.id = ed.print_house_id),'-') as printHouseName,
            edc.id AS examDetailCourseId,
            edc.course_code AS courseCode,
            edc.course_name AS courseName,
            edc.paper_number AS paperNumber,
            edc.total_subjects AS examDetailCourseTotalSubjects,
            edc.clazz_id AS clazzIds,
            epp.id AS examPrintPlanId,
            epp.name AS examPrintPlanName,
            epp.print_content AS printContent,
            epp.backup_method AS backupMethod,
            ed.backup_count AS backupCount,
            epp.status AS examPrintPlanStatus
        FROM
            exam_detail ed
                INNER JOIN
            exam_detail_course edc ON edc.exam_detail_id = ed.id
                INNER JOIN
            exam_print_plan epp ON epp.id = ed.print_plan_id
        <where>
            <if test="schoolId != null and schoolId != ''">
                AND ed.school_id = #{schoolId}
            </if>
            <if test="examId != null and examId != ''">
                AND epp.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                AND edc.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                AND edc.paper_number = #{paperNumber}
            </if>
            <if test="printPlanStatusEnumList != null and printPlanStatusEnumList != '' and printPlanStatusEnumList.size > 0">
                and epp.status in
                <foreach item="item" collection="printPlanStatusEnumList" separator="," open="(" close=")" index="">
                    #{item}
                </foreach>
            </if>
        </where>
    </select>

    <select id="findByFlowId" resultType="com.qmth.distributed.print.business.entity.ExamTask">
        select * from exam_task et where
            EXISTS(
            select tffj.object_id from t_f_flow_join tffj
            where tffj.flow_id = #{flowId} and tffj.object_id = et.id
            )
    </select>

    <select id="getByExamTaskId" resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDto">
        SELECT
            a.id,
            a.school_id schoolId,
            a.course_code courseCode,
            a.course_name courseName,
            a.specialty,
            a.paper_number paperNumber,
            a.card_rule_id cardRuleId,
            CASE a.card_rule_id
            WHEN - 1 THEN '全部通卡'
            ELSE b.name
            END cardRuleName,
            a.user_id userId,
            c.real_name userName,
            a.start_time startTime,
            a.end_time endTime,
            a.status,
            a.teacher_name teacherName,
            a.lecturer_name lecturerName,
            d.name teachingRoomName,
            e.status  auditStatus,
            a.enable,
            a.review,
            a.create_id createId,
            a.create_time createTime,
            e.setup,
            a.flow_id flowId
        FROM
            exam_task a
        LEFT JOIN
            basic_card_rule b ON a.card_rule_id = b.id
        LEFT JOIN
            sys_user c ON a.user_id = c.id
        LEFT JOIN
            sys_org d ON a.teaching_room_id = d.id
        LEFT JOIN
            t_f_flow_approve e ON a.flow_id = e.flow_id
        where a.id = #{examTaskId}
    </select>
    <select id="listExamTaskDetailDownload"
            resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDetailDto">
        SELECT
            a.id,
            be.name examName,
            bs.name semesterName,
            a.course_code courseCode,
            a.course_name courseName,
            a.paper_number paperNumber,
            b.enable,
            b.paper_type paperType,
            b.exposed_paper_type exposedPaperType,
            b.unexposed_paper_type unexposedPaperType,
            a.user_id userId,
            su.login_name loginName,
            su.real_name userName,
            so.name orgName
        FROM
            exam_task a
                LEFT JOIN
            exam_task_detail b ON a.id = b.exam_task_id
                LEFT JOIN
            basic_course bc ON a.school_id = bc.school_id and a.course_code = bc.code
                LEFT JOIN
            sys_org so ON bc.teaching_room_id = so.id
                LEFT JOIN
            basic_card_rule e ON a.card_rule_id = e.id
                LEFT JOIN
            basic_exam be ON a.exam_id = be.id
                LEFT JOIN
            basic_semester bs ON be.semester_id = bs.id
                LEFT JOIN
            t_f_flow_approve g ON g.flow_id = a.flow_id
                LEFT JOIN
            sys_user su ON a.user_id = su.id
        <where>
            a.status = 'SUBMIT'
          AND (a.review = 0
            OR (a.review = 1 AND g.status = 'FINISH'))
            <if test="examId != null and examId != ''">
                and a.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and a.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and a.paper_number = #{paperNumber}
            </if>
            <if test="orgId != null">
                and bc.teaching_room_id = #{orgId}
            </if>
            <if test="userParam != null and userParam != ''">
                and (su.login_name like concat('%',#{userParam},'%') or su.real_name like concat('%',#{userParam},'%'))
            </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 su.org_id IN
                    <foreach collection="dpr.orgIdSet" item="item" index="index" open="(" separator="," close=")">
                        #{item}
                    </foreach>
                </if>
            </if>
        </where>
        ORDER BY a.update_time DESC
    </select>
    <select id="listExamTaskDetailDownloadByQuery"
            resultType="com.qmth.distributed.print.business.bean.dto.ExamTaskDetailDto">
        SELECT
            a.id,
            a.card_rule_id cardRuleId,
            be.name examName,
            bs.name semesterName,
            a.course_code courseCode,
            a.course_name courseName,
            a.paper_number paperNumber,
            b.enable,
            b.paper_type paperType,
            IFNULL(b.exposed_paper_type,'') exposedPaperType,
            IFNULL(b.unexposed_paper_type,'') unexposedPaperType,
            so.name orgName,
            b.paper_attachment_ids paperAttachmentIds
        FROM
            exam_task a
                LEFT JOIN
            exam_task_detail b ON a.id = b.exam_task_id
                LEFT JOIN
            basic_course bc ON a.school_id = bc.school_id
                AND a.course_code = bc.code
                LEFT JOIN
            basic_card_rule e ON a.card_rule_id = e.id
                LEFT JOIN
            basic_exam be ON a.exam_id = be.id
                LEFT JOIN
            basic_semester bs ON be.semester_id = bs.id
                LEFT JOIN
            t_f_flow_approve g ON g.flow_id = a.flow_id
                LEFT JOIN
            sys_org so ON bc.teaching_room_id = so.id and so.enable
                LEFT JOIN
            sys_user su ON a.user_id = su.id
        <where>
            a.status = 'SUBMIT'
            AND (a.review = 0
            OR (a.review = 1 AND g.status = 'FINISH'))
            <if test="semesterId != null and semesterId != ''">
                and be.semester_id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and a.exam_id = #{examId}
            </if>
            <if test="courseName != null and courseName != ''">
                and a.course_name like concat('%', #{courseName}, '%')
            </if>
            <if test="orgId != null">
                and bc.teaching_room_id = #{orgId}
            </if>
            <if test="idSet != null and idSet != '' and idSet.size > 0">
                and a.id in
                <foreach collection="idSet" item="item" separator="," open="(" close=")">
                    #{item}
                </foreach>
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND a.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>
        ORDER BY a.update_time DESC
    </select>

    <select id="getFlowInfo" resultType="com.qmth.distributed.print.business.bean.result.WorkResult">
        select
              et.id,
              et.school_id schoolId,
              et.org_id as orgId,
              et.course_code courseCode,
              et.course_name courseName,
              et.specialty,
              et.paper_number paperNumber,
              et.card_rule_id cardRuleId,
              case
                  et.card_rule_id when -1 then '全部通卡'
                                  else b.name
                  end cardRuleName,
              cast(art.ASSIGNEE_ as Decimal(24)) as userId,
              c.real_name userName,
              c.login_name loginName,
              et.start_time startTime,
              et.end_time endTime,
              tffa.status,
              tffa.status auditStatus,
              et.enable,
              et.create_time as createTime,
              et.update_time as updateTime,
              art.EXECUTION_ID_ as executionId,
              art.PROC_INST_ID_ as flowId,
              art.NAME_ as taskName,
              art.TASK_DEF_KEY_ as taskDefKey,
              art.ID_ as taskId,
              tffa.setup,
              et.teacher_name teacherName,
              et.lecturer_name lecturerName,
              d.real_name as propositionName,
              so.name teachingRoomName,
              et.exam_id as examId
          from
              ACT_RU_TASK art
                  left join t_f_flow_join tffj on
                  tffj.flow_id = art.PROC_INST_ID_
                  left join exam_task et on
                  et.id = tffj.object_id
                  left join basic_card_rule b ON
                  et.card_rule_id = b.id
                  left join t_f_flow_approve tffa on
                  tffa.school_id = et.school_id and tffa.flow_id = et.flow_id
                  left join sys_user c ON
                  art.ASSIGNEE_ = c.id
                  LEFT JOIN sys_user d ON
                  et.user_id= d.id
                  LEFT JOIN
                  sys_org so ON et.teaching_room_id = so.id
          <where>
              et.enable = true
              <if test="flowId != null and flowId != ''">
                  and art.PROC_INST_ID_ = cast(#{flowId} as char)
              </if>
              <if test="taskId != null and taskId != ''">
                  and art.ID_ = cast(#{taskId} as char);
              </if>
          </where>
    </select>
    <select id="listMakeupExamTask"
            resultType="com.qmth.distributed.print.business.bean.dto.MakeupExamTaskDto">
        SELECT
            a.exam_id,
            a.course_code,
            a.course_name,
            a.paper_number,
            a.login_name,
            a.real_name,
            a.unexposed_paper_type,
            a.exposed_paper_type,
            b.id examDetailId,
            b.print_plan_id,
            b.total_subjects,
            b.backup_count,
            b.status,
            b.print_house_id,
            b.exam_place campusName,
            b.clazz_name className
        FROM
        (SELECT
            et.exam_id,
            et.course_code,
            et.course_name,
            et.paper_number,
            su.login_name,
            su.real_name,
            etd.unexposed_paper_type,
            etd.exposed_paper_type
        FROM
            exam_task et
        LEFT JOIN exam_task_detail etd ON et.id = etd.exam_task_id
        LEFT JOIN basic_exam be ON et.exam_id = be.id
        LEFT JOIN sys_user su ON et.user_id = su.id
        WHERE
        be.semester_id = #{semesterId}
        AND et.exam_id = #{examId}
        <if test="courseCode != null and courseCode != ''">
            AND et.course_code = #{courseCode}
        </if>
        <if test="paperNumber != null and paperNumber != ''">
            AND et.paper_number = #{paperNumber}
        </if>
        <if test="userName != null and userName !=''">
            AND (su.login_name = #{userName} or su.real_name = #{userName})
        </if>
        <if test="collegeId != null">
            AND EXISTS( SELECT
            1
            FROM
            basic_course bc
            WHERE
            et.course_code = bc.code
            AND bc.teaching_room_id = #{collegeId})
        </if>
        AND etd.unexposed_paper_type is not null) a
        LEFT JOIN
        (SELECT
            ed.id,
            ed.print_plan_id,
            ed.exam_id,
            edc.course_code,
            edc.paper_number,
            ed.total_subjects,
            ed.backup_count,
            ed.status,
            ed.print_house_id,
            ed.exam_place,
            edc.clazz_name
        FROM
            exam_detail ed
        LEFT JOIN exam_detail_course edc ON ed.id = edc.exam_detail_id
        WHERE
        print_plan_id = #{printPlanId}) b ON a.exam_id = b.exam_id
        AND a.paper_number = b.paper_number
    </select>
</mapper>
