<?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.FtbCultivatePositionCourseMapper">
  <!--学习课程数-->
  <select id="numberOfStudyCourses" resultType="java.lang.Long">
    SELECT COUNT(DISTINCT a.F_CourseId) FROM ftb_cultivate_position_course a join ftb_cultivate_course b on a.F_CourseId=b.F_Id
    WHERE a.F_PostLearnId=#{postLearnId} and a.F_EnabledMark = 0 and b.F_EnabledMark = 0 and b.F_IsGrounding=1 and b.F_Label = 2
    </select>

  <!--该岗位是否绑定课程-->
  <select id="learnMapRelearn" resultType="java.lang.String">
    SELECT b.F_CourseId FROM ftb_cultivate_position a JOIN ftb_cultivate_position_course b ON a.F_Id = b.F_PostLearnId
    WHERE a.F_EnabledMark = 0 and b.F_EnabledMark = 0 and a.F_IsGrounding = 1 and b.F_PostRankId = #{positionId}
  </select>
  <!--课程是否存在-->
  <select id="getAnExistingCourse" resultType="java.lang.String">
    SELECT F_CourceId FROM ftb_cultivate_position_cource_learning
    WHERE F_UserId = #{userId} and F_EnabledMark = 0 AND F_CourceId in
    <foreach collection="courseIds" item="courseId" separator="," open="(" close=")">
      #{courseId}
    </foreach>
  </select>
  <!--课程是否初始化-->
  <select id="getAnExistingCoursePostId" resultType="java.lang.String">
    SELECT a.F_CourseId
    FROM ftb_cultivate_position_course a JOIN ftb_cultivate_course b on a.F_CourseId=b.F_Id
    WHERE a.F_PostRankId=#{postId} and a.F_EnabledMark = 0 and b.F_EnabledMark = 0 and b.F_IsGrounding=1
  </select>
  <!--判断是否为最后一个岗位学习课程-->
  <select id="determineTheLastPostStudyCourse" resultType="java.lang.Integer">
    SELECT COUNT(0) FROM ftb_cultivate_position_course a JOIN ftb_cultivate_course b on a.F_CourseId=b.F_Id
    WHERE a.F_PostLearnId=#{postLearnId} and a.F_EnabledMark = 0 and b.F_EnabledMark = 0 and b.F_IsGrounding=1
  </select>

  <sql id="Base_Column_List">
    F_Id, F_EnabledMark, F_CreatorUserId, F_CreatorTime, F_LastModifyUserId, F_LastModifyTime,
    F_DeleteUserId, F_DeleteTime, F_TenantId, F_CourseId, F_PostRankId, F_PostLearnId,
    F_Compulsory, F_GradeId, F_SortCode, F_Sorts
  </sql>

    <!--根据岗位学习ID列表统计课程数量-->
    <select id="countByPositionLearnIds" resultType="jnpf.model.cultivate.dto.learn.BatchCommonCountDto">
        SELECT F_PostLearnId as selectKey, COUNT(*) as num
        FROM ftb_cultivate_position_course
        WHERE F_PostLearnId IN
        <foreach collection="positionLearnIds" item="id" separator="," open="(" close=")">
            #{id}
        </foreach>
        AND F_EnabledMark = 0
        GROUP BY F_PostLearnId
    </select>

    <select id="queryAllPositionLearn" resultType="jnpf.model.cultivate.v2.position.vo.CultivatePositionSimpleVo">
        SELECT
       distinct p1.F_PostId as postId,
        pc.F_GradeId AS gradeId
        FROM
        ftb_cultivate_position AS p1
        JOIN ftb_cultivate_position_course AS pc ON pc.F_PostLearnId = p1.F_Id
        JOIN ftb_cultivate_course AS course ON pc.F_CourseId = course.F_Id
        WHERE
        p1.F_EnabledMark = 0
        AND p1.F_IsGrounding = 1
        AND pc.F_EnabledMark = 0
        AND course.F_EnabledMark = 0
        AND course.F_IsGrounding = 1
        <if test="courseName != null ">
            AND course.F_Name LIKE CONCAT('%', #{courseName}, '%')
        </if>
        order by p1.F_CreatorTime desc
    </select>

    <select id="queryAllPositionLearnCourseLists" resultType="jnpf.model.cultivate.v2.course.vo.app.AppCourseSimpleVo">
        SELECT
        p1.F_Id as businessSourceId,
        p1.F_PostId as postId,
        pc.F_GradeId AS gradeId,
        pc.F_CourseId as courseId,
        course.F_Name as courseName,
        course.F_CoverUrl as coverUrl,
        course.F_CoverId as coverId,
        course.F_Label as label,
        course.F_TypeId as typeId,
        pc.F_Compulsory as isRequired

        FROM
        ftb_cultivate_position AS p1
        JOIN ftb_cultivate_position_course AS pc ON pc.F_PostLearnId = p1.F_Id
        JOIN ftb_cultivate_course AS course ON pc.F_CourseId = course.F_Id
        WHERE
        p1.F_EnabledMark = 0
        AND p1.F_IsGrounding = 1
        AND pc.F_EnabledMark = 0
        AND course.F_EnabledMark = 0
        AND course.F_IsGrounding = 1

        <if test="params.keyWord != null and params.keyWord != ''">
            AND course.F_Name LIKE CONCAT('%', #{params.keyWord}, '%')
        </if>
        <if test="params.postId != null and params.postId != ''">
            AND p1.F_PostId = #{params.postId}
        </if>

        <if test="params.gradeId != null">
            AND pc.F_GradeId = #{params.gradeId}
        </if>
        order by p1.F_CreatorTime desc
    </select>

    <select id="listByPostLearnIdAndExamId" resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourse">
        select pc.F_Id          as id,
               pc.F_CourseId    as courseId,
               pc.F_PostRankId  as postRankId,
               pc.F_PostLearnId as postLearnId,
               pc.F_Compulsory  as compulsory,
               pc.F_GradeId     as gradeId,
               pc.F_SortCode    as sortCode
        from ftb_cultivate_position_course as pc
                 left join ftb_cultivate_position_course_exam as ce on pc.F_CourseId = ce.F_CourseId
        where pc.F_PostLearnId = #{positionLearnId}
          and ce.F_PostLearnId = #{positionLearnId}
          and pc.F_GradeId = #{gradeId}
          and ce.F_GradeId = #{gradeId}
          and pc.F_EnabledMark = 0
          and ce.F_EnabledMark = 0
          and ce.F_ExamId = #{examId}
    </select>

    <select id="listByPostLearnIdAndPracticeId"
            resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourse">
        select pc.F_Id          as id,
               pc.F_CourseId    as courseId,
               pc.F_PostRankId  as postRankId,
               pc.F_PostLearnId as postLearnId,
               pc.F_Compulsory  as compulsory,
               pc.F_GradeId     as gradeId,
               pc.F_SortCode    as sortCode
        from ftb_cultivate_position_course as pc
                 left join ftb_cultivate_position_course_practice as ce on pc.F_CourseId = ce.F_CourseId
        where pc.F_PostLearnId = #{positionLearnId}
          and ce.F_PostLearnId = #{positionLearnId}
          and pc.F_GradeId = #{gradeId}
          and ce.F_GradeId = #{gradeId}
          and pc.F_EnabledMark = 0
          and ce.F_EnabledMark = 0
          and ce.F_BusinessId = #{practiceId}
    </select>

    <select id="listByPostLearnIdAndIdentityId"
            resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourse">
        select pc.F_Id          as id,
               pc.F_CourseId    as courseId,
               pc.F_PostRankId  as postRankId,
               pc.F_PostLearnId as postLearnId,
               pc.F_Compulsory  as compulsory,
               pc.F_GradeId     as gradeId,
               pc.F_SortCode    as sortCode
        from ftb_cultivate_position_course as pc
                 left join ftb_cultivate_position_course_identity as ce on pc.F_CourseId = ce.F_CourseId
        where pc.F_PostLearnId = #{positionLearnId}
          and ce.F_PostLearnId = #{positionLearnId}
          and pc.F_GradeId = #{gradeId}
          and ce.F_GradeId = #{gradeId}
          and pc.F_EnabledMark = 0
          and ce.F_EnabledMark = 0
          and ce.F_identityId = #{practiceId}
    </select>

  <select id="queryAllPositionCourseAndLearnIdAndGradeId"
            resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourse">
      select pc.F_Id          as id,
             pc.F_CourseId    as courseId,
             pc.F_PostRankId  as postRankId,
             pc.F_PostLearnId as postLearnId,
             pc.F_Compulsory  as compulsory,
             pc.F_GradeId     as gradeId,
             pc.F_SortCode    as sortCode
      from ftb_cultivate_position_course as pc

      where pc.F_PostLearnId = #{positionLearnId}
        and pc.F_GradeId = #{gradeId}
        and pc.F_EnabledMark = 0
    </select>

    <select id="listPositionCourseList" resultType="jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseWithNameVo">
        select pc.F_Id as id,
        pc.F_CourseId as courseId,
        course.F_Name as courseName,
        course.F_CourseId as showCourseId,
        course.F_TypeId as typeId,
        course.F_Label as label,
        pc.F_PostRankId as postRankId,
        pc.F_PostLearnId as postLearnId,
        pc.F_Compulsory as compulsory,
        pc.F_GradeId as gradeId,
        pc.F_SortCode as sortCode
        from ftb_cultivate_position_course as pc
        join ftb_cultivate_course as course on pc.F_CourseId = course.F_Id

        where pc.F_EnabledMark = 0
        and course.F_EnabledMark = 0
        and course.F_IsGrounding = 1
        and pc.F_PostLearnId = #{position.id}
        <if test="position.isConfiguredToGrade ==1">
            and pc.F_GradeId = #{req.gradeId}
        </if>

        order by pc.F_SortCode asc
    </select>

    <!--根据岗位学习 ID 查询所有有效课程（关联课程表，过滤上架且未删除）-->
    <select id="listByPostLearnId" resultType="jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseWithNameVo">
        select pc.F_Id          as id,
               pc.F_CourseId    as courseId,
               pc.F_PostRankId  as postRankId,
               pc.F_PostLearnId as postLearnId,
               pc.F_Compulsory  as compulsory,
               pc.F_GradeId     as gradeId,
               pc.F_SortCode    as sortCode,
               course.F_Name as courseName,
               course.F_CourseId as showCourseId,
                course.F_Name as courseName,
                course.F_CourseId as showCourseId,
                course.F_IsGrounding as isGrounding,
                course.F_CoverUrl as coverUrl
        from ftb_cultivate_position_course as pc
        join ftb_cultivate_course as course on pc.F_CourseId = course.F_Id
        where pc.F_PostLearnId = #{postLearnId}
          and pc.F_EnabledMark = 0
          and course.F_EnabledMark = 0
          and course.F_IsGrounding = 1
        <if test="courseId != null and courseId != ''">
          and pc.F_CourseId = #{courseId}
        </if>
        order by pc.F_SortCode asc
    </select>

    <!--根据岗位学习 ID 和职级 ID 查询所有有效课程（关联课程表，过滤上架且未删除）-->
    <select id="listByPostLearnIdAndGradeId" resultType="jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseWithNameVo">
        select pc.F_Id          as id,
               pc.F_CourseId    as courseId,
               pc.F_PostRankId  as postRankId,
               pc.F_PostLearnId as postLearnId,
               pc.F_Compulsory  as compulsory,
               pc.F_GradeId     as gradeId,
               pc.F_SortCode    as sortCode,
               course.F_Name as courseName,
               course.F_CourseId as showCourseId,
               course.F_IsGrounding as isGrounding,
               course.F_CoverUrl as coverUrl
        from ftb_cultivate_position_course as pc
        join ftb_cultivate_course as course on pc.F_CourseId = course.F_Id
        where pc.F_PostLearnId = #{postLearnId}
          and pc.F_GradeId = #{gradeId}
          and pc.F_EnabledMark = 0
          and course.F_EnabledMark = 0
          and course.F_IsGrounding = 1
        <if test="courseId != null and courseId != ''">
          and pc.F_CourseId = #{courseId}
        </if>
        order by pc.F_SortCode asc
    </select>

  <select id="queryPositionBindItem" resultType="java.lang.String">
      SELECT
      pc.F_CourseId as courseId
      FROM
      ftb_cultivate_position_course AS pc
      JOIN ftb_cultivate_course AS course ON pc.F_CourseId = course.F_Id
      WHERE
      pc.F_EnabledMark = 0
      AND course.F_EnabledMark = 0
      AND course.F_IsGrounding = 1

      and pc.F_PostLearnId = #{position.id}
      <if test="position.isConfiguredToGrade ==1">
          and pc.F_GradeId = #{gradeId}
      </if>
    </select>

    <!--根据岗位学习ID查询所有有效课程（包含课程名称）-->
    <select id="listByPostLearnIdWithName" resultType="jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseWithNameVo">
        select pc.F_Id          as id,
               pc.F_CourseId    as courseId,
               course.F_Name    as courseName,
               course.F_CourseId    as showCourseId,
               course.F_TypeId    as typeId,
               course.F_Label      as label,
               pc.F_PostRankId  as postRankId,
               pc.F_PostLearnId as postLearnId,
               pc.F_Compulsory  as compulsory,
               pc.F_GradeId     as gradeId,
               pc.F_SortCode    as sortCode
        from ftb_cultivate_position_course as pc
        join ftb_cultivate_course as course on pc.F_CourseId = course.F_Id
        where pc.F_PostLearnId = #{postLearnId}
          and pc.F_EnabledMark = 0
          and course.F_EnabledMark = 0
          and course.F_IsGrounding = 1
        <if test="courseId != null and courseId != ''">
          and pc.F_CourseId = #{courseId}
        </if>
        order by pc.F_SortCode asc
    </select>

    <!--根据岗位学习配置查询所有课程ID列表（关联课程表过滤）-->
    <select id="queryCourseIdsByPositionAndGrade" resultType="java.lang.String">
        SELECT DISTINCT pc.F_CourseId
        FROM ftb_cultivate_position_course AS pc
        JOIN ftb_cultivate_course AS course ON pc.F_CourseId = course.F_Id
        WHERE pc.F_PostLearnId = #{postLearnId}
          AND pc.F_EnabledMark = 0
          AND course.F_EnabledMark = 0
          AND course.F_IsGrounding = 1
          AND course.F_Label = 2
        <if test="isConfiguredToGrade == 1">
          AND pc.F_GradeId = #{gradeId}
        </if>
    </select>
</mapper>