<?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.FtbCultivateLearnTaskMapper">
    <!--统计查询分类下任务数量-->
    <select id="groupCountCateNum" resultType="jnpf.model.cultivate.dto.learn.BatchCommonCountDto">
        select F_TaskClass as selectKey,count(*) as num from  ftb_cultivate_learn_task where F_EnableMark=0 group by F_TaskClass
    </select>
    <!--web查询任务列表-->
    <select id="getMyWebPageList" resultType="jnpf.model.cultivate.vo.learn.FtbCultivateLearnTaskInfoListVO">
        select
        t.F_Id as id,
        t.F_TaskClass as taskClass,
        t.F_LearnTaskShowId as learnTaskShowId,
        t.F_TaskName as taskName,
        t.F_TaskType as taskType,
        t.F_TimeLimitStartTime as timeLimitStartTime,
        t.F_TimeLimitEndTime as timeLimitEndTime,
        t.F_StudentTimeCompletion as studentTimeCompletion,
        t.F_TimeCompletionUnit as timeCompletionUnit,
        t.F_TimeCompletionDays as timeCompletionDays,
        t.F_Status as `status`,
        t.F_AssignmentRule as assignmentRule,
        t.F_StartEntryPeriod as startEntryPeriod,
        t.F_EndEntryPeriod as endEntryPeriod,
        t.F_CoverUrl as coverUrl,
        t.F_CreatorUserId as creatorUserId,
        t.F_CreatorTime as creatorTime
        from ftb_cultivate_learn_task as t
        where t.F_EnableMark=0
        <if test="params.startDate!= null and params.endDate!= null">
            and t.F_CreatorTime between #{params.startDate} and #{params.endDate}
        </if>

        <if test="params.keyWord!= null and params.keyWord!= ''">
            and  t.F_TaskName like CONCAT('%',#{params.keyWord},'%')
        </if>

        <if test="params.status!= null">
            and t.F_Status =#{params.status}
        </if>

        <if test="params.categoryId!= null and params.categoryId!= '' and params.categoryId!= '0'">
            and  t.F_TaskClass = #{params.categoryId}
        </if>
        order by t.F_CreatorTime desc
    </select>
    <!--任务统计查询学习任务统计列表-->
    <select id="queryLearnCountList"
            resultType="jnpf.model.cultivate.vo.learn.FtbCultivateLearnTaskInfoCountListVO">
        select
        t.F_Id as id,
        t.F_TaskClass as taskClass,
        t.F_LearnTaskShowId as learnTaskShowId,
        t.F_TaskName as taskName,
        t.F_TaskType as taskType,
        t.F_TimeLimitStartTime as timeLimitStartTime,
        t.F_TimeLimitEndTime as timeLimitEndTime,
        t.F_StudentTimeCompletion as studentTimeCompletion,
        t.F_TimeCompletionDays as timeCompletionDays,
        t.F_Status as `status`,
        t.F_AssignmentRule as assignmentRule,
        t.F_StartEntryPeriod as startEntryPeriod,
        t.F_EndEntryPeriod as endEntryPeriod,
        t.F_CoverUrl as coverUrl,
        t.F_CreatorUserId as creatorUserId,
        t.F_CreatorTime as creatorTime
        from ftb_cultivate_learn_task as t
        where t.F_EnableMark=0
        <if test="params.startDate!= null and params.endDate!= null">
            and t.F_CreatorTime between #{params.startDate} and #{params.endDate}
        </if>

        <if test="params.keyWord!= null and params.keyWord!= ''">
            and  t.F_TaskName like CONCAT('%',#{params.keyWord},'%')
        </if>

        <if test="params.status!= null">
            and t.F_Status =#{params.status}
        </if>
        <if test="params.status== null">
            and t.F_Status not in (0,4)
        </if>
        order by t.F_CreatorTime desc
    </select>
    <!--查询需要提醒的任务列表-->
    <select id="queryNeedPerDayAlert" resultType="jnpf.model.cultivate.dto.learn.NeedPerDayAlertDto">
        SELECT
            t.F_Id as taskId,t.F_TaskName AS taskName,
            re.F_PerInterval AS perInterval
        FROM
            ftb_cultivate_learn_task AS t
                LEFT JOIN ftb_cultivate_learn_task_reminder_rule AS re ON t.F_Id = re.F_TaskId
        WHERE
            t.F_Status = 2
          AND t.F_EnableMark = 0
          AND re.F_TaskEndReminder = 1
          AND re.F_EnableMark = 0
    </select>

    <!--查询任务列表及其统计信息-->
    <select id="queryTaskListForApp" resultType="jnpf.model.cultivate.v2.task.vo.V2CultivateTaskListForManagerVo">
        SELECT
        t.F_Id as id,
        t.F_TaskName as taskName,
        t.F_CoverId as coverId,
        t.F_CoverUrl as coverUrl,
        t.F_Status as status,
        t.F_TaskType as taskType,
        t.F_TimeLimitStartTime as learningStartTime,
        t.F_TimeLimitEndTime as learningEndTime,
        t.F_CreatorTime as createTime,
        t.F_Description as description
        FROM
        ftb_cultivate_learn_task as t
        WHERE
        t.F_EnableMark = 0
        <if test="keyWord != null and keyWord != ''">
            AND t.F_TaskName LIKE CONCAT('%', #{keyWord}, '%')
        </if>
        <if test="status != null">
            AND t.F_Status = #{status}
        </if>
    </select>

    <select id="queryTaskCountForApp" resultType="java.lang.Long">
        SELECT
        COUNT(1)
        FROM
        ftb_cultivate_learn_task as t
        WHERE
        t.F_EnableMark = 0
        <if test="keyWord != null and keyWord != ''">
            AND t.F_TaskName LIKE CONCAT('%', #{keyWord}, '%')
        </if>
        <if test="status != null">
            AND t.F_Status = #{status}
        </if>
    </select>

    <select id="queryMyTaskListForApp" resultType="jnpf.model.cultivate.v2.task.vo.V2MyCultivateLearnTaskListVo">
        select
        m.F_Id as id,
        t.F_Id as taskId,
        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,
        t.F_CoverId as coverId,
        t.F_CoverUrl as coverUrl,
        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
            <![CDATA[
                and (t.F_TaskType=0 or (t.F_TaskType=1 and t.F_TimeLimitStartTime <= CURRENT_TIMESTAMP) )
             ]]>
            <if test="params.keyWord!= null and params.keyWord!= ''">
                and t.F_TaskName like CONCAT('%',#{params.keyWord},'%')
            </if>
            <if test="params.status != null  and params.status == 1 ">
                and m.F_StudyStats in (0,1)
            </if>
            <if test="params.status != null  and params.status == 2 ">
                and m.F_StudyStats = 2
            </if>
            <if test="params.status != null  and params.status == 3 ">
                and m.F_StudyStats = 3
            </if>
            <if test="userId != null and userId != ''">
                and m.F_UserId = #{userId}
            </if>

        </where>
    </select>

    <select id="queryMyTaskCountForApp" resultType="java.lang.Integer">
        select
        count(1)
        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
            <![CDATA[
                and (t.F_TaskType=0 or (t.F_TaskType=1 and t.F_TimeLimitStartTime <= CURRENT_TIMESTAMP) )
             ]]>
            <if test="keyWord!= null and keyWord!= ''">
                and t.F_TaskName like CONCAT('%',#{keyWord},'%')
            </if>
            <if test="status != null  and status == 1 ">
                and m.F_StudyStats in (0,1)
            </if>
            <if test="status != null  and status == 2 ">
                and m.F_StudyStats = 2
            </if>
            <if test="status != null  and status == 3 ">
                and m.F_StudyStats = 3
            </if>
            <if test="userId != null and userId != ''">
                and m.F_UserId = #{userId}
            </if>
        </where>
    </select>

    <select id="queryMyNoCompleteTaskListForUserId"
            resultType="jnpf.model.cultivate.v2.task.vo.V2MyCultivateLearnTaskListVo">
        select
        m.F_Id as id,
        t.F_Id as taskId,
        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,
        t.F_CoverId as coverId,
        t.F_CoverUrl as coverUrl,
        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
            <![CDATA[
                and (t.F_TaskType=0 or (t.F_TaskType=1 and t.F_Status = 2) )
             ]]>
            and m.F_StudyStats in (0,1)
            <if test="userId != null and userId != ''">
                and m.F_UserId = #{userId}
            </if>
        </where>

    </select>

    <select id="queryTaskHasExam" resultType="java.lang.String">
        select F_TaskId as taskId from ftb_cultivate_learn_task_exam
        WHERE
        F_EnableMark = 0 and
        F_TaskId IN
        <foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
            #{taskId}
        </foreach>
        and F_ExamId=#{examId}
    </select>

    <select id="queryTaskHasPracticeId" resultType="java.lang.String">
        select F_TaskId as taskId from ftb_cultivate_learn_task_practice
        WHERE
        F_EnableMark = 0 and
        F_TaskId IN
        <foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
            #{taskId}
        </foreach>
        and F_BusinessId=#{practiceId}
    </select>

    <select id="queryTaskHasIdentityId" resultType="java.lang.String">
        select F_TaskId as taskId from ftb_cultivate_learn_task_identification
        WHERE
        F_EnableMark = 0 and
        F_TaskId IN
        <foreach collection="taskIds" item="taskId" open="(" separator="," close=")">
            #{taskId}
        </foreach>
        and F_IdentificationId=#{identityId}
    </select>

    <select id="queryMyRunningTask" resultType="java.lang.String">
        select
        t.F_Id as taskId
        from ftb_cultivate_learn_task as t
        inner join ftb_cultivate_learn_task_assignment as m on t.F_Id=m.F_TaskId
        inner join ftb_cultivate_learn_task_course as c on t.F_Id=c.F_TaskId
        inner join ftb_cultivate_course as course on c.F_CourseId=course.F_Id
        <where>
            t.F_EnableMark = 0 and t.F_Status =2 and m.F_EnableMark = 0
            and c.F_CourseId=#{courseId}
            and c.F_EnableMark= 0
            and m.F_StudyStats =1
            and course.F_EnabledMark = 0
            and course.F_IsGrounding=1
            <if test="userId != null and userId != ''">
                and m.F_UserId = #{userId}
            </if>
        </where>
    </select>

    <!--V2查询任务统计列表-->
    <select id="queryV2TaskCountList" resultType="jnpf.model.cultivate.v2.task.vo.V2CultivateTaskCountVo">
        select
        t.F_Id as id,
        t.F_TaskName as taskName,
        t.F_TaskType as taskType,
        t.F_Status as status,
        t.F_TimeLimitStartTime as timeLimitStartTime,
        t.F_TimeLimitEndTime as timeLimitEndTime,
        t.F_CreatorTime as creatorTime
        from ftb_cultivate_learn_task as t
        where t.F_EnableMark=0
        <if test="taskName != null and taskName != ''">
            and t.F_TaskName like CONCAT('%',#{taskName},'%')
        </if>
        <if test="status != null">
            and t.F_Status = #{status}
        </if>
        <if test="creatorTimeStart != null and creatorTimeEnd != null">
            and t.F_CreatorTime between #{creatorTimeStart} and #{creatorTimeEnd}
        </if>
        order by t.F_CreatorTime desc
    </select>
</mapper>