<?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.tongran.rocketmq.mapper.RmPppoeConfigSubMapper">

    <resultMap type="RmPppoeConfigSub" id="RmPppoeConfigSubResult">
        <result property="id"    column="id"    />
        <result property="clientId"    column="client_id"    />
        <result property="serialNumber"    column="serial_number"    />
        <result property="vlanId"    column="vlan_id"    />
        <result property="ipv4Address"    column="ipv4_address"    />
        <result property="ipv4MaskBits"    column="ipv4_mask_bits"    />
        <result property="ipv4Gateway"    column="ipv4_gateway"    />
        <result property="bandwidthResult"    column="bandwidth_result"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateTime"    column="update_time"    />
        <result property="createBy"    column="create_by"    />
        <result property="updateBy"    column="update_by"    />
        <result property="status"    column="status"    />
    </resultMap>

    <sql id="selectRmPppoeConfigSubVo">
        select id, client_id, serial_number, vlan_id, ipv4_address, ipv4_mask_bits, ipv4_gateway, bandwidth_result, create_time, update_time, create_by, update_by, status from rm_pppoe_config_sub
    </sql>

    <select id="selectRmPppoeConfigSubList" parameterType="RmPppoeConfigSub" resultType="RmPppoeConfigSub">
        SELECT
        a.id,
        a.client_id clientId,
        a.serial_number serialNumber,
        a.vlan_id vlanId,
        a.ipv4_address ipv4Address,
        a.ipv4_mask_bits ipv4MaskBits,
        a.ipv4_gateway ipv4Gateway,
        a.bandwidth_result bandwidthResult,
        a.create_time createTime,
        a.update_time updateTime,
        a.create_by createBy,
        a.update_by updateBy,
        b.interface_name interfaceName,
        b.status status,
        b.mac_address macAddress,
        b.net_create_time netCreateTime,
        CASE
        WHEN b.net_create_time IS NULL THEN NULL
        ELSE CONCAT(
        FLOOR((UNIX_TIMESTAMP() - b.net_create_time) / 3600),  -- 计算小时
        'h',
        LPAD(FLOOR(((UNIX_TIMESTAMP() - b.net_create_time) % 3600) / 60), 2, '0'),  -- 计算分钟
        'm'
        )
        END as netRunTime
        FROM
        rm_pppoe_config_sub a
        LEFT JOIN
        rm_network_interface_child b
        ON a.client_id = b.client_id
        AND a.ipv4_address = b.ipv4_address
        <where>
            <if test="clientId != null  and clientId != ''"> and a.client_id = #{clientId}</if>
            <if test="serialNumber != null "> and a.serial_number = #{serialNumber}</if>
            <if test="vlanId != null "> and a.vlan_id = #{vlanId}</if>
            <if test="ipv4Address != null  and ipv4Address != ''"> and a.ipv4_address = #{ipv4Address}</if>
            <if test="ipv4MaskBits != null "> and a.ipv4_mask_bits = #{ipv4MaskBits}</if>
            <if test="ipv4Gateway != null  and ipv4Gateway != ''"> and a.ipv4_gateway = #{ipv4Gateway}</if>
            <if test="status != null  and status != ''"> and a.status = #{status}</if>
        </where>
    </select>

    <select id="selectRmPppoeConfigSubById" parameterType="Long" resultMap="RmPppoeConfigSubResult">
        <include refid="selectRmPppoeConfigSubVo"/>
        where id = #{id}
    </select>

    <insert id="insertRmPppoeConfigSub" parameterType="RmPppoeConfigSub" useGeneratedKeys="true" keyProperty="id">
        insert into rm_pppoe_config_sub
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="clientId != null">client_id,</if>
            <if test="serialNumber != null">serial_number,</if>
            <if test="vlanId != null">vlan_id,</if>
            <if test="ipv4Address != null">ipv4_address,</if>
            <if test="ipv4MaskBits != null">ipv4_mask_bits,</if>
            <if test="ipv4Gateway != null">ipv4_gateway,</if>
            <if test="bandwidthResult != null">bandwidth_result,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="createBy != null">create_by,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="status != null">status,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="clientId != null">#{clientId},</if>
            <if test="serialNumber != null">#{serialNumber},</if>
            <if test="vlanId != null">#{vlanId},</if>
            <if test="ipv4Address != null">#{ipv4Address},</if>
            <if test="ipv4MaskBits != null">#{ipv4MaskBits},</if>
            <if test="ipv4Gateway != null">#{ipv4Gateway},</if>
            <if test="bandwidthResult != null">#{bandwidthResult},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="status != null">#{status},</if>
        </trim>
    </insert>

    <update id="updateRmPppoeConfigSub" parameterType="RmPppoeConfigSub">
        update rm_pppoe_config_sub
        <trim prefix="SET" suffixOverrides=",">
            <if test="clientId != null">client_id = #{clientId},</if>
            <if test="serialNumber != null">serial_number = #{serialNumber},</if>
            <if test="vlanId != null">vlan_id = #{vlanId},</if>
            <if test="ipv4Address != null">ipv4_address = #{ipv4Address},</if>
            <if test="ipv4MaskBits != null">ipv4_mask_bits = #{ipv4MaskBits},</if>
            <if test="ipv4Gateway != null">ipv4_gateway = #{ipv4Gateway},</if>
            <if test="bandwidthResult != null">bandwidth_result = #{bandwidthResult},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="status != null">status = #{status},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRmPppoeConfigSubById" parameterType="Long">
        delete from rm_pppoe_config_sub where id = #{id}
    </delete>

    <delete id="deleteRmPppoeConfigSubByIds" parameterType="String">
        delete from rm_pppoe_config_sub where id in 
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
    <insert id="batchInsertRmPppoeConfigSub" parameterType="java.util.List">
        insert into rm_pppoe_config_sub
        (client_id, serial_number, vlan_id, ipv4_address, ipv4_mask_bits, ipv4_gateway,
        bandwidth_result, create_time, update_time, create_by, update_by)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.clientId},
            #{item.serialNumber},
            #{item.vlanId},
            #{item.ipv4Address},
            #{item.ipv4MaskBits},
            #{item.ipv4Gateway},
            #{item.bandwidthResult},
            #{item.createTime},
            #{item.updateTime},
            #{item.createBy},
            #{item.updateBy}
            )
        </foreach>
    </insert>
    <delete id="deleteRmPppoeConfigSubByClientId" parameterType="String">
        delete from rm_pppoe_config_sub where client_id = #{clientId}
    </delete>
    <update id="updateRmPppoeConfigSubByVlan" parameterType="RmPppoeConfigSub">
        update rm_pppoe_config_sub
        <trim prefix="SET" suffixOverrides=",">
            <if test="status != null">status = #{status},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        <where>
            <choose>
                <when test="clientId!=null and vlanId!=null and serialNumber != null">
                    and client_id=#{clientId} and vlan_id=#{vlanId} and serial_number=#{serialNumber}
                </when>
                <otherwise>
                    and 1=0
                </otherwise>
            </choose>
        </where>
    </update>
</mapper>