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

    <update id="createEpsTrafficTable">
        CREATE TABLE IF NOT EXISTS ${tableName} (
            id BIGINT(20) AUTO_INCREMENT COMMENT '唯一标识ID',
            `name` VARCHAR(255) COMMENT '接口名称',
            `mac` VARCHAR(50) COMMENT 'MAC地址',
            `status` VARCHAR(20) COMMENT '运行状态',
            `type` VARCHAR(30) COMMENT '接口类型',
            ipV4 VARCHAR(20) COMMENT 'IPv4地址',
            `in_dropped` DECIMAL(20,2) COMMENT '入站丢包率(%)',
            `out_dropped` DECIMAL(20,2) COMMENT '出站丢包率(%)',
            `in_speed` varchar(50) COMMENT '接收带宽(bit)',
            `out_speed` varchar(50) COMMENT '发送带宽(bit)',
            `total_in_speed` varchar(50) COMMENT '总接收带宽(bit)',
            `total_out_speed` varchar(50) COMMENT '总发送带宽(bit)',
            `machine_flow` varchar(50) COMMENT '金山带宽(bit)',
            `speed` varchar(100) COMMENT '协商速度',
            `duplex` varchar(100) COMMENT '工作模式',
            business_id varchar(50) COMMENT '业务代码',
            business_name varchar(200) COMMENT '业务名称',
            service_sn varchar(64) COMMENT '服务器SN',
            node_name varchar(200) COMMENT '服务器名称',
            revenue_method varchar(10) COMMENT '收益方式(1.流量,2包端)',
            package_bandwidth DECIMAL(10,2) COMMENT '包端带宽值',
            create_time DATETIME COMMENT '创建时间',
            update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
            create_by VARCHAR(50) COMMENT '创建人',
            update_by VARCHAR(50) COMMENT '修改人',
            client_id VARCHAR(255) COMMENT '设备唯一标识',
            PRIMARY KEY (id),
            UNIQUE KEY uk_client_mac_name_time (client_id, mac, name, create_time),
            INDEX idx_name_time (`name`,create_time),
            INDEX idx_revenue_method (revenue_method),
            INDEX idx_service_sn (service_sn)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='EPS设备流量初始数据表';
    </update>

    <update id="createEpsInitialTrafficTable">
        CREATE TABLE IF NOT EXISTS ${tableName} (
            id BIGINT(20) AUTO_INCREMENT COMMENT '唯一标识ID',
            `name` VARCHAR(255) COMMENT '接口名称',
            `mac` VARCHAR(20) COMMENT 'MAC地址',
            `status` VARCHAR(20) COMMENT '运行状态',
            `type` VARCHAR(30) COMMENT '接口类型',
            ipV4 VARCHAR(20) COMMENT 'IPv4地址',
            `in_dropped` DECIMAL(20,2) COMMENT '入站丢包率(%)',
            `out_dropped` DECIMAL(20,2) COMMENT '出站丢包率(%)',
            `in_speed` VARCHAR(50) COMMENT '接收带宽(Mbps)',
            `out_speed` VARCHAR(50) COMMENT '发送带宽(Mbps)',
            `total_in_speed` VARCHAR(50) COMMENT '总接收带宽(Mbps)',
            `total_out_speed` VARCHAR(50) COMMENT '总发送带宽(Mbps)',
            `speed` varchar(100) COMMENT '协商速度',
            `duplex` varchar(100) COMMENT '工作模式',
            create_time DATETIME COMMENT '创建时间',
            update_time DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
            create_by VARCHAR(50) COMMENT '创建人',
            update_by VARCHAR(50) COMMENT '修改人',
            client_id VARCHAR(255) COMMENT '设备唯一标识',
            PRIMARY KEY (id),
            UNIQUE KEY uk_client_mac_name_time (client_id, mac, name, create_time),
            INDEX idx_name_time (`name`,create_time)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='初始带宽流量表';
    </update>

    <!-- 单条插入语句 -->
    <insert id="insert">
        INSERT INTO ${tableName} (
            `name`,
            `mac`,
            `status`,
            `type`,
            ipV4,
            `in_dropped`,
            `out_dropped`,
            `in_speed`,
            `out_speed`,
            `total_in_speed`,
            `total_out_speed`,
            `speed`,
            `duplex`,
            business_id,
            business_name,
            service_sn,
            node_name,
            revenue_method,
            package_bandwidth,
            create_by,
            update_by,
            client_id
        ) VALUES (
                     #{name},
                     #{mac},
                     #{status},
                     #{type},
                     #{ipV4},
                     #{inDropped},
                     #{outDropped},
                     #{inSpeed},
                     #{outSpeed},
                     #{totalInSpeed},
                     #{totalOutSpeed},
                     #{speed},
                     #{duplex},
                     #{businessId},
                     #{businessName},
                     #{serviceSn},
                     #{nodeName},
                     #{revenueMethod},
                     #{packageBandwidth},
                     #{createBy},
                     #{updateBy},
                     #{clientId}
                 )
    </insert>

    <!-- 批量插入语句 -->
    <insert id="batchInsert">
        INSERT IGNORE INTO ${tableName} (
        id,
        `name`,
        `mac`,
        `status`,
        `type`,
        ipV4,
        `in_dropped`,
        `out_dropped`,
        `in_speed`,
        `out_speed`,
        `total_in_speed`,
        `total_out_speed`,
        `speed`,
        `duplex`,
        business_id,
        business_name,
        service_sn,
        node_name,
        revenue_method,
        package_bandwidth,
        create_time,
        update_time,
        create_by,
        update_by,
        client_id
        ) VALUES
        <foreach collection="dataList" item="data" separator=",">
            (
            #{data.id,jdbcType=BIGINT},
            #{data.name,jdbcType=VARCHAR},
            #{data.mac,jdbcType=VARCHAR},
            #{data.status,jdbcType=VARCHAR},
            #{data.type,jdbcType=VARCHAR},
            #{data.ipV4,jdbcType=VARCHAR},
            #{data.inDropped,jdbcType=DECIMAL},
            #{data.outDropped,jdbcType=DECIMAL},
            #{data.inSpeed,jdbcType=VARCHAR},
            #{data.outSpeed,jdbcType=VARCHAR},
            #{data.totalInSpeed,jdbcType=VARCHAR},
            #{data.totalOutSpeed,jdbcType=VARCHAR},
            #{data.speed,jdbcType=VARCHAR},
            #{data.duplex,jdbcType=VARCHAR},
            #{data.businessId,jdbcType=VARCHAR},
            #{data.businessName,jdbcType=VARCHAR},
            #{data.serviceSn,jdbcType=VARCHAR},
            #{data.nodeName,jdbcType=VARCHAR},
            #{data.revenueMethod,jdbcType=VARCHAR},
            #{data.packageBandwidth,jdbcType=DECIMAL},
            #{data.createTime,jdbcType=TIMESTAMP},
            #{data.updateTime,jdbcType=TIMESTAMP},
            #{data.createBy,jdbcType=VARCHAR},
            #{data.updateBy,jdbcType=VARCHAR},
            #{data.clientId,jdbcType=VARCHAR}
            )
        </foreach>
    </insert>

    <!-- 条件查询 -->
    <select id="selectByCondition" resultType="EpsInitialTrafficData">
        SELECT
        id,
        `name` AS name,
        `mac` AS mac,
        `status` AS status,
        `type` AS type,
        `ipV4` AS ipV4,
        `in_dropped` AS inDropped,
        `out_dropped` AS outDropped,
        sum(ifnull(`in_speed`,0)) AS inSpeed,
        sum(ifnull(`out_speed`,0)) AS outSpeed,
        sum(ifnull(`total_in_speed`,0)) AS totalInSpeed,
        sum(ifnull(`total_out_speed`,0)) AS totalOutSpeed,
        `machine_flow` AS machineFlow,
        `speed` AS speed,
        `duplex` AS duplex,
        business_id AS businessId,
        business_name AS businessName,
        service_sn AS serviceSn,
        node_name AS nodeName,
        revenue_method AS revenueMethod,
        package_bandwidth AS packageBandwidth,
        create_time AS createTime,
        update_time AS updateTime,
        create_by AS createBy,
        update_by AS updateBy,
        client_id AS clientId
        FROM ${tableName}
        <where>
            <if test="serviceSn != '' and serviceSn != null">
                and service_sn = #{serviceSn}
            </if>
            <if test="businessId != '' and businessId != null">
                and business_id = #{businessId}
            </if>
            <if test="clientId != '' and clientId != null">
                and client_id = #{clientId}
            </if>
            <if test="startTime != null">
                and create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and create_time &lt;= #{endTime}
            </if>
            <if test="revenueMethod != null">
                and revenue_method = #{revenueMethod}
            </if>
        </where>
        group BY create_time desc
    </select>

    <!-- 条件查询 -->
    <select id="getAllTraficMsg" resultType="EpsInitialTrafficData">
        SELECT
        id,
        `name` AS name,
        `mac` AS mac,
        `status` AS status,
        `type` AS type,
        `ipV4` AS ipV4,
        `in_dropped` AS inDropped,
        `out_dropped` AS outDropped,
        `in_speed` AS inSpeed,
        `out_speed` AS outSpeed,
        `total_in_speed` AS totalInSpeed,
        `total_out_speed` AS totalOutSpeed,
        `speed` AS speed,
        `duplex` AS duplex,
        create_time AS createTime,
        update_time AS updateTime,
        create_by AS createBy,
        update_by AS updateBy,
        client_id AS clientId
        FROM ${tableName}
        <where>
            <if test="startTime != null">
                and create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and create_time &lt;= #{endTime}
            </if>
        </where>
        ORDER BY create_time desc
    </select>
    <update id="updateMachineTraffic" parameterType="EpsInitialTrafficData">
        update ${tableName} set machine_flow=#{machineFlow} where client_id=#{clientId} and create_time=#{createTime}
    </update>

    <select id="getTrafficListByClientIds" resultType="EpsInitialTrafficData">
        SELECT
        sum(ifnull(out_speed,0)) AS outSpeed,
        create_time AS createTime
        FROM ${tableName}
        <where>
            <if test="businessId != '' and businessId != null">
                and business_id = #{businessId}
            </if>
            <if test="startTime != null">
                and create_time &gt;= #{startTime}
            </if>
            <if test="endTime != null">
                and create_time &lt;= #{endTime}
            </if>
            <if test="clientIds != '' and clientIds != null">
                and client_id in
                <foreach collection="clientIds.split(',')" item="clientId" open="(" separator="," close=")">
                    #{clientId}
                </foreach>
            </if>
        </where>
        group by create_time
        ORDER BY out_speed desc
    </select>
</mapper>