<?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="com.persagy.dmp.rwd.migrate.dao.DigitalMigrateMapper">

    <insert id="migrateDefineType">
        replace into `dt_define_type`(`id`,`code`,`name`,`alias_code`,
          `alias_name`,`obj_type`,`group_code`,`project_id`,`major_code`,
          `system_code`,`equipment_code`,`parent_code`,`version`,`creator`,
          `creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`code`,`name`,`alias_code`,
          `alias_name`,`obj_type`,`group_code`,`project_id`,`major_code`,
          `system_code`,`equipment_code`,`parent_code`,null,'0',
          date_format(now(), '%Y%m%d%h%i%s'),null,null,1
        FROM `${schema}`.`rwd_def_class_${groupCode}`
	</insert>
    <insert id="migrateDefineInfo">
        replace into `dt_define_info`(`id`,`code`,`name`,`alias_code`,
          `alias_name`,`class_code`,`group_code`,`project_id`,`category`,
          `first_tag`,`second_tag`,`priority`,`input_mode`,`unit`,`data_type`,
          `is_multiple`,`is_region`,`formater`,`data_source`,`remark`,
          `sub_flag`,`weak_point`,`show_order`,`version`,
          `creator`,`creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`code`,`name`,`alias_code`,
          `alias_name`,`class_code`,`group_code`,`project_id`,`category`,
          `first_tag`,`second_tag`,`priority`,`input_mode`,`unit`,`data_type`,
          `is_multiple`,`is_region`,`formater`,`data_source`,`note`,
          `sub_flag`,`weak_point`,null,null,
          '0',date_format(now(), '%Y%m%d%h%i%s'),null,null,1
        FROM `${schema}`.`rwd_def_funcid_${groupCode}`
    </insert>
    <insert id="migrateDefineGraph">
        replace into `dt_define_graph`(`id`,`code`,`name`,`first_category`,
          `second_category`,`remark`,`object_types`,
          `creator`,`creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`code`,`name`,`first_category`,
          `second_category`,`category_remark`,`object_types`,
          '0',date_format(now(), '%Y%m%d%h%i%s'),null,null,1
        FROM `${schema}`.`rwd_def_graph_${groupCode}`
    </insert>
    <insert id="migrateDefineRel">
        replace into `dt_define_rel`(`id`,`code`,`name`,`graph_code`,
          `directional`,`attribute`,`target_objs`,`remark`,
          `creator`,`creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`code`,`name`,`graph_code`,
          `directional`,`attribute`,`target_objs`,`remark`,
          '0',date_format(now(), '%Y%m%d%h%i%s'),null,null,1
        FROM `${schema}`.`rwd_def_rel_${groupCode}`
    </insert>
    <insert id="migrateObject">
        replace into `dt_object`(`id`,`name`,`local_id`,
          `local_name`,`group_code`,`project_id`,`obj_type`,`class_code`,
          `grouping`,`infos`,`virtual_codes`,`create_app`,`update_app`,
          `creator`,`creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`name`,`local_id`,
          `local_name`,`group_code`,`project_id`,`obj_type`,`class_code`,
          `grouping`,`infos`,`virtual_info_codes`,`create_app`,`update_app`,
          '0',date_format(now(), '%Y%m%d%h%i%s'),null,null,`valid`
        FROM `${schema}`.`rwd_object_${groupCode}_${projectId}`
    </insert>
    <insert id="migrateRelation">
        replace into `dt_relation`(`id`,`graph_id`,`graph_code`,
          `rel_code`,`rel_value`,`group_code`,`project_id`,`obj_from`,`obj_to`,
          `create_app`,`update_app`,
          `creator`,`creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`graph_id`,`graph_code`,
          `rel_code`,`rel_value`,`group_code`,`project_id`,`obj_from`,`obj_to`,
          `create_app`,`update_app`,
          '0',date_format(now(), '%Y%m%d%h%i%s'),null,null,`status`
        FROM `${schema}`.`rwd_relation_${groupCode}_${projectId}`
    </insert>
    <insert id="migrateStandardDefineType">
        replace into `dt_define_type`(`id`,`code`,`name`,`alias_code`,
                                      `alias_name`,`obj_type`,`group_code`,`project_id`,`major_code`,
                                      `system_code`,`equipment_code`,`parent_code`,`version`,`creator`,
                                      `creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`code`,`name`,`alias_code`,
               `alias_name`,`obj_type`,`group_code`,`project_id`,`major_code`,
               `system_code`,`equipment_code`,`parent_code`,null,'0',
               date_format(now(), '%Y%m%d%h%i%s'),null,null,1
        FROM (SELECT * FROM `${schema}`.rwd_def_class AS rdc
         WHERE CONCAT( rdc.CODE, '_', rdc.obj_type ) NOT IN ( SELECT CONCAT( CODE, '_', obj_type ) FROM dt_define_type WHERE group_code = '0' AND project_id = '0' )
         ) AS define_type
    </insert>
    <insert id="migrateStandardDefineInfo">
        replace into `dt_define_info`(`id`,`code`,`name`,`alias_code`,
                                      `alias_name`,`class_code`,`group_code`,`project_id`,`category`,
                                      `first_tag`,`second_tag`,`priority`,`input_mode`,`unit`,`data_type`,
                                      `is_multiple`,`is_region`,`formater`,`data_source`,`remark`,
                                      `sub_flag`,`weak_point`,`show_order`,`version`,
                                      `creator`,`creation_time`,`modifier`,`modified_time`,`valid`)
        SELECT `id`,`code`,`name`,`alias_code`,
               `alias_name`,`class_code`,`group_code`,`project_id`,`category`,
               `first_tag`,`second_tag`,`priority`,`input_mode`,`unit`,`data_type`,
               `is_multiple`,`is_region`,`formater`,`data_source`,`note`,
               `sub_flag`,`weak_point`,null,null,
               '0',date_format(now(), '%Y%m%d%h%i%s'),null,null,1
        FROM (SELECT * FROM `${schema}`.rwd_def_funcid AS rdf
             WHERE
                     CONCAT( rdf.CODE, '_', rdf.class_code, '_', rdf.group_code, '_', rdf.project_id ) NOT IN (
                     SELECT
                         CONCAT( CODE, '_', class_code, '_', group_code, '_', project_id )
                     FROM
                         dt_define_info
                     WHERE
                         group_code = '0'
                       AND project_id = '0'
                 )
        ) AS define_info
    </insert>

</mapper>
