<?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="io.dataease.base.mapper.ext.ExtDatasetTableUnionMapper">
    <resultMap id="BaseResultMap" type="io.dataease.dto.dataset.DataSetTableUnionDTO">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="source_table_id" jdbcType="VARCHAR" property="sourceTableId"/>
        <result column="source_table_field_id" jdbcType="VARCHAR" property="sourceTableFieldId"/>
        <result column="source_union_relation" jdbcType="VARCHAR" property="sourceUnionRelation"/>
        <result column="target_table_id" jdbcType="VARCHAR" property="targetTableId"/>
        <result column="target_table_field_id" jdbcType="VARCHAR" property="targetTableFieldId"/>
        <result column="target_union_relation" jdbcType="VARCHAR" property="targetUnionRelation"/>
        <result column="create_by" jdbcType="VARCHAR" property="createBy"/>
        <result column="create_time" jdbcType="BIGINT" property="createTime"/>

        <result column="sourceTableName" jdbcType="VARCHAR" property="sourceTableName"/>
        <result column="sourceTableFieldName" jdbcType="VARCHAR" property="sourceTableFieldName"/>
        <result column="targetTableName" jdbcType="VARCHAR" property="targetTableName"/>
        <result column="targetTableFieldName" jdbcType="VARCHAR" property="targetTableFieldName"/>
    </resultMap>

    <select id="selectBySourceTableId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select dtu.*,dt.name as sourceTableName,dtf.name as sourceTableFieldName,dt1.name as targetTableName,dtf1.name as targetTableFieldName
        from dataset_table_union dtu
        left join dataset_table dt on dt.id = dtu.source_table_id
        left join dataset_table_field dtf on dtf.id = dtu.source_table_field_id
        left join dataset_table dt1 on dt1.id = dtu.target_table_id
        left join dataset_table_field dtf1 on dtf1.id = dtu.target_table_field_id
        where dtu.source_table_id = #{tableId,jdbcType=VARCHAR}
        order by dtu.create_time
    </select>

    <select id="selectByTargetTableId" resultMap="BaseResultMap" parameterType="java.lang.String">
        select dtu.*,dt.name as sourceTableName,dtf.name as sourceTableFieldName,dt1.name as targetTableName,dtf1.name as targetTableFieldName
        from dataset_table_union dtu
        left join dataset_table dt on dt.id = dtu.source_table_id
        left join dataset_table_field dtf on dtf.id = dtu.source_table_field_id
        left join dataset_table dt1 on dt1.id = dtu.target_table_id
        left join dataset_table_field dtf1 on dtf1.id = dtu.target_table_field_id
        where dtu.target_table_id = #{tableId,jdbcType=VARCHAR}
        order by dtu.create_time
    </select>
</mapper>