<?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.live.mapper.LiveDataMapper">

    <resultMap type="LiveData" id="LiveDataResult">
        <result property="liveId"    column="live_id"    />
        <result property="createTime"    column="create_time"    />
        <result property="pageViews"    column="page_views"    />
        <result property="uniqueVisitors"    column="unique_visitors"    />
        <result property="totalViews"    column="total_views"    />
        <result property="uniqueViewers"    column="unique_viewers"    />
        <result property="peakConcurrentViewers"    column="peak_concurrent_viewers"    />
        <result property="favouriteNum"    column="favourite_num"    />
        <result property="followNum"    column="follow_num"    />
    </resultMap>

    <sql id="selectLiveDataVo">
        SELECT favourite_num,follow_num,likes,live_id,page_views,peak_concurrent_viewers,unique_viewers,unique_visitors,total_views FROM live_data
    </sql>

    <select id="selectLiveDataList" parameterType="LiveData" resultMap="LiveDataResult">
        <include refid="selectLiveDataVo"/>
        <where>
            <if test="pageViews != null "> and page_views = #{pageViews}</if>
            <if test="uniqueVisitors != null "> and unique_visitors = #{uniqueVisitors}</if>
            <if test="totalViews != null "> and total_views = #{totalViews}</if>
            <if test="uniqueViewers != null "> and unique_viewers = #{uniqueViewers}</if>
            <if test="peakConcurrentViewers != null "> and peak_concurrent_viewers = #{peakConcurrentViewers}</if>
            <if test="favouriteNum != null "> and favourite_num = #{favouriteNum}</if>
            <if test="followNum != null "> and follow_num = #{followNum}</if>
            <if test="likes != null "> and likes = #{likes}</if>
            <if test="liveId != null "> and live_id = #{live_id}</if>
        </where>
    </select>

    <select id="selectLiveDataByLiveId" parameterType="Long" resultMap="LiveDataResult">
        <include refid="selectLiveDataVo"/>
        where live_id = #{liveId}
    </select>
    <select id="getAllLiveIds" resultType="java.lang.Long">
        SELECT B.live_id
        FROM live B
        where B.status = '2'
    </select>

    <select id="getAllLiveDatas" resultMap="LiveDataResult">
        SELECT A.favourite_num,A.follow_num,A.likes,A.live_id,A.page_views,
               A.peak_concurrent_viewers,A.unique_viewers,A.unique_visitors,A.total_views
        FROM live_data A
                 inner join live B on A.live_id = B.live_id
        where B.status = '2'
    </select>
    <select id="getRecentLive" resultType="com.fs.live.vo.RecentLiveDataVo">
        SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors, B.live_id AS liveId,B.live_name AS liveName,B.`status` AS status,B.live_img_url AS liveImgUrl,B.start_time AS startTime
        FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id ORDER BY B.start_time LIMIT 4
    </select>
    <select id="getCompanyRecentLive" resultType="com.fs.live.vo.RecentLiveDataVo">
        SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors, B.live_id AS liveId,B.live_name AS liveName,B.`status` AS status,B.live_img_url AS liveImgUrl,B.start_time AS startTime
        FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
        <if test="companyId != null">
        where b.company_id = #{companyId}
        </if>
        ORDER BY B.start_time LIMIT 4
    </select>
    <select id="getLiveTop" resultType="com.fs.live.vo.RecentLiveDataVo">
        SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors,A.total_views AS totalViews,A.unique_viewers AS uniqueViewers,A.peak_concurrent_viewers AS peakConcurrentViewers,
        B.live_name AS liveName,B.live_img_url AS liveImgUrl
        FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
        <choose>
            <when test="rankType == 'highFlow'">
                ORDER BY A.page_views DESC
            </when>
            <when test="rankType == 'highView'">
                ORDER BY A.total_views DESC
            </when>
            <when test="rankType == 'lowFlow'">
                ORDER BY A.page_views ASC
            </when>
            <when test="rankType == 'lowView'">
                ORDER BY A.total_views ASC
            </when>
            <otherwise>
                ORDER BY A.page_views DESC
            </otherwise>
        </choose>
        LIMIT 10
    </select>
    <select id="getCompanyLiveTop" resultType="com.fs.live.vo.RecentLiveDataVo">
        SELECT A.page_views AS pageViews,A.unique_visitors AS uniqueVisitors,A.total_views AS totalViews,A.unique_viewers AS uniqueViewers,A.peak_concurrent_viewers AS peakConcurrentViewers,
        B.live_name AS liveName,B.live_img_url AS liveImgUrl
        FROM live_data A LEFT JOIN live B ON A.live_id = B.live_id
        <if test="companyId != null">
            where b.company_id = #{companyId}
        </if>
        <choose>
            <when test="rankType == 'highFlow'">
                ORDER BY A.page_views DESC
            </when>
            <when test="rankType == 'highView'">
                ORDER BY A.total_views DESC
            </when>
            <when test="rankType == 'lowFlow'">
                ORDER BY A.page_views ASC
            </when>
            <when test="rankType == 'lowView'">
                ORDER BY A.total_views ASC
            </when>
            <otherwise>
                ORDER BY A.page_views DESC
            </otherwise>
        </choose>
        LIMIT 10
    </select>
    <select id="getCurrentData" resultType="com.fs.live.vo.TrendDataVO">
        SELECT
            SUM(ld.page_views) AS views,
            SUM(ld.unique_visitors) AS visitors,
            COUNT(l.live_id) AS streams,
            SUM(ld.total_views) AS pv,
            SUM(ld.unique_viewers) AS uv
            FROM live_data ld
            JOIN live l ON ld.live_id = l.live_id
            WHERE DATE(l.start_time) BETWEEN #{startDate} AND #{endDate};

    </select>
    <select id="getChartData" resultType="java.util.Map">
        SELECT
            CASE
                WHEN #{format} = 'day' THEN DATE(l.start_time)
                WHEN #{format} = 'week' THEN
                CONCAT(
                DATE_FORMAT(DATE_SUB(l.start_time, INTERVAL WEEKDAY(l.start_time) DAY), '%m/%d'),
                '-',
                DATE_FORMAT(DATE_ADD(l.start_time, INTERVAL (6 - WEEKDAY(l.start_time)) DAY), '%m/%d')
                )
                WHEN #{format} = 'month' THEN DATE_FORMAT(l.start_time, '%Y-%m')
                END AS date_range,
        <choose>
            <when test="category == 'streams'"> COUNT(l.live_id) </when>
            <otherwise> SUM(ld.${category}) </otherwise>
        </choose>
         AS views
            FROM live_data ld
            LEFT JOIN live l ON ld.live_id = l.live_id
            WHERE DATE(l.start_time) BETWEEN #{chartStartDate} AND #{chartEndDate}
            GROUP BY date_range
            ORDER BY date_range;
    </select>
    <select id="getPreviousData" resultType="com.fs.live.vo.TrendDataVO">
        SELECT
            SUM(ld.page_views) AS prevViews,
            SUM(ld.unique_visitors) AS prevVisitors,
            COUNT(l.live_id) AS prevStreams,
            SUM(ld.total_views) AS prevPv,
            SUM(ld.unique_viewers) AS prevUv
            FROM live_data ld
            LEFT JOIN live l ON ld.live_id = l.live_id
            WHERE DATE(l.start_time) BETWEEN #{prevStartDate} AND #{prevStartDate};
    </select>
    <select id="getCompanyCurrentData" resultType="com.fs.live.vo.TrendDataVO">
        SELECT
            SUM(ld.page_views) AS views,
            SUM(ld.unique_visitors) AS visitors,
            COUNT(l.live_id) AS streams,
            SUM(ld.total_views) AS pv,
            SUM(ld.unique_viewers) AS uv
            FROM live_data ld
            JOIN live l ON ld.live_id = l.live_id
            WHERE DATE(l.start_time) BETWEEN #{startDate} AND #{endDate}
        <if test="companyId != null">
          and l.company_id = #{companyId}
        </if>
        ;

    </select>
    <select id="getCompanyChartData" resultType="java.util.Map">
        SELECT
            CASE
                WHEN #{format} = 'day' THEN DATE(l.start_time)
                WHEN #{format} = 'week' THEN
                CONCAT(
                DATE_FORMAT(DATE_SUB(l.start_time, INTERVAL WEEKDAY(l.start_time) DAY), '%m/%d'),
                '-',
                DATE_FORMAT(DATE_ADD(l.start_time, INTERVAL (6 - WEEKDAY(l.start_time)) DAY), '%m/%d')
                )
                WHEN #{format} = 'month' THEN DATE_FORMAT(l.start_time, '%Y-%m')
                END AS date_range,
        <choose>
            <when test="category == 'streams'"> COUNT(l.live_id) </when>
            <otherwise> SUM(ld.${category}) </otherwise>
        </choose>
         AS views
            FROM live_data ld
            LEFT JOIN live l ON ld.live_id = l.live_id
            WHERE DATE(l.start_time) BETWEEN #{chartStartDate} AND #{chartEndDate}
            <if test="companyId != null">
                and  l.company_id = #{companyId}
            </if>
            GROUP BY date_range
            ORDER BY date_range;
    </select>
    <select id="getCompanyPreviousData" resultType="com.fs.live.vo.TrendDataVO">
        SELECT
            SUM(ld.page_views) AS prevViews,
            SUM(ld.unique_visitors) AS prevVisitors,
            COUNT(l.live_id) AS prevStreams,
            SUM(ld.total_views) AS prevPv,
            SUM(ld.unique_viewers) AS prevUv
            FROM live_data ld
            LEFT JOIN live l ON ld.live_id = l.live_id
            WHERE DATE(l.start_time) BETWEEN #{prevStartDate} AND #{prevStartDate}
        <if test="companyId != null">
            and l.company_id = #{companyId}
        </if>
        ;
    </select>

    <insert id="insertLiveData" parameterType="LiveData">
        insert into live_data
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="liveId != null">live_id,</if>
            <if test="pageViews != null">page_views,</if>
            <if test="uniqueVisitors != null">unique_visitors,</if>
            <if test="totalViews != null">total_views,</if>
            <if test="uniqueViewers != null">unique_viewers,</if>
            <if test="peakConcurrentViewers != null">peak_concurrent_viewers,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="liveId != null">#{liveId},</if>
            <if test="pageViews != null">#{pageViews},</if>
            <if test="uniqueVisitors != null">#{uniqueVisitors},</if>
            <if test="totalViews != null">#{totalViews},</if>
            <if test="uniqueViewers != null">#{uniqueViewers},</if>
            <if test="peakConcurrentViewers != null">#{peakConcurrentViewers},</if>
         </trim>
    </insert>

    <update id="updateLiveData" parameterType="LiveData">
        update live_data
        <trim prefix="SET" suffixOverrides=",">
            <if test="pageViews != null">page_views = #{pageViews},</if>
            <if test="uniqueVisitors != null">unique_visitors = #{uniqueVisitors},</if>
            <if test="totalViews != null">total_views = #{totalViews},</if>
            <if test="uniqueViewers != null">unique_viewers = #{uniqueViewers},</if>
            <if test="peakConcurrentViewers != null">peak_concurrent_viewers = #{peakConcurrentViewers},</if>
            <if test="likes != null">likes = #{likes},</if>
            <if test="favouriteNum != null">favourite_num = #{favouriteNum},</if>
            <if test="followNum != null">follow_num = #{followNum},</if>
        </trim>
        where live_id = #{liveId}
    </update>

    <delete id="deleteLiveDataByLiveId" parameterType="Long">
        delete from live_data where live_id = #{liveId}
    </delete>

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

    <update id="updateBatchLiveData">
        <foreach collection="liveDatas" item="liveData" separator=";">
            UPDATE live_data
            <set>
                <if test="liveData.pageViews != null">page_views = #{liveData.pageViews},</if>
                <if test="liveData.uniqueVisitors != null">unique_visitors = #{liveData.uniqueVisitors},</if>
                <if test="liveData.totalViews != null">total_views = #{liveData.totalViews},</if>
                <if test="liveData.uniqueViewers != null">unique_viewers = #{liveData.uniqueViewers},</if>
                <if test="liveData.peakConcurrentViewers != null">peak_concurrent_viewers = #{liveData.peakConcurrentViewers},</if>
                <if test="liveData.likes != null">likes = #{liveData.likes},</if>
                <if test="liveData.favouriteNum != null">favourite_num = #{liveData.favouriteNum},</if>
                <if test="liveData.followNum != null">follow_num = #{liveData.followNum},</if>
            </set>
            WHERE live_id = #{liveData.liveId}
        </foreach>
    </update>
</mapper>
