<?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.FsUserWatchStatisticsMapper">

    <resultMap type="FsUserWatchStatistics" id="FsUserWatchStatisticsResult">
        <result property="id"    column="id"    />
        <result property="periodId"    column="period_id"    />
        <result property="periodName"    column="period_name"    />
        <result property="periodStartingTime"    column="period_starting_time"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="newUserNum"    column="new_user_num"    />
        <result property="userNum"    column="user_num"    />
        <result property="watchNum"    column="watch_num"    />
        <result property="completeWatchNum"    column="complete_watch_num"    />
        <result property="onlineRate"    column="online_rate"    />
        <result property="completeWatchRate"    column="complete_watch_rate"    />
    </resultMap>

    <sql id="selectFsUserWatchStatisticsVo">
        select id, period_id, period_name, period_starting_time, new_user_num, user_num, watch_num, complete_watch_num, online_rate, complete_watch_rate, company_id, company_name from fs_user_watch_statistics
    </sql>

    <select id="selectFsUserWatchStatisticsList" parameterType="FsUserWatchStatistics" resultMap="FsUserWatchStatisticsResult">
        <include refid="selectFsUserWatchStatisticsVo"/>
        <where>
            <if test="periodId != null "> and period_id = #{periodId}</if>
            <if test="periodName != null  and periodName != ''"> and period_name like concat('%', #{periodName}, '%')</if>
            <if test="periodStartingTime != null "> and period_starting_time = #{periodStartingTime}</if>
            <if test="companyId != null and companyId !='' "> and company_id = #{companyId}</if>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="newUserNum != null "> and new_user_num = #{newUserNum}</if>
            <if test="userNum != null "> and user_num = #{userNum}</if>
            <if test="watchNum != null "> and watch_num = #{watchNum}</if>
            <if test="completeWatchNum != null "> and complete_watch_num = #{completeWatchNum}</if>
            <if test="completeWatchRate != null "> and complete_watch_rate = #{completeWatchRate}</if>
        </where>
    </select>

    <select id="selectFsUserWatchStatisticsById" parameterType="Long" resultMap="FsUserWatchStatisticsResult">
        <include refid="selectFsUserWatchStatisticsVo"/>
        where id = #{id}
    </select>

    <insert id="insertFsUserWatchStatistics" parameterType="FsUserWatchStatistics" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_watch_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="periodId != null">period_id,</if>
            <if test="periodName != null">period_name,</if>
            <if test="periodStartingTime != null">period_starting_time,</if>
            <if test="companyId != null and companyId !='' ">company_id,</if>
            <if test="companyName != null">company_name,</if>
            <if test="newUserNum != null">new_user_num,</if>
            <if test="userNum != null">user_num,</if>
            <if test="watchNum != null">watch_num,</if>
            <if test="completeWatchNum != null">complete_watch_num,</if>
            <if test="onlineRate != null">online_rate,</if>
            <if test="completeWatchRate != null">complete_watch_rate,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="periodId != null">#{periodId},</if>
            <if test="periodName != null">#{periodName},</if>
            <if test="periodStartingTime != null">#{periodStartingTime},</if>
            <if test="companyId != null and companyId !='' ">#{companyId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="newUserNum != null">#{newUserNum},</if>
            <if test="userNum != null">#{userNum},</if>
            <if test="watchNum != null">#{watchNum},</if>
            <if test="completeWatchNum != null">#{completeWatchNum},</if>
            <if test="onlineRate != null">#{onlineRate},</if>
            <if test="completeWatchRate != null">#{completeWatchRate},</if>
         </trim>
    </insert>

    <update id="updateFsUserWatchStatistics" parameterType="FsUserWatchStatistics">
        update fs_user_watch_statistics
        <trim prefix="SET" suffixOverrides=",">
            <if test="periodId != null">period_id = #{periodId},</if>
            <if test="periodName != null">period_name = #{periodName},</if>
            <if test="periodStartingTime != null">period_starting_time = #{periodStartingTime},</if>
            <if test="companyId != null and companyId !='' ">company_id = #{companyId},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="newUserNum != null">new_user_num = #{newUserNum},</if>
            <if test="userNum != null">user_num = #{userNum},</if>
            <if test="watchNum != null">watch_num = #{watchNum},</if>
            <if test="completeWatchNum != null">complete_watch_num = #{completeWatchNum},</if>
            <if test="onlineRate != null">online_rate = #{onlineRate},</if>
            <if test="completeWatchRate != null">complete_watch_rate = #{completeWatchRate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFsUserWatchStatisticsById" parameterType="Long">
        delete from fs_user_watch_statistics where id = #{id}
    </delete>

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

    <select id="getCourseWatchStatistics" resultType="FsUserWatchStatistics">
        SELECT
            count( DISTINCT CASE WHEN fwl.log_type != 3 THEN fwl.user_id END ) AS watchNum,
            count( DISTINCT CASE WHEN fwl.log_type = 2 THEN fwl.user_id END ) AS completeWatchNum,
            ifnull(
                    ROUND(
                            (
                                COUNT( DISTINCT CASE WHEN fwl.log_type = 2 THEN fwl.user_id END ) / count( DISTINCT CASE WHEN fwl.log_type != 3 THEN fwl.user_id END )) * 100,
                            2
                    ),
                    0
            ) AS completeWatchRate,
            fwl.period_id,
            fwl.company_id
        FROM
            fs_course_watch_log fwl

        WHERE
            fwl.send_type = 1
        GROUP BY
            fwl.period_id, fwl.company_id
    </select>

    <insert id="insertFsUserWatchStatisticsTask" parameterType="FsUserWatchStatistics" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_watch_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="periodId != null">period_id,</if>
            <if test="periodName != null">period_name,</if>
            <if test="periodStartingTime != null">period_starting_time,</if>
            <if test="companyId != null and companyId !='' ">company_id,</if>
            <if test="companyName != null">company_name,</if>
            <if test="newUserNum != null">new_user_num,</if>
            <if test="userNum != null">user_num,</if>
            <if test="watchNum != null">watch_num,</if>
            <if test="completeWatchNum != null">complete_watch_num,</if>
            <if test="completeWatchRate != null">complete_watch_rate,</if>
            <if test="onlineRate != null">online_rate,</if>
            <if test="createTime != null">create_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="periodId != null">#{periodId},</if>
            <if test="periodName != null">#{periodName},</if>
            <if test="periodStartingTime != null">#{periodStartingTime},</if>
            <if test="companyId != null and companyId !='' ">#{companyId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="newUserNum != null">#{newUserNum},</if>
            <if test="userNum != null">#{userNum},</if>
            <if test="watchNum != null">#{watchNum},</if>
            <if test="completeWatchNum != null">#{completeWatchNum},</if>
            <if test="onlineRate != null">#{onlineRate},</if>
            <if test="completeWatchRate != null">#{completeWatchRate},</if>
            <if test="createTime != null">#{createTime},</if>
        </trim>
        on duplicate key update
        <trim suffixOverrides=",">
            <if test="periodId != null">period_id = #{periodId},</if>
            <if test="periodName != null">period_name = #{periodName},</if>
            <if test="periodStartingTime != null">period_starting_time = #{periodStartingTime},</if>
            <if test="companyId != null and companyId !='' ">company_id = #{companyId},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="newUserNum != null">new_user_num = #{newUserNum},</if>
            <if test="userNum != null">user_num = #{userNum},</if>
            <if test="watchNum != null">watch_num = #{watchNum},</if>
            <if test="completeWatchNum != null">complete_watch_num = #{completeWatchNum},</if>
            <if test="onlineRate != null">online_rate = #{onlineRate},</if>
            <if test="completeWatchRate != null">complete_watch_rate = #{completeWatchRate},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
    </insert>

</mapper>
