<?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.ExtDataVisualizationMapper">
    <insert id="viewCopyWithDv">
        INSERT INTO
        <choose>
            <when test="resourceTable == 'snapshot'">
                  `snapshot_core_chart_view`
            </when>
            <otherwise>
                  `core_chart_view`
            </otherwise>
        </choose>
                                       (`id`,
                                       `title`,
                                       `scene_id`,
                                       `table_id`,
                                       `type`,
                                       `render`,
                                       `result_count`,
                                       `result_mode`,
                                       `x_axis`,
                                       `x_axis_ext`,
                                       `y_axis`,
                                       `y_axis_ext`,
                                       `ext_stack`,
                                       `ext_bubble`,
                                       `ext_label`,
                                       `ext_tooltip`,
                                       `custom_attr`,
                                       `custom_attr_mobile`,
                                       `custom_style`,
                                       `custom_style_mobile`,
                                       `custom_filter`,
                                       `drill_fields`,
                                       `senior`,
                                       `create_by`,
                                       `create_time`,
                                       `update_time`,
                                       `snapshot`,
                                       `style_priority`,
                                       `chart_type`,
                                       `is_plugin`,
                                       `data_from`,
                                       `view_fields`,
                                       `refresh_view_enable`,
                                       `refresh_unit`,
                                       `refresh_time`,
                                       `linkage_active`,
                                       `jump_active`,
                                       `copy_from`,
                                       `copy_id`,
                                       `flow_map_start_name`,
                                       `flow_map_end_name`,
                                       `ext_color`)
        SELECT ccv.`id` + #{copyId} as id,
               `title`,
               #{newDvId}                       as scene_id,
               `table_id`,
               `type`,
               `render`,
               `result_count`,
               `result_mode`,
               `x_axis`,
               `x_axis_ext`,
               `y_axis`,
               `y_axis_ext`,
               `ext_stack`,
               `ext_bubble`,
               `ext_label`,
               `ext_tooltip`,
               `custom_attr`,
               `custom_attr_mobile`,
               `custom_style`,
               `custom_style_mobile`,
               `custom_filter`,
               `drill_fields`,
               `senior`,
               `create_by`,
               `create_time`,
               `update_time`,
               `snapshot`,
               `style_priority`,
               `chart_type`,
               `is_plugin`,
               `data_from`,
               `view_fields`,
               `refresh_view_enable`,
               `refresh_unit`,
               `refresh_time`,
               `linkage_active`,
               `jump_active`,
               ccv.`id`             as copy_from,
               #{copyId}                        as copy_id,
               `flow_map_start_name`,
               `flow_map_end_name`,
               `ext_color`
        FROM
        <choose>
            <when test="resourceTable == 'snapshot'">
                  snapshot_core_chart_view ccv
            </when>
            <otherwise>
                  core_chart_view ccv
            </otherwise>
        </choose>
        WHERE ccv.scene_id = #{sourceDvId}
    </insert>
    <insert id="dvCopy">
        INSERT INTO `data_visualization_info` (`id`,
                                               `name`,
                                               `pid`,
                                               `org_id`,
                                               `level`,
                                               `node_type`,
                                               `type`,
                                               `canvas_style_data`,
                                               `component_data`,
                                               `mobile_layout`,
                                               `status`,
                                               `self_watermark_status`,
                                               `sort`,
                                               `create_time`,
                                               `create_by`,
                                               `update_time`,
                                               `update_by`,
                                               `remark`,
                                               `source`,
                                               `delete_flag`,
                                               `delete_time`,
                                               `delete_by`)
        select #{newDvId},
               `name`,
               `pid`,
               `org_id`,
               `level`,
               `node_type`,
               `type`,
               `canvas_style_data`,
               `component_data`,
               `mobile_layout`,
               `status`,
               `self_watermark_status`,
               `sort`,
               `create_time`,
               `create_by`,
               `update_time`,
               `update_by`,
               `remark`,
               `source`,
               `delete_flag`,
               `delete_time`,
               `delete_by`
        from data_visualization_info dvInfo
        where dvInfo.id = #{sourceDvId}
    </insert>
    <select id="findViewInfoByCopyId" resultType="io.gisbi.chart.dao.auto.entity.CoreChartView">
        select id, copy_from
        from core_chart_view
        where copy_id = #{copyId}
    </select>
    <select id="findDvInfo" resultType="io.gisbi.api.visualization.vo.DataVisualizationVO">
        SELECT `id`,
               `name`,
               `pid`,
               `org_id`,
               `level`,
               `node_type`,
               `type`,
               `canvas_style_data`,
               `component_data`,
               `mobile_layout`,
               `mobile_layout` as ext_flag,
               `status`,
               `self_watermark_status`,
               `sort`,
               `create_time`,
               `create_by`,
               `update_time`,
               `update_by`,
               `remark`,
               `source`,
               `delete_flag`,
               `delete_time`,
               `delete_by`,
               `version`,
               `content_id`,
               `check_version`
        FROM
           <choose>
               <when test="resourceTable == 'snapshot'">
                  snapshot_data_visualization_info dvi
               </when>
                <otherwise>
                  data_visualization_info dvi
                </otherwise>
           </choose>
        where dvi.delete_flag = 0
          and dvi.id = #{dvId}
          <if test="dvType">
            and dvi.type = #{dvType}
          </if>
    </select>

    <select id="findRecent" resultType="io.gisbi.visualization.dao.ext.po.VisualizationResourcePO">
        SELECT dvResource.id,
               dvResource.resource_id,
               dvResource.name,
               dvResource.ext_flag,
               dvResource.type,
               dvResource.creator,
               core_opt_recent.uid  AS last_editor,
               core_opt_recent.time AS last_edit_time,
               (
                   CASE

                       WHEN core_store.resource_id IS NULL THEN
                           0
                       ELSE 1
                       END
                   )                AS favorite
        FROM (SELECT core_dataset_group.id,
                     core_dataset_group.id        AS resource_id,
                     core_dataset_group.NAME,
                     0 as ext_flag,
                     'dataset'                    AS type,
                     core_dataset_group.create_by AS creator
              FROM core_dataset_group
              WHERE core_dataset_group.node_type = 'dataset'
              UNION ALL
              SELECT core_datasource.id,
                     core_datasource.id        AS resource_id,
                     core_datasource.NAME,
                     0 as ext_flag,
                     'datasource'              AS type,
                     core_datasource.create_by AS creator
              FROM core_datasource
              WHERE core_datasource.type != 'folder'
              UNION ALL
              SELECT
                  data_visualization_info.id,
                  data_visualization_info.id AS resource_id,
                  data_visualization_info.NAME,
                  data_visualization_info.`mobile_layout` as ext_flag,
                  (
                  CASE
                  data_visualization_info.type
                  WHEN 'dataV' THEN
                  'screen' ELSE 'panel'
                  END
                  ) AS type,
                  data_visualization_info.create_by AS creator
              FROM
                  data_visualization_info
              WHERE
                  data_visualization_info.delete_flag = 0
                AND node_type = 'leaf' and data_visualization_info.status != 0 ) dvResource
                 LEFT JOIN core_store ON dvResource.id = core_store.resource_id
            AND core_store.uid = #{uid}
                 INNER JOIN core_opt_recent ON dvResource.resource_id = core_opt_recent.resource_id
            AND core_opt_recent.uid = #{uid}
             <if test="keyword">
                AND LOWER(dvResource.name) LIKE LOWER(CONCAT('%', #{keyword}, '%'))
             </if>
             <if test="ew.type != null">
                AND dvResource.type = #{ew.type}
             </if>
             <if test="ew.info != null">
                AND NOT EXISTS(select 1 from per_busi_resource community where core_opt_recent.resource_id = community.id)
             </if>
             <if test="ew.isAsc">
                order by core_opt_recent.time asc
             </if>
    </select>

    <insert id="copyLinkJump">
        INSERT INTO snapshot_visualization_link_jump (`id`,
                                             `source_dv_id`,
                                             `source_view_id`,
                                             `link_jump_info`,
                                             `checked`,
                                             `copy_from`,
                                             `copy_id`)
        SELECT snapshot_visualization_link_jump.`id` + #{copyId} as id,
               dv_view_copy.t_dv_id                     as source_dv_id,
               dv_view_copy.t_chart_view_id             as source_view_id,
               snapshot_visualization_link_jump.`link_jump_info`,
               snapshot_visualization_link_jump.`checked`,
               snapshot_visualization_link_jump.`id`             as copy_from,
               #{copyId}                                as copy_id
        FROM snapshot_visualization_link_jump
                 INNER JOIN (SELECT pvs.scene_id AS s_dv_id,
                                    pvs.id       AS s_chart_view_id,
                                    pvt.scene_id AS t_dv_id,
                                    pvt.id       AS t_chart_view_id
                             FROM snapshot_core_chart_view pvt
                                      INNER JOIN snapshot_core_chart_view pvs ON pvt.copy_from = pvs.id
                             WHERE pvt.copy_id = #{copyId}) dv_view_copy
                            ON snapshot_visualization_link_jump.source_dv_id = dv_view_copy.s_dv_id
                                AND snapshot_visualization_link_jump.source_view_id = dv_view_copy.s_chart_view_id
    </insert>

    <insert id="copyLinkJumpInfo">
        INSERT INTO snapshot_visualization_link_jump_info (id,
                                                  link_jump_id,
                                                  link_type,
                                                  jump_type,
                                                  target_dv_id,
                                                  source_field_id,
                                                  content,
                                                  `checked`,
                                                  `attach_params`,
                                                  copy_from,
                                                  copy_id)
        SELECT snapshot_visualization_link_jump_info.`id` + #{copyId} as id,
               plj_copy.t_id                                 as link_jump_id,
               snapshot_visualization_link_jump_info.`link_type`,
               snapshot_visualization_link_jump_info.`jump_type`,
               snapshot_visualization_link_jump_info.`target_dv_id`,
               snapshot_visualization_link_jump_info.`source_field_id`,
               snapshot_visualization_link_jump_info.`content`,
               snapshot_visualization_link_jump_info.`checked`,
               snapshot_visualization_link_jump_info.`attach_params`,
               snapshot_visualization_link_jump_info.`id`             AS copy_from,
               #{copyId}                                     AS copy_id
        FROM snapshot_visualization_link_jump_info
                 INNER JOIN (SELECT id        AS t_id,
                                    copy_from AS s_id
                             FROM snapshot_visualization_link_jump
                             WHERE copy_id = #{copyId}) plj_copy
                            ON snapshot_visualization_link_jump_info.link_jump_id = plj_copy.s_id
    </insert>


    <insert id="copyLinkJumpTargetInfo">
        INSERT INTO snapshot_visualization_link_jump_target_view_info (`target_id`,
                                                              `link_jump_info_id`,
                                                              `source_field_active_id`,
                                                              `target_view_id`,
                                                              `target_field_id`,
                                                              `copy_from`,
                                                              `copy_id`)
        SELECT vljtvi.`target_id` + #{copyId} as target_id,
               plji_copy.t_id                                                   as link_jump_info_id,
               vljtvi.`source_field_active_id`,
               vljtvi.`target_view_id`,
               vljtvi.`target_field_id`,
               vljtvi.`target_id`             AS copy_from,
               #{copyId}                                                        AS copy_id
        FROM snapshot_visualization_link_jump_target_view_info vljtvi
                 INNER JOIN (SELECT id        AS t_id,
                                    copy_from AS s_id
                             FROM visualization_link_jump_info
                             WHERE copy_id = #{copyId}) plji_copy
                            ON vljtvi.link_jump_info_id = plji_copy.s_id
    </insert>

    <insert id="copyLinkage">
        INSERT INTO snapshot_visualization_linkage (`id`,
                                           dv_id,
                                           source_view_id,
                                           target_view_id,
                                           `update_time`,
                                           `update_people`,
                                           `linkage_active`,
                                           `ext1`,
                                           `ext2`,
                                           `copy_from`,
                                           `copy_id`)
        SELECT snapshot_visualization_linkage.`id` + #{copyId} as `id`,
               pv_source_copy.t_dv_id                 AS dv_id,
               pv_source_copy.t_chart_view_id         AS source_view_id,
               pv_target_copy.t_chart_view_id         AS target_view_id,
               `update_time`,
               `update_people`,
               `linkage_active`,
               `ext1`,
               `ext2`,
               snapshot_visualization_linkage.`id`             as copy_from,
               #{copyId}                              as copy_id
        FROM snapshot_visualization_linkage
                 INNER JOIN (SELECT pvs.scene_id AS s_dv_id,
                                    pvs.id       AS s_chart_view_id,
                                    pvt.scene_id AS t_dv_id,
                                    pvt.id       AS t_chart_view_id
                             FROM snapshot_core_chart_view pvt
                                      INNER JOIN snapshot_core_chart_view pvs ON pvt.copy_from = pvs.id
                             WHERE pvt.copy_id = #{copyId}) pv_source_copy
                            ON snapshot_visualization_linkage.dv_id = pv_source_copy.s_dv_id
                                AND snapshot_visualization_linkage.source_view_id = pv_source_copy.s_chart_view_id
                 INNER JOIN (SELECT pvs.scene_id AS s_dv_id,
                                    pvs.id       AS s_chart_view_id,
                                    pvt.scene_id AS t_dv_id,
                                    pvt.id       AS t_chart_view_id
                             FROM snapshot_core_chart_view pvt
                                      INNER JOIN snapshot_core_chart_view pvs ON pvt.copy_from = pvs.id
                             WHERE pvt.copy_id = #{copyId}) pv_target_copy
                            ON snapshot_visualization_linkage.dv_id = pv_target_copy.s_dv_id
                                AND snapshot_visualization_linkage.target_view_id = pv_target_copy.s_chart_view_id
    </insert>

    <insert id="copyLinkageField">

        INSERT INTO snapshot_visualization_linkage_field (id,
                                                 `linkage_id`,
                                                 `source_field`,
                                                 `target_field`,
                                                 `update_time`,
                                                 `copy_from`,
                                                 `copy_id`)
        SELECT snapshot_visualization_linkage_field.`id` + #{copyId} as `id`,
               pvlf_copy.t_id                               as `linkage_id`,
               `source_field`,
               `target_field`,
               `update_time`,
               id                                           AS copy_from,
               #{copyId}                                    as copy_id
        FROM snapshot_visualization_linkage_field
                 INNER JOIN (SELECT id        AS t_id,
                                    copy_from AS s_id
                             FROM snapshot_visualization_linkage
                             WHERE copy_id = #{copyId}) pvlf_copy
                            ON snapshot_visualization_linkage_field.linkage_id = pvlf_copy.s_id
    </insert>

    <resultMap  id="ViewDetailsMap" type="io.gisbi.api.visualization.dto.VisualizationViewTableDTO">
        <result column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="scene_id" property="sceneId"/>
        <result column="table_id" property="tableId"/>
        <result column="type" property="type"/>
        <result column="render" property="render"/>
        <collection property="tableFields" ofType="io.gisbi.extensions.datasource.dto.DatasetTableFieldDTO">
            <result column="field_id" jdbcType="VARCHAR" 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>

    <select id="getVisualizationViewDetails" resultMap="ViewDetailsMap">
        SELECT ccv.id,
               ccv.title,
               ccv.scene_id,
               ccv.table_id,
               ccv.`type`,
               ccv.render,
               ccv.scene_id as `visualization_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 snapshot_core_chart_view ccv
                 LEFT JOIN core_dataset_table_field
                           ON ccv.table_id = core_dataset_table_field.dataset_group_id
        WHERE ccv.scene_id = #{dvId}
          AND ccv.id IS NOT NULL and ccv.`type` != 'VQuery'
    </select>

    <select id="queryReportFilter" resultType="io.gisbi.api.visualization.vo.VisualizationReportFilterVO">
        SELECT
            `id`, `report_id`, `resource_id`, `dv_type`, `component_id`, `filter_id`, `filter_info`, `filter_version`, `create_time`, `create_user`
        FROM
            visualization_report_filter
        where visualization_report_filter.resource_id = #{dvId} and visualization_report_filter.task_id = #{taskId}
    </select>

    <delete id="deleteDataVBatch">
        DELETE
        FROM
         <choose>
            <when test="resourceTable == 'snapshot'">
               snapshot_data_visualization_info dvi
            </when>
            <otherwise>
               data_visualization_info dvi
            </otherwise>
         </choose>
        WHERE
            dvi.id IN
            <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
              #{id}
            </foreach>
    </delete>
    <delete id="deleteViewsBatch">
        DELETE
        FROM
        <choose>
           <when test="resourceTable == 'snapshot'">
               snapshot_core_chart_view ccv
           </when>
           <otherwise>
              core_chart_view ccv
           </otherwise>
        </choose>
        WHERE
            ccv.scene_id IN
            <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
              #{id}
            </foreach>
    </delete>

     <select id="queryInnerUserInfo" resultType="io.gisbi.api.permissions.user.vo.UserFormVO">
            select id,account,name from per_user where id =  #{id}
     </select>

     <insert id="snapshotDataV">
        INSERT into snapshot_data_visualization_info (
            `id`,
            `name`,
            `pid`,
            `org_id`,
            `level`,
            `node_type`,
            `type`,
            `canvas_style_data`,
            `component_data`,
            `mobile_layout`,
            `status`,
            `self_watermark_status`,
            `sort`,
            `create_time`,
            `create_by`,
            `update_time`,
            `update_by`,
            `remark`,
            `source`,
            `delete_flag`,
            `delete_time`,
            `delete_by`,
            `version`,
            `content_id`,
            `check_version`
        ) SELECT
        `id`,
        `name`,
        `pid`,
        `org_id`,
        `level`,
        `node_type`,
        `type`,
        `canvas_style_data`,
        `component_data`,
        `mobile_layout`,
        `status`,
        `self_watermark_status`,
        `sort`,
        `create_time`,
        `create_by`,
        `update_time`,
        `update_by`,
        `remark`,
        `source`,
        `delete_flag`,
        `delete_time`,
        `delete_by`,
        `version`,
        `content_id`,
        `check_version`
        FROM
            data_visualization_info
        WHERE
            id = #{dvId}
     </insert>

     <insert id="snapshotViews">
        INSERT into snapshot_core_chart_view (
            `id`,
            `title`,
            `scene_id`,
            `table_id`,
            `type`,
            `render`,
            `result_count`,
            `result_mode`,
            `x_axis`,
            `x_axis_ext`,
            `y_axis`,
            `y_axis_ext`,
            `ext_stack`,
            `ext_bubble`,
            `ext_label`,
            `ext_tooltip`,
            `custom_attr`,
            `custom_attr_mobile`,
            `custom_style`,
            `custom_style_mobile`,
            `custom_filter`,
            `drill_fields`,
            `senior`,
            `create_by`,
            `create_time`,
            `update_time`,
            `snapshot`,
            `style_priority`,
            `chart_type`,
            `is_plugin`,
            `data_from`,
            `view_fields`,
            `refresh_view_enable`,
            `refresh_unit`,
            `refresh_time`,
            `linkage_active`,
            `jump_active`,
            `copy_from`,
            `copy_id`,
            `aggregate`,
            `flow_map_start_name`,
            `flow_map_end_name`,
            `ext_color`,
            `sort_priority`
        ) SELECT
        `id`,
        `title`,
        `scene_id`,
        `table_id`,
        `type`,
        `render`,
        `result_count`,
        `result_mode`,
        `x_axis`,
        `x_axis_ext`,
        `y_axis`,
        `y_axis_ext`,
        `ext_stack`,
        `ext_bubble`,
        `ext_label`,
        `ext_tooltip`,
        `custom_attr`,
        `custom_attr_mobile`,
        `custom_style`,
        `custom_style_mobile`,
        `custom_filter`,
        `drill_fields`,
        `senior`,
        `create_by`,
        `create_time`,
        `update_time`,
        `snapshot`,
        `style_priority`,
        `chart_type`,
        `is_plugin`,
        `data_from`,
        `view_fields`,
        `refresh_view_enable`,
        `refresh_unit`,
        `refresh_time`,
        `linkage_active`,
        `jump_active`,
        `copy_from`,
        `copy_id`,
        `aggregate`,
        `flow_map_start_name`,
        `flow_map_end_name`,
        `ext_color`,
        `sort_priority`
        FROM
            core_chart_view
        WHERE
            scene_id = #{dvId}
     </insert>

     <insert id="snapshotLinkJumpTargetViewInfo">
        INSERT into snapshot_visualization_link_jump_target_view_info (
            `target_id`,
            `link_jump_info_id`,
            `source_field_active_id`,
            `target_view_id`,
            `target_field_id`,
            `copy_from`,
            `copy_id`,
            `target_type`
        ) SELECT
        vljtvi.`target_id`,
        vljtvi.`link_jump_info_id`,
        vljtvi.`source_field_active_id`,
        vljtvi.`target_view_id`,
        vljtvi.`target_field_id`,
        vljtvi.`copy_from`,
        vljtvi.`copy_id`,
        vljtvi.`target_type`
        FROM
            visualization_link_jump_target_view_info vljtvi
            LEFT JOIN visualization_link_jump_info vlji ON vljtvi.link_jump_info_id = vlji.id
            LEFT JOIN visualization_link_jump vlj ON vlji.link_jump_id = vlj.id
        WHERE
            vlj.source_dv_id = #{dvId}
     </insert>

     <insert id="snapshotLinkJumpInfo">
        INSERT into snapshot_visualization_link_jump_info (
            `id`,
            `link_jump_id`,
            `link_type`,
            `jump_type`,
            `target_dv_id`,
            `source_field_id`,
            `content`,
            `checked`,
            `attach_params`,
            `copy_from`,
            `copy_id`,
            `window_size`
        ) SELECT
        vlji.`id`,
        vlji.`link_jump_id`,
        vlji.`link_type`,
        vlji.`jump_type`,
        vlji.`target_dv_id`,
        vlji.`source_field_id`,
        vlji.`content`,
        vlji.`checked`,
        vlji.`attach_params`,
        vlji.`copy_from`,
        vlji.`copy_id`,
        vlji.`window_size`
        FROM
            visualization_link_jump_info vlji
            LEFT JOIN visualization_link_jump vlj ON vlji.link_jump_id = vlj.id
        WHERE
            vlj.source_dv_id = #{dvId}
     </insert>
     <insert id="snapshotLinkJump">
        INSERT into snapshot_visualization_link_jump (
            `id`,
            `source_dv_id`,
            `source_view_id`,
            `link_jump_info`,
            `checked`,
            `copy_from`,
            `copy_id`
        ) SELECT
        vlj.`id`,
        vlj.`source_dv_id`,
        vlj.`source_view_id`,
        vlj.`link_jump_info`,
        vlj.`checked`,
        vlj.`copy_from`,
        vlj.`copy_id`
        FROM
            visualization_link_jump vlj
        WHERE
            vlj.source_dv_id = #{dvId}
     </insert>

     <insert id="snapshotLinkageField">
        INSERT into snapshot_visualization_linkage_field (
            `id`,
            `linkage_id`,
            `source_field`,
            `target_field`,
            `update_time`,
            `copy_from`,
            `copy_id`
        ) SELECT
        vlf.`id`,
        vlf.`linkage_id`,
        vlf.`source_field`,
        vlf.`target_field`,
        vlf.`update_time`,
        vlf.`copy_from`,
        vlf.`copy_id`
        FROM
            visualization_linkage_field vlf
            LEFT JOIN visualization_linkage vl ON vlf.linkage_id = vl.id
        WHERE
            vl.dv_id =  #{dvId}
     </insert>
     <insert id="snapshotLinkage">
         INSERT into snapshot_visualization_linkage (
         	`id`,
         	`dv_id`,
         	`source_view_id`,
         	`target_view_id`,
         	`update_time`,
         	`update_people`,
         	`linkage_active`,
         	`ext1`,
         	`ext2`,
         	`copy_from`,
         	`copy_id`
         ) SELECT
         vl.`id`,
         vl.`dv_id`,
         vl.`source_view_id`,
         vl.`target_view_id`,
         vl.`update_time`,
         vl.`update_people`,
         vl.`linkage_active`,
         vl.`ext1`,
         vl.`ext2`,
         vl.`copy_from`,
         vl.`copy_id`
         FROM
         	visualization_linkage vl
         WHERE
         	vl.dv_id = #{dvId}
     </insert>
     <insert id="snapshotOuterParamsTargetViewInfo">
         INSERT into snapshot_visualization_outer_params_target_view_info (
         	`target_id`,
         	`params_info_id`,
         	`target_view_id`,
         	`target_field_id`,
         	`copy_from`,
         	`copy_id`,
         	`target_ds_id`
         ) SELECT
         voptvi.`target_id`,
         voptvi.`params_info_id`,
         voptvi.`target_view_id`,
         voptvi.`target_field_id`,
         voptvi.`copy_from`,
         voptvi.`copy_id`,
         voptvi.`target_ds_id`
         FROM
         	visualization_outer_params_target_view_info voptvi
         	LEFT JOIN visualization_outer_params_info vopi ON vopi.params_info_id = voptvi.params_info_id
         	LEFT JOIN visualization_outer_params vop ON vop.params_id = vopi.params_id
         WHERE
         	vop.visualization_id = #{dvId}
     </insert>
     <insert id="snapshotOuterParamsInfo">
         INSERT INTO snapshot_visualization_outer_params_info (
         	`params_info_id`,
         	`params_id`,
         	`param_name`,
         	`checked`,
         	`copy_from`,
         	`copy_id`,
         	`required`,
         	`default_value`,
         	`enabled_default`
         ) SELECT
         vopi.`params_info_id`,
         vopi.`params_id`,
         vopi.`param_name`,
         vopi.`checked`,
         vopi.`copy_from`,
         vopi.`copy_id`,
         vopi.`required`,
         vopi.`default_value`,
         vopi.`enabled_default`
         FROM
         	visualization_outer_params_info vopi
         	LEFT JOIN visualization_outer_params vop ON vop.params_id = vopi.params_id
         WHERE
         	vop.visualization_id =  #{dvId}
    </insert>
    <insert id="snapshotOuterParams">
       INSERT into snapshot_visualization_outer_params (
       	`params_id`,
       	`visualization_id`,
       	`checked`,
       	`remark`,
       	`copy_from`,
       	`copy_id`
       ) SELECT
       vop.`params_id`,
       vop.`visualization_id`,
       vop.`checked`,
       vop.`remark`,
       vop.`copy_from`,
       vop.`copy_id`
       FROM
       	visualization_outer_params vop
       WHERE
       	vop.visualization_id =  #{dvId}
    </insert>


    <insert id="restoreDataV">
       INSERT INTO data_visualization_info (
       	`id`,
       	`name`,
       	`pid`,
       	`org_id`,
       	`level`,
       	`node_type`,
       	`type`,
       	`canvas_style_data`,
       	`component_data`,
       	`mobile_layout`,
       	`status`,
       	`self_watermark_status`,
       	`sort`,
       	`create_time`,
       	`create_by`,
       	`update_time`,
       	`update_by`,
       	`remark`,
       	`source`,
       	`delete_flag`,
       	`delete_time`,
       	`delete_by`,
       	`version`,
       	`content_id`,
       	`check_version`
       ) SELECT
       `id`,
       `name`,
       `pid`,
       `org_id`,
       `level`,
       `node_type`,
       `type`,
       `canvas_style_data`,
       `component_data`,
       `mobile_layout`,
       `status`,
       `self_watermark_status`,
       `sort`,
       `create_time`,
       `create_by`,
       `update_time`,
       `update_by`,
       `remark`,
       `source`,
       `delete_flag`,
       `delete_time`,
       `delete_by`,
       `version`,
       `content_id`,
       `check_version`
       FROM
       	snapshot_data_visualization_info
       WHERE
       	id =  #{dvId}
     </insert>

     <insert id="restoreViews">
       INSERT INTO core_chart_view (
       	`id`,
       	`title`,
       	`scene_id`,
       	`table_id`,
       	`type`,
       	`render`,
       	`result_count`,
       	`result_mode`,
       	`x_axis`,
       	`x_axis_ext`,
       	`y_axis`,
       	`y_axis_ext`,
       	`ext_stack`,
       	`ext_bubble`,
       	`ext_label`,
       	`ext_tooltip`,
       	`custom_attr`,
       	`custom_attr_mobile`,
       	`custom_style`,
       	`custom_style_mobile`,
       	`custom_filter`,
       	`drill_fields`,
       	`senior`,
       	`create_by`,
       	`create_time`,
       	`update_time`,
       	`snapshot`,
       	`style_priority`,
       	`chart_type`,
       	`is_plugin`,
       	`data_from`,
       	`view_fields`,
       	`refresh_view_enable`,
       	`refresh_unit`,
       	`refresh_time`,
       	`linkage_active`,
       	`jump_active`,
       	`copy_from`,
       	`copy_id`,
       	`aggregate`,
       	`flow_map_start_name`,
       	`flow_map_end_name`,
       	`ext_color`,
       	`sort_priority`
       ) SELECT
       `id`,
       `title`,
       `scene_id`,
       `table_id`,
       `type`,
       `render`,
       `result_count`,
       `result_mode`,
       `x_axis`,
       `x_axis_ext`,
       `y_axis`,
       `y_axis_ext`,
       `ext_stack`,
       `ext_bubble`,
       `ext_label`,
       `ext_tooltip`,
       `custom_attr`,
       `custom_attr_mobile`,
       `custom_style`,
       `custom_style_mobile`,
       `custom_filter`,
       `drill_fields`,
       `senior`,
       `create_by`,
       `create_time`,
       `update_time`,
       `snapshot`,
       `style_priority`,
       `chart_type`,
       `is_plugin`,
       `data_from`,
       `view_fields`,
       `refresh_view_enable`,
       `refresh_unit`,
       `refresh_time`,
       `linkage_active`,
       `jump_active`,
       `copy_from`,
       `copy_id`,
       `aggregate`,
       `flow_map_start_name`,
       `flow_map_end_name`,
       `ext_color`,
       `sort_priority`
       FROM
       	snapshot_core_chart_view
       WHERE
       	scene_id = #{dvId}
     </insert>

     <insert id="restoreLinkJumpTargetViewInfo">
         INSERT into visualization_link_jump_target_view_info (
         	`target_id`,
         	`link_jump_info_id`,
         	`source_field_active_id`,
         	`target_view_id`,
         	`target_field_id`,
         	`copy_from`,
         	`copy_id`,
         	`target_type`
         ) SELECT
         vljtvi.`target_id`,
         vljtvi.`link_jump_info_id`,
         vljtvi.`source_field_active_id`,
         vljtvi.`target_view_id`,
         vljtvi.`target_field_id`,
         vljtvi.`copy_from`,
         vljtvi.`copy_id`,
         vljtvi.`target_type`
         FROM
         	snapshot_visualization_link_jump_target_view_info vljtvi
         	LEFT JOIN snapshot_visualization_link_jump_info vlji ON vljtvi.link_jump_info_id = vlji.id
         	LEFT JOIN snapshot_visualization_link_jump vlj ON vlji.link_jump_id = vlj.id
         WHERE
         	vlj.source_dv_id = #{dvId}
     </insert>

     <insert id="restoreLinkJumpInfo">
         INSERT INTO visualization_link_jump_info (
         	`id`,
         	`link_jump_id`,
         	`link_type`,
         	`jump_type`,
         	`target_dv_id`,
         	`source_field_id`,
         	`content`,
         	`checked`,
         	`attach_params`,
         	`copy_from`,
         	`copy_id`,
         	`window_size`
         ) SELECT
         vlji.`id`,
         vlji.`link_jump_id`,
         vlji.`link_type`,
         vlji.`jump_type`,
         vlji.`target_dv_id`,
         vlji.`source_field_id`,
         vlji.`content`,
         vlji.`checked`,
         vlji.`attach_params`,
         vlji.`copy_from`,
         vlji.`copy_id`,
         vlji.`window_size`
         FROM
         	snapshot_visualization_link_jump_info vlji
         	LEFT JOIN snapshot_visualization_link_jump vlj ON vlji.link_jump_id = vlj.id
         WHERE
         	vlj.source_dv_id = #{dvId}
     </insert>
     <insert id="restoreLinkJump">
         INSERT INTO visualization_link_jump (
         	`id`,
         	`source_dv_id`,
         	`source_view_id`,
         	`link_jump_info`,
         	`checked`,
         	`copy_from`,
         	`copy_id`
         ) SELECT
         vlj.`id`,
         vlj.`source_dv_id`,
         vlj.`source_view_id`,
         vlj.`link_jump_info`,
         vlj.`checked`,
         vlj.`copy_from`,
         vlj.`copy_id`
         FROM
         	snapshot_visualization_link_jump vlj
         WHERE
         	vlj.source_dv_id = #{dvId}
     </insert>

     <insert id="restoreLinkageField">
         INSERT INTO visualization_linkage_field (
         	`id`,
         	`linkage_id`,
         	`source_field`,
         	`target_field`,
         	`update_time`,
         	`copy_from`,
         	`copy_id`
         ) SELECT
         vlf.`id`,
         vlf.`linkage_id`,
         vlf.`source_field`,
         vlf.`target_field`,
         vlf.`update_time`,
         vlf.`copy_from`,
         vlf.`copy_id`
         FROM
         	snapshot_visualization_linkage_field vlf
         	LEFT JOIN snapshot_visualization_linkage vl ON vlf.linkage_id = vl.id
         WHERE
         	vl.dv_id = #{dvId}
     </insert>
     <insert id="restoreLinkage">
        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
        vl.`id`,
        vl.`dv_id`,
        vl.`source_view_id`,
        vl.`target_view_id`,
        vl.`update_time`,
        vl.`update_people`,
        vl.`linkage_active`,
        vl.`ext1`,
        vl.`ext2`,
        vl.`copy_from`,
        vl.`copy_id`
        FROM
            snapshot_visualization_linkage vl
        WHERE
            vl.dv_id = #{dvId}
     </insert>
     <insert id="restoreOuterParamsTargetViewInfo">
         INSERT into visualization_outer_params_target_view_info (
         	`target_id`,
         	`params_info_id`,
         	`target_view_id`,
         	`target_field_id`,
         	`copy_from`,
         	`copy_id`,
         	`target_ds_id`
         ) SELECT
         voptvi.`target_id`,
         voptvi.`params_info_id`,
         voptvi.`target_view_id`,
         voptvi.`target_field_id`,
         voptvi.`copy_from`,
         voptvi.`copy_id`,
         voptvi.`target_ds_id`
         FROM
         	snapshot_visualization_outer_params_target_view_info voptvi
         	LEFT JOIN snapshot_visualization_outer_params_info vopi ON vopi.params_info_id = voptvi.params_info_id
         	LEFT JOIN snapshot_visualization_outer_params vop ON vop.params_id = vopi.params_id
         WHERE
         	vop.visualization_id = #{dvId}
     </insert>
     <insert id="restoreOuterParamsInfo">
         INSERT into visualization_outer_params_info (
         	`params_info_id`,
         	`params_id`,
         	`param_name`,
         	`checked`,
         	`copy_from`,
         	`copy_id`,
         	`required`,
         	`default_value`,
         	`enabled_default`
         ) SELECT
         vopi.`params_info_id`,
         vopi.`params_id`,
         vopi.`param_name`,
         vopi.`checked`,
         vopi.`copy_from`,
         vopi.`copy_id`,
         vopi.`required`,
         vopi.`default_value`,
         vopi.`enabled_default`
         FROM
         	snapshot_visualization_outer_params_info vopi
         	LEFT JOIN snapshot_visualization_outer_params vop ON vop.params_id = vopi.params_id
         WHERE
         	vop.visualization_id = #{dvId}
    </insert>
    <insert id="restoreOuterParams">
       INSERT into visualization_outer_params (
       	`params_id`,
       	`visualization_id`,
       	`checked`,
       	`remark`,
       	`copy_from`,
       	`copy_id`
       ) SELECT
       vop.`params_id`,
       vop.`visualization_id`,
       vop.`checked`,
       vop.`remark`,
       vop.`copy_from`,
       vop.`copy_id`
       FROM
       	snapshot_visualization_outer_params vop
       WHERE
       	vop.visualization_id = #{dvId}
    </insert>
</mapper>
