<?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.zsc.edu.gateway.modules.iot.device.repo.DeviceRepository">
    <resultMap id="BaseResultMap" type="com.zsc.edu.gateway.modules.iot.device.vo.DeviceVo">
        <id column="id" property="id"/>
        <result column="name" property="name"/>
        <result column="online" property="online"/>
        <result column="state" property="state"/>
        <result column="hardware_version" property="hardwareVersion"/>
        <result column="firmware_version" property="firmwareVersion"/>
        <result column="factory_id" property="factoryId"/>
        <result column="client_id" property="clientId"/>
        <result column="product_id" property="productId"/>
        <result column="extend_params" property="extendParams" jdbcType="VARCHAR"
                typeHandler="com.zsc.edu.gateway.framework.json.JsonbTypeHandler"/>
        <result column="properties" property="properties" jdbcType="VARCHAR"
                typeHandler="com.zsc.edu.gateway.framework.json.JsonbTypeHandler"/>
        <result column="create_by" jdbcType="VARCHAR" property="deviceCreateBy"/>
        <result column="update_by" jdbcType="VARCHAR" property="deviceUpdateBy"/>
        <result column="create_time" jdbcType="DATE" property="deviceCreateTime"/>
        <result column="update_time" jdbcType="DATE" property="deviceUpdateTime"/>
        <result column="remark" jdbcType="VARCHAR" property="deviceRemark"/>
        <result column="longitude" jdbcType="DOUBLE" property="longitude"/>
        <result column="latitude" jdbcType="DOUBLE" property="latitude"/>
        <result column="icon" jdbcType="VARCHAR" property="icon"/>
        <result column="name" property="productName"/>
        <result column="product_type" jdbcType="VARCHAR" property="productType"/>
        <result column="model" property="model"/>
        <result column="link" property="link"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="update_by" jdbcType="VARCHAR" property="updateBy"/>
        <result column="create_time" jdbcType="DATE" property="createTime"/>
        <result column="update_time" jdbcType="DATE" property="updateTime"/>
        <result column="remark" jdbcType="VARCHAR" property="remark"/>
        <result column="enabled" jdbcType="BOOLEAN" property="enabled"/>
        <collection property="params" ofType="com.zsc.edu.gateway.modules.iot.tsl.entity.Param" autoMapping="true"
                    columnPrefix="param_">
            <id column="id" property="id"/>
            <result column="data_type" property="dataType"/>
            <result column="uint" property="uint"/>
            <result column="type" property="type"/>
            <result column="identifier" property="identifier"/>
            <result column="name" property="name"/>
            <result column="remark" property="remark"/>
        </collection>
    </resultMap>

    <select id="findById" resultMap="BaseResultMap">
        select d.*,
               p.*,
               ip.id         as param_id,
               ip.data_type  as param_data_type,
               ip.uint       as param_uint,
               ip.type       as param_type,
               ip.identifier as param_identifier,
               ip.name       as param_name,
               ip.remark     as param_remark
        from iot_device d
        left join iot_product p on d.product_id = p.id
        left join iot_param ip on p.id = ip.foreign_id and ip.foreign_type = 3
        where d.id = #{id}
    </select>

    <select id="selectList" resultType="com.zsc.edu.gateway.modules.iot.device.entity.Device">
        SELECT d.*, p.name AS productName
        FROM iot_device d
                 LEFT JOIN iot_product p ON d.product_id = p.id
    </select>
</mapper>