<?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="org.wfc.user.mapper.UCdrMapper">

    <select id="getByUser" resultType="org.wfc.user.domain.vo.UCdrUserVo">
        SELECT
        	t.id,
        	max( t.activity ) activity,
        	sum( t.duration ) duration,
        	sum( t.traffic_down ) traffic_down,
        	sum( t.traffic_up ) traffic_up
        FROM
        	(
        	SELECT
        		cdr.user_id id,
        		max( cdr.activity ) activity,
        		sum(
        		ifnull( ch.duration, 0 )) duration,
        		sum(
        		ifnull( ch.traffic_down, 0 )) traffic_down,
        		sum(
        		ifnull( ch.traffic_up, 0 )) traffic_up
        	FROM
        		u_cdr cdr
        		LEFT JOIN u_cdr_history ch ON cdr.id = ch.cdr_id
        		AND ch.del_flag = 0
        	WHERE
        		cdr.del_flag = 0
                AND ch.use_type = 0
                <if test="userId != null and userId != ''">
                    AND cdr.user_id = #{userId}
                </if>
                <if test="endTime != null">
                    AND ch.start_time &lt;= #{endTime}
                </if>
                <if test="startTime != null">
                    AND ch.start_time >= #{startTime}
                </if>
        	GROUP BY
        		cdr.user_id UNION
        	SELECT
        		cdr.user_id id,
        		max( cdr.activity ) activity,
        		sum(
        		ifnull( cdr.up_time, 0 )) duration,
        		sum(
        		ifnull( cdr.traffic_down, 0 )) traffic_down,
        		sum(
        		ifnull( cdr.traffic_up, 0 )) traffic_up
        	FROM
        		u_cdr cdr
            LEFT JOIN u_client c ON cdr.client_id = c.id AND c.del_flag = 0
        	WHERE
        		cdr.del_flag = 0
                <if test="userId != null and userId != ''">
                    AND cdr.user_id = #{userId}
                </if>
                AND c.active = 1
        	GROUP BY
        		cdr.user_id
        	) t
        GROUP BY
        	t.id
    </select>

    <select id="getByClient" resultType="org.wfc.user.domain.vo.UCdrClientVo">
        SELECT
            c.id,
            c.client_mac,
            c.client_name,
            c.client_device_type,
            h.*
        FROM
            u_client c
                LEFT JOIN (
                SELECT
                    cdr.client_id,
                    min( ch.start_time ) start_time,
                    max( cdr.last_seen_time ) end_time,
                    sum(
                            ifnull( cdr.up_time, 0 ))+ sum(
                            ifnull( ch.duration, 0 )) duration,
                    sum(
                            ifnull( cdr.traffic_down, 0 )) + sum(
                            ifnull( ch.traffic_down, 0 )) traffic_down,
                    sum(
                            ifnull( cdr.traffic_up, 0 )) + sum(
                            ifnull( ch.traffic_up, 0 )) traffic_up
                FROM
                    u_cdr cdr
                        LEFT JOIN u_cdr_history ch ON cdr.id = ch.cdr_id
                        AND ch.del_flag = 0
                WHERE
                    cdr.del_flag = 0
                  AND cdr.last_seen_time != ifnull( ch.end_time, 0 )
                GROUP BY
                    cdr.client_id
            ) h ON c.id = h.client_id
        WHERE
            c.del_flag = 0
            <if test="client.clientName != null and client.clientName != ''">
                AND c.client_name like concat('%', #{client.clientName}, '%')
            </if>
            <if test="client.clientMac != null and client.clientMac != ''">
                AND c.client_mac = like concat('%', #{client.clientMac}, '%')
            </if>
    </select>

    <select id="getHistoryByUser" resultType="org.wfc.user.domain.vo.UCdrHistoryUserVo">
        SELECT
            ch.id,
            c.client_name,
            c.client_mac,
            c.client_device_type,
            ch.start_time,
            ch.end_time,
            ifnull( ch.duration, 0 ) duration,
            ifnull( ch.traffic_down, 0 ) traffic_down,
            ifnull( ch.traffic_up, 0 ) traffic_up
        FROM
            u_cdr cdr
                LEFT JOIN u_cdr_history ch ON cdr.id = ch.cdr_id
                AND ch.del_flag = 0
                LEFT JOIN u_client c ON cdr.client_id = c.id
                AND c.del_flag = 0
        WHERE
            cdr.del_flag = 0
            AND ch.id is not null
            <if test="item.userId != null and item.userId != ''">
                AND cdr.user_id = #{item.userId}
            </if>
            <if test="item.clientName != null and item.clientName != ''">
                AND c.client_name like concat('%', #{item.clientName}, '%')
            </if>
            <if test="item.clientMac != null and item.clientMac != ''">
                AND c.client_mac like concat('%', #{item.clientMac}, '%')
            </if>
            <if test="item.startTimeS != null and item.startTimeE != null">
                AND ch.start_time between #{item.startTimeS} and #{item.startTimeE}
            </if>
            <if test="item.endTimeS != null and item.endTimeE != null">
                AND ch.end_time between #{item.endTimeS} and #{item.endTimeE}
            </if>
        ORDER BY
            ch.start_time DESC
    </select>

    <select id="getLatestHistory" resultType="org.wfc.user.domain.vo.UCdrLatestHistoryVo">
        SELECT
            h.*,
            c.user_id
        FROM
            u_cdr_history h
                LEFT JOIN u_cdr c ON h.cdr_id = c.id
                AND c.del_flag = 0
        WHERE
            h.del_flag = 0
        <if test="userIds != null and userIds.size() > 0">
            AND c.user_id in
            <foreach collection="userIds" item="userId" open="(" separator="," close=")">
                #{userId}
            </foreach>
        </if>
        <if test="endTime != null">
            AND h.create_time > #{endTime}
        </if>
    </select>

    <select id="getCurrentTraffic" resultType="long">
        SELECT
        	sum(ifnull( cdr.traffic_down, 0 )) +
        	sum(ifnull( cdr.traffic_up, 0 )) traffic
        FROM
        	u_client c
        	LEFT JOIN u_cdr cdr ON c.id = cdr.client_id
        	AND cdr.del_flag = 0
        WHERE
        	c.del_flag = 0
        	AND c.active = 1
        	AND c.user_id = #{userId}
        	AND cdr.id IS NOT NULL
        GROUP BY
        	c.user_id
    </select>

    <select id="getClientByFirstSeen" resultType="org.wfc.user.domain.UClient">
        SELECT
        	c.*
        FROM
        	u_client c
        	LEFT JOIN u_cdr cdr ON c.id = cdr.client_id AND cdr.del_flag = 0
        WHERE
          c.del_flag = 0
        	AND c.site_id = #{siteId}
          AND c.client_mac = #{clientMac}
        	AND cdr.last_seen_time - cdr.up_time * 10000 &lt; #{firstSeen}
        	AND cdr.last_seen_time - cdr.up_time * 1000 >= #{firstSeen}
        	AND cdr.id is not NULL
        LIMIT 1
    </select>
</mapper>
