
DROP PROCEDURE IF EXISTS sp_city_appendix;
DELIMITER $$
CREATE PROCEDURE sp_city_appendix(IN n INT,
                                   IN masIdOffset BIGINT(20))

  BEGIN

    # 每迁移一个wcm节点，@n 必须 +1
    SET @n = n;

    # 每个wcm节点，元数据表需要加上后缀，以区分同名元数据表
    SET @citySuffix = concat('_city', @n);

    #附件迁移
    # 迁移 MAS 视频，6个wcm节点的视频是在一起的。可重复执行。
    # 支持断点续传，上一次迁移的最后一个 xx ID
    SET @prevMaterialquoteId = ifnull((SELECT prev_id from data_migration_log where table_name = 'xwcmmaterial'), 0);
    SET @prevAppendixId = ifnull((SELECT prev_id from data_migration_log where table_name = 'wcmappendix'), 0);


    SET @wcmdocument_offset_num = ifnull((SELECT max(offset_num) FROM data_migration_offset WHERE table_name = 'wcmdocument'), 0);
    SET @docIdOffset = @wcmdocument_offset_num;
    #附件迁移
	  SET @maxAppendixId= IFNULL((SELECT MAX(appendixId) from trs_hycloud_iip.wcmappendix),0);
    SET @appendixIdOffset = @maxAppendixId - @prevAppendixId;
    call sp_data_migration_wcmappendix(@prevAppendixId, @appendixIdOffset, @docIdOffset);
    #处理素材表
    SET @maxNAppendixId= IFNULL((SELECT MAX(appendixId) from trs_hycloud_iip.wcmappendix),0);
    SET @nappendixIdOffset = @maxNAppendixId - @prevMaterialquoteId;
    call sp_data_migration_xwcmmaterial(@prevMaterialquoteId, @nappendixIdOffset, @docIdOffset, masIdOffset);

  END $$
DELIMITER ;