<?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.DeviceMapper">
    <resultMap id="DevMap" type="com.securitycontrol.entity.background.vo.DeviceVo">
        <id property="deviceId" column="device_id"/>
        <result property="deviceName" column="device_name"/>
        <result property="puId" column="device_code"/>
        <result property="macId" column="mac_id"/>
        <result property="gbCode" column="gb_code"/>
        <result property="deviceType" column="device_type"/>
        <result property="typeName" column="dict_name"/>
        <result property="delFlag" column="del_flag"/>
        <result property="manufacturer" column="manufacturer"/>
        <result property="onLine" column="on_line"/>
        <result property="tyCode" column="t_code"/>
        <result property="idx" column="idx"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>
    <insert id="insertData" parameterType="com.securitycontrol.entity.background.vo.DeviceVo">
        insert into  tb_device(device_id,device_name,device_code,
            mac_id,gb_code,device_type,create_time,del_flag,manufacturer,on_line,t_code,idx) value (
                             #{deviceId},#{deviceName},#{puId},
            #{macId},#{gbCode},#{deviceType},now(),0,#{manufacturer},#{onLine},#{tyCode},#{idx}
        )
    </insert>
    <update id="updateData">
         update   tb_device
                    set device_name=#{deviceName},device_code=#{puId},mac_id=#{macId},
                        gb_code=#{gbCode},device_type=#{deviceType},manufacturer=#{manufacturer},
                        on_line=#{onLine},t_code=#{tyCode},idx=#{idx},update_time=now()
                    where device_id=#{deviceId}
    </update>
    <delete id="deleteDevice">
            update   tb_device  set del_flag=1     where device_id=#{deviceId} and del_flag=0
    </delete>

    <select id="getDeviceList"  resultMap="DevMap">
        select td.device_id,device_name,device_code,mac_id,
                gb_code,device_type,td.create_time,td.update_time,
                td.del_flag,manufacturer,on_line,t_code,idx,sd.dict_name
            from  tb_device td
            left join sys_dict sd on sd.dict_code=td.device_type  and sd.p_code=1800
            where   td.del_flag=0
            <if test="keyWord!=null and keyWord!=''">
               and (device_name like concat('%',#{keyWord},'%')   or
                    sd.dict_name like concat('%',#{keyWord},'%')   or
                    mac_id =#{keyWord}  or
                    device_code =#{keyWord}  or
                    gb_code =#{keyWord}  or
                    t_code =#{keyWord}
                    )
            </if>
        <if test="keyWord!=null and keyWord!=''">
              and  on_line =#{onLine}
        </if>
    </select>
    <select id="getDeviceNumByQuery" resultType="java.lang.Integer">
        select COUNT(1)
            from  tb_device
                where  del_flag=0
                    <if test='type=="1" and dto.deviceName!="" and dto.deviceName!=null'>
                        and device_name=#{dto.deviceName}
                    </if>
                     <if test='type=="2" and dto.puId!="" and dto.puId!=null'>
                        and device_code=#{dto.puId}  and  idx=#{dto.idx}
                    </if>
                     <if test='type=="3" and dto.gbCode!="" and dto.gbCode!=null'>
                           and gb_code=#{dto.gbCode}
                    </if>
                    <if test='type=="4" and dto.macId!="" and dto.macId!=null'>
                        and mac_id=#{dto.macId}
                    </if>
                    <if test="dto.deviceId!=null and dto.deviceId!=''">
                                and device_id!=#{dto.deviceId}
                    </if>

    </select>
    <select id="getDeviceDetail" resultMap="DevMap">
          select td.device_id,device_name,device_code,mac_id,
                        gb_code,device_type,td.create_time,td.update_time,
                        td.del_flag,manufacturer,on_line,t_code,idx
                    from  tb_device td
                    left join sys_dict sd on sd.dict_code=td.device_type  and sd.p_code=1800
                    where   td.device_id=#{deviceId}
    </select>

</mapper>