<?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.tsl.repo.ServeRepository">
    <resultMap id="paramMap" type="com.zsc.edu.gateway.modules.iot.tsl.entity.Param">
        <id column="param_id" property="id"/>
        <result column="param_data_type" property="dataType"/>
        <result column="param_uint" property="uint"/>
        <result column="param_type" property="type"/>
        <result column="param_identifier" property="identifier"/>
        <result column="param_foreign_type" property="foreignType"/>
        <result column="param_foreign_id" property="foreignId"/>
        <result column="param_name" property="name"/>
        <result column="param_remark" property="remark"/>
    </resultMap>

    <resultMap id="ServeMap" type="com.zsc.edu.gateway.modules.iot.tsl.entity.Serve">
        <id column="id" property="id"/>
        <result column="product_id" property="productId"/>
        <result column="identifier" property="identifier"/>
        <result column="name" property="name"/>
        <result column="remark" property="remark"/>
        <collection property="inputs" resultMap="paramMap" autoMapping="true" columnPrefix="input_">
        </collection>
        <collection property="outputs" resultMap="paramMap" autoMapping="true" columnPrefix="output_">
        </collection>
    </resultMap>

    <select id="selectById" resultMap="ServeMap">
        SELECT s.id,
               s.product_id,
               s.identifier,
               s.name,
               s.remark,
               input_params.id          as input_param_id,
               input_params.data_type   as input_param_data_type,
               input_params.uint        as input_param_uint,
               input_params.type        as input_param_type,
               input_params.identifier  as input_param_identifier,
               input_params.foreign_type  as input_param_foreign_type,
               input_params.foreign_id    as input_param_foreign_id,
               input_params.name        as input_param_name,
               input_params.remark      as input_param_remark,
               output_params.id         as output_param_id,
               output_params.data_type  as output_param_data_type,
               output_params.uint       as output_param_uint,
               output_params.type       as output_param_type,
               output_params.identifier as output_param_identifier,
               output_params.foreign_type as output_param_foreign_type,
               output_params.foreign_id   as output_param_foreign_id,
               output_params.name       as output_param_name,
               output_params.remark     as output_param_remark
        FROM iot_serve s
                 LEFT JOIN (SELECT ip.id,
                                   ip.data_type,
                                   ip.uint,
                                   ip.type,
                                   ip.identifier,
                                   ip.name,
                                   ip.remark,
                                   ip.foreign_id,
                                   ip.foreign_type
                            FROM iot_param ip
                            WHERE ip.foreign_type = 1
                              and ip.type = 1) input_params ON s.id = input_params.foreign_id
                 LEFT JOIN (SELECT ip.id,
                                   ip.data_type,
                                   ip.uint,
                                   ip.type,
                                   ip.identifier,
                                   ip.name,
                                   ip.remark,
                                   ip.foreign_id,
                                   ip.foreign_type
                            FROM iot_param ip
                            WHERE ip.foreign_type = 1
                              and ip.type = 2) output_params ON s.id = output_params.foreign_id
        WHERE s.id = #{id}
    </select>
</mapper>
