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

    <resultMap type="FsUserWatchCourseStatistics" id="FsUserWatchCourseStatisticsResult">
        <result property="id"    column="id"    />
        <result property="periodId"    column="period_id"    />
        <result property="periodName"    column="period_name"    />
        <result property="courseId"    column="course_id"    />
        <result property="courseName"    column="course_name"    />
        <result property="videoId"    column="video_id"    />
        <result property="videoTitle"    column="video_title"    />
        <result property="companyId"    column="company_id"    />
        <result property="companyName"    column="company_name"    />
        <result property="companyUserId"    column="company_user_id"    />
        <result property="courseStartDateTime"    column="course_start_date_time"    />
        <result property="companyUserName"    column="company_user_name"    />
        <result property="periodStartingTime"    column="period_starting_time"    />
        <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"    />
        <result property="answerNum"    column="answer_num"    />
        <result property="answerRightNum"    column="answer_right_num"    />
        <result property="answerRightRate"    column="answer_right_rate"    />
        <result property="redPacketNum"    column="red_packet_num"    />
        <result property="redPacketAmount"    column="red_packet_amount"    />
    </resultMap>

    <sql id="selectFsUserWatchCourseStatisticsVo">
        select id, period_id, period_name, course_id, course_name, video_id, video_title, company_id, company_name, company_user_id, course_start_date_time, company_user_name, period_starting_time, new_user_num, user_num, watch_num, complete_watch_num, online_rate, complete_watch_rate, answer_num, answer_right_num, answer_right_rate, red_packet_num, red_packet_amount from fs_user_watch_course_statistics
    </sql>

    <select id="selectFsUserWatchCourseStatisticsList" parameterType="FsUserWatchCourseStatistics" resultMap="FsUserWatchCourseStatisticsResult">
        <include refid="selectFsUserWatchCourseStatisticsVo"/>
        <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="courseId != null "> and course_id = #{courseId}</if>
            <if test="courseName != null  and courseName != ''"> and course_name like concat('%', #{courseName}, '%')</if>
            <if test="videoId != null "> and video_id = #{videoId}</if>
            <if test="videoTitle != null  and videoTitle != ''"> and video_title like concat('%', #{videoTitle}, '%')</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="courseStartDateTime != null "> and course_start_date_time like concat(DATE(#{courseStartDateTime}),'%')</if>
            <if test="companyUserName != null  and companyUserName != ''"> and company_user_name like concat('%', #{companyUserName}, '%')</if>
            <if test="periodStartingTime != null "> and period_starting_time like concat(DATE(#{periodStartingTime}), '%')</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>
            <if test="answerNum != null "> and answer_num = #{answerNum}</if>
            <if test="answerRightNum != null "> and answer_right_num = #{answerRightNum}</if>
            <if test="answerRightRate != null "> and answer_right_rate = #{answerRightRate}</if>
            <if test="redPacketNum != null "> and red_packet_num = #{redPacketNum}</if>
            <if test="redPacketAmount != null "> and red_packet_amount = #{redPacketAmount}</if>
        </where>
    </select>

    <select id="selectFsUserWatchCourseStatisticsListTotal" parameterType="FsUserWatchCourseStatistics" resultMap="FsUserWatchCourseStatisticsResult">
        SELECT
        id,
        period_id,
        period_name,
        course_id,
        course_name,
        video_id,
        video_title,
        company_id,
        company_name,
        company_user_id,
        course_start_date_time,
        period_starting_time,
        sum(new_user_num) as new_user_num,
        sum(user_num) as user_num,
        sum(watch_num) as watch_num,
        ifnull(
        ROUND(
        ( sum(watch_num) / sum(user_num) ) * 100, 2
        ), 0
        ) as online_rate,
        sum(complete_watch_num) as complete_watch_num ,
        complete_watch_rate as complete_watch_rate1,
        ifnull(
        ROUND(
        ( sum(complete_watch_num) / sum(watch_num) ) * 100, 2
        ), 0
        ) as complete_watch_rate,
        sum(red_packet_num) as red_packet_num
        from fs_user_watch_course_statistics
        <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="courseId != null "> and course_id = #{courseId}</if>
            <if test="courseName != null  and courseName != ''"> and course_name like concat('%', #{courseName}, '%')</if>
            <if test="videoId != null "> and video_id = #{videoId}</if>
            <if test="videoTitle != null  and videoTitle != ''"> and video_title like concat('%', #{videoTitle}, '%')</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
            <if test="companyName != null  and companyName != ''"> and company_name like concat('%', #{companyName}, '%')</if>
            <if test="companyUserId != null "> and company_user_id = #{companyUserId}</if>
            <if test="courseStartDateTime != null "> and course_start_date_time like concat(DATE(#{courseStartDateTime}),'%')</if>
            <if test="companyUserName != null  and companyUserName != ''"> and company_user_name like concat('%', #{companyUserName}, '%')</if>
            <if test="periodStartingTime != null "> and period_starting_time like concat(DATE(#{periodStartingTime}), '%')</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>
            <if test="answerNum != null "> and answer_num = #{answerNum}</if>
            <if test="answerRightNum != null "> and answer_right_num = #{answerRightNum}</if>
            <if test="answerRightRate != null "> and answer_right_rate = #{answerRightRate}</if>
            <if test="redPacketNum != null "> and red_packet_num = #{redPacketNum}</if>
            <if test="redPacketAmount != null "> and red_packet_amount = #{redPacketAmount}</if>
        </where>
        group by fs_user_watch_course_statistics.company_id, course_start_date_time
    </select>

    <select id="selectFsUserWatchCourseStatisticsById" parameterType="Long" resultMap="FsUserWatchCourseStatisticsResult">
        <include refid="selectFsUserWatchCourseStatisticsVo"/>
        where id = #{id}
    </select>

    <insert id="insertFsUserWatchCourseStatistics" parameterType="FsUserWatchCourseStatistics" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_watch_course_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="periodId != null">period_id,</if>
            <if test="periodName != null">period_name,</if>
            <if test="courseId != null">course_id,</if>
            <if test="courseName != null">course_name,</if>
            <if test="videoId != null">video_id,</if>
            <if test="videoTitle != null">video_title,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyName != null">company_name,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="courseStartDateTime != null">course_start_date_time,</if>
            <if test="companyUserName != null">company_user_name,</if>
            <if test="periodStartingTime != null">period_starting_time,</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>
            <if test="answerNum != null">answer_num,</if>
            <if test="answerRightNum != null">answer_right_num,</if>
            <if test="answerRightRate != null">answer_right_rate,</if>
            <if test="redPacketNum != null">red_packet_num,</if>
            <if test="redPacketAmount != null">red_packet_amount,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="periodId != null">#{periodId},</if>
            <if test="periodName != null">#{periodName},</if>
            <if test="courseId != null">#{courseId},</if>
            <if test="courseName != null">#{courseName},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="videoTitle != null">#{videoTitle},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="courseStartDateTime != null">#{courseStartDateTime},</if>
            <if test="companyUserName != null">#{companyUserName},</if>
            <if test="periodStartingTime != null">#{periodStartingTime},</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="answerNum != null">#{answerNum},</if>
            <if test="answerRightNum != null">#{answerRightNum},</if>
            <if test="answerRightRate != null">#{answerRightRate},</if>
            <if test="redPacketNum != null">#{redPacketNum},</if>
            <if test="redPacketAmount != null">#{redPacketAmount},</if>
         </trim>
    </insert>

    <update id="updateFsUserWatchCourseStatistics" parameterType="FsUserWatchCourseStatistics">
        update fs_user_watch_course_statistics
        <trim prefix="SET" suffixOverrides=",">
            <if test="periodId != null">period_id = #{periodId},</if>
            <if test="periodName != null">period_name = #{periodName},</if>
            <if test="courseId != null">course_id = #{courseId},</if>
            <if test="courseName != null">course_name = #{courseName},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="videoTitle != null">video_title = #{videoTitle},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="courseStartDateTime != null">course_start_date_time = #{courseStartDateTime},</if>
            <if test="companyUserName != null">company_user_name = #{companyUserName},</if>
            <if test="periodStartingTime != null">period_starting_time = #{periodStartingTime},</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="answerNum != null">answer_num = #{answerNum},</if>
            <if test="answerRightNum != null">answer_right_num = #{answerRightNum},</if>
            <if test="answerRightRate != null">answer_right_rate = #{answerRightRate},</if>
            <if test="redPacketNum != null">red_packet_num = #{redPacketNum},</if>
            <if test="redPacketAmount != null">red_packet_amount = #{redPacketAmount},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteFsUserWatchCourseStatisticsById" parameterType="Long">
        delete from fs_user_watch_course_statistics where id = #{id}
    </delete>

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


    <insert id="insertFsUserWatchCourseStatisticsTask" parameterType="FsUserWatchCourseStatistics" useGeneratedKeys="true" keyProperty="id">
        insert into fs_user_watch_course_statistics
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="periodId != null">period_id,</if>
            <if test="periodName != null">period_name,</if>
            <if test="courseId != null">course_id,</if>
            <if test="courseName != null">course_name,</if>
            <if test="videoId != null">video_id,</if>
            <if test="videoTitle != null">video_title,</if>
            <if test="companyId != null">company_id,</if>
            <if test="companyName != null">company_name,</if>
            <if test="companyUserId != null">company_user_id,</if>
            <if test="courseStartDateTime != null">course_start_date_time,</if>
            <if test="companyUserName != null">company_user_name,</if>
            <if test="periodStartingTime != null">period_starting_time,</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>
            <if test="answerNum != null">answer_num,</if>
            <if test="answerRightNum != null">answer_right_num,</if>
            <if test="answerRightRate != null">answer_right_rate,</if>
            <if test="redPacketNum != null">red_packet_num,</if>
            <if test="redPacketAmount != null">red_packet_amount,</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="courseId != null">#{courseId},</if>
            <if test="courseName != null">#{courseName},</if>
            <if test="videoId != null">#{videoId},</if>
            <if test="videoTitle != null">#{videoTitle},</if>
            <if test="companyId != null">#{companyId},</if>
            <if test="companyName != null">#{companyName},</if>
            <if test="companyUserId != null">#{companyUserId},</if>
            <if test="courseStartDateTime != null">#{courseStartDateTime},</if>
            <if test="companyUserName != null">#{companyUserName},</if>
            <if test="periodStartingTime != null">#{periodStartingTime},</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="answerNum != null">#{answerNum},</if>
            <if test="answerRightNum != null">#{answerRightNum},</if>
            <if test="answerRightRate != null">#{answerRightRate},</if>
            <if test="redPacketNum != null">#{redPacketNum},</if>
            <if test="redPacketAmount != null">#{redPacketAmount},</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="courseId != null">course_id = #{courseId},</if>
            <if test="courseName != null">course_name = #{courseName},</if>
            <if test="videoId != null">video_id = #{videoId},</if>
            <if test="videoTitle != null">video_title = #{videoTitle},</if>
            <if test="companyId != null">company_id = #{companyId},</if>
            <if test="companyName != null">company_name = #{companyName},</if>
            <if test="companyUserId != null">company_user_id = #{companyUserId},</if>
            <if test="courseStartDateTime != null">course_start_date_time = #{courseStartDateTime},</if>
            <if test="companyUserName != null">company_user_name = #{companyUserName},</if>
            <if test="periodStartingTime != null">period_starting_time = #{periodStartingTime},</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="answerNum != null">answer_num = #{answerNum},</if>
            <if test="answerRightNum != null">answer_right_num = #{answerRightNum},</if>
            <if test="answerRightRate != null">answer_right_rate = #{answerRightRate},</if>
            <if test="redPacketNum != null">red_packet_num = #{redPacketNum},</if>
            <if test="redPacketAmount != null">red_packet_amount = #{redPacketAmount},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
    </insert>

</mapper>
