<?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.FtbCultivateCourseMapper">
    <!--课程格式统计-->
    <select id="getCount" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM ftb_cultivate_course
        <where>
            F_EnabledMark = 0
            <if test="flag == 1">
                and F_Format = 1
            </if>
            <if test="flag == 2">
                and F_Format = 2
            </if>
            <if test="flag == 3">
                and F_Format = 3
            </if>
            <if test="flag == 4">
                and F_Label = 1
            </if>
            <if test="flag == 5">
                and F_Label = 2
            </if>
            <if test="flag == 7">
                and F_Format = 4
            </if>
        </where>
    </select>
    <!--课程列表分页查询-->
    <select id="pagingQuery" resultType="jnpf.model.cultivate.vo.course.web.FtbCultivateCoursePageVO">
        select a.F_Id as id,
        a.F_Name as `name`,
        a.F_Format as format,
        a.F_ChapterNumber as chapterNumber,
        a.F_IsGrounding as isGroundIng,
        a.F_Label as label,
        a.F_CourseId as courseId,
        a.F_LastModifyTime as updateTime,
        b.F_Name as typeName
        from ftb_cultivate_course a LEFT JOIN ftb_cultivate_course_type b ON a.F_TypeId=b.F_Id
        <where>
            a.F_EnabledMark = 0
            <if test="params.format!= null">
                and a.F_Format = #{params.format}
            </if>
            <if test="params.label!= null">
                and a.F_Label = #{params.label}
            </if>
            <if test="params.isGroundIng!= null">
                and a.F_IsGrounding = #{params.isGroundIng}
            </if>
            <if test="params.name!= null and params.name!= '' and ( params.onlySearchCourseName==null or params.onlySearchCourseName==0)">
                and (a.F_Name like CONCAT('%',#{params.name},'%') or b.F_Name like CONCAT('%',#{params.name},'%'))
            </if>
            <if test="params.name!= null and params.name!= '' and  params.onlySearchCourseName!=null and params.onlySearchCourseName==1">
                and a.F_Name like CONCAT('%',#{params.name},'%')
            </if>
            <if test="params.typeId!= null and params.typeId!= ''">
                and a.F_TypeId = #{params.typeId}
            </if>
            <if test="params.startTime != null and params.startTime != '' and params.endTime != null and params.endTime != '' ">
                and a.F_LastModifyTime >= #{params.startTime} and a.F_LastModifyTime &lt;= #{params.endTime}
            </if>
        </where>
        order by a.F_LastModifyTime desc
    </select>
    <!--校验岗位学习-->
    <select id="getCountLearnByCourseId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM ftb_cultivate_position_course
        WHERE F_CourseId = #{courseId}
          and F_EnabledMark = 0
    </select>
    <!--校验关联题库-->
    <select id="getCountQuestionBankByCourseId" resultType="java.lang.Integer">
        select count(*)
        from ftb_cultivate_question_bank_course
        where F_CourseId = #{courseId}
          and F_EnabledMark = 1
    </select>
    <!--岗位学习分页列表-->
    <select id="listJobLearningByCourseId" resultType="jnpf.model.cultivate.vo.course.web.FtbCourseDeleteJobLearnVO">
        SELECT F_PostLearnId as id, F_PostRankId as positionId
        FROM ftb_cultivate_position_course
        WHERE F_EnabledMark = 0
          AND F_CourseId = #{courseId}
    </select>
    <!--关联题库分页列表-->
    <select id="listRelatedQuestionBankList"
            resultType="jnpf.model.cultivate.vo.course.web.FtbCourseDeleteQuestionBankVO">
        SELECT a.F_ClassifyId as id, b.F_BankContent as questionBankName
        FROM ftb_cultivate_question_bank_course a
                 JOIN ftb_cultivate_question_bank b ON a.F_ClassifyId = b.F_Id
        WHERE a.F_EnabledMark = 1
          and a.F_CourseId = #{courseId}
    </select>

    <select id="getCountTestPaperByQuestionBankId" resultType="java.lang.Integer">
        SELECT COUNT(*)
        FROM ftb_cultivate_test_paper
        WHERE F_EnabledMark = 1
          AND FIND_IN_SET(#{questionBankId}, F_RelationQuestionBankId) > 0
    </select>
    <!--已学习课程列表-->
    <select id="promotionPathwayCourses" resultType="jnpf.model.cultivate.vo.course.web.PromotionChannelLearnCourseVO">
        SELECT a.F_Name as courseName, a.F_Format as courseType, b.F_LastModifyTime as learnTime
        FROM ftb_cultivate_course a
                 JOIN ftb_cultivate_position_cource_learning b ON a.F_Id = b.F_CourceId
        WHERE b.F_state = 1
          AND F_UserId = #{userId}
          and b.F_EnabledMark = 0
    </select>
    <!--通用课程列表-->
    <select id="generalCourseList" resultType="jnpf.model.cultivate.vo.course.app.FtbGlobalCurriculumAppVO">
        SELECT a.F_Id as courseId,a.F_Name as courseName,a.F_Highlights as courseHighlights,b.F_state as learnState
        FROM ftb_cultivate_course a JOIN ftb_cultivate_position_cource_learning b ON a.F_Id=b.F_CourceId
        WHERE b.F_UserId=#{userId} AND a.F_EnabledMark=0 AND a.F_Label = 1 AND b.F_EnabledMark = 0 and a.F_IsGrounding =
        1
        <if test="params.courseTypeId != null">
            and a.F_TypeId = #{params.courseTypeId}
        </if>
        <if test="params.courseName != null and params.courseName != ''">
            and a.F_Name like CONCAT('%',#{params.courseName},'%')
        </if>
        order by a.F_CreatorTime desc
    </select>
    <!--已学习课程数-->
    <select id="learnTotalNumber" resultType="java.lang.Integer">
        SELECT count(*)
        FROM ftb_cultivate_course a
                 JOIN ftb_cultivate_position_cource_learning b ON a.F_Id = b.F_CourceId
        WHERE b.F_UserId = #{userId}
          AND a.F_EnabledMark = 0
          AND a.F_Label = 1
          and a.F_IsGrounding = 1
          AND b.F_EnabledMark = 0
          and b.F_state = 1
          and a.F_TypeId = #{typeId}
    </select>
    <!--根据考试id查询考试名称-->
    <select id="queryExamNameByExamId" resultType="java.lang.String">
        SELECT F_ExamName
        FROM ftb_cultivate_exam
        WHERE F_Id = #{examId}
    </select>
    <!--校验岗位是否建立岗位学习-->
    <select id="queryJobBindingCourses"
            resultType="jnpf.model.cultivate.dto.course.FtbCultivateCourseDTO$PositionLearnCourseInternal">
        SELECT F_PostId as postId,F_Id as postLearnId FROM ftb_cultivate_position where F_EnabledMark = 0
        and F_PostId in
        <foreach collection="postIds" item="postId" open="(" separator="," close=")">
            #{postId}
        </foreach>
    </select>
    <select id="getCountMediaList" resultType="jnpf.model.cultivate.dto.course.FtbCommonKeyAndValDto">
        SELECT F_Format AS k, COUNT(*) as v
        FROM ftb_cultivate_course
        where F_EnabledMark = 0
        group by F_Format
    </select>
    <select id="getCountLabelList" resultType="jnpf.model.cultivate.dto.course.FtbCommonKeyAndValDto">
        SELECT F_Label AS k, COUNT(*) as v
        FROM ftb_cultivate_course
        where F_EnabledMark = 0
        group by F_Label
    </select>

    <select id="queryNoStudyCommonCourseNum" resultType="java.lang.Long">
        SELECT count(a.F_Id) as totalNum
        FROM ftb_cultivate_course a
                 JOIN ftb_cultivate_position_cource_learning b ON a.F_Id = b.F_CourceId
        WHERE b.F_UserId = #{userId}
          AND a.F_EnabledMark = 0
          AND a.F_Label = 1
          AND b.F_EnabledMark = 0
          and a.F_IsGrounding = 1
          and b.F_state in (0, 2)
    </select>

    <select id="queryBindingCourses"
            resultType="jnpf.model.cultivate.v2.position.vo.V2CultivateJobLearnCourseVo">
        SELECT a.F_ID as id,
        a.F_Name as `name`,
        a.F_ChapterNumber as chapterNumber,
        a.F_Label as label,
        a.F_IsGrounding as isGroundIng,
        a.F_CourseId as courseId,
        a.F_TypeId as typeId,
        a.F_LastModifyTime as updateTime,
        b.F_Name as typeName
        from ftb_cultivate_course as a LEFT JOIN ftb_cultivate_label b ON a.F_TypeId=b.F_Id
        WHERE a.F_EnabledMark = 0
        <if test="params.keyWord != null and params.keyWord != ''">
            AND a.F_Name LIKE CONCAT('%', #{params.keyWord}, '%')
        </if>
        <if test="params.label != null">
            <choose>
                <when test="params.label == 1">
                    AND a.F_Label = 1
                </when>
                <when test="params.label == 2">
                    AND a.F_Label = 2
                </when>
            </choose>
        </if>
    </select>

    <select id="jobLearningCourseList" resultType="jnpf.model.cultivate.v2.course.web.vo.V2CultivateCourseSelectVo">
        SELECT F_Id as id,
        F_Name as `name`,
        F_ChapterNumber as chapterNumber,
        F_Format as format,
        F_Label as label,
        F_IsGrounding as isGroundIng,
        F_CourseId as courseId,
        F_LastModifyTime as updateTime
        from ftb_cultivate_course
        WHERE F_EnabledMark = 0
        <if test="params.searchValue != null">
            and F_Name like concat('%',#{params.searchValue},'%')
        </if>
        <if test="params.label!= null">
            and F_Label = #{params.label}
        </if>
    </select>

    <select id="webList" resultType="jnpf.model.cultivate.v2.course.web.vo.V2CultivateCoursePageVo">
        select a.F_Id as id,
        a.F_Name as `name`,
        a.F_CourseId as courseId,
        a.F_ChapterNumber as chapterNumber,
        a.F_IsGrounding as isGroundIng,
        a.F_TypeId as typeId,
        b.F_Name as typeName,
        a.F_Label as label,
        a.F_LastModifyTime as updateTime
        from ftb_cultivate_course a LEFT JOIN ftb_cultivate_label b ON a.F_TypeId=b.F_Id
        <where>
            a.F_EnabledMark = 0
            <if test="params.label != null and params.label &gt; 0">
                and a.F_Label = #{params.label}
            </if>
            <if test="params.isGroundIng!= null">
                and a.F_IsGrounding = #{params.isGroundIng}
            </if>
            <if test="params.name!= null and params.name!= ''">
                and a.F_Name LIKE CONCAT('%', #{params.name}, '%')
            </if>
            <if test="params.typeId!= null and params.typeId!= ''">
                and a.F_TypeId = #{params.typeId}
            </if>
        </where>
        order by a.F_CreatorTime desc
    </select>

    <select id="queryCommonCourseApp" resultType="jnpf.model.cultivate.v2.course.vo.app.AppCommonCourseSimpleVo">
        SELECT
        p1.F_Id as courseId,
        p1.F_Name as courseName,
        p1.F_CoverUrl as coverUrl,
        p1.F_Label as label,
        p1.F_TypeId as typeId,
        p2.F_Name as typeName
        from ftb_cultivate_course as p1 LEFT JOIN ftb_cultivate_label as p2 ON p1.F_TypeId=p2.F_Id
        WHERE p1.F_EnabledMark = 0 and p1.F_Label = 1 and p1.F_IsGrounding = 1 and p1.F_EnabledMark=0
        <if test="params.keyWord != null">
            and p1.F_Name like concat('%',#{params.keyWord},'%')
        </if>
        <if test="params.typeId!= null">
            and p1.F_TypeId = #{params.typeId}
        </if>
        order by p1.F_CreatorTime desc
    </select>

    <select id="commonCourseCount" resultType="java.lang.String">
        SELECT p1.F_Id as courseId
        from ftb_cultivate_course p1 LEFT JOIN ftb_cultivate_label as p2 ON p1.F_TypeId=p2.F_Id
        WHERE p1.F_EnabledMark = 0 and p1.F_Label = 1 and p1.F_IsGrounding = 1 and p1.F_EnabledMark=0
        <if test="params.keyWord != null and params.keyWord!=''">
            and p1.F_Name like concat('%',#{params.keyWord},'%')
        </if>
        <if test="params.typeId!= null and params.typeId!= ''">
            and p1.F_TypeId = #{params.typeId}
        </if>
    </select>

    <!-- 根据课程ID统计课程学习数据 -->
    <select id="getCourseStatistics" resultType="jnpf.model.cultivate.v2.course.vo.AiHelperCourseStatisticsVo">
        SELECT
            COUNT(CASE WHEN F_state = 1 THEN 1 END) AS completedCount,
            COUNT(CASE WHEN F_state = 0 THEN 1 END) AS learningCount
        FROM ftb_cultivate_position_cource_learning
        WHERE F_CourceId = #{courseId}
          AND F_EnabledMark = 0
    </select>
</mapper>