<?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.ExtVisualizationLinkJumpMapper">
    <resultMap id="LinkJumpBaseResultMap" type="io.gisbi.api.visualization.vo.VisualizationLinkJumpVO">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="source_dv_id" jdbcType="BIGINT" property="sourceDvId"/>
        <result column="source_view_id" jdbcType="BIGINT" property="sourceViewId"/>
        <result column="link_jump_info" jdbcType="VARCHAR" property="linkJumpInfo"/>
        <result column="checked" jdbcType="BIT" property="checked"/>
        <result column="copy_from" jdbcType="BIGINT" property="copyFrom"/>
        <result column="copy_id" jdbcType="BIGINT" property="copyId"/>
    </resultMap>

    <resultMap id="LinkJumpInfoBaseResultMap" type="io.gisbi.api.visualization.vo.VisualizationLinkJumpInfoVO">
        <id column="id" jdbcType="VARCHAR" property="id"/>
        <result column="link_jump_id" jdbcType="BIGINT" property="linkJumpId"/>
        <result column="link_type" jdbcType="VARCHAR" property="linkType"/>
        <result column="jump_type" jdbcType="VARCHAR" property="jumpType"/>
        <result column="window_size" jdbcType="VARCHAR" property="windowSize"/>
        <result column="target_dv_id" jdbcType="BIGINT" property="targetDvId"/>
        <result column="source_field_id" jdbcType="BIGINT" property="sourceFieldId"/>
        <result column="content" jdbcType="VARCHAR" property="content"/>
        <result column="checked" jdbcType="BIT" property="checked"/>
        <result column="attach_params" jdbcType="BIT" property="attachParams"/>
        <result column="copy_from" jdbcType="BIGINT" property="copyFrom"/>
        <result column="copy_id" jdbcType="BIGINT" property="copyId"/>
    </resultMap>


    <resultMap id="BaseResultMapDTO" type="io.gisbi.api.visualization.dto.VisualizationLinkJumpDTO"
               extends="LinkJumpBaseResultMap">
        <collection property="linkJumpInfoArray" ofType="io.gisbi.api.visualization.dto.VisualizationLinkJumpInfoDTO"
                    column="{id=id,source_view_id=source_view_id,uid=queryUid, isDesktop=isDesktop}"
                    select="getLinkJumpInfo">
        </collection>
    </resultMap>

        <resultMap id="BaseResultMapDTOSnapshot" type="io.gisbi.api.visualization.dto.VisualizationLinkJumpDTO"
                   extends="LinkJumpBaseResultMap">
            <collection property="linkJumpInfoArray" ofType="io.gisbi.api.visualization.dto.VisualizationLinkJumpInfoDTO"
                        column="{id=id,source_view_id=source_view_id,uid=queryUid, isDesktop=isDesktop}"
                        select="getLinkJumpInfoSnapshot">
            </collection>
        </resultMap>

    <resultMap id="LinkJumpInfoMap" type="io.gisbi.api.visualization.dto.VisualizationLinkJumpInfoDTO"
               extends="LinkJumpInfoBaseResultMap">
        <result column="target_dv_type" jdbcType="VARCHAR" property="targetDvType"/>
        <result column="source_field_id" jdbcType="BIGINT" property="sourceFieldId"/>
        <result column="source_de_type" jdbcType="INTEGER" property="sourceDeType"/>
        <result column="source_field_name" jdbcType="VARCHAR" property="sourceFieldName"/>
        <result column="publicJumpId" jdbcType="VARCHAR" property="publicJumpId"/>
        <collection property="targetViewInfoList"
                    ofType="io.gisbi.api.visualization.vo.VisualizationLinkJumpTargetViewInfoVO">
            <result column="target_id" jdbcType="BIGINT" property="targetId"/>
            <result column="target_view_id" jdbcType="BIGINT" property="targetViewId"/>
            <result column="target_field_id" jdbcType="BIGINT" property="targetFieldId"/>
            <result column="source_field_active_id" jdbcType="VARCHAR" property="sourceFieldActiveId"/>
            <result column="target_type" jdbcType="VARCHAR" property="targetType"/>
            <result column="outer_params_name" jdbcType="VARCHAR" property="outerParamsName"/>
        </collection>
    </resultMap>
    <resultMap id="ViewTableFieldDetailsMap" type="io.gisbi.api.visualization.vo.VisualizationViewTableVO">
        <result column="id" jdbcType="BIGINT" property="id"/>
        <result column="title" jdbcType="VARCHAR" property="title"/>
        <result column="type" jdbcType="VARCHAR" property="type"/>
        <result column="dv_id" jdbcType="BIGINT" property="dvId"/>
        <collection property="tableFields"
                    ofType="io.gisbi.extensions.datasource.dto.DatasetTableFieldDTO">
            <result column="field_id" jdbcType="BIGINT" property="id"/>
            <result column="origin_name" jdbcType="VARCHAR" property="originName"/>
            <result column="field_name" jdbcType="VARCHAR" property="name"/>
            <result column="field_type" jdbcType="VARCHAR" property="type"/>
            <result column="de_type" jdbcType="VARCHAR" property="deType"/>
        </collection>
    </resultMap>

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

    <select id="getLinkJumpInfoSnapshot" resultMap="LinkJumpInfoMap">
            SELECT
                cdtf.id AS source_field_id,
                cdtf.de_type AS source_de_type,
                cdtf.NAME AS source_field_name,
                vlji.id,
                vlji.link_jump_id,
                vlji.link_type,
                vlji.jump_type,
                vlji.window_size,
                vlji.target_dv_id,
                dvi.type as target_dv_type,
                vlji.content,
                <if test="!isDesktop">
                xpack_share.uuid AS publicJumpId,
                </if>
                ifnull( vlji.checked, 0 ) AS checked,
                ifnull( vlji.attach_params, 0 ) AS attach_params,
                vljtvi.target_id,
                vljtvi.target_view_id,
                vljtvi.target_field_id,
                vljtvi.target_type,
                vljtvi.source_field_active_id,
                vopi.param_name as outer_params_name
            FROM
                snapshot_core_chart_view ccv
                    LEFT JOIN core_dataset_table_field cdtf ON ccv.table_id = cdtf.dataset_group_id
                    LEFT JOIN snapshot_visualization_link_jump vlj ON ccv.id = vlj.source_view_id
                    AND vlj.id = #{id}
                    LEFT JOIN snapshot_visualization_link_jump_info vlji ON vlj.id = vlji.link_jump_id
                    AND cdtf.id = vlji.source_field_id
                    LEFT JOIN data_visualization_info dvi ON vlji.target_dv_id = dvi.id
                    LEFT JOIN snapshot_visualization_link_jump_target_view_info vljtvi ON vlji.id = vljtvi.link_jump_info_id
                    <if test="!isDesktop">
                    LEFT JOIN xpack_share ON xpack_share.creator = #{uid}
                    AND vlji.target_dv_id = xpack_share.resource_id
                    </if>
                    left join snapshot_visualization_outer_params_info vopi on vopi.params_info_id = vljtvi.target_view_id
            WHERE
                ccv.id = #{source_view_id}
              AND ccv.type != 'VQuery'
              <if test="!isDesktop">
               ORDER BY
                          CONVERT (
                          cdtf.NAME USING gbk)
              </if>
              <if test="isDesktop">
              ORDER BY cdtf.name;
             </if>
        </select>

    <select id="getLinkJumpInfo" resultMap="LinkJumpInfoMap">
        SELECT
            cdtf.id AS source_field_id,
            cdtf.de_type AS source_de_type,
            cdtf.NAME AS source_field_name,
            vlji.id,
            vlji.link_jump_id,
            vlji.link_type,
            vlji.jump_type,
            vlji.window_size,
            vlji.target_dv_id,
            dvi.type as target_dv_type,
            vlji.content,
            <if test="!isDesktop">
            xpack_share.uuid AS publicJumpId,
            </if>
            ifnull( vlji.checked, 0 ) AS checked,
            ifnull( vlji.attach_params, 0 ) AS attach_params,
            vljtvi.target_id,
            vljtvi.target_view_id,
            vljtvi.target_field_id,
            vljtvi.target_type,
            vljtvi.source_field_active_id,
            vopi.param_name as outer_params_name
        FROM
            core_chart_view ccv
                LEFT JOIN core_dataset_table_field cdtf ON ccv.table_id = cdtf.dataset_group_id
                LEFT JOIN visualization_link_jump vlj ON ccv.id = vlj.source_view_id
                AND vlj.id = #{id}
                LEFT JOIN visualization_link_jump_info vlji ON vlj.id = vlji.link_jump_id
                AND cdtf.id = vlji.source_field_id
                LEFT JOIN data_visualization_info dvi ON vlji.target_dv_id = dvi.id
                LEFT JOIN visualization_link_jump_target_view_info vljtvi ON vlji.id = vljtvi.link_jump_info_id
                <if test="!isDesktop">
                LEFT JOIN xpack_share ON xpack_share.creator = #{uid}
                AND vlji.target_dv_id = xpack_share.resource_id
                </if>
                left join visualization_outer_params_info vopi on vopi.params_info_id = vljtvi.target_view_id
        WHERE
            ccv.id = #{source_view_id}
          AND ccv.type != 'VQuery'
          <if test="!isDesktop">
           ORDER BY
                      CONVERT (
                      cdtf.NAME USING gbk)
          </if>
          <if test="isDesktop">
          ORDER BY cdtf.name;
         </if>
    </select>

    <select id="queryWithDvId" resultMap="BaseResultMapDTO">
        SELECT ccv.id                     AS source_view_id,
               #{uid}                                 as queryUid,
               #{isDesktop}                                 as isDesktop,
               vlj.id,
               #{dvId} as source_dv_id, vlj.link_jump_info,
               ifnull(ccv.jump_active, 0) AS checked
        FROM core_chart_view ccv
                 LEFT JOIN visualization_link_jump vlj ON ccv.id = vlj.source_view_id
        WHERE vlj.source_dv_id = #{dvId}
          and ccv.jump_active = 1
    </select>

    <select id="queryWithDvIdSnapshot" resultMap="BaseResultMapDTOSnapshot">
        SELECT ccv.id                     AS source_view_id,
               #{uid}                                 as queryUid,
               #{isDesktop}                                 as isDesktop,
               vlj.id,
               #{dvId} as source_dv_id, vlj.link_jump_info,
               ifnull(ccv.jump_active, 0) AS checked
        FROM snapshot_core_chart_view ccv
                 LEFT JOIN snapshot_visualization_link_jump vlj ON ccv.id = vlj.source_view_id
        WHERE vlj.source_dv_id = #{dvId}
          and ccv.jump_active = 1
    </select>

    <select id="getViewTableDetails" resultMap="ViewTableFieldDetailsMap">
        SELECT
        	core_chart_view.id,
        	core_chart_view.title,
        	core_chart_view.type,
        	core_chart_view.scene_id AS dv_id,
        	core_dataset_table_field.id AS field_id,
        	core_dataset_table_field.origin_name,
        	core_dataset_table_field.`name` AS field_name,
        	core_dataset_table_field.type AS field_type,
        	core_dataset_table_field.de_type
        FROM
        	core_chart_view
        	LEFT JOIN core_dataset_table_field ON core_chart_view.table_id = core_dataset_table_field.dataset_group_id
        	INNER JOIN data_visualization_info dvi ON core_chart_view.scene_id = dvi.id
        WHERE
        	core_chart_view.scene_id = #{dvId}
        	AND core_chart_view.type != 'VQuery'
        	AND core_chart_view.table_id IS NOT NULL
        	AND dvi.id = #{dvId}
        	AND LOCATE(
        	core_chart_view.id,
        	dvi.component_data)
    </select>

    <select id="queryWithViewId" resultMap="BaseResultMapDTOSnapshot">
        SELECT ccv.id                         AS source_view_id,
               #{uid}                                     as queryUid,
               #{isDesktop}                                 as isDesktop,
               vlj.id,
               #{dvId} as source_dv_id, vlj.link_jump_info,
               ifnull(vlj.checked, 0) AS checked
        FROM snapshot_core_chart_view ccv
                 LEFT JOIN snapshot_visualization_link_jump vlj ON ccv.id = vlj.source_view_id
            AND vlj.source_dv_id = #{dvId}
        WHERE ccv.id = #{viewId}
    </select>

        <select id="queryOutParamsTargetWithDvId" resultType="io.gisbi.api.visualization.vo.VisualizationOutParamsJumpVO">
            SELECT
            	vopi.params_info_id as id,
            	vopi.param_name as name,
            	vopi.param_name as title,
            	'outerParams' as type
            FROM
            	visualization_outer_params_info vopi
            	LEFT JOIN visualization_outer_params vop ON vopi.params_id = vop.params_id
            WHERE
            	vop.visualization_id = #{dvId}
        </select>

    <delete id="deleteJumpTargetViewInfoSnapshot">
        DELETE FROM snapshot_visualization_link_jump_target_view_info
        WHERE link_jump_info_id IN (
            SELECT lji.id
            FROM snapshot_visualization_link_jump_info lji
            JOIN snapshot_visualization_link_jump lj ON lji.link_jump_id = lj.id
            WHERE lj.source_dv_id = #{dvId}
              AND lj.source_view_id = #{viewId}
        );
    </delete>

    <delete id="deleteJumpInfoSnapshot">
        DELETE FROM snapshot_visualization_link_jump_info
        WHERE link_jump_id IN (
            SELECT lj.id
            FROM snapshot_visualization_link_jump lj
            WHERE lj.source_dv_id = #{dvId}
              AND lj.source_view_id = #{viewId}
        );
    </delete>

    <delete id="deleteJumpSnapshot">
        DELETE FROM snapshot_visualization_link_jump
        WHERE source_dv_id = #{dvId}
          AND source_view_id = #{viewId}
    </delete>


    <delete id="deleteJumpTargetViewInfoWithVisualization">
        DELETE FROM visualization_link_jump_target_view_info
        WHERE link_jump_info_id IN (
            SELECT lji.id
            FROM visualization_link_jump_info lji
            JOIN visualization_link_jump lj ON lji.link_jump_id = lj.id
            WHERE lj.source_dv_id = #{dvId}
        )
    </delete>

    <delete id="deleteJumpTargetViewInfoWithVisualizationSnapshot">
        DELETE FROM snapshot_visualization_link_jump_target_view_info
        WHERE link_jump_info_id IN (
            SELECT lji.id
            FROM snapshot_visualization_link_jump_info lji
            JOIN snapshot_visualization_link_jump lj ON lji.link_jump_id = lj.id
            WHERE lj.source_dv_id = #{dvId}
        )
    </delete>

    <delete id="deleteJumpInfoWithVisualization">
        DELETE FROM visualization_link_jump_info
        WHERE link_jump_id IN (
            SELECT lj.id
            FROM visualization_link_jump lj
            WHERE lj.source_dv_id = #{dvId}
        )
    </delete>

    <delete id="deleteJumpInfoWithVisualizationSnapshot">
        DELETE FROM snapshot_visualization_link_jump_info
        WHERE link_jump_id IN (
            SELECT lj.id
            FROM snapshot_visualization_link_jump lj
            WHERE lj.source_dv_id = #{dvId}
        )
    </delete>

   <delete id="deleteJumpWithVisualization">
       DELETE FROM visualization_link_jump
       WHERE source_dv_id = #{dvId}
   </delete>

      <delete id="deleteJumpWithVisualizationSnapshot">
          DELETE FROM snapshot_visualization_link_jump
          WHERE source_dv_id = #{dvId}
      </delete>

    <select id="getTargetVisualizationJumpInfo" resultMap="AllJumpMap">
        SELECT DISTINCT
        concat( lj.source_view_id, '#', jtvi.source_field_active_id ) AS sourceInfo,
        concat( jtvi.target_view_id, '#', jtvi.target_field_id ) AS targetInfo
        FROM
        visualization_link_jump_target_view_info jtvi
        LEFT JOIN visualization_link_jump_info lji ON jtvi.link_jump_info_id = lji.id
        LEFT JOIN visualization_link_jump lj ON lji.link_jump_id = lj.id
        WHERE
        lji.checked = 1
        and lj.source_dv_id = #{request.sourceDvId}
        AND lj.source_view_id = #{request.sourceViewId}
        AND lji.target_dv_id = #{request.targetDvId}
        <if test="request.sourceFieldId != null">
            AND lji.source_field_id = #{request.sourceFieldId}
        </if>
    </select>

    <select id="getTargetVisualizationJumpInfoSnapshot" resultMap="AllJumpMap">
            SELECT DISTINCT
            concat( lj.source_view_id, '#', jtvi.source_field_active_id ) AS sourceInfo,
            concat( jtvi.target_view_id, '#', jtvi.target_field_id ) AS targetInfo
            FROM
            snapshot_visualization_link_jump_target_view_info jtvi
            LEFT JOIN snapshot_visualization_link_jump_info lji ON jtvi.link_jump_info_id = lji.id
            LEFT JOIN snapshot_visualization_link_jump lj ON lji.link_jump_id = lj.id
            WHERE
            lji.checked = 1
            and lj.source_dv_id = #{request.sourceDvId}
            AND lj.source_view_id = #{request.sourceViewId}
            AND lji.target_dv_id = #{request.targetDvId}
            <if test="request.sourceFieldId != null">
                AND lji.source_field_id = #{request.sourceFieldId}
            </if>
        </select>

    <insert id="copyLinkJump">
        insert into visualization_link_jump (id, source_dv_id, source_view_id, link_jump_info, `checked`, copy_from,
                                             copy_id)
        select UUID()                                       as id,
               visualization_view_copy.t_dv_id              as source_dv_id,
               visualization_view_copy.t_core_chart_view_id as source_view_id,
               visualization_link_jump.link_jump_info,
               visualization_link_jump.checked,
               visualization_link_jump.id                   as copy_from,
               #{copyId}                                    as copy_id
        from visualization_link_jump
                 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 visualization_view pvt
                       inner JOIN visualization_view pvs ON pvt.copy_from = pvs.id
              WHERE pvt.copy_id = #{copyId}) visualization_view_copy
             on visualization_link_jump.source_dv_id = visualization_view_copy.s_dv_id
                 and visualization_link_jump.source_view_id = visualization_view_copy.s_core_chart_view_id
    </insert>

    <insert id="copyLinkJumpInfo">
        INSERT INTO visualization_link_jump_info (id,
                                                  link_jump_id,
                                                  link_type,
                                                  jump_type,
                                                  window_size,
                                                  target_dv_id,
                                                  source_field_id,
                                                  content,
                                                  `checked`,
                                                  `attach_params`,
                                                  copy_from,
                                                  copy_id)
        SELECT uuid() AS id,
               plj_copy.t_id,
               link_type,
               jump_type,
               window_size,
               target_dv_id,
               source_field_id,
               content,
               `checked`,
               `attach_params`,
               id     AS copy_from,
               #{copyId}
        FROM visualization_link_jump_info
                 inner JOIN (SELECT id        AS t_id,
                                    copy_from AS s_id
                             FROM visualization_link_jump
                             WHERE copy_id = #{copyId}) plj_copy
                            ON visualization_link_jump_info.link_jump_id = plj_copy.s_id
    </insert>

    <insert id="copyLinkJumpTarget">
        INSERT INTO visualization_link_jump_target_view_info (target_id,
                                                              link_jump_info_id,
                                                              target_view_id,
                                                              target_field_id,
                                                              copy_from,
                                                              copy_id)
        SELECT uuid()         AS id,
               plji_copy.t_id AS link_jump_info_id,
               target_view_id,
               target_field_id,
               copy_from,
               #{copyId}      AS copy_id
        FROM visualization_link_jump_target_view_info
                 inner JOIN (SELECT id        AS t_id,
                                    copy_from AS s_id
                             FROM visualization_link_jump_info
                             WHERE copy_id = #{copyId}) plji_copy
                            ON visualization_link_jump_target_view_info.link_jump_info_id = plji_copy.s_id
    </insert>

    <select id="findLinkJumpWithDvId" resultType="io.gisbi.api.visualization.vo.VisualizationLinkJumpVO">
        SELECT *
        FROM visualization_link_jump
        WHERE source_dv_id = #{dvId}
    </select>

    <select id="findLinkJumpInfoWithDvId" resultType="io.gisbi.api.visualization.vo.VisualizationLinkJumpInfoVO">
        SELECT *
        FROM visualization_link_jump_info
        WHERE link_type = 'outer'
          AND link_jump_id IN (SELECT id
                               FROM visualization_link_jump
                               WHERE source_dv_id = #{dvId})
    </select>
</mapper>
