<?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.FtbCultivatePositionCourseExamMapper">
  <resultMap id="BaseResultMap" type="jnpf.model.cultivate.po.position.FtbCultivatePositionCourseExam">
    <!--@mbg.generated-->
    <!--@Table ftb_cultivate_position_course_exam-->
    <id column="F_Id" jdbcType="VARCHAR" property="fId" />
    <result column="F_EnabledMark" jdbcType="INTEGER" property="fEnabledmark" />
    <result column="F_CreatorUserId" jdbcType="VARCHAR" property="fCreatoruserid" />
    <result column="F_CreatorTime" jdbcType="TIMESTAMP" property="fCreatortime" />
    <result column="F_LastModifyUserId" jdbcType="VARCHAR" property="fLastmodifyuserid" />
    <result column="F_LastModifyTime" jdbcType="TIMESTAMP" property="fLastmodifytime" />
    <result column="F_DeleteUserId" jdbcType="VARCHAR" property="fDeleteuserid" />
    <result column="F_DeleteTime" jdbcType="TIMESTAMP" property="fDeletetime" />
    <result column="F_TenantId" jdbcType="VARCHAR" property="fTenantid" />
    <result column="F_CourseId" jdbcType="VARCHAR" property="fCourseid" />
    <result column="F_PostRankId" jdbcType="VARCHAR" property="fPostrankid" />
    <result column="F_PostLearnId" jdbcType="VARCHAR" property="fPostlearnid" />
    <result column="F_examId" jdbcType="VARCHAR" property="fExamid" />
  </resultMap>
  <sql id="Base_Column_List">
    <!--@mbg.generated-->
    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_examId
  </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_exam
    WHERE F_PostLearnId IN
    <foreach collection="positionLearnIds" item="id" separator="," open="(" close=")">
      #{id}
    </foreach>
    AND F_EnabledMark = 0
    GROUP BY F_PostLearnId
  </select>
    
  <!--根据岗位学习 ID 查询考试（关联考试表，要求考试未删除且有效）-->
  <select id="listByPostLearnIdWithExam" resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourseExam">
    SELECT pc.F_Id as id,
      pc.F_CourseId as courseId,
      pc.F_PostRankId as postRankId,
      pc.F_GradeId as gradeId,
      pc.F_PostLearnId as postLearnId,
      pc.F_examId as examId,
      pc.F_EnabledMark as enabledMark
    FROM ftb_cultivate_position_course_exam pc
    INNER JOIN ftb_cultivate_exam e ON pc.F_examId = e.F_Id
    WHERE pc.F_PostLearnId = #{postLearnId}
    AND pc.F_EnabledMark = 0
    AND e.F_EnabledMark = 1
  </select>
    
  <!--根据岗位学习 ID 和课程 ID 列表查询考试（关联考试表，要求考试未删除且有效）-->
  <select id="listByPostLearnIdAndCourseIdsWithExam" resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourseExam">
    SELECT pc.F_Id as id,
    pc.F_CourseId as courseId,
    pc.F_PostRankId as postRankId,
    pc.F_GradeId as gradeId,
    pc.F_PostLearnId as postLearnId,
    pc.F_examId as examId,
    pc.F_EnabledMark as enabledMark
    FROM ftb_cultivate_position_course_exam pc
    INNER JOIN ftb_cultivate_exam e ON pc.F_examId = e.F_Id
    WHERE pc.F_PostLearnId = #{postLearnId}
    AND pc.F_CourseId IN
    <foreach collection="courseIds" item="courseId" separator="," open="(" close=")">
      #{courseId}
    </foreach>
    <if test="gradeId != null and gradeId!=''">
      and pc.F_GradeId = #{gradeId}
    </if>

    AND pc.F_EnabledMark = 0
    AND e.F_EnabledMark = 1
  </select>
    
  <!--查询所有岗位学习考试（关联考试表，要求考试未删除且有效）-->
  <select id="queryAllPositionLearnExamListsWithExam" resultType="jnpf.model.cultivate.po.position.FtbCultivatePositionCourseExam">
    SELECT pc.F_Id as id,
           pc.F_CourseId as courseId,
           pc.F_PostRankId as postRankId,
           pc.F_GradeId as gradeId,
           pc.F_PostLearnId as postLearnId,
           pc.F_examId as examId,
           pc.F_EnabledMark as enabledMark
    FROM ftb_cultivate_position_course_exam pc
    INNER JOIN ftb_cultivate_exam e ON pc.F_examId = e.F_Id
    WHERE pc.F_EnabledMark = 0
    AND e.F_EnabledMark = 1
  </select>

  <select id="queryPositionBindItem" resultType="java.lang.String">
    SELECT
           pc.F_examId as examId
    FROM ftb_cultivate_position_course_exam pc
           INNER JOIN ftb_cultivate_exam e ON pc.F_examId = e.F_Id
    WHERE pc.F_EnabledMark = 0
      AND e.F_EnabledMark = 1

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

  </select>

  <select id="queryAllConfigExamId" resultType="jnpf.model.cultivate.v2.position.vo.V2AllCultivatePositionCourseExam">
    SELECT
          distinct pc.F_examId as examId,
           pc.F_PostLearnId as postLearnId,
           pc.F_GradeId as gradeId
    FROM ftb_cultivate_position_course_exam pc
           INNER JOIN ftb_cultivate_exam e ON pc.F_examId = e.F_Id
           inner join ftb_cultivate_course c on pc.F_CourseId = c.F_Id
    WHERE pc.F_EnabledMark = 0
      AND e.F_EnabledMark = 1
    and c.F_EnabledMark = 0 and c.F_IsGrounding = 1
    AND pc.F_PostLearnId IN
    <foreach collection="postLearnIds" item="id" separator="," open="(" close=")">
      #{id}
    </foreach>
    <if test="gradeId != null and gradeId!=''">
      and pc.F_GradeId = #{gradeId}
    </if>
    <if test="courseId != null">
      and pc.F_CourseId = #{courseId}
    </if>
    </select>

  <!--根据岗位学习ID查询所有有效考试（包含考试名称）-->
  <select id="listByPostLearnIdWithName" resultType="jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseExamWithNameVo">
    SELECT pc.F_Id as id,
      pc.F_CourseId as courseId,
      pc.F_PostRankId as postRankId,
      pc.F_GradeId as gradeId,
      pc.F_PostLearnId as postLearnId,
      pc.F_examId as examId,
      e.F_ExamName as examName,
      pc.F_EnabledMark as enabledMark
    FROM ftb_cultivate_position_course_exam pc
    INNER JOIN ftb_cultivate_exam e ON pc.F_examId = e.F_Id
    WHERE pc.F_PostLearnId = #{postLearnId}
    AND pc.F_EnabledMark = 0
    AND e.F_EnabledMark = 1
  </select>

  <!--根据岗位学习 ID 和课程 ID 列表查询考试（包含考试名称）-->
  <select id="listByPostLearnIdAndCourseIdsWithName" resultType="jnpf.model.cultivate.v2.position.vo.FtbCultivatePositionCourseExamWithNameVo">
    SELECT pc.F_Id as id,
      pc.F_CourseId as courseId,
      pc.F_PostRankId as postRankId,
      pc.F_GradeId as gradeId,
      pc.F_PostLearnId as postLearnId,
      pc.F_examId as examId,
      e.F_ExamName as examName,
      pc.F_EnabledMark as enabledMark
    FROM ftb_cultivate_position_course_exam pc
    INNER JOIN ftb_cultivate_exam e ON pc.F_examId = e.F_Id
    WHERE pc.F_PostLearnId = #{postLearnId}
    AND pc.F_CourseId IN
    <foreach collection="courseIds" item="courseId" separator="," open="(" close=")">
      #{courseId}
    </foreach>
    AND pc.F_EnabledMark = 0
    AND e.F_EnabledMark = 1
  </select>

  <!--检查考试是否被岗位学习绑定（关联岗位学习主表和课程表）-->
  <select id="checkExamBinding" resultType="java.lang.Integer">
    SELECT COUNT(1)
    FROM ftb_cultivate_position_course_exam pce
             INNER JOIN ftb_cultivate_position_course pc ON pce.F_CourseId = pc.F_CourseId
             AND pce.F_PostLearnId = pc.F_PostLearnId
             INNER JOIN ftb_cultivate_position p ON pc.F_PostLearnId = p.F_Id
    WHERE pce.F_examId = #{examId}
      AND pce.F_EnabledMark = 0
      AND pc.F_EnabledMark = 0
      AND p.F_EnabledMark = 0
  </select>
</mapper>