<?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="jnpf.cultivate.mapper.FtbCultivateLearnTaskExamMapper">
    <!--批量查询任务管理的考试信息-->
    <select id="queryExamBaseInfo" resultType="jnpf.model.cultivate.resp.TaskRelationExamVo">
        SELECT
        a.F_TaskId as taskId,
        a.F_ExamId as examId,
        exam.F_ExamName AS examName
        FROM
        ftb_cultivate_learn_task_exam a join ftb_cultivate_exam exam on a.F_ExamId=exam.F_Id
        WHERE
        a.F_EnableMark=0
        and a.F_TaskId IN
        <foreach collection="taskIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="queryTaskListForExam" resultType="jnpf.model.cultivate.po.learn.FtbCultivateLearnTask">
        select t.F_Id       as id,
               t.F_TaskName as taskName
        from ftb_cultivate_learn_task_exam exam
                 left join ftb_cultivate_learn_task t on exam.F_TaskId = t.F_Id
        where t.F_EnableMark = 0
          and exam.F_EnableMark = 0
          and t.F_Status = 2
          and exam.F_ExamId = #{examId}
    </select>

    <select id="queryTaskListForExamNoPublish" resultType="jnpf.model.cultivate.po.learn.FtbCultivateLearnTask">
        select t.F_Id       as id,
               t.F_TaskName as taskName
        from ftb_cultivate_learn_task_exam exam
                 left join ftb_cultivate_learn_task t on exam.F_TaskId = t.F_Id
        where t.F_EnableMark = 0
          and exam.F_EnableMark = 0
          and t.F_Status in (0, 1)
          and exam.F_ExamId = #{examId}
    </select>

    <select id="queryTaskListForExamAll" resultType="jnpf.model.cultivate.po.learn.FtbCultivateLearnTask">
        select t.F_Id       as id,
               t.F_TaskName as taskName
        from ftb_cultivate_learn_task_exam exam
                 left join ftb_cultivate_learn_task t on exam.F_TaskId = t.F_Id
        where t.F_EnableMark = 0
          and exam.F_EnableMark = 0
          and t.F_Status in (0, 1, 2)
          and exam.F_ExamId = #{examId}
    </select>

    <insert id="insertBatch" parameterType="java.util.List">
        INSERT INTO ftb_cultivate_learn_task_exam (
        F_Id,F_TaskId, F_ExamId, F_PhaseId,
        F_CreatorTime, F_CreatorUserId, F_LastModifyTime, F_LastModifyUserId
        )
        VALUES
        <foreach collection="list" item="item" separator=",">
            (
            #{item.id}, #{item.taskId}, #{item.examId}, #{item.phaseId},
            #{item.creatorTime}, #{item.creatorUserId}, #{item.lastModifyTime},
            #{item.lastModifyUserId}
            )
        </foreach>
    </insert>

    <!--根据任务 ID 查询考试列表 (关联考试表，要求考试未删除且有效)-->
    <select id="listByTaskIdWithExam" resultType="jnpf.model.cultivate.v2.task.vo.FtbCultivateLearnTaskExamVo">
        SELECT lte.F_Id             as id,
               lte.F_TaskId         as taskId,
               lte.F_ExamId         as examId,
               lte.F_PhaseId        as phaseId,
               lte.F_IsPassComplete as isPassComplete,
               lte.F_EnableMark     as enableMark,
               e.F_ExamName         as examName
        FROM ftb_cultivate_learn_task_exam lte
                 INNER JOIN ftb_cultivate_exam e ON lte.F_ExamId = e.F_Id
        WHERE lte.F_TaskId = #{taskId}
          AND lte.F_EnableMark = 0
          AND e.F_EnabledMark = 1
    </select>

    <select id="listByTaskIdAndPhaseId" resultType="jnpf.model.cultivate.v2.task.vo.FtbCultivateLearnTaskExamVo">
        SELECT lte.F_Id             as id,
               lte.F_TaskId         as taskId,
               lte.F_ExamId         as examId,
               lte.F_PhaseId        as phaseId,
               lte.F_IsPassComplete as isPassComplete,
               lte.F_EnableMark     as enableMark,
               e.F_ExamName         as examName,
               e.F_CreatorTime       as creatorTime,
               e.F_LastModifyTime  as lastModifyTime
        FROM ftb_cultivate_learn_task_exam lte
                 INNER JOIN ftb_cultivate_exam e ON lte.F_ExamId = e.F_Id
        WHERE lte.F_TaskId = #{taskId}
            AND lte.F_PhaseId = #{phaseId}
          AND lte.F_EnableMark = 0
          AND e.F_EnabledMark = 1
    </select>

    <!--检查考试是否被学习任务绑定（关联任务主表）-->
    <select id="checkExamBinding" resultType="java.lang.Integer">
        SELECT COUNT(1)
        FROM ftb_cultivate_learn_task_exam lte
                 INNER JOIN ftb_cultivate_learn_task t ON lte.F_TaskId = t.F_Id
        WHERE lte.F_ExamId = #{examId}
          AND lte.F_EnableMark = 0
          AND t.F_EnableMark = 0
          and t.F_Status not in (3, 4)
    </select>
</mapper>