

DROP PROCEDURE IF EXISTS sp_data_migration_wcmtemplatequote;
DELIMITER $$
CREATE PROCEDURE sp_data_migration_wcmtemplatequote(IN prevTemplateQuoteId BIGINT(20),
                                              IN templateQuoteIdOffset BIGINT(20),
                                              IN templateIdOffset BIGINT(20),
                                              IN siteIdOffset BIGINT(20),
                                              IN channelIdOffset BIGINT(20))
	BEGIN 
	
    DECLARE isover INT DEFAULT 0;

    DECLARE migr_table_name varchar(255) DEFAULT 'wcmtemplatequote';
    DECLARE template_migr_table_name varchar(255) DEFAULT 'wcmtemplate';
    DECLARE site_migr_table_name varchar(255) DEFAULT 'wcmwebsite';
    DECLARE channel_migr_table_name varchar(255) DEFAULT 'wcmchannel';

    DECLARE  f_TEMPLATEQUOTEID int(11) DEFAULT '0';
    DECLARE  f_TEMPLATEID int(11) DEFAULT '0';
    DECLARE  f_TEMPLATETYPE smallint(6) DEFAULT '0';
    DECLARE  f_QUOTEDFOLDERTYPE int(11) DEFAULT '0';
    DECLARE  f_QUOTEDFOLDERID int(11) DEFAULT '0';
    DECLARE  f_FAMILYINDEX smallint(6) DEFAULT '0';
    DECLARE  f_QUOTETYPE smallint(6) DEFAULT '0';


    DECLARE cur CURSOR FOR
		SELECT
      TEMPLATEQUOTEID,
      TEMPLATEID,
      TEMPLATETYPE,
      QUOTEDFOLDERTYPE,
      QUOTEDFOLDERID,
      FAMILYINDEX,
      QUOTETYPE
    FROM wcmtemplatequote
		ORDER BY TEMPLATEQUOTEID asc;

	DECLARE CONTINUE HANDLER FOR NOT FOUND SET isover = 1;
	
	OPEN cur;
	
	FETCH cur INTO
    f_TEMPLATEQUOTEID,
    f_TEMPLATEID,
    f_TEMPLATETYPE,
    f_QUOTEDFOLDERTYPE,
    f_QUOTEDFOLDERID,
    f_FAMILYINDEX,
    f_QUOTETYPE
    ;



    select concat('开始迁移 ', migr_table_name) info;




    WHILE isover= 0 DO

      # 断点续传
      IF (f_TEMPLATEQUOTEID > prevTemplateQuoteId)

        THEN

            SET @TEMPLATEQUOTEID = f_TEMPLATEQUOTEID;
            SET f_TEMPLATEQUOTEID = f_TEMPLATEQUOTEID + templateQuoteIdOffset;

            SET @template_offset_num = ifnull((SELECT offset_num FROM data_migration_offset WHERE table_name = template_migr_table_name AND start_id < f_TEMPLATEID and  end_id >= f_TEMPLATEID), templateIdOffset);
            SET f_TEMPLATEID = f_TEMPLATEID + @template_offset_num;

            IF (f_QUOTEDFOLDERTYPE = 103)
              THEN
                SET @site_offset_num = ifnull((SELECT offset_num FROM data_migration_offset WHERE table_name = site_migr_table_name AND start_id < f_QUOTEDFOLDERID and  end_id >= f_QUOTEDFOLDERID), siteIdOffset);
                SET f_QUOTEDFOLDERID = f_QUOTEDFOLDERID + @site_offset_num;
            END IF ;

            IF (f_QUOTEDFOLDERTYPE = 101)
              THEN
                SET @channel_offset_num = ifnull((SELECT offset_num FROM data_migration_offset WHERE table_name = channel_migr_table_name AND start_id < f_QUOTEDFOLDERID and  end_id >= f_QUOTEDFOLDERID), channelIdOffset);
                SET f_QUOTEDFOLDERID = f_QUOTEDFOLDERID + @channel_offset_num;
            END IF ;



            insert into trs_hycloud_iip.wcmtemplatequote
              (
                TEMPLATEQUOTEID,
                TEMPLATEID,
                TEMPLATETYPE,
                QUOTEDFOLDERTYPE,
                QUOTEDFOLDERID,
                FAMILYINDEX,
                QUOTETYPE
              )
              VALUES
                (
                  f_TEMPLATEQUOTEID,
                  f_TEMPLATEID,
                  f_TEMPLATETYPE,
                  f_QUOTEDFOLDERTYPE,
                  f_QUOTEDFOLDERID,
                  f_FAMILYINDEX,
                  f_QUOTETYPE
                );

          SET @log_sql = build_migration_log_sql(migr_table_name, @TEMPLATEQUOTEID, now());

          prepare stmt from @log_sql;
          EXECUTE stmt;
          deallocate prepare stmt;
          call setOffset(migr_table_name, templateQuoteIdOffset, prevTemplateQuoteId, @TEMPLATEQUOTEID);
    END IF ;

		FETCH cur INTO
      f_TEMPLATEQUOTEID,
      f_TEMPLATEID,
      f_TEMPLATETYPE,
      f_QUOTEDFOLDERTYPE,
      f_QUOTEDFOLDERID,
      f_FAMILYINDEX,
      f_QUOTETYPE
    ;
		
	END WHILE;
	
	CLOSE cur;

    select concat('完成迁移 ', migr_table_name) info;


  END $$
DELIMITER ;