<?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.ruoyi.business.mapper.HwDeviceMapper">
    
    <resultMap type="HwDevice" id="HwDeviceResult">
        <result property="deviceId"    column="device_id"    />
        <result property="deviceCode"    column="device_code"    />
        <result property="deviceName"    column="device_name"    />
        <result property="sceneId"    column="scene_id"    />
        <result property="monitorUnitId"    column="monitor_unit_id"    />
        <result property="deviceType"    column="device_type"    />
        <result property="networkingMode"    column="networking_mode"    />
        <result property="accessProtocol"    column="access_protocol"    />
        <result property="dataFormat"    column="data_format"    />
        <result property="releatedDeviceId"    column="releated_device_id"    />
        <result property="deviceModeId"    column="device_mode_id"    />
        <result property="accessGwProtocol"    column="access_gw_protocol"    />
        <result property="activeStatus"    column="active_status"    />
        <result property="deviceStatus"    column="device_status"    />
        <result property="activeTime"    column="active_time"    />
        <result property="devicePic"    column="device_pic"    />
        <result property="ipAddress"    column="ip_address"    />
        <result property="areaId"    column="area_id"    />
        <result property="deviceLocation"    column="device_location"    />
        <result property="currentModuleVersion"    column="current_module_version"    />
        <result property="currentSinglechipVersion"    column="current_singlechip_version"    />
        <result property="remark"    column="remark"    />
        <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="deviceField"    column="device_field"    />
        <result property="tenantId"    column="tenant_id"    />
        <result property="onlineStatus"    column="online_status"    />
    </resultMap>

    <!--以下为监控平台接口所用-->
    <resultMap id="DeviceModeVoResult" type="com.ruoyi.business.domain.VO.DeviceModeVo" >
        <result property="deviceModeName" column="device_mode_name"/>
        <result property="sum" column="sum"/>
        <result property="err" column="err"/>
    </resultMap>
    <select id="selectDeviceModeNameVo" resultMap="DeviceModeVoResult">
        SELECT b.device_mode_name ,a.sum FROM hw_device_mode b
                                                  LEFT JOIN (SELECT device_mode_id,count(*) as sum FROM hw_device GROUP BY device_mode_id order by sum desc
        ) a on a.device_mode_id = b.device_mode_id
    </select>
    <select id="selectDeviceByDeviceModeByDeviceModeFunction" parameterType="Long" resultType="String">
        select count(*) from hw_device_mode_function where device_mode_id =
                                                           (SELECT device_mode_id from hw_device where device_id = #{deviceId}) and function_type = '1'
    </select>
    <select id="selectDeviceModeFunctionByFunctionType" parameterType="Long" resultType="String">

    </select>
    <select id="selectDeviceNum" parameterType="Long" resultMap="HwDeviceResult" >
        <include refid="selectHwDeviceVo"/>
        where scene_id = #{sceneId}
    </select>
    <select id="selectDeviceByMonitorId" parameterType="Long" resultMap="HwDeviceResult">
        <include refid="selectHwDeviceVo"/>
        where monitor_unit_id = #{monitorUnitId}
    </select>
    <select id="selectDeviceByDeviceModeBySceneId" parameterType="Long" resultMap="HwDeviceResult">
        select * from hw_device where device_mode_id = (
            SELECT device_mode_id from hw_device_mode where scene_id = #{sceneId} )
    </select>
    <select id="selectDeviceByDeviceModeByMonitorUnitId" parameterType="Long" resultMap="HwDeviceResult">
        select * from hw_device where device_mode_id = (
            SELECT device_mode_id from hw_device_mode where monitor_unit_id = #{monitorUnitId} )
    </select>
    <!--以上为监控平台接口所用-->

    <sql id="selectHwDeviceVo">
        select device_id, device_code, device_name, scene_id, monitor_unit_id, device_type, networking_mode, access_protocol, data_format, releated_device_id, device_mode_id, access_gw_protocol, active_status, device_status, active_time, device_pic, ip_address, area_id, device_location, current_module_version, current_singlechip_version, remark, create_by, create_time, update_by, update_time, device_field, tenant_id, online_status from hw_device
    </sql>

    <select id="selectHwDeviceList" parameterType="HwDevice" resultMap="HwDeviceResult">
        <include refid="selectHwDeviceVo"/>
        <where>  
            <if test="deviceCode != null  and deviceCode != ''"> and device_code = #{deviceCode}</if>
            <if test="deviceName != null  and deviceName != ''"> and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="sceneId != null "> and scene_id = #{sceneId}</if>
            <if test="monitorUnitId != null "> and monitor_unit_id = #{monitorUnitId}</if>
            <if test="deviceType != null  and deviceType != ''"> and device_type = #{deviceType}</if>
            <if test="networkingMode != null  and networkingMode != ''"> and networking_mode = #{networkingMode}</if>
            <if test="accessProtocol != null "> and access_protocol = #{accessProtocol}</if>
            <if test="dataFormat != null "> and data_format = #{dataFormat}</if>
            <if test="releatedDeviceId != null "> and releated_device_id = #{releatedDeviceId}</if>
            <if test="deviceModeId != null "> and device_mode_id = #{deviceModeId}</if>
            <if test="accessGwProtocol != null "> and access_gw_protocol = #{accessGwProtocol}</if>
            <if test="activeStatus != null  and activeStatus != ''"> and active_status = #{activeStatus}</if>
            <if test="deviceStatus != null  and deviceStatus != ''"> and device_status = #{deviceStatus}</if>
            <if test="activeTime != null "> and active_time = #{activeTime}</if>
            <if test="devicePic != null  and devicePic != ''"> and device_pic = #{devicePic}</if>
            <if test="ipAddress != null  and ipAddress != ''"> and ip_address = #{ipAddress}</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="deviceLocation != null  and deviceLocation != ''"> and device_location = #{deviceLocation}</if>
            <if test="currentModuleVersion != null  and currentModuleVersion != ''"> and current_module_version = #{currentModuleVersion}</if>
            <if test="currentSinglechipVersion != null  and currentSinglechipVersion != ''"> and current_singlechip_version = #{currentSinglechipVersion}</if>
            <if test="deviceField != null  and deviceField != ''"> and device_field = #{deviceField}</if>
            <if test="tenantId != null "> and tenant_id = #{tenantId}</if>
            <if test="onlineStatus != null  and onlineStatus != ''"> and online_status = #{onlineStatus}</if>
        </where>
    </select>
    
    <select id="selectHwDeviceByDeviceId" parameterType="Long" resultMap="HwDeviceResult">
        <include refid="selectHwDeviceVo"/>
        where device_id = #{deviceId}
    </select>
        
    <insert id="insertHwDevice" parameterType="HwDevice" useGeneratedKeys="true" keyProperty="deviceId">
        insert into hw_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="sceneId != null">scene_id,</if>
            <if test="monitorUnitId != null">monitor_unit_id,</if>
            <if test="deviceType != null and deviceType != ''">device_type,</if>
            <if test="networkingMode != null and networkingMode != ''">networking_mode,</if>
            <if test="accessProtocol != null">access_protocol,</if>
            <if test="dataFormat != null">data_format,</if>
            <if test="releatedDeviceId != null">releated_device_id,</if>
            <if test="deviceModeId != null">device_mode_id,</if>
            <if test="accessGwProtocol != null">access_gw_protocol,</if>
            <if test="activeStatus != null and activeStatus != ''">active_status,</if>
            <if test="deviceStatus != null and deviceStatus != ''">device_status,</if>
            <if test="activeTime != null">active_time,</if>
            <if test="devicePic != null">device_pic,</if>
            <if test="ipAddress != null">ip_address,</if>
            <if test="areaId != null">area_id,</if>
            <if test="deviceLocation != null">device_location,</if>
            <if test="currentModuleVersion != null">current_module_version,</if>
            <if test="currentSinglechipVersion != null">current_singlechip_version,</if>
            <if test="remark != null">remark,</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="deviceField != null">device_field,</if>
            <if test="tenantId != null">tenant_id,</if>
            <if test="onlineStatus != null and onlineStatus != ''">online_status,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">#{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="sceneId != null">#{sceneId},</if>
            <if test="monitorUnitId != null">#{monitorUnitId},</if>
            <if test="deviceType != null and deviceType != ''">#{deviceType},</if>
            <if test="networkingMode != null and networkingMode != ''">#{networkingMode},</if>
            <if test="accessProtocol != null">#{accessProtocol},</if>
            <if test="dataFormat != null">#{dataFormat},</if>
            <if test="releatedDeviceId != null">#{releatedDeviceId},</if>
            <if test="deviceModeId != null">#{deviceModeId},</if>
            <if test="accessGwProtocol != null">#{accessGwProtocol},</if>
            <if test="activeStatus != null and activeStatus != ''">#{activeStatus},</if>
            <if test="deviceStatus != null and deviceStatus != ''">#{deviceStatus},</if>
            <if test="activeTime != null">#{activeTime},</if>
            <if test="devicePic != null">#{devicePic},</if>
            <if test="ipAddress != null">#{ipAddress},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="deviceLocation != null">#{deviceLocation},</if>
            <if test="currentModuleVersion != null">#{currentModuleVersion},</if>
            <if test="currentSinglechipVersion != null">#{currentSinglechipVersion},</if>
            <if test="remark != null">#{remark},</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="deviceField != null">#{deviceField},</if>
            <if test="tenantId != null">#{tenantId},</if>
            <if test="onlineStatus != null and onlineStatus != ''">#{onlineStatus},</if>
         </trim>
    </insert>

    <update id="updateHwDevice" parameterType="HwDevice">
        update hw_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceCode != null and deviceCode != ''">device_code = #{deviceCode},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="sceneId != null">scene_id = #{sceneId},</if>
            <if test="monitorUnitId != null">monitor_unit_id = #{monitorUnitId},</if>
            <if test="deviceType != null and deviceType != ''">device_type = #{deviceType},</if>
            <if test="networkingMode != null and networkingMode != ''">networking_mode = #{networkingMode},</if>
            <if test="accessProtocol != null">access_protocol = #{accessProtocol},</if>
            <if test="dataFormat != null">data_format = #{dataFormat},</if>
            <if test="releatedDeviceId != null">releated_device_id = #{releatedDeviceId},</if>
            <if test="deviceModeId != null">device_mode_id = #{deviceModeId},</if>
            <if test="accessGwProtocol != null">access_gw_protocol = #{accessGwProtocol},</if>
            <if test="activeStatus != null and activeStatus != ''">active_status = #{activeStatus},</if>
            <if test="deviceStatus != null and deviceStatus != ''">device_status = #{deviceStatus},</if>
            <if test="activeTime != null">active_time = #{activeTime},</if>
            <if test="devicePic != null">device_pic = #{devicePic},</if>
            <if test="ipAddress != null">ip_address = #{ipAddress},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="deviceLocation != null">device_location = #{deviceLocation},</if>
            <if test="currentModuleVersion != null">current_module_version = #{currentModuleVersion},</if>
            <if test="currentSinglechipVersion != null">current_singlechip_version = #{currentSinglechipVersion},</if>
            <if test="remark != null">remark = #{remark},</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="deviceField != null">device_field = #{deviceField},</if>
            <if test="tenantId != null">tenant_id = #{tenantId},</if>
            <if test="onlineStatus != null and onlineStatus != ''">online_status = #{onlineStatus},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

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

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