<?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.system.mapper.UCdrMapper">

     <select id="getHistoryByUser" resultType="org.wfc.system.domain.vo.UCdrHistoryUserVo">
        SELECT
            ch.id,
            u.user_id,
            u.user_name,
            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
                LEFT JOIN u_user u on cdr.user_id = u.user_id
                AND u.del_flag = 0
        WHERE
            cdr.del_flag = 0
            AND ch.id is not null
            AND cdr.user_id is not null
            <if test="item.userId != null and item.userId != ''">
                AND cdr.user_id = #{item.userId}
            </if>
            <if test="item.userName != null and item.userName != ''">
                AND u.user_name like concat('%', #{item.userName}, '%')
             </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>

</mapper>
