<?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.FtbCultivateOfflineTrainMapper">
    <!--线下培训课程信息-->
  <select id="listOfflineCourseVO" resultType="jnpf.model.cultivate.vo.offline.OfflineCourseVO">
      select
      a.F_CourseId as courseId,
      b.F_CoverUrl as coverUrl,
      b.F_Name as courseName

      from
      ftb_cultivate_offline_course a JOIN ftb_cultivate_course b ON a.F_CourseId = b.F_Id
      where b.F_EnabledMark = 0 and a.F_OffilneId = #{offlineId}
    </select>
    <sql id="signInTotalNum">
        (SELECT COUNT(0) FROM ftb_cultivate_offline_user WHERE F_OffilneId = a.F_Id and F_EnabledMark=0) as signInTotalNum
    </sql>
    <sql id="signInNum">
        (SELECT COUNT(0) FROM ftb_cultivate_offline_user WHERE F_OffilneId = a.F_Id and F_SignWhether = 1  and F_EnabledMark=0) as signInNum
    </sql>
    <!--线下培训分页-->
  <select id="listPage" resultType="jnpf.model.cultivate.vo.offline.FtbCultivateOfflineTrainPageVO">
      select a.F_Id as id,
             a.F_Name as `name`,
             a.F_OfflinePlace as offlinePlace,
             a.F_UserId as userId,
             a.F_HeadUserId as headUserId,
             a.F_TrainStartTime as trainStartTime,
             a.F_TrainEndTime as trainEndTime,
             a.F_State as state,
             a.F_SignWay as signWay,
             <include refid="signInTotalNum"/>,
             <include refid="signInNum"/>
        from ftb_cultivate_offline_train a
      <where>
          a.F_EnabledMark = 0
          <if test="keywords!= null and keywords!= ''">
              and a.F_Name like CONCAT('%',#{keywords},'%')
          </if>
      </where>
  </select>
    <!--线下培训app查询-->
  <select id="listOfflineTrainingApp" resultType="jnpf.model.cultivate.vo.offline.FtbOfflineTrainingAppPageVO">
      SELECT a.F_Id as id,a.F_Name as name,
      a.F_HeadUserId as headUserId,a.F_TrainStartTime as trainStartTime,a.F_TrainEndTime as trainEndTime
      ,a.F_TrainContent as trainContent,a.F_SignWay as signWay,a.F_State as state,
      (select F_SignWhether FROM ftb_cultivate_offline_user WHERE F_OffilneId = a.F_Id and F_UserId = #{userId} and F_EnabledMark=0) as checkInOrNot
      <if test="type == 3">
          ,<include refid="signInTotalNum"/>,
          <include refid="signInNum"/>
      </if>
      FROM ftb_cultivate_offline_train a
      <where>
          a.F_EnabledMark = 0 and a.F_State != 3
          <if test="type == null ">
             and (a.F_UserId = #{userId} or FIND_IN_SET(#{userId},a.F_Participants) > 0 or a.F_HeadUserId = #{userId})
          </if>
          <if test="type == 1">
              and a.F_UserId = #{userId}
          </if>
          <if test="type == 2">
              and FIND_IN_SET(#{userId},a.F_Participants) > 0
          </if>
          <if test="type == 3">
              and a.F_HeadUserId = #{userId}
          </if>
      </where>
  </select>
    <!--线下培训课程信息根据线下培训id查询-->
  <select id="listOfflineWebCourseVO" resultType="jnpf.model.cultivate.vo.offline.OfflineCourseVO">
      select a.F_CourseId as courseId,
      b.F_Name as courseName
      from
      ftb_cultivate_offline_course a JOIN ftb_cultivate_course b ON a.F_CourseId = b.F_Id
      where a.F_OffilneId = #{offlineId}
    </select>
    <!--线下培训附件删除-->
  <delete id="deleteOfflineTrainFileById">
      delete from ftb_cultivate_file where F_BusinessId = #{id}
   </delete>
    <update id="deleteLeaveUser">
        UPDATE ftb_cultivate_offline_user
        SET F_EnabledMark = 1
        WHERE F_UserId IN
        <foreach collection="userIds" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>
    </update>
    <!--线下培训已绑定课程-->
  <select id="queryCourseIdsByOfflineId" resultType="java.lang.String">
      SELECT F_CourseId from ftb_cultivate_offline_course WHERE F_OffilneId = #{offlineId} and F_EnabledMark = 0
   </select>
    <!--线下培训签到人数分页查询-->
  <select id="numberOfflineTraining"
            resultType="jnpf.model.cultivate.vo.offline.FtbCultivateOfflineTrainPeopleSigningInVO">
      SELECT
      a.F_UserId as userId,
      a.F_SignPhoto as checkInPhoto,
      a.F_SignWhether as checkInStatus,
      a.F_SignTime as checkInTime,
      b.F_RealName as signInPersonName
      FROM ftb_cultivate_offline_user a left JOIN base_user b ON a.F_UserId=b.F_Id
      where a.F_OffilneId = #{params.offlineTrainId} and a.F_EnabledMark=0
      <if test="params.checkInStatus != null">
          and a.F_SignWhether = #{params.checkInStatus}
      </if>
      <if test="params.signInPersonName != null and params.signInPersonName != ''">
          and b.F_RealName like CONCAT('%',#{params.signInPersonName},'%')
      </if>
    </select>
    <select id="queryMyOfflineTrainList" resultType="jnpf.model.cultivate.dto.storestatistics.dto.StoreOfflineTrainDto">
        SELECT
            t.F_Id AS id,
            t.F_Name AS name,
            t.F_State as state,
            t.F_TrainStartTime as trainStartTime,
            t.F_TrainEndTime as trainEndTime,
            u.F_SignTime AS signTime,
            u.F_SignWhether AS signWhether
        FROM
            ftb_cultivate_offline_train AS t
                JOIN
            ftb_cultivate_offline_user AS u ON t.F_Id = u.F_OffilneId
        WHERE
            t.F_EnabledMark = 0 and u.F_EnabledMark=0
          AND u.F_UserId = #{userId}
    </select>
    <select id="queryMyOfflineTrainingHeader"
            resultType="jnpf.model.cultivate.dto.storestatistics.dto.StoreOfflineTrainDto">
        SELECT t.F_Id             AS id,
               t.F_Name           AS name,
               t.F_State          as state,
               t.F_TrainStartTime as trainStartTime,
               t.F_TrainEndTime   as trainEndTime
        FROM ftb_cultivate_offline_train AS t
        WHERE t.F_EnabledMark = 0
          AND t.F_HeadUserId = #{userId}

    </select>
</mapper>