<?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 `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`,
                                       `flow_map_start_name`,
                                       `flow_map_end_name`,
                                       `ext_color`)
        SELECT core_chart_view.`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`,
               core_chart_view.`id`             as copy_from,
               #{copyId}                        as copy_id,
               `flow_map_start_name`,
               `flow_map_end_name`,
               `ext_color`
        FROM core_chart_view
        WHERE core_chart_view.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 data_visualization_info
        where data_visualization_info.delete_flag = 0
          and data_visualization_info.id = #{dvId}
          <if test="dvType">
            and data_visualization_info.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') 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>
             ${ew.customSqlSegment}
    </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 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,
               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.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 core_chart_view pvt
                                      INNER JOIN core_chart_view pvs ON pvt.copy_from = pvs.id
                             WHERE pvt.copy_id = #{copyId}) dv_view_copy
                            ON visualization_link_jump.source_dv_id = dv_view_copy.s_dv_id
                                AND visualization_link_jump.source_view_id = dv_view_copy.s_chart_view_id
    </insert>

    <insert id="copyLinkJumpInfo">
        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)
        SELECT visualization_link_jump_info.`id` + #{copyId} as id,
               plj_copy.t_id                                 as link_jump_id,
               visualization_link_jump_info.`link_type`,
               visualization_link_jump_info.`jump_type`,
               visualization_link_jump_info.`target_dv_id`,
               visualization_link_jump_info.`source_field_id`,
               visualization_link_jump_info.`content`,
               visualization_link_jump_info.`checked`,
               visualization_link_jump_info.`attach_params`,
               visualization_link_jump_info.`id`             AS copy_from,
               #{copyId}                                     AS copy_id
        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="copyLinkJumpTargetInfo">
        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`)
        SELECT visualization_link_jump_target_view_info.`target_id` + #{copyId} as target_id,
               plji_copy.t_id                                                   as link_jump_info_id,
               visualization_link_jump_target_view_info.`source_field_active_id`,
               visualization_link_jump_target_view_info.`target_view_id`,
               visualization_link_jump_target_view_info.`target_field_id`,
               visualization_link_jump_target_view_info.`target_id`             AS 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>

    <insert id="copyLinkage">
        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 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`,
               visualization_linkage.`id`             as copy_from,
               #{copyId}                              as copy_id
        FROM 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 core_chart_view pvt
                                      INNER JOIN core_chart_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_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 core_chart_view pvt
                                      INNER JOIN core_chart_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_chart_view_id
    </insert>

    <insert id="copyLinkageField">

        INSERT INTO visualization_linkage_field (id,
                                                 `linkage_id`,
                                                 `source_field`,
                                                 `target_field`,
                                                 `update_time`,
                                                 `copy_from`,
                                                 `copy_id`)
        SELECT 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 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>

    <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 core_chart_view.id,
               core_chart_view.title,
               core_chart_view.scene_id,
               core_chart_view.table_id,
               core_chart_view.`type`,
               core_chart_view.render,
               core_chart_view.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 core_chart_view
                 LEFT JOIN core_dataset_table_field
                           ON core_chart_view.table_id = core_dataset_table_field.dataset_group_id
        WHERE core_chart_view.scene_id = #{dvId}
          AND core_chart_view.id IS NOT NULL and core_chart_view.`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
            data_visualization_info dvi
        WHERE
            dvi.id IN
            <foreach collection="ids" item="id" index="index" open="(" close=")" separator=",">
              #{id}
            </foreach>
    </delete>
    <delete id="deleteViewsBatch">
        DELETE
        FROM
            core_chart_view ccv
        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>
</mapper>
