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

    <!-- 查询我的任务列表 -->
    <select id="queryTaskList" resultType="jnpf.model.cultivate.vo.learn.FtbCultivateMyLearnTaskListVO">
        select
        t.F_Id as id,
        t.F_TaskName as taskName,
        t.F_TaskType as taskType,
        t.F_TimeLimitStartTime as timeLimitStartTime,
        t.F_TimeLimitEndTime as timeLimitEndTime,
        t.F_Status as taskStats,
        m.F_StudyStats as studyStats,
        m.F_LearningStartTime as learningStartTime,
        m.F_LearningEndTime as learningEndTime,
        t.F_StudentTimeCompletion as studentTimeCompletion,
        t.F_TimeCompletionUnit as timeCompletionUnit,
        t.F_StudentTimeCompletion as timeCompletionDays
        from ftb_cultivate_learn_task as t
        left join ftb_cultivate_learn_task_assignment as m on t.F_Id=m.F_TaskId
        <where>
            t.F_EnableMark = 0  and t.F_Status != 0 and m.F_EnableMark = 0
            <if test="taskListDto.keyWords!= null and taskListDto.keyWords!= ''">
                and  t.F_TaskName like CONCAT('%',#{taskListDto.keyWords},'%')
            </if>
            <if test="taskListDto.flag != null  and taskListDto.flag == 1 ">
                and m.F_StudyStats  = 2
            </if>
            <if test="taskListDto.flag != null  and taskListDto.flag == 0 ">
                and m.F_StudyStats  != 2
            </if>
            <if  test="taskListDto.userId != null and taskListDto.userId != ''">
                and m.F_UserId = #{taskListDto.userId}
            </if>

            <if test="taskListDto.flag != null  and taskListDto.flag == 0 ">
               order by t.F_CreatorTime desc
            </if>
        </where>
    </select>
    <!--查看任务详情列表-->
    <select id="getListOfCompletions" resultType="jnpf.model.cultivate.vo.learn.FtbCultivateLearnTaskFinishInfoVO">
        SELECT
        F_Id id,
        F_TaskId taskId,
        F_UserId userId,
        F_StudyStats completionStatus,
        F_LearningStartTime taskStartTime,
        F_LearningEndTime taskEndTime
        FROM
        ftb_cultivate_learn_task_assignment
        <where>
            F_TaskId = #{taskId}
            <if test="ids != null and ids.size() > 0">
                and F_Id in
                <foreach collection="ids" item="id" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        and F_EnableMark = 0
        </where>
    </select>
    <!--查询对应课程完成情况-->
    <select id="queryCourseUserInfo"
            resultType="jnpf.model.cultivate.vo.learn.info.FtbCultivateLearnTaskCourseUserInfoVO">
        SELECT
        po.F_CourceId as courseId,
        cs.F_Name as courseName,
        po.F_LearnTime as unavailable
        FROM
        ftb_cultivate_position_cource_learning AS po
        INNER JOIN ftb_cultivate_learn_task_course AS task ON task.F_CourseId = po.F_CourceId
        LEFT JOIN ftb_cultivate_course AS cs ON po.F_CourceId = cs.F_Id
        <where>
              po.F_EnabledMark = 0 and
            task.F_TaskId = #{taskId}
            and po.F_UserId = #{userId}
        </where>
    </select>
    <!--查询证书信息-->
    <select id="queryCertificateInfo"
            resultType="jnpf.model.cultivate.vo.learn.info.FtbCultivateLearnTaskCertificateUserInfoVO">
        SELECT
        ag.F_IssuedCertificate as isFinish,
        ce.F_CertificateId as certificateId,
        cf.F_Name as certificateName
        FROM
        ftb_cultivate_learn_task_assignment AS ag
        LEFT JOIN ftb_cultivate_learn_task_certificate AS ce ON ag.F_TaskId = ce.F_TaskId
        LEFT JOIN ftb_cultivate_certificate as cf ON ce.F_CertificateId  = cf.F_Id
        <where>
            ag.F_TaskId = #{taskId}
            and ag.F_UserId = #{userId}
            and ag.F_EnableMark=0
        </where>

    </select>
    <!--查询实操鉴定-->
    <select id="queryIdentificationInfo"
            resultType="jnpf.model.cultivate.vo.learn.info.FtbCultivateLearnTaskIdentificationUserInfoVO">
        SELECT
        ba.F_TableId as identificationId,
        ba.F_Name as identificationName,
        us.F_Status as identifyStatus,
        us.F_Result as identifyResult,
        CASE us.F_Status
        WHEN 0 THEN 0
        WHEN 1 THEN 1
        WHEN 2 THEN 0 END as isFinish
        FROM
        ftb_cultivate_identify_apply AS us
        LEFT JOIN ftb_cultivate_identify_apply_table_backups AS ba ON us.F_TableId = ba.F_Id
        inner JOIN ftb_cultivate_learn_task_identification AS tas ON tas.F_IdentificationId = ba.F_TableId and tas.F_TaskId = us.F_SourceId
        <where>
            tas.F_TaskId = #{taskId}
            and us.F_DeleteMark = 0
            and us.F_BeIdentifyUserId = #{userId}
        </where>
    </select>
    <!--查询考试-->
    <select id="queryExamInfo" resultType="jnpf.model.cultivate.vo.learn.info.FtbCultivateLearnTaskExamUserInfoVO">
        SELECT
        ex.F_ExamId as examId,
        ex.F_ExamName as examName,
        CASE
        us.F_Status
        WHEN 0 THEN 0
        WHEN 2 THEN 0
        WHEN 1 THEN 1
        WHEN 3 THEN 1
        WHEN 4 THEN 1
        WHEN 5 THEN 1
        END AS isFinish,
        us.F_Duration as unavailable
        FROM
        ftb_cultivate_exam_user AS us
        INNER JOIN ftb_cultivate_learn_task_exam AS tas ON tas.F_TaskId = us.F_RelationTaskId AND us.F_ExamId AND tas.F_ExamId
        LEFT JOIN ftb_cultivate_exam AS ex ON us.F_ExamId = tas.F_ExamId
        <where>
           us.F_ExamSource = 4
            and tas.F_TaskId = #{taskId}
            and us.F_UserId = #{userId}
        </where>
    </select>
    <!--查询任务列表-->
    <select id="getTaskList" resultType="jnpf.model.cultivate.vo.learn.FtbCultivateLearnTaskListVO">
        SELECT
        task.F_Id as id,
        task.F_TaskName as taskName,
        task.F_Status as status,
        task.F_TaskType as taskType,
        task.F_TimeLimitStartTime as learningStartTime,
        task.F_TimeLimitEndTime as learningEndTime,
        COUNT(ag.F_UserId) as taskNumber,
        task.F_CreatorTime as createTime
        FROM
        ftb_cultivate_learn_task as task
        LEFT JOIN ftb_cultivate_learn_task_assignment as ag ON task.F_Id = ag.F_TaskId
        <where>
            task.F_EnableMark = 0  and task.F_Status != 0 and ag.F_EnableMark = 0
            <if test="taskListDto.keyWords!= null and taskListDto.keyWords!= ''">
                and task.F_TaskName like CONCAT('%',#{taskListDto.keyWords},'%')
            </if>
            <if test="taskListDto.taskFlag != null and taskListDto.taskFlag != ''">
                and task.F_Status = #{taskListDto.taskFlag}
            </if>
        </where>
        GROUP BY task.F_Id order by task.F_CreatorTime desc
    </select>


</mapper>