<?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="com.fs.course.mapper.FsCourseWatchLogMapper">

    <resultMap type="FsCourseWatchLog" id="FsCourseWatchLogResult">
        <result property="logId"    column="log_id"    />
        <result property="userId"    column="user_id"    />
        <result property="videoId"    column="video_id"    />
        <result property="logType"    column="log_type"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="qwExternalContactId"    column="qw_external_contact_id"    />
        <result property="duration"    column="duration"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="companyId"    column="company_id"    />
        <result property="courseId"    column="course_id"    />
        <result property="sendType"    column="send_type"    />
        <result property="rewardType"    column="reward_type"    />
        <result property="sopId"    column="sop_id"    />
        <result property="finishTime"    column="finish_time"    />
        <result property="sendFinishMsg"    column="send_finish_msg"    />
        <result property="campPeriodTime"    column="camp_period_time"    />
        <result property="lastHeartbeatTime"    column="last_heartbeat_time"    />
    </resultMap>

    <sql id="selectFsCourseWatchLogVo">
        select log_id, user_id,finish_time,send_finish_msg,sop_id,video_id,reward_type, log_type, create_time, update_time, qw_external_contact_id, duration, qw_user_id, company_user_id, company_id, course_id,camp_period_time from fs_course_watch_log
    </sql>

    <select id="selectFsCourseWatchLogList" parameterType="FsCourseWatchLog" resultMap="FsCourseWatchLogResult">
        <include refid="selectFsCourseWatchLogVo"/>
        <where>
            <if test="userId != null "> and user_id = #{userId}</if>
            <if test="videoId != null "> and video_id = #{videoId}</if>
            <if test="logType != null "> and log_type = #{logType}</if>
            <if test="qwExternalContactId != null "> and qw_external_contact_id = #{qwExternalContactId}</if>
            <if test="duration != null "> and duration = #{duration}</if>
            <if test="qwUserId != null  and qwUserId != ''"> and qw_user_id = #{qwUserId}</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="courseId != null "> and course_id = #{courseId}</if>
            <if test="sendType != null "> and send_type = #{sendType}</if>
            <if test="campPeriodTime != null "> and camp_period_time = #{campPeriodTime}</if>
        </where>
    </select>

    <select id="selectFsCourseWatchLogByLogId" parameterType="Long" resultMap="FsCourseWatchLogResult">
        <include refid="selectFsCourseWatchLogVo"/>
        where log_id = #{logId}
    </select>
    <select id="selectFsCourseWatchLogListVO" resultType="com.fs.course.vo.FsCourseWatchLogListVO">
        select l.log_id,
               l.project as project,
               l.user_id,uc.course_name,v.title as video_name,u.nick_name as fsNickName, u.avatar as fsAvatar,
        l.log_type,SEC_TO_TIME(l.duration) as duration,c.company_name,l.camp_period_time,l.finish_time,
        cu.nick_name as company_user_name ,l.send_type,l.create_time,l.update_time,l.last_heartbeat_time,
        qu.qw_user_name,qec.name as external_user_name,c.company_id
         from fs_course_watch_log l
         left join fs_user_course_video v on v.video_id = l.video_id
         left join fs_user_course uc on uc.course_id = l.course_id
         left join fs_user u on u.user_id = l.user_id
         left join company_user cu on cu.user_id = l.company_user_id
         left join company c on c.company_id = l.company_id
         LEFT JOIN qw_user qu on qu.id= l.qw_user_id
         LEFT JOIN qw_external_contact qec on l.qw_external_contact_id = qec.id
        <where>
            <if test ='maps.userId !=null'>
                and l.user_id = #{maps.userId}
            </if>
            <if test ='maps.qwExternalContactId !=null'>
                and l.qw_external_contact_id = #{maps.qwExternalContactId}
            </if>
            <if test ='maps.qwUserId !=null'>
                and l.qw_user_id = #{maps.qwUserId}
            </if>
            <if test ='maps.courseId !=null'>
                and l.course_id = #{maps.courseId}
            </if>
            <if test ='maps.videoId !=null'>
                and l.video_id = #{maps.videoId}
            </if>
            <if test ='maps.logType !=null'>
                and l.log_type = #{maps.logType}
            </if>
            <if test ='maps.companyId !=null'>
                and l.company_id = #{maps.companyId}
            </if>
            <if test ='maps.companyUserId !=null'>
                and l.company_user_id = #{maps.companyUserId}
            </if>
            <if test ='maps.companyUserName !=null and maps.companyUserName!=""'>
                and cu.nick_name  like concat('%', #{maps.companyUserName}, '%')
            </if>
            <if test ='maps.nickName !=null and maps.nickName!=""'>
                and u.nick_name  like concat('%', #{maps.nickName}, '%')
            </if>
            <if test ='maps.externalUserName !=null and maps.externalUserName!=""'>
                and qec.name  like concat('%', #{maps.externalUserName}, '%')
            </if>
            <if test= 'maps.sTime != null '>
                and DATE(l.create_time) &gt;= DATE(#{maps.sTime})
            </if>
            <if test='maps.eTime != null '>
                and DATE(l.create_time) &lt;= DATE(#{maps.eTime})
            </if>
            <if test= 'maps.scheduleStartTime != null '>
                and DATE(l.camp_period_time) &gt;= DATE(#{maps.scheduleStartTime})
            </if>
            <if test='maps.scheduleEndTime != null '>
                and DATE(l.camp_period_time) &lt;= DATE(#{maps.scheduleEndTime})
            </if>
            <if test= 'maps.upSTime != null '>
                and DATE(l.update_time) &gt;= DATE(#{maps.upSTime})
            </if>
            <if test='maps.upETime != null '>
                and DATE(l.update_time) &lt;= DATE(#{maps.upETime})
            </if>
            <if test="maps.sopIds != null and maps.sopIds.size() > 0">
                and l.sop_id in
                <foreach item="sopId" index="index" collection="maps.sopIds" open="(" separator="," close=")">
                    #{sopId}
                </foreach>
            </if>
        </where>
         order by l.log_id desc
    </select>

    <select id="selectFsCourseWatchLogListByParam" resultType="com.fs.course.vo.FsCourseWatchLogListVO">
        select l.log_id,l.user_id,uc.course_name,v.title as video_name,u.nick_name as fsNickName, u.avatar as fsAvatar,
        l.log_type,SEC_TO_TIME(l.duration) as duration,c.company_name,l.camp_period_time,l.finish_time,
        cu.nick_name as company_user_name ,l.send_type,l.create_time, qu.qw_user_name,qec.name as external_user_name
        from fs_course_watch_log l
        left join fs_user_course_video v on v.video_id = l.video_id
        left join fs_user_course uc on uc.course_id = l.course_id
        left join fs_user u on u.user_id = l.user_id
        left join company_user cu on cu.user_id = l.company_user_id
        left join company c on c.company_id = l.company_id
        LEFT JOIN qw_user qu on qu.id= l.qw_user_id
        LEFT JOIN qw_external_contact qec on l.qw_external_contact_id = qec.id
        <where>
            <if test ='userId !=null'>
                and l.user_id = #{userId}
            </if>
            <if test ='qwUserId !=null'>
                and l.qw_user_id = #{qwUserId}
            </if>
            <if test ='courseId !=null'>
                and l.course_id = #{courseId}
            </if>
            <if test ='videoId !=null'>
                and l.video_id = #{videoId}
            </if>
            <if test ='logType !=null'>
                and l.log_type = #{logType}
            </if>
            <if test ='companyId !=null'>
                and l.company_id = #{companyId}
            </if>
            <if test ='companyUserId !=null'>
                and l.company_user_id = #{companyUserId}
            </if>
            <if test ='companyUserName !=null and maps.companyUserName!=""'>
                and cu.nick_name  like concat('%', #{companyUserName}, '%')
            </if>
            <if test ='nickName !=null and maps.nickName!=""'>
                and u.nick_name  like concat('%', #{nickName}, '%')
            </if>
            <if test= 'sTime != null '>
                and DATE(l.create_time) &gt;= DATE(#{sTime})
            </if>
            <if test='eTime != null '>
                and DATE(l.create_time) &lt;= DATE(#{eTime})
            </if>
            <if test= 'maps.scheduleStartTime != null '>
                and DATE(l.camp_period_time) &gt;= DATE(#{maps.scheduleStartTime})
            </if>
            <if test='maps.scheduleEndTime != null '>
                and DATE(l.camp_period_time) &lt;= DATE(#{maps.scheduleEndTime})
            </if>
            <if test="sopIds != null and sopIds.size() > 0">
                and l.sop_id in
                <foreach item="sopId" index="index" collection="sopIds" open="(" separator="," close=")">
                    #{sopId}
                </foreach>
            </if>
        </where>
        order by l.log_id desc
    </select>
    <insert id="insertFsCourseWatchLog" parameterType="FsCourseWatchLog" useGeneratedKeys="true" keyProperty="logId">
        insert into fs_course_watch_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="videoId != null">video_id,</if>
            <if test="logType != null">log_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="qwExternalContactId != null">qw_external_contact_id,</if>
            <if test="duration != null">duration,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="courseId != null">course_id,</if>
            <if test="sendType != null">send_type,</if>
            <if test="rewardType != null">reward_type,</if>
            <if test="sopId != null">sop_id,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="sendFinishMsg != null">send_finish_msg,</if>
            <if test="campPeriodTime != null">camp_period_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="logType != null">#{logType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="qwExternalContactId != null">#{qwExternalContactId},</if>
            <if test="duration != null">#{duration},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="courseId != null">#{courseId},</if>
            <if test="sendType != null">#{sendType},</if>
            <if test="rewardType != null">#{rewardType},</if>
            <if test="sopId != null">#{sopId},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="sendFinishMsg != null">#{sendFinishMsg},</if>
            <if test="campPeriodTime != null">#{campPeriodTime},</if>
        </trim>
    </insert>

    <insert id="insertOrUpdateFsCourseWatchLog" parameterType="FsCourseWatchLog">
        insert into fs_course_watch_log
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="videoId != null">video_id,</if>
            <if test="logType != null">log_type,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="qwExternalContactId != null">qw_external_contact_id,</if>
            <if test="duration != null">duration,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="companyId != null">company_id,</if>
            <if test="courseId != null">course_id,</if>
            <if test="sendType != null">send_type,</if>
            <if test="rewardType != null">reward_type,</if>
            <if test="sopId != null">sop_id,</if>
            <if test="finishTime != null">finish_time,</if>
            <if test="sendFinishMsg != null">send_finish_msg,</if>
            <if test="campPeriodTime != null">camp_period_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="logType != null">#{logType},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="qwExternalContactId != null">#{qwExternalContactId},</if>
            <if test="duration != null">#{duration},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="courseId != null">#{courseId},</if>
            <if test="sendType != null">#{sendType},</if>
            <if test="rewardType != null">#{rewardType},</if>
            <if test="sopId != null">#{sopId},</if>
            <if test="finishTime != null">#{finishTime},</if>
            <if test="sendFinishMsg != null">#{sendFinishMsg},</if>
            <if test="campPeriodTime != null">#{campPeriodTime},</if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
    </insert>


    <insert id="insertFsCourseWatchLogBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="logId">
        INSERT INTO fs_course_watch_log (
        user_id,
        video_id,
        log_type,
        create_time,
        update_time,
        qw_external_contact_id,
        duration,
        qw_user_id,
        company_user_id,
        company_id,
        course_id,
        send_type,
        reward_type,
        sop_id,
        camp_period_time
        )
        VALUES
        <foreach collection="watchLogs" item="log" separator=",">
            (
            #{log.userId},
            #{log.videoId},
            #{log.logType},
            #{log.createTime},
            #{log.updateTime},
            #{log.qwExternalContactId},
            #{log.duration},
            #{log.qwUserId},
            #{log.companyUserId},
            #{log.companyId},
            #{log.courseId},
            #{log.sendType},
            #{log.rewardType},
            #{log.sopId},
            #{log.campPeriodTime}
            )
        </foreach>
        ON DUPLICATE KEY UPDATE
        update_time = VALUES(update_time)
    </insert>



    <update id="updateFsCourseWatchLog" parameterType="FsCourseWatchLog">
        update fs_course_watch_log
        <trim prefix="SET" suffixOverrides=",">
            <if test="userId != null">user_id = #{userId},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="logType != null">log_type = #{logType},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="qwExternalContactId != null">qw_external_contact_id = #{qwExternalContactId},</if>
            <if test="duration != null">duration = #{duration},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="courseId != null">course_id = #{courseId},</if>
            <if test="sendType != null">send_type = #{sendType},</if>
            <if test="rewardType != null">reward_type = #{rewardType},</if>
            <if test="sopId != null">sop_id = #{sopId},</if>
            <if test="finishTime != null">finish_time = #{finishTime},</if>
            <if test="sendFinishMsg != null">send_finish_msg = #{sendFinishMsg},</if>
            <if test="lastHeartbeatTime != null">last_heartbeat_time = #{lastHeartbeatTime},</if>
        </trim>
        where log_id = #{logId}
    </update>

    <delete id="deleteFsCourseWatchLogByLogId" parameterType="Long">
        delete from fs_course_watch_log where log_id = #{logId}
    </delete>

    <delete id="deleteFsCourseWatchLogByLogIds" parameterType="String">
        delete from fs_course_watch_log where log_id in
        <foreach item="logId" collection="array" open="(" separator="," close=")">
            #{logId}
        </foreach>
    </delete>

    <select id="selectFsCourseWatchLogByFinishTime" resultType="com.fs.course.param.FsCourseWatchLogByFinishTimeParam">
        <![CDATA[
        SELECT
            fcwl.log_id,
            fcwl.create_time,
            fcwl.qw_external_contact_id,
            fcwl.qw_user_id,
            fcwl.user_id,
            fcwl.company_user_id,
            fcwl.company_id,
            fcwl.sop_id,
            fcwl.finish_time,
            fcwl.camp_period_time,
            qec.corp_id,
            qec.external_user_id,
            qec.tag_ids,
            qec.user_id AS qw_user,
            qec.name AS external_contact_name
        FROM
            fs_course_watch_log fcwl
                LEFT JOIN qw_external_contact qec ON fcwl.qw_external_contact_id = qec.id
        WHERE
            DATE(fcwl.finish_time)= '2025-02-09'
          and fcwl.camp_period_time is not NULL
        ]]>
    </select>

    <update id="batchUpdateWatchLog" parameterType="java.util.List">
        UPDATE fs_course_watch_log
        SET
        duration = CASE
        <foreach collection="list" item="item" index="index">
            WHEN video_id = #{item.videoId} AND qw_external_contact_id = #{item.qwExternalContactId} AND qw_user_id = #{item.qwUserId} THEN
            CASE
            <!-- 仅当传入的duration > 当前值时才更新 -->
            WHEN #{item.duration} IS NOT NULL AND #{item.duration} > duration THEN #{item.duration}
            ELSE duration <!-- 如果 duration 为 null，保持原值 -->
            END
        </foreach>
        END,
        last_heartbeat_time = CASE
        <foreach collection="list" item="item" index="index">
            WHEN video_id = #{item.videoId} AND qw_external_contact_id = #{item.qwExternalContactId} AND qw_user_id = #{item.qwUserId} THEN
            CASE
            WHEN #{item.lastHeartbeatTime} IS NOT NULL THEN #{item.lastHeartbeatTime}
            ELSE last_heartbeat_time <!-- 如果 last_heartbeat_time 为 null，保持原值 -->
            END
        </foreach>
        END,
        finish_time = CASE
        <foreach collection="list" item="item" index="index">
            WHEN video_id = #{item.videoId} AND qw_external_contact_id = #{item.qwExternalContactId} AND qw_user_id = #{item.qwUserId} THEN
            CASE
            WHEN finish_time IS NULL THEN #{item.finishTime} <!-- 如果表中 finish_time 为 null，更新为传入的值 -->
            ELSE finish_time <!-- 如果表中 finish_time 不为 null，保持原值 -->
            END
        </foreach>
        END,
        log_type = CASE
        <foreach collection="list" item="item" index="index">
            WHEN video_id = #{item.videoId} AND qw_external_contact_id = #{item.qwExternalContactId} AND qw_user_id = #{item.qwUserId} THEN
            CASE
            WHEN log_type = 2 THEN log_type <!-- 如果 log_type 已经是 2，保持原值 -->
            WHEN #{item.logType} IS NOT NULL AND log_type != 2 THEN #{item.logType} <!-- 如果 log_type 不是 2，更新为传入的值 -->
            ELSE log_type <!-- 其他情况保持原值 -->
            END
        </foreach>
        END
        WHERE
        (video_id, qw_external_contact_id, qw_user_id) IN
        <foreach collection="list" item="item" index="index" open="(" separator="," close=")">
            (#{item.videoId}, #{item.qwExternalContactId}, #{item.qwUserId})
        </foreach>
    </update>


    <update id="batchUpdateWatchLogSendMsg" parameterType="java.util.List">
        UPDATE fs_course_watch_log
        SET send_finish_msg = CASE
        <foreach collection="list" item="item">
            WHEN log_id = #{item.logId} THEN #{item.sendFinishMsg}
        </foreach>
        ELSE send_finish_msg
        END
        WHERE log_id IN
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item.logId}
        </foreach>
    </update>
    <select id="selectFsCourseWatchLogStatisticsListVONew"
            resultType="com.fs.course.vo.FsCourseWatchLogStatisticsListVO">
        SELECT
        o.company_user_id,o.user_id,DATE(o.create_time) create_time,
        SUM(CASE WHEN o.log_type = '1' THEN 1 ELSE 0 END) AS type1,
        SUM(CASE WHEN o.log_type = '2' THEN 1 ELSE 0 END) AS type2,
        SUM(CASE WHEN o.log_type = '3' THEN 1 ELSE 0 END) AS type3,
        SUM(CASE WHEN o.log_type = '4' THEN 1 ELSE 0 END) AS type4,
        o.project as project,
        o.course_id as course_id,
        o.video_id as video_id
        FROM fs_course_watch_log o
        <where>
            <if test="companyId != null">
                o.company_id=#{companyId}
            </if>
            <if test= 'sTime != null '>
                and o.create_time &gt;= #{sTime}
            </if>
            <if test='eTime != null '>
                and o.create_time &lt;= #{eTime}
            </if>
            <if test ='courseId !=null'>
                and o.course_id = #{courseId}
            </if>
            <if test ='videoId !=null'>
                and o.video_id = #{videoId}
            </if>
            <if test="companyUserId != null">
                and o.company_user_id = #{companyUserId}
            </if>
            <if test="project != null">
                and o.project = #{project}
            </if>
            <if test="userId != null">
                and o.user_id = #{userId}
            </if>
        </where>

        GROUP BY o.video_id,o.user_id,DATE(o.create_time),o.project,o.course_id
        ORDER BY o.video_id ,DATE(o.create_time)
    </select>
    <select id="selectFsCourseWatchLogListVONew" resultType="com.fs.course.vo.FsCourseWatchLogListVO">
        SELECT
        l.log_id,
        l.project AS project,
        l.user_id,
        l.log_type,
        SEC_TO_TIME(l.duration) AS duration,
        l.camp_period_time,
        l.finish_time,
        l.send_type,
        l.create_time,
        l.update_time,
        l.last_heartbeat_time,
        l.company_id,
        l.company_user_id,
        l.course_id,
        l.video_id
        FROM
        fs_course_watch_log l
        INNER JOIN (
        SELECT log_id
        FROM fs_course_watch_log
        <where>
            <if test='maps.userId != null'>
                AND user_id = #{maps.userId}
            </if>
            <if test='maps.courseId != null'>
                AND course_id = #{maps.courseId}
            </if>
            <if test='maps.videoId != null'>
                AND video_id = #{maps.videoId}
            </if>
            <if test='maps.logType != null'>
                AND log_type = #{maps.logType}
            </if>
            <if test='maps.companyId != null'>
                AND company_id = #{maps.companyId}
            </if>
            <if test='maps.companyUserId != null'>
                AND company_user_id = #{maps.companyUserId}
            </if>
            <if test= 'maps.sTime != null '>
                and DATE(l.create_time) &gt;= DATE(#{maps.sTime})
            </if>
            <if test='maps.eTime != null '>
                and DATE(l.create_time) &lt;= DATE(#{maps.eTime})
            </if>
            <if test= 'maps.scheduleStartTime != null '>
                and DATE(l.camp_period_time) &gt;= DATE(#{maps.scheduleStartTime})
            </if>
            <if test='maps.scheduleEndTime != null '>
                and DATE(l.camp_period_time) &lt;= DATE(#{maps.scheduleEndTime})
            </if>
            <if test= 'maps.upSTime != null '>
                and DATE(l.update_time) &gt;= DATE(#{maps.upSTime})
            </if>
            <if test='maps.upETime != null '>
                and DATE(l.update_time) &lt;= DATE(#{maps.upETime})
            </if>
            <if test="maps.sopIds != null and maps.sopIds.size() > 0">
            and l.sop_id in
                AND sop_id IN
                <foreach item="sopId" index="index" collection="maps.sopIds" open="(" separator="," close=")">
                    #{sopId}
                </foreach>
            </if>
        </where>
        ORDER BY log_id DESC
        LIMIT ${(maps.pageNum-1)*maps.pageSize}, ${maps.pageSize}
        ) AS page_ids ON l.log_id = page_ids.log_id
    </select>
    <select id="selectFsCourseWatchLogListVONewCount" resultType="java.lang.Long">
        SELECT count(log_id)
        FROM fs_course_watch_log
        <where>
            <if test='maps.userId != null'>
                AND user_id = #{maps.userId}
            </if>
            <if test='maps.courseId != null'>
                AND course_id = #{maps.courseId}
            </if>
            <if test='maps.videoId != null'>
                AND video_id = #{maps.videoId}
            </if>
            <if test='maps.logType != null'>
                AND log_type = #{maps.logType}
            </if>
            <if test='maps.companyId != null'>
                AND company_id = #{maps.companyId}
            </if>
            <if test='maps.companyUserId != null'>
                AND company_user_id = #{maps.companyUserId}
            </if>
            <if test= 'maps.sTime != null '>
                and DATE(l.create_time) &gt;= DATE(#{maps.sTime})
            </if>
            <if test='maps.eTime != null '>
                and DATE(l.create_time) &lt;= DATE(#{maps.eTime})
            </if>
            <if test= 'maps.scheduleStartTime != null '>
                and DATE(l.camp_period_time) &gt;= DATE(#{maps.scheduleStartTime})
            </if>
            <if test='maps.scheduleEndTime != null '>
                and DATE(l.camp_period_time) &lt;= DATE(#{maps.scheduleEndTime})
            </if>
            <if test= 'maps.upSTime != null '>
                and DATE(l.update_time) &gt;= DATE(#{maps.upSTime})
            </if>
            <if test='maps.upETime != null '>
                and DATE(l.update_time) &lt;= DATE(#{maps.upETime})
            </if>
            <if test="maps.sopIds != null and maps.sopIds.size() > 0">
                and l.sop_id in
                AND sop_id IN
                <foreach item="sopId" index="index" collection="maps.sopIds" open="(" separator="," close=")">
                    #{sopId}
                </foreach>
            </if>
        </where>
    </select>

    <!-- 根据条件查询条数 -->
    <select id="countByMap" resultType="java.lang.Integer">
        select count(fcwl.log_id) from fs_course_watch_log fcwl
        <where>
            <if test="params.logTypes != null and params.logTypes.size() > 0">
                and fcwl.log_type in
                <foreach collection="params.logTypes" open="(" close=")" separator="," item="logType">
                    #{logType}
                </foreach>
            </if>
            <if test="params.companyUserId != null">
                and fcwl.company_user_id = #{params.companyUserId}
            </if>
            <if test="params.date != null">
                and date(fcwl.create_time) = #{params.date}
            </if>
        </where>
    </select>
    <select id="selectFsCourseWatchLogOverStatisticsListVO" resultType="com.fs.course.vo.FsCourseOverVO">
        select MIN(o.create_time) createTime,ANY_VALUE(qu.qw_user_name) qwUserName ,ext.name externalUserName,ext.create_time userCreateTime from fs_course_watch_log o LEFT JOIN qw_user qu on qu.id=o.qw_user_id LEFT JOIN qw_external_contact ext ON ext.id =  o.qw_external_contact_id where log_type=2 and o.company_id=#{companyId}
        <if test= 'sTime != null '>
            and DATE(o.create_time) &gt;= DATE(#{sTime})
        </if>
        <if test='eTime != null '>
            and DATE(o.create_time) &lt;= DATE(#{eTime})
        </if>
        <if test ='nickName !=null and nickName!=""'>
            and qu.qw_user_name like concat( #{nickName}, '%')
        </if>
        GROUP BY o.qw_external_contact_id
    </select>


    <select id="getWatchLogByFsUser" resultType="com.fs.course.domain.FsCourseWatchLog">
        SELECT
            log_id,
            user_id,
            video_id,
            log_type,
            create_time,
            update_time,
            duration,
            company_user_id,
            company_id,
            course_id,
            send_type,
            reward_type,
            last_heartbeat_time,
            sop_id,
            finish_time,
            send_finish_msg,
            camp_period_time
        FROM
            fs_course_watch_log
        WHERE
            send_type = 1
          AND video_id = #{videoId}
          AND user_id = #{fsUserId}
    </select>

</mapper>
