<?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.TMapDeviceInfoMapper">
    
    <resultMap type="TMapDeviceInfo" id="TMapDeviceInfoResult">
        <result property="mapDeviceId"    column="map_device_id"    />
        <result property="mapDeviceName"    column="map_device_name"    />
        <result property="mapDeviceType"    column="map_device_type"    />
        <result property="mapDeviceNum"    column="map_device_num"    />
        <result property="mapDeviceMedium"    column="map_device_medium"    />
        <result property="mapDeviceUnit"    column="map_device_unit"    />
        <result property="longitude"    column="longitude"    />
        <result property="latitude"    column="latitude"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="isDel"    column="is_del"    />
        <result property="remark"    column="remark"    />
        <result property="monitorDeviceStatus"  column="monitor_device_status"    />
        <result property="monitorValue"  column="monitor_value"    />
    </resultMap>

    <sql id="selectTMapDeviceInfoVo">
        select map_device_id, map_device_name, map_device_type, map_device_num, map_device_medium, map_device_unit, longitude, latitude, create_by, create_time, update_by, update_time, is_del, remark from t_map_device_info
    </sql>

    <select id="selectTMapDeviceInfoList" parameterType="TMapDeviceInfo" resultMap="TMapDeviceInfoResult">
        <include refid="selectTMapDeviceInfoVo"/>
        <where>  
            <if test="mapDeviceName != null  and mapDeviceName != ''"> and map_device_name like concat('%', #{mapDeviceName}, '%')</if>
            <if test="mapDeviceType != null  and mapDeviceType != ''"> and map_device_type = #{mapDeviceType}</if>
            <if test="mapDeviceNum != null  and mapDeviceNum != ''"> and map_device_num = #{mapDeviceNum}</if>
            <if test="mapDeviceMedium != null  and mapDeviceMedium != ''"> and map_device_medium = #{mapDeviceMedium}</if>
            <if test="mapDeviceUnit != null  and mapDeviceUnit != ''"> and map_device_unit = #{mapDeviceUnit}</if>
            <if test="longitude != null "> and longitude = #{longitude}</if>
            <if test="latitude != null "> and latitude = #{latitude}</if>
            <if test="isDel != null  and isDel != ''"> and is_del = #{isDel}</if>
        </where>
    </select>
    
    <select id="selectTMapDeviceInfoById" parameterType="Long" resultMap="TMapDeviceInfoResult">
       SELECT
            device.map_device_id,
            device.map_device_name,
            device.map_device_type,
            device.map_device_num,
            device.map_device_medium,
            device.map_device_unit,
            device.longitude,
            device.latitude,
            mon.monitor_device_status,
            mon.monitor_value
        FROM
            t_map_device_info device
        LEFT JOIN (
          SELECT
                 max(monitor.monitor_id),
                 monitor.monitor_value,
                 monitor.monitor_device_status,
             monitor.monitor_device_num
            FROM
                t_map_device_monitor monitor
            GROUP BY
                monitor.monitor_device_num
        )mon ON device.map_device_num = mon.monitor_device_num
        where device.map_device_id = #{mapDeviceId}
    </select>
        
    <insert id="insertTMapDeviceInfo" parameterType="TMapDeviceInfo" useGeneratedKeys="true" keyProperty="mapDeviceId">
        insert into t_map_device_info
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mapDeviceName != null">map_device_name,</if>
            <if test="mapDeviceType != null">map_device_type,</if>
            <if test="mapDeviceNum != null">map_device_num,</if>
            <if test="mapDeviceMedium != null">map_device_medium,</if>
            <if test="mapDeviceUnit != null">map_device_unit,</if>
            <if test="longitude != null">longitude,</if>
            <if test="latitude != null">latitude,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
            <if test="isDel != null">is_del,</if>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mapDeviceName != null">#{mapDeviceName},</if>
            <if test="mapDeviceType != null">#{mapDeviceType},</if>
            <if test="mapDeviceNum != null">#{mapDeviceNum},</if>
            <if test="mapDeviceMedium != null">#{mapDeviceMedium},</if>
            <if test="mapDeviceUnit != null">#{mapDeviceUnit},</if>
            <if test="longitude != null">#{longitude},</if>
            <if test="latitude != null">#{latitude},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
            <if test="isDel != null">#{isDel},</if>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateTMapDeviceInfo" parameterType="TMapDeviceInfo">
        update t_map_device_info
        <trim prefix="SET" suffixOverrides=",">
            <if test="mapDeviceName != null">map_device_name = #{mapDeviceName},</if>
            <if test="mapDeviceType != null">map_device_type = #{mapDeviceType},</if>
            <if test="mapDeviceNum != null">map_device_num = #{mapDeviceNum},</if>
            <if test="mapDeviceMedium != null">map_device_medium = #{mapDeviceMedium},</if>
            <if test="mapDeviceUnit != null">map_device_unit = #{mapDeviceUnit},</if>
            <if test="longitude != null">longitude = #{longitude},</if>
            <if test="latitude != null">latitude = #{latitude},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
            <if test="isDel != null">is_del = #{isDel},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where map_device_id = #{mapDeviceId}
    </update>

    <delete id="deleteTMapDeviceInfoById" parameterType="Long">
        delete from t_map_device_info where map_device_id = #{mapDeviceId}
    </delete>

    <delete id="deleteTMapDeviceInfoByIds" parameterType="String">
        delete from t_map_device_info where map_device_id in 
        <foreach item="mapDeviceId" collection="array" open="(" separator="," close=")">
            #{mapDeviceId}
        </foreach>
    </delete>

    <select id="deviceInfoLists" parameterType="TMapDeviceInfo" resultMap="TMapDeviceInfoResult">
        SELECT
            device.map_device_id,
            device.map_device_name,
            device.map_device_type,
            device.map_device_num,
            device.map_device_medium,
            device.map_device_unit,
            device.longitude,
            device.latitude,
            mon.monitor_device_status,
            mon.monitor_value
        FROM
            t_map_device_info device
        LEFT JOIN (
          SELECT
                 max(monitor.monitor_id),
                 monitor.monitor_value,
                 monitor.monitor_device_status,
             monitor.monitor_device_num
            FROM
                t_map_device_monitor monitor
            GROUP BY
                monitor.monitor_device_num
        )mon ON device.map_device_num = mon.monitor_device_num
        <where>
            <if test="mapDeviceName != null  and mapDeviceName != ''"> and device.map_device_name like concat('%', #{mapDeviceName}, '%')</if>
            <if test="mapDeviceType != null  and mapDeviceType != ''"> and device.map_device_type = #{mapDeviceType}</if>
            <if test="mapDeviceNum != null  and mapDeviceNum != ''"> and device.map_device_num = #{mapDeviceNum}</if>
            <if test="isDel != null  and isDel != ''"> and device.is_del = #{isDel}</if>
        </where>
    </select>
</mapper>