DROP PROCEDURE IF EXISTS update_data_relation_appendix;
DELIMITER $$
CREATE PROCEDURE update_data_relation_appendix(IN n INT,
                                               IN mas_url VARCHAR(200),
                                               IN prevTableInfoId BIGINT(20),
                                               IN prevDocId BIGINT(20))
	BEGIN

    SET @citySuffix = concat('_city', n);

    SET @wcmdocument_offset_num = ifnull((SELECT max(offset_num) FROM data_migration_offset WHERE table_name = 'wcmdocument'), 0);
    SET @docIdOffset = @wcmdocument_offset_num;

    SET @appendix_offset_num = ifnull((SELECT max(offset_num) FROM data_migration_offset WHERE table_name = 'xwcmmaterial'), 0);
    SET @appendixIdOffset = @appendix_offset_num;

    call update_data_relation_appendix_1(prevDocId, @appendixIdOffset, @docIdOffset);

    call update_data_relation_appendix_2(@citySuffix, prevTableInfoId, prevDocId);

    UPDATE trs_hycloud_iip.wcmappendix SET APPFILE = CONCAT(mas_url, AppFromId) WHERE APPFLAG = 90 AND APPENDIXID > @appendixIdOffset;

  END $$
DELIMITER ;