<?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.zehong.system.mapper.TDeviceInfoMapper">
    
    <resultMap type="TDeviceInfo" id="TDeviceInfoResult">
        <result property="deviceId"    column="device_id"    />
        <result property="enterpriseId"    column="enterprise_id"    />
        <result property="pipeCode"    column="pipe_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceAddr"    column="device_addr"    />
        <result property="deviceModel"    column="device_model"    />
        <result property="deviceType"    column="device_type"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="iotNo"    column="iot_no"    />
        <result property="iconUrl"    column="icon_url"    />
        <result property="linkman"    column="linkman"    />
        <result property="phone"    column="phone"    />
        <result property="installationTime"    column="installation_time"    />
        <result property="inspectionTime"    column="inspection_time"    />
        <result property="remarks"    column="remarks"    />
    </resultMap>

    <sql id="selectTDeviceInfoVo">
        select device_id, enterprise_id, pipe_code, device_name, device_code, device_addr, device_model, device_type, longitude, latitude, iot_no, icon_url, linkman, phone, installation_time, inspection_time, remarks from t_device_info
    </sql>

    <select id="selectTDeviceInfoList" parameterType="TDeviceInfo" resultMap="TDeviceInfoResult">
        <include refid="selectTDeviceInfoVo"/>
        <where>  
            <if test="enterpriseId != null and enterpriseId != 0 "> and enterprise_id = #{enterpriseId}</if>
            <if test="pipeCode != null "> and pipe_code = #{pipeCode}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="deviceCode != null  and deviceCode != ''"> and device_code like concat('%', #{deviceCode}, '%')</if>
            <if test="deviceAddr != null  and deviceAddr != ''"> and device_addr = #{deviceAddr}</if>
            <if test="deviceModel != null  and deviceModel != ''"> and device_model = #{deviceModel}</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="iotNo != null  and iotNo != ''"> and iot_no = #{iotNo}</if>
            <if test="iconUrl != null  and iconUrl != ''"> and icon_url = #{iconUrl}</if>
            <if test="linkman != null  and linkman != ''"> and linkman = #{linkman}</if>
            <if test="phone != null  and phone != ''"> and phone = #{phone}</if>
            <if test="installationTime != null "> and installation_time = #{installationTime}</if>
            <if test="inspectionTime != null "> and inspection_time = #{inspectionTime}</if>
            <if test="remarks != null  and remarks != ''"> and remarks = #{remarks}</if>
        </where>
    </select>
    
    <select id="selectTDeviceInfoById" parameterType="int" resultMap="TDeviceInfoResult">
        <include refid="selectTDeviceInfoVo"/>
        where device_id = #{deviceId}
    </select>

    <select id="selectTDeviceInfoByCode" parameterType="String" resultMap="TDeviceInfoResult">
        <include refid="selectTDeviceInfoVo"/>
        where device_code = #{deviceCode}
    </select>

    <select id="countDeviceByType" resultType="map">
        select count(device_id) number, device_type type from t_device_info
        group by device_type
    </select>
        
    <insert id="insertTDeviceInfo" parameterType="TDeviceInfo" useGeneratedKeys="true" keyProperty="deviceId">
        insert into t_device_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="enterpriseId != null and enterpriseId != 0">enterprise_id,</if>
            <if test="pipeCode != null">pipe_code,</if>
            <if test="deviceName != null">device_name,</if>
            <if test="deviceCode != null">device_code,</if>
            <if test="deviceAddr != null">device_addr,</if>
            <if test="deviceModel != null">device_model,</if>
            <if test="deviceType != null">device_type,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="iotNo != null">iot_no,</if>
            <if test="iconUrl != null">icon_url,</if>
            <if test="linkman != null">linkman,</if>
            <if test="phone != null">phone,</if>
            <if test="installationTime != null">installation_time,</if>
            <if test="inspectionTime != null">inspection_time,</if>
            <if test="remarks != null">remarks,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="enterpriseId != null and enterpriseId != 0">#{enterpriseId},</if>
            <if test="pipeCode != null">#{pipeCode},</if>
            <if test="deviceName != null">#{deviceName},</if>
            <if test="deviceCode != null">#{deviceCode},</if>
            <if test="deviceAddr != null">#{deviceAddr},</if>
            <if test="deviceModel != null">#{deviceModel},</if>
            <if test="deviceType != null">#{deviceType},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="iotNo != null">#{iotNo},</if>
            <if test="iconUrl != null">#{iconUrl},</if>
            <if test="linkman != null">#{linkman},</if>
            <if test="phone != null">#{phone},</if>
            <if test="installationTime != null">#{installationTime},</if>
            <if test="inspectionTime != null">#{inspectionTime},</if>
            <if test="remarks != null">#{remarks},</if>
         </trim>
    </insert>

    <update id="updateTDeviceInfo" parameterType="TDeviceInfo">
        update t_device_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="enterpriseId != null and enterpriseId != 0">enterprise_id = #{enterpriseId},</if>
            <if test="pipeCode != null">pipe_code = #{pipeCode},</if>
            <if test="deviceName != null">device_name = #{deviceName},</if>
            <if test="deviceCode != null">device_code = #{deviceCode},</if>
            <if test="deviceAddr != null">device_addr = #{deviceAddr},</if>
            <if test="deviceModel != null">device_model = #{deviceModel},</if>
            <if test="deviceType != null">device_type = #{deviceType},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="iotNo != null">iot_no = #{iotNo},</if>
            <if test="iconUrl != null">icon_url = #{iconUrl},</if>
            <if test="linkman != null">linkman = #{linkman},</if>
            <if test="phone != null">phone = #{phone},</if>
            <if test="installationTime != null">installation_time = #{installationTime},</if>
            <if test="inspectionTime != null">inspection_time = #{inspectionTime},</if>
            <if test="remarks != null">remarks = #{remarks},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

    <delete id="deleteTDeviceInfoById" parameterType="Long">
        delete from t_device_info where device_id = #{deviceId}
    </delete>

    <delete id="deleteTDeviceInfoByIds" parameterType="String">
        delete from t_device_info where device_id in 
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </delete>
</mapper>