<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.qmth.teachcloud.common.mapper.TBTaskMapper">

    <select id="query" resultType="com.qmth.teachcloud.common.bean.result.TaskListResult">
        select
            tbt.id,
            tbt.semester_id semesterId,
            bs.name semesterName,
            tbt.exam_id examId,
            be.name examName,
            tbt.course_code courseCode,
            tbt.course_name courseName,
            tbt.paper_number paperNumber,
            tbt.print_plan_id as printPlanId,
            concat_ws('-', epp.name, tbt.obj_name) as printPlanName,
            tbt.`type`,
            tbt.status,
            tbt.`result`,
            tbt.create_time as createTime,
            tbt.create_id as createId,
            su.real_name as createName,
            if(ISNULL(tbt.import_file_path),false,true) as hasImportFile,
            IF(tbt.result_file_path is null, IF(tbt.import_file_path is null, false, true), true) as hasResultFile,
            if(ISNULL(tbt.report_file_path),false,true) as hasReportFile,
            if(ISNULL(tbt.error_file_path),false,true) as hasErrorFile,
            tbt.reset_count as resetCount,
            tbt.error_message as errorMessage,
            tbt.summary
        from
            t_b_task tbt
        left join exam_print_plan epp on
            epp.id = tbt.print_plan_id
        left join sys_user su on su.id = tbt.create_id
        left join basic_semester bs on tbt.semester_id = bs.id
        left join basic_exam be on tbt.exam_id = be.id
        <where>
            <if test="schoolId != null and schoolId != ''">
                and tbt.school_id = #{schoolId}
            </if>
            <if test="semesterId != null and semesterId != ''">
                and tbt.semester_id = #{semesterId}
            </if>
            <if test="examId != null and examId != ''">
                and tbt.exam_id = #{examId}
            </if>
            <if test="courseCode != null and courseCode != ''">
                and tbt.course_code = #{courseCode}
            </if>
            <if test="paperNumber != null and paperNumber != ''">
                and tbt.paper_number = #{paperNumber}
            </if>
            <if test="printPlanId != null and printPlanId != ''">
                and tbt.print_plan_id = #{printPlanId}
            </if>
            <if test="status != null and status != ''">
                and tbt.status = #{status}
            </if>
            <if test="type != null and type != ''">
                and tbt.type = #{type}
            </if>
            <if test="result != null and result != ''">
                and tbt.result = #{result}
            </if>
            <if test="dpr != null">
                <if test="dpr.requestUserId != null">
                    AND su.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 tbt.create_time desc
    </select>
</mapper>
