<?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.securitycontrol.background.mapper.IDeviceOfBdMapper">
    <!--新增/修改边带-->
    <insert id="addOrUpdateDeviceBd">
        <if test="type == 1">
            INSERT INTO tb_pro_bd
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="id != null and id != ''">id,</if>
                <if test="bdName != null and bdName!=''">bd_name,</if>
                <if test="bdCode != null and bdCode!=''">bd_code,</if>
                <if test="bdIp != null and bdIp!=''">bd_ip,</if>
                <if test="bdType != null and bdType!=''">bd_type,</if>
                <if test="typeCode != null and typeCode!=''">type_code,</if>
                <if test="gtId != null and gtId!=''">gt_id,</if>
                <if test="bidCode != null and bidCode!=''">bid_code,</if>
                del_flag,
                <if test="updateTime != null and updateTime!=''">update_time,</if>
                <if test="createTime != null and createTime!=''">create_time,</if>
                <if test="bindTime != null and bindTime!=''">bind_time,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="id != null and id != ''">#{id},</if>
                <if test="bdName != null and bdName!=''">#{bdName},</if>
                <if test="bdCode != null and bdCode!=''">#{bdCode},</if>
                <if test="bdIp != null and bdIp!=''">#{bdIp},</if>
                <if test="bdType != null and bdType != ''">#{bdType},</if>
                <if test="typeCode != null and typeCode!=''">#{typeCode},</if>
                <if test="gtId != null and gtId!=''">#{gtId},</if>
                <if test="bidCode != null and bidCode!=''">#{bidCode},</if>
                0,
                <if test="updateTime != null and updateTime!=''">#{updateTime},</if>
                <if test="createTime != null and createTime!=''">#{createTime},</if>
                <if test="bindTime != null and bindTime!=''">#{bindTime},</if>
            </trim>
        </if>
        <if test="type == 2">
            UPDATE tb_pro_bd
            <set>
                <if test="bdName != null and bdName != ''">bd_name = #{bdName},</if>
                <if test="bdCode != null and bdCode != ''">bd_code = #{bdCode},</if>
                <if test="bdIp != null and bdIp != ''">bd_ip = #{bdIp},</if>
                <if test="bdType != null and bdType != ''">bd_type = #{bdType},</if>
                <if test="typeCode != null and typeCode != ''">type_code = #{typeCode},</if>
                <if test="gtId != null and gtId != ''">gt_id = #{gtId},</if>
                <if test="bidCode != null and bidCode != ''">bid_code = #{bidCode},</if>
                <if test="bindTime != null and bindTime != ''">bind_time = #{bindTime},</if>
            </set>
            WHERE id = #{id}
        </if>
    </insert>
    <!--新增/修改 边带子设备-->
    <insert id="addOrUpdateDeviceBdChild">
        <if test="type == 1">
            INSERT INTO tb_bd_device
            <trim prefix="(" suffix=")" suffixOverrides=",">
                <if test="deviceId != null and deviceId != ''">device_id,</if>
                <if test="deviceCode != null and deviceCode!=''">device_code,</if>
                <if test="deviceType != null and deviceType!=''">devic_type,</if>
                <if test="status != null and status!=''">status,</if>
                <if test="ip != null and ip!=''">ip,</if>
                <if test="deviceName != null and deviceName!=''">device_name,</if>
                <if test="createTime != null and createTime!=''">create_time,</if>
                <if test="updateTime != null and updateTime!=''">update_time,</if>
                del_flage,
                <if test="updateTime != null and updateTime!=''">device_model,</if>
                <if test="bdId != null and bdId!=''">bd_id,</if>
                <if test="areaId != null and areaId!=''">area_id,</if>
                <if test="gtId != null and gtId!=''">gt_id,</if>
            </trim>
            <trim prefix="values (" suffix=")" suffixOverrides=",">
                <if test="deviceId != null and deviceId != ''">#{deviceId},</if>
                <if test="deviceCode != null and deviceCode!=''">#{deviceCode},</if>
                <if test="deviceType != null and deviceType!=''">#{deviceType},</if>
                <if test="status != null and status!=''">#{status},</if>
                <if test="ip != null and ip != ''">#{ip},</if>
                <if test="deviceName != null and deviceName!=''">#{deviceName},</if>
                <if test="createTime != null and createTime!=''">#{createTime},</if>
                <if test="updateTime != null and updateTime!=''">#{updateTime},</if>
                0,
                <if test="deviceModel != null and deviceModel!=''">#{deviceModel},</if>
                <if test="bdId != null and bdId!=''">#{bdId},</if>
                <if test="areaId != null and areaId!=''">#{areaId},</if>
                <if test="gtId != null and gtId!=''">#{gtId},</if>
            </trim>
        </if>
        <if test="type == 2">
            UPDATE tb_bd_device
            <set>
                <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
                <if test="deviceType != null and deviceType != ''">devic_type = #{deviceType},</if>
                <if test="status != null and status != ''">status = #{status},</if>
                <if test="ip != null and ip != ''">ip = #{ip},</if>
                <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
                <if test="updateTime != null and updateTime != ''">update_time = #{updateTime},</if>
                <if test="deviceModel != null and deviceModel != ''">device_model = #{deviceModel},</if>
                <if test="bdId != null and bdId != ''">bd_id = #{bdId},</if>
                <if test="areaId != null and areaId != ''">area_id = #{areaId},</if>
                <if test="gtId != null and gtId != ''">gt_id = #{gtId},</if>
            </set>
            WHERE device_id = #{deviceId}
        </if>
    </insert>
    <!--边带设备绑定记录-->
    <insert id="addProBdDevData">
        INSERT INTO tb_pro_bd_dev
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">id,</if>
            <if test="devId != null and devId!=''">dev_id,</if>
            <if test="bidCode != null and bidCode!=''">bid_code,</if>
            <if test="bidTime != null and bidTime!=''">bid_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null and id != ''">#{id},</if>
            <if test="devId != null and devId!=''">#{devId},</if>
            <if test="bidCode != null and bidCode!=''">#{bidCode},</if>
            <if test="bidTime != null and bidTime!=''">#{bidTime},</if>
        </trim>
    </insert>
    <!--删除边带设备-->
    <delete id="delDeviceBdById">
        UPDATE tb_pro_bd SET del_flag = 0 WHERE id = #{id}
    </delete>
    <!--删除边带设备-->
    <delete id="delDeviceBdChildById">
        UPDATE tb_bd_device SET del_flage = 1 WHERE device_id = #{id}
    </delete>

    <!--获取边带列表-->
    <select id="getDeviceBdList" resultType="com.securitycontrol.entity.background.vo.DeviceBdVo">
        SELECT tpb.id,
        tpb.bd_name AS bdName,
        tpb.bd_code AS bdCode,
        tpb.bd_ip AS bdIp,
        sd.dict_name AS bdTypeName,
        sd2.dict_name AS typeCodeName,
        tp.pro_name AS proName,
        tpg.gt_name AS gtName,
        tpb.bind_time AS bindTime,
        tpb.bid_code AS bidCode,
        tpb.gt_id AS gtId
        FROM tb_pro_bd tpb
        LEFT JOIN sys_dict sd ON sd.dict_code = tpb.bd_type
        LEFT JOIN sys_dict sd2 ON sd2.dict_code = tpb.type_code
        LEFT JOIN tb_project tp ON tp.bid_code = tpb.bid_code AND tp.del_flag = 0
        LEFT JOIN t_pro_gt tpg ON tpg.gt_id = tpb.gt_id
        <where>
            tpb.del_flag = 0
            <if test="keyWord!=null and keyWord!=''">
                AND (
                INSTR(tpb.bd_name,#{keyWord}) > 0 OR
                INSTR(tpb.bd_code,#{keyWord}) > 0 OR
                INSTR(tp.pro_name,#{keyWord}) > 0 OR
                INSTR(tpg.gt_name,#{keyWord}) > 0
                )
            </if>
        </where>
    </select>
    <!--边带编码是否重复-->
    <select id="isBdCodeExist" resultType="java.lang.Integer">
        <if test="id == null or id == ''">
            SELECT COUNT(*) FROM tb_pro_bd tpb WHERE bd_code = #{bdCode} AND del_flag = 0
        </if>
        <if test="id != null and id != ''">
            SELECT COUNT(*) FROM tb_pro_bd tpb WHERE bd_code = #{bdCode} AND id != #{id} AND del_flag = 0
        </if>
    </select>
    <!--边带设备详情-->
    <select id="getDeviceBdById" resultType="com.securitycontrol.entity.background.vo.DeviceBdVo">
        SELECT tpb.id,
               tpb.bd_name AS bdName,
               tpb.bd_code AS bdCode,
               tpb.bd_ip AS bdIp,
               tpb.bid_code AS bidCode,
               tpb.type_code AS typeCode,
               tpb.bd_type AS bdType,
               tpb.gt_id AS gtId
        FROM tb_pro_bd tpb
        WHERE id = #{id}
    </select>
    <!--边带设备是否包含子设备-->
    <select id="isHasChildDeviceBd" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_bd_device WHERE bd_id = #{id} AND del_flage = 0
    </select>
    <!--获取边带子设备列表-->
    <select id="getDeviceBdChildList" resultType="com.securitycontrol.entity.background.vo.DeviceBdChildVo">
        SELECT tbd.device_id AS deviceId,
               tpb.bd_name AS bdName,
               tbd.device_name AS deviceName,
               tbd.update_time AS updateTime,
               tbd.device_model AS deviceModel,
               tbd.device_code AS deviceCode,
               sd2.dict_name AS status,
               tbd.ip,
               sd.dict_name AS deviceType,
               sa.area_name AS areaName,
               tbd.bd_id AS bdId
        FROM tb_bd_device tbd
        LEFT JOIN tb_pro_bd tpb ON tbd.bd_id = tpb.id
        LEFT JOIN sys_dict sd ON sd.dict_code = tbd.devic_type
        LEFT JOIN sys_dict sd2 ON sd2.dict_code = tbd.status
        LEFT JOIN tb_area sa ON tbd.area_id = sa.area_id
        <where>
            tbd.del_flage = 0
            <if test="keyWord!=null and keyWord!=''">
                AND (
                    INSTR(tpb.bd_name,#{keyWord}) > 0
                )
            </if>
        </where>
    </select>
    <!--边带设备编码是否重复-->
    <select id="isDeviceCodeExist" resultType="java.lang.Integer">
        <if test="deviceId == null or deviceId == ''">
            SELECT COUNT(*) FROM tb_bd_device tbd WHERE device_code = #{deviceCode} AND del_flage = 0
        </if>
        <if test="deviceId != null and deviceId != ''">
            SELECT COUNT(*) FROM tb_bd_device tbd WHERE device_code = #{deviceCode} AND device_id != #{deviceId} AND tbd.del_flage = 0
        </if>
    </select>
    <!--边带设备详情-->
    <select id="getDeviceBdChildById" resultType="com.securitycontrol.entity.background.vo.DeviceBdChildVo">
        SELECT tbd.device_id AS deviceId,
               tbd.device_name AS deviceName,
               tbd.update_time AS updateTime,
               tbd.device_model AS deviceModel,
               tbd.device_code AS deviceCode,
               tbd.ip,
               tbd.bd_id AS bdId,
               tbd.area_id AS areaId,
               tbd.devic_type AS deviceType,
               tbd.status
        FROM tb_bd_device tbd
        WHERE device_id = #{id}
    </select>
    <!--边带是否绑定重复工程-->
    <select id="isRepeatBanding" resultType="java.lang.Integer">
        SELECT COUNT(*) FROM tb_pro_bd WHERE id = #{id} AND bid_code = #{bidCode}
    </select>
</mapper>