<?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.qw.mapper.QwWatchLogExMapper">

    <resultMap type="QwWatchLog" id="QwWatchLogResult">
        <result property="id"    column="id"    />
        <result property="extId"    column="ext_id"    />
        <result property="qwUserId"    column="qw_user_id"    />
        <result property="status"    column="status"    />
        <result property="day"    column="day"    />
        <result property="project"    column="project"    />
        <result property="createTime"    column="create_time"    />
        <result property="lineTime"    column="line_time"    />
    </resultMap>

    <sql id="selectQwWatchLogVo">
        select id, ext_id, qw_user_id,line_time, status, day, project, create_time from qw_watch_log_ex
    </sql>

    <select id="selectQwWatchLogList" parameterType="QwWatchLog" resultMap="QwWatchLogResult">
        <include refid="selectQwWatchLogVo"/>
        <where>
            <if test="extId != null "> and ext_id = #{extId}</if>
            <if test="qwUserId != null "> and qw_user_id = #{qwUserId}</if>
            <if test="status != null "> and status = #{status}</if>
            <if test="day != null "> and day = #{day}</if>
            <if test="project != null "> and project = #{project}</if>
        </where>
    </select>

    <select id="selectQwWatchLogById" parameterType="Long" resultMap="QwWatchLogResult">
        <include refid="selectQwWatchLogVo"/>
        where id = #{id}
    </select>
    <select id="selectQwWatchLogByCompanyUserId" resultType="com.fs.qw.vo.QwWatchLogStatisticsListVO">
            select
            COUNT(CASE WHEN day = 0 and status in (1,2) THEN 1 END) AS firstOnline,
            COUNT(CASE WHEN day = 0 and status=2 THEN 1 END) AS firstOver,
            COUNT(CASE WHEN day = 1 and status in (1,2) THEN 1 END) AS d1Online,
            COUNT(CASE WHEN day = 1 and status=2 THEN 1 END) AS d1Over,
            COUNT(1) AS line,project,course_id,video_id
             from qw_watch_log_ex
             <where>
                 <if test="companyUserId">
                     and company_user_id = #{companyUserId}
                 </if>
                 <if test="companyId">
                     and company_id = #{companyId}
                 </if>
                 <if test="project != null">
                     and project=#{project}
                 </if>
                 <if test="courseId != null">
                     and course_id=#{courseId}
                 </if>
                 <if test="videoId != null">
                     and video_id=#{videoId}
                 </if>
                 and DATE(line_time) between #{sTime} and #{dTime}
             </where>
            group by project,course_id,video_id
    </select>
    <select id="selectQwWatchLogAllStatisticsListVONew"
            resultType="com.fs.qw.vo.QwWatchLogAllStatisticsListVO">
            select
            COUNT(CASE WHEN day = 0 and status in (1,2) THEN 1 END) AS firstOnline,
            COUNT(CASE WHEN day = 0 and status=2 THEN 1 END) AS firstOver,
            COUNT(CASE WHEN day = 1 and status in (1,2) THEN 1 END) AS d1Online,
            COUNT(CASE WHEN day = 1 and status=2 THEN 1 END) AS d1Over,
            COUNT(CASE WHEN day = 2 and status in (1,2) THEN 1 END) AS d2Online,
            COUNT(CASE WHEN day = 2 and status=2 THEN 1 END) AS d2Over,
            COUNT(CASE WHEN day = 3 and status in (1,2) THEN 1 END) AS d3Online,
            COUNT(CASE WHEN day = 3 and status=2 THEN 1 END) AS d3Over,
            COUNT(CASE WHEN day = 4 and status in (1,2) THEN 1 END) AS d4Online,
            COUNT(CASE WHEN day = 4 and status=2 THEN 1 END) AS d4Over,
            COUNT(CASE WHEN day = 5 and status in (1,2) THEN 1 END) AS d5Online,
            COUNT(CASE WHEN day = 5 and status=2 THEN 1 END) AS d5Over,
            COUNT(CASE WHEN day = 6 and status in (1,2) THEN 1 END) AS d6Online,
            COUNT(CASE WHEN day = 6 and status=2 THEN 1 END) AS d6Over,
            COUNT(CASE WHEN day = 7 and status in (1,2) THEN 1 END) AS d7Online,
            COUNT(CASE WHEN day = 7 and status=2 THEN 1 END) AS d7Over,
            COUNT(CASE WHEN day = 8 and status in (1,2) THEN 1 END) AS d8Online,
            COUNT(CASE WHEN day = 8 and status=2 THEN 1 END) AS d8Over,
            COUNT(CASE WHEN day = 9 and status in (1,2) THEN 1 END) AS d9Online,
            COUNT(CASE WHEN day = 9 and status=2 THEN 1 END) AS d9Over,
            COUNT(CASE WHEN day = 10 and status in (1,2) THEN 1 END) AS d10Online,
            COUNT(CASE WHEN day = 10 and status=2 THEN 1 END) AS d10Over,
            COUNT(CASE WHEN day = 11 and status in (1,2) THEN 1 END) AS d11Online,
            COUNT(CASE WHEN day = 11 and status=2 THEN 1 END) AS d11Over,
            COUNT(CASE WHEN day = 12 and status in (1,2) THEN 1 END) AS d12Online,
            COUNT(CASE WHEN day = 12 and status=2 THEN 1 END) AS d12Over,
            COUNT(CASE WHEN day = 13 and status in (1,2) THEN 1 END) AS d13Online,
            COUNT(CASE WHEN day = 13 and status=2 THEN 1 END) AS d13Over,
            COUNT(CASE WHEN day = 14 and status in (1,2) THEN 1 END) AS d14Online,
            COUNT(CASE WHEN day = 14 and status=2 THEN 1 END) AS d14Over,
            COUNT(CASE WHEN day = 15 and status in (1,2) THEN 1 END) AS d15Online,
            COUNT(CASE WHEN day = 15 and status=2 THEN 1 END) AS d15Over,
            COUNT(CASE WHEN day = 16 and status in (1,2) THEN 1 END) AS d16Online,
            COUNT(CASE WHEN day = 16 and status=2 THEN 1 END) AS d16Over,
            COUNT(CASE WHEN day = 17 and status in (1,2) THEN 1 END) AS d17Online,
            COUNT(CASE WHEN day = 17 and status=2 THEN 1 END) AS d17Over,
            COUNT(CASE WHEN day = 18 and status in (1,2) THEN 1 END) AS d18Online,
            COUNT(CASE WHEN day = 18 and status=2 THEN 1 END) AS d18Over,
            COUNT(CASE WHEN day = 19 and status in (1,2) THEN 1 END) AS d19Online,
            COUNT(CASE WHEN day = 19 and status=2 THEN 1 END) AS d19Over,
            COUNT(CASE WHEN day = 20 and status in (1,2) THEN 1 END) AS d20Online,
            COUNT(CASE WHEN day = 20 and status=2 THEN 1 END) AS d20Over,
            COUNT(CASE WHEN day = 21 and status in (1,2) THEN 1 END) AS d21Online,
            COUNT(CASE WHEN day = 21 and status=2 THEN 1 END) AS d21Over,
            COUNT(CASE WHEN day = 22 and status in (1,2) THEN 1 END) AS d22Online,
            COUNT(CASE WHEN day = 22 and status=2 THEN 1 END) AS d22Over,
            COUNT(CASE WHEN day = 23 and status in (1,2) THEN 1 END) AS d23Online,
            COUNT(CASE WHEN day = 23 and status=2 THEN 1 END) AS d23Over,
            COUNT(CASE WHEN day = 24 and status in (1,2) THEN 1 END) AS d24Online,
            COUNT(CASE WHEN day = 24 and status=2 THEN 1 END) AS d24Over,
            COUNT(CASE WHEN day = 25 and status in (1,2) THEN 1 END) AS d25Online,
            COUNT(CASE WHEN day = 25 and status=2 THEN 1 END) AS d25Over,
            COUNT(CASE WHEN day = 26 and status in (1,2) THEN 1 END) AS d26Online,
            COUNT(CASE WHEN day = 26 and status=2 THEN 1 END) AS d26Over,
            COUNT(CASE WHEN day = 27 and status in (1,2) THEN 1 END) AS d27Online,
            COUNT(CASE WHEN day = 27 and status=2 THEN 1 END) AS d27Over,
            COUNT(CASE WHEN day = 28 and status in (1,2) THEN 1 END) AS d28Online,
            COUNT(CASE WHEN day = 28 and status=2 THEN 1 END) AS d28Over,
            COUNT(CASE WHEN day = 29 and status in (1,2) THEN 1 END) AS d29Online,
            COUNT(CASE WHEN day = 29 and status=2 THEN 1 END) AS d29Over,
            COUNT(CASE WHEN day = 30 and status in (1,2) THEN 1 END) AS d30Online,
            COUNT(CASE WHEN day = 30 and status=2 THEN 1 END) AS d30Over,
            COUNT(1) AS line,project,course_id,video_id
             from qw_watch_log_ex
            <where>
                 <if test="companyUserId">
                     and company_user_id=#{companyUserId}
                 </if>
                <if test="companyId">
                    and company_id = #{companyId}
                </if>
                <if test="project != null">
                    and project = #{project}
                </if>
                <if test="courseId != null">
                    and course_id = #{courseId}
                </if>
                <if test="videoId != null">
                    and video_id =#{videoId}
                </if>
                and DATE(line_time) between #{sDate} AND #{eDate} group by project,course_id,video_id
            </where>
    </select>

    <insert id="insertQwWatchLog" parameterType="QwWatchLog" useGeneratedKeys="true" keyProperty="id">
        insert into qw_watch_log_ex
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="extId != null">ext_id,</if>
            <if test="qwUserId != null">qw_user_id,</if>
            <if test="status != null">status,</if>
            <if test="day != null">day,</if>
            <if test="project != null">project,</if>
            <if test="createTime != null">create_time,</if>
            <if test="lineTime != null">line_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="extId != null">#{extId},</if>
            <if test="qwUserId != null">#{qwUserId},</if>
            <if test="status != null">#{status},</if>
            <if test="day != null">#{day},</if>
            <if test="project != null">#{project},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="lineTime != null">#{lineTime},</if>
         </trim>
    </insert>

    <insert id="insertQwWatchLogBatch" parameterType="java.util.List" useGeneratedKeys="true" keyProperty="id">
        INSERT INTO qw_watch_log_ex (
        ext_id,
        qw_user_id,
        status,
        day,
        project,
        create_time,
        line_time,
        fs_user_id,
        company_id,
        company_user_id,
        course_id,
        video_id
        )
        VALUES
    <foreach collection="watchLogs" item="log" separator=",">
            (
            #{log.extId},
            #{log.qwUserId},
            #{log.status},
            #{log.day},
            #{log.project},
            #{log.createTime},
            #{log.lineTime},
             #{log.fsUserId},
             #{log.companyId},
             #{log.companyUserId},
             #{log.courseId},
             #{log.videoId}
            )
    </foreach>
    </insert>

    <update id="updateQwWatchLog" parameterType="QwWatchLog">
        update qw_watch_log_ex
        <trim prefix="SET" suffixOverrides=",">
            <if test="extId != null">ext_id = #{extId},</if>
            <if test="qwUserId != null">qw_user_id = #{qwUserId},</if>
            <if test="status != null">status = #{status},</if>
            <if test="day != null">day = #{day},</if>
            <if test="project != null">project = #{project},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="lineTime != null">line_time = #{lineTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteQwWatchLogById" parameterType="Long">
        delete from qw_watch_log_ex where id = #{id}
    </delete>

    <delete id="deleteQwWatchLogByIds" parameterType="String">
        delete from qw_watch_log_ex where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>
