<?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.gisbi.visualization.dao.ext.mapper.ExtVisualizationLinkageMapper">

    <resultMap id="TableFieldMap" type="io.gisbi.extensions.datasource.dto.DatasetTableFieldDTO">
        <result column="id" jdbcType="BIGINT" property="id"/>
        <result column="table_id" jdbcType="BIGINT" property="datasetTableId"/>
        <result column="origin_name" jdbcType="VARCHAR" property="originName"/>
        <result column="name" jdbcType="VARCHAR" property="name"/>
        <result column="de_type" jdbcType="INTEGER" property="deType"/>
    </resultMap>

    <resultMap id="LinkageGatherMap" type="io.gisbi.api.visualization.dto.VisualizationLinkageDTO">
        <result column="target_view_id" jdbcType="BIGINT" property="targetViewId"/>
        <result column="target_view_type" jdbcType="VARCHAR" property="targetViewType"/>
        <result column="table_id" jdbcType="BIGINT" property="tableId"/>
        <result column="targetViewName" jdbcType="VARCHAR" property="targetViewName"/>
        <result column="sourceViewId" jdbcType="BIGINT" property="sourceViewId"/>
        <result column="linkageActive" property="linkageActive"/>
        <collection property="targetViewFields" ofType="io.gisbi.extensions.datasource.dto.DatasetTableFieldDTO" column="table_id"
                    select="queryTableField">
        </collection>
        <collection property="linkageFields" ofType="io.gisbi.api.visualization.vo.VisualizationLinkageFieldVO">
            <result column="source_field" jdbcType="BIGINT" property="sourceField"/>
            <result column="target_field" jdbcType="BIGINT" property="targetField"/>
        </collection>
    </resultMap>


    <resultMap id="AllLinkageMap" type="io.gisbi.api.visualization.dto.LinkageInfoDTO">
        <result column="sourceInfo" jdbcType="VARCHAR" property="sourceInfo"/>
        <collection property="targetInfoList" ofType="String">
            <result column="targetInfo" jdbcType="VARCHAR"/>
        </collection>
    </resultMap>

    <select id="getViewLinkageGather" resultMap="LinkageGatherMap">
        SELECT
        core_chart_view.`title` as targetViewName,
        core_chart_view.id as target_view_id,
        core_chart_view.type as target_view_type,
        core_chart_view.table_id,
        visualization_linkage.source_view_id as sourceViewId,
        (case when visualization_linkage.target_view_id is null then 0 else visualization_linkage.linkage_active end) as linkageActive,
        visualization_linkage_field.source_field,
        visualization_linkage_field.target_field
        FROM
        core_chart_view
        LEFT JOIN visualization_linkage ON core_chart_view.id = visualization_linkage.target_view_id
        AND visualization_linkage.dv_id = #{dvId}
        AND visualization_linkage.source_view_id = #{sourceViewId}
        LEFT JOIN visualization_linkage_field ON visualization_linkage.id = visualization_linkage_field.linkage_id
        where core_chart_view.type != 'VQuery' and core_chart_view.id in
        <foreach collection="targetViewIds" item="targetViewId" index="index" open="(" close=")" separator=",">
            #{targetViewId}
        </foreach>
    </select>

    <select id="queryTableField" resultMap="TableFieldMap">
       select
       core_dataset_table_field.id,
       core_dataset_table_field.dataset_table_id,
       core_dataset_table_field.origin_name,
       core_dataset_table_field.`name`,
       core_dataset_table_field.de_type
    from core_dataset_table_field where dataset_group_id = #{table_id}
    </select>

    <select id="queryTableFieldWithViewId" resultMap="TableFieldMap">
        select
       core_dataset_table_field.id,
       core_dataset_table_field.dataset_table_id,
       core_dataset_table_field.origin_name,
       core_dataset_table_field.`name`,
       core_dataset_table_field.de_type
        from core_dataset_table_field
		left join core_chart_view on core_dataset_table_field.dataset_table_id= core_chart_view.table_id
		where core_chart_view.id = #{viewId}
    </select>

    <delete id="deleteViewLinkage">
        delete from visualization_linkage where   visualization_linkage.dv_id = #{dvId}
        AND visualization_linkage.source_view_id = #{sourceViewId}
    </delete>


    <delete id="deleteViewLinkageField">
        DELETE FROM visualization_linkage
        WHERE id IN (
            SELECT id FROM (
                SELECT pvl.id
                FROM visualization_linkage pvl
                JOIN visualization_linkage_field pvlf
                    ON pvl.id = pvlf.linkage_id
                WHERE pvl.source_view_id = #{sourceViewId}
                  AND pvl.dv_id = #{dvId}
            ) AS temp_table
        )
    </delete>

    <select id="getPanelAllLinkageInfo" resultMap="AllLinkageMap">
        SELECT DISTINCT
            CONCAT( visualization_linkage.source_view_id, '#', visualization_linkage_field.source_field ) AS sourceInfo,
                CONCAT( visualization_linkage.target_view_id, '#', visualization_linkage_field.target_field ) AS targetInfo
        FROM
            visualization_linkage
                LEFT JOIN core_chart_view ON visualization_linkage.source_view_id = core_chart_view.id
                LEFT JOIN visualization_linkage_field ON visualization_linkage.id = visualization_linkage_field.linkage_id
        WHERE
            visualization_linkage.dv_id = #{dvId}
          AND core_chart_view.linkage_active = 1
          AND visualization_linkage.linkage_active = 1
          AND visualization_linkage_field.id IS NOT NULL
    </select>

    <insert id="copyViewLinkage">
        INSERT INTO visualization_linkage (
                id,
                dv_id,
                source_view_id,
                target_view_id,
                update_time,
                update_people,
                linkage_active,
                ext1,
                ext2,
                copy_from,
                copy_id
            ) SELECT
            UUID() AS id,
            pv_source_copy.t_dv_id AS dv_id,
            pv_source_copy.t_core_chart_view_id AS source_view_id,
            pv_target_copy.t_core_chart_view_id target_view_id,
            update_time,
            update_people,
            linkage_active,
            ext1,
            ext2,
            id as copy_from,
            #{copyId} as copy_id
            FROM
                visualization_linkage
                INNER JOIN (
                SELECT
                    pvs.dv_id AS s_dv_id,
                    pvs.core_chart_view_id AS s_core_chart_view_id,
                    pvt.dv_id AS t_dv_id,
                    pvt.core_chart_view_id AS t_core_chart_view_id
                FROM
                    panel_view pvt
                    inner JOIN panel_view pvs ON pvt.copy_from = pvs.id
                WHERE
                    pvt.copy_id = #{copyId}
                ) pv_source_copy ON visualization_linkage.dv_id = pv_source_copy.s_dv_id
                AND visualization_linkage.source_view_id = pv_source_copy.s_core_chart_view_id
                INNER JOIN (
                SELECT
                    pvs.dv_id AS s_dv_id,
                    pvs.core_chart_view_id AS s_core_chart_view_id,
                    pvt.dv_id AS t_dv_id,
                    pvt.core_chart_view_id AS t_core_chart_view_id
                FROM
                    panel_view pvt
                    inner JOIN panel_view pvs ON pvt.copy_from = pvs.id
                WHERE
                    pvt.copy_id = #{copyId}
                ) pv_target_copy ON visualization_linkage.dv_id = pv_target_copy.s_dv_id
                AND visualization_linkage.target_view_id = pv_target_copy.s_core_chart_view_id

    </insert>

    <insert id ="copyViewLinkageField">
        INSERT INTO visualization_linkage_field (
            id,
            linkage_id,
            source_field,
            target_field,
            update_time,
            copy_from,
            copy_id
        ) SELECT
        uuid() AS id,
        pvlf_copy.t_id,
        source_field,
        target_field,
        update_time,
        id AS copy_from,
        #{copyId} AS copy_id
        FROM
            visualization_linkage_field
            INNER JOIN (
            SELECT
                id AS t_id,
                copy_from AS s_id
            FROM
                visualization_linkage
            WHERE
                copy_id = #{copyId}
            ) pvlf_copy ON visualization_linkage_field.linkage_id = pvlf_copy.s_id

    </insert>


    <select id="findLinkageWithDvId" resultType="io.gisbi.visualization.dao.auto.entity.VisualizationLinkage">
        select * from visualization_linkage  where dv_id =#{dvId};
    </select>

    <select id="findLinkageFieldWithDvId" resultType="io.gisbi.visualization.dao.auto.entity.VisualizationLinkageField">
        SELECT
            *
        FROM
            visualization_linkage_field
        WHERE
                linkage_id IN ( SELECT id FROM visualization_linkage WHERE dv_id = #{dvId} );
    </select>
</mapper>
