<?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.FtbCultivatePositionCourceLearningMapper">
    <!--岗位学习课程列表-->
    <select id="globalCurriculumList" resultType="jnpf.model.cultivate.vo.course.app.FtbGlobalCurriculumAppVO">
        SELECT a.F_CourseId as courseId,b.F_Name as courseName,b.F_Highlights as courseHighlights,c.F_state as learnState
        FROM ftb_cultivate_position_course a JOIN ftb_cultivate_course b ON a.F_CourseId = b.F_Id
        JOIN ftb_cultivate_position_cource_learning c ON a.F_CourseId=c.F_CourceId
        WHERE a.F_EnabledMark=0 AND b.F_EnabledMark=0 and b.F_IsGrounding = 1 and a.F_PostRankId = #{gradeId} AND c.F_UserId=#{userId} and c.F_EnabledMark=0
         <if test="type == 0">
             AND b.F_Label = 1
         </if>
        <if test="type == 1">
             AND b.F_Label = 2 and FIND_IN_SET(#{postId},b.F_LearnJob) > 0
         </if>
        order by c.F_state ASC
    </select>
    <!--岗位学习-下属实操鉴定-->
    <select id="practicalAppraisalList" resultType="java.lang.String">
        SELECT F_IdentifyRecordId FROM ftb_cultivate_position_identify_result
        WHERE F_EnabledMark = 0
        <if test="gradeIds != null and gradeIds.size() != 0">
            and F_PostRankId in
            <foreach collection="gradeIds" open="(" close=")" item="gradeId" separator=",">
                #{gradeId}
            </foreach>
        </if>
        and F_UserId = #{userId}
    </select>
    <!--是否为线下培训课程-->
    <select id="whetherToAddOfflineTrainingCourses" resultType="java.lang.Integer">
        SELECT count(*)
        FROM ftb_cultivate_offline_course a JOIN ftb_cultivate_offline_train c ON a.F_OffilneId=c.F_Id
        where a.F_EnabledMark = 0 and a.F_CourseId = #{courseId} and FIND_IN_SET(#{userId},c.F_Participants) > 0
    </select>
    <!--本岗课程内容-->
    <select id="onTheJobLearningCourse" resultType="jnpf.model.cultivate.vo.course.app.FtbGlobalCurriculumAppVO">
        SELECT a.F_CourseId as courseId,b.F_Name as courseName,b.F_Highlights as courseHighlights,c.F_state as learnState
        FROM ftb_cultivate_position_course a JOIN ftb_cultivate_course b ON a.F_CourseId = b.F_Id
        JOIN ftb_cultivate_position_cource_learning c ON a.F_CourseId=c.F_CourceId
        join ftb_cultivate_position d on d.F_PostId = a.F_PostRankId
        WHERE a.F_EnabledMark = 0 AND b.F_EnabledMark = 0 and d.F_EnabledMark = 0 and b.F_IsGrounding = 1 and d.F_IsGrounding = 1 and c.F_EnabledMark=0
        and b.F_Label = 2
        and a.F_PostRankId = #{params.postId}
        AND c.F_UserId=#{params.userId}
        <if test="params.compulsory != null">
            and a.F_Compulsory = #{params.compulsory}
        </if>
        order by c.F_state ASC
    </select>
    <!--查看课程培训数据-->
    <select id="subordinateLearningCourses"
            resultType="jnpf.model.cultivate.vo.position.app.FtbSubordinateLearningCoursesVO">
        SELECT a.F_CourceId as courseId,b.F_Name as courseName,b.F_Format as format,a.F_LastModifyTime as completeLearningTime
        from ftb_cultivate_position_cource_learning a JOIN ftb_cultivate_course b ON a.F_CourceId=b.F_Id
        WHERE b.F_EnabledMark = 0 and a.F_state = 1 and a.F_UserId = #{params.userId} and a.F_EnabledMark=0
    </select>
    <!--下属学习课程-->
    <select id="subordinateLearningCoursess"
            resultType="jnpf.model.cultivate.vo.position.app.FtbSubordinateLearningCoursesVO">
        SELECT a.F_CourceId as courseId,b.F_Name as courseName,b.F_Format as format,a.F_LastModifyTime as completeLearningTime
        from ftb_cultivate_position_cource_learning a JOIN ftb_cultivate_course b ON a.F_CourceId=b.F_Id
        join ftb_cultivate_position_course c on c.F_CourseId = b.F_Id join ftb_cultivate_position d on d.F_PostId = c.F_PostRankId
        WHERE
        b.F_EnabledMark = 0 and a.F_state = 1 and c.F_EnabledMark = 0 and b.F_Label = 2 and b.F_IsGrounding = 1 and a.F_EnabledMark=0
        and d.F_IsGrounding = 1 and d.F_EnabledMark = 0
        and a.F_UserId = #{params.userId} and
        c.F_PostRankId in (
            <foreach collection="postIds" item="item" separator=",">
                #{item}
            </foreach>
        )
        group by a.F_CourceId
    </select>


    <!--批量查询用户完成的学习课程-->
    <select id="listCompleteCourseForUserIds"
            resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourceLearning">
        SELECT F_id as id,
               F_UserId as userId,
               F_CourceId as courceId
        from ftb_cultivate_position_cource_learning
        WHERE
        F_state = 1
        and F_EnabledMark = 0
        and F_UserId in
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>
    <select id="queryStudyTime" resultType="java.lang.Long">
        SELECT sum(F_LearnTime)
        from ftb_cultivate_position_cource_learning
        WHERE
        F_state != 0
        and F_EnabledMark = 0
        and F_UserId = #{userId}
        and F_CourceId in
        <foreach collection="courseIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </select>

    <select id="countForCourseIds" resultType="jnpf.model.cultivate.dto.learn.BatchCommonCountDto">

        SELECT
        F_CourceId as selectKey,
        count( * ) num
        FROM
        ftb_cultivate_position_cource_learning
        WHERE
        F_EnabledMark = 0 and F_CourceId in
        <foreach collection="courseIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
        and F_state != 0
        GROUP BY
        F_CourceId

    </select>

    <select id="queryUserUnreadMsgV2" resultType="jnpf.model.cultivate.po.course.FtbCultivateCourse">
        SELECT b.F_Id as id, b.F_Name as name
        from ftb_cultivate_position_cource_learning a
                 JOIN ftb_cultivate_course b
                      ON a.F_CourceId = b.F_Id
        WHERE b.F_IsGrounding = 1
          AND b.F_EnabledMark = 0
          and a.F_EnabledMark = 0
          and a.F_state in (0,2)
          and a.F_UserId = #{userId}
        <if test="courseId!= null and courseId!= ''">
            and  b.F_Id = #{courseId}
        </if>
    </select>

    <!--根据课程ID列表和用户ID统计已完成的学习课程数量-->
    <select id="countCompletedCoursesByUserAndCourseIds" resultType="java.lang.Long">
        SELECT COUNT(DISTINCT F_CourceId)
        FROM ftb_cultivate_position_cource_learning
        WHERE F_EnabledMark = 0
          AND F_state = 1
          AND F_UserId = #{userId}
          AND F_CourceId IN
        <foreach collection="courseIds" item="courseId" open="(" separator="," close=")">
            #{courseId}
        </foreach>
    </select>

    <select id="accessCourseStatistics" resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourceLearning">
        SELECT
            a.F_UserId AS userId,
            a.F_CourceId AS courceId,
            a.F_state AS state,
            a.F_LearnTime AS learnTime
        FROM
            ftb_cultivate_position_cource_learning a
                JOIN ftb_cultivate_course b ON a.F_CourceId = b.F_Id
        WHERE
            a.F_EnabledMark = 0
          AND b.F_EnabledMark = 0
          <if test="userIds != null and userIds.size() > 0">
              and a.F_UserId IN
            <foreach collection="userIds" open="(" close=")" separator="," item="x">
                #{x}
            </foreach>
          </if>
    </select>

    <!--查询用户已完成的课程列表（关联查询课程信息）-->
    <select id="queryCompletedCoursesWithInfo" resultType="jnpf.model.cultivate.v2.course.vo.UserLearningStatusVo$CourseInfo">
        SELECT
            a.F_CourceId AS courseId,
            b.F_Name AS courseName,
            a.F_state AS state,
            a.F_LearnTime AS learnTime
        FROM
            ftb_cultivate_position_cource_learning a
        INNER JOIN ftb_cultivate_course b ON a.F_CourceId = b.F_Id
        WHERE
            a.F_EnabledMark = 0
          AND b.F_EnabledMark = 0
          and b.F_IsGrounding = 1
          AND a.F_state = 1
          AND a.F_UserId = #{userId}
        ORDER BY a.F_LastModifyTime DESC
    </select>

</mapper>