--加商户发卷菜单
INSERT INTO `mall_permission`(`id`, `name`, `parent_id`, `available`, `permission`, `resource_type`, `module_color`, `module_color_num`, `url`, `icon`, `version_type`, `sort`) VALUES (646, '商户建券  ', 602, 'Y', NULL, 1, NULL, NULL, 'commercialPreferential', NULL, 0, 646);

--销售类型加权限
update mall_sale_type set menus = '[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 50, 101, 102, 103, 104, 105, 106, 107, 108, 201, 202, 203, 204, 205, 206, 211, 212, 221, 222, 251, 299, 300, 301, 302, 303, 304, 305, 306, 401, 402, 403, 404, 405, 406, 407, 408, 409, 410, 411, 412, 413, 414, 415, 416, 417, 501, 502, 503, 504, 505, 506, 507, 508, 509, 511, 512, 513, 521, 522, 523, 531, 532, 533, 591, 592, 595, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 621, 622, 623, 624, 625, 626, 627, 641, 642, 643, 644, 645, 646, 651, 661, 662, 663, 671, 672, 673, 674, 675, 681, 682, 683, 684, 685, 686, 691, 692, 693, 694, 695, 701, 702, 703, 711, 712, 713, 901, 902, 903, 931, 932, 951, 952, 961, 962, 963, 965, 966, 967, 968, 1001, 1002, 1003, 1004]' where id=1;
update mall_sale_type set menus = '[1, 2, 4, 5, 6, 7, 8, 9, 10, 50, 101, 102, 103, 104, 105, 106, 107, 201, 202, 203, 204, 205, 211, 212, 221, 222, 251, 409, 410, 411, 416, 501, 502, 503, 504, 505, 506, 507, 508, 509, 511, 512, 513, 521, 522, 523, 531, 532, 533, 591, 592, 595, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 621, 622, 623, 624, 625, 626, 627, 641, 642, 643, 644, 645, 646, 651, 661, 662, 663, 671, 672, 673, 674, 675, 681, 682, 683, 684, 685, 686, 691, 692, 693, 694, 695, 701, 702, 703, 711, 712, 713, 901, 902, 903, 931, 932, 951, 961]' where id=2;
update mall_sale_type set menus = '[2, 4, 5, 6, 7, 10, 50, 201, 202, 205, 211, 212, 221, 222, 251, 409, 410, 411, 416, 501, 502, 503, 504, 505, 506, 507, 508, 511, 512, 513, 521, 522, 523, 531, 532, 533, 591, 592, 601, 602, 603, 604, 605, 606, 607, 608, 609, 610, 611, 612, 621, 622, 623, 624, 625, 626, 641, 642, 643, 644, 645, 646, 651, 661, 662, 663, 671, 672, 673, 674, 675, 681, 682, 683, 684, 685, 686, 691, 692, 693, 694, 695, 701, 702, 703, 711, 712, 713, 901, 902, 903, 931, 932]' where id=3;

--所有广场管理员角色加权限
INSERT INTO `mall_role_permission`(`id`, `tenant_id`, `parent_tenant_id`, `permission_id`, `role_id`)
 select  CEILING(RAND()*90000000000+10000000000) ,mui.tenant_id,mui.parent_tenant_id,646,mur.role_id
from mall_user_info mui
left join mall_user_role mur on mui.id = mur.uid
where mui.is_admin = 1 and mur.role_id is not null GROUP BY mur.role_id

ALTER TABLE `wx_coupon`
ADD COLUMN `source_type` tinyint(1) NOT NULL DEFAULT 1 COMMENT '1:A端发卷，2:B端发卷' AFTER `password_support`;

--用户
ALTER TABLE `wx_c_user_basic_info`
MODIFY COLUMN `tenant_id` varchar(250) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '租户ID' AFTER `id`;

update wx_c_user_basic_info set tenant_id = concat(",",final_tenant_id,",");



ALTER TABLE `wx_c_user`
MODIFY COLUMN `tenant_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '租户ID' AFTER `id`,
MODIFY COLUMN `parent_tenant_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '父租户ID' AFTER `tenant_id`,
MODIFY COLUMN `open_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '微信openId' AFTER `parent_tenant_id`,
MODIFY COLUMN `union_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT '微信unionId' AFTER `open_id`,
MODIFY COLUMN `user_id` bigint(20) DEFAULT NULL COMMENT '用户ID' AFTER `active_time`;



CREATE DEFINER=`root`@`%` PROCEDURE `move_after_group`(IN tenantIds VARCHAR(125),IN groupTenantId VARCHAR(125))
BEGIN

	DECLARE done INT DEFAULT 0;
	DECLARE phonestr VARCHAR(50);
	DECLARE phones CURSOR FOR	SELECT phone FROM `wx_c_user_basic_info` WHERE FIND_IN_SET(final_tenant_id,tenantIds)  GROUP BY phone HAVING count(1) > 1;

	DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET done = -1;
	DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

	START TRANSACTION;
		OPEN phones;
			FETCH phones INTO phonestr;
			while done<>1 DO
				BEGIN
				DECLARE done2 int DEFAULT 0;
				DECLARE userid BIGINT;
				DECLARE num INT DEFAULT 0;
				DECLARE oldCuserId BIGINT DEFAULT 0;
				DECLARE newCuserId BIGINT DEFAULT 0;

				DECLARE users CURSOR FOR    SELECT id FROM `wx_c_user_basic_info` WHERE phone = phonestr and FIND_IN_SET(final_tenant_id,tenantIds) order by active_time DESC;
				DECLARE CONTINUE HANDLER FOR NOT FOUND SET done2 = 1;

				OPEN users;
				FETCH users INTO userid;
				WHILE done2 <> 1 DO
				if num = 0 THEN
					SET oldCuserId =userid;
				ELSE
					SET newCuserId =userid;
          CALL cuser_old_to_new(oldCuserId,newCuserId);
				END IF;
				SET  num = num +1;

				FETCH users INTO userid;
				END WHILE;
				CLOSE users;
				END;

			FETCH phones INTO phonestr;
			END WHILE;
		CLOSE phones;

		UPDATE wx_c_user_basic_info SET final_tenant_id = groupTenantId WHERE FIND_IN_SET(final_tenant_id,tenantIds);
		UPDATE wx_c_user_tags SET tenant_id = groupTenantId WHERE FIND_IN_SET(tenant_id,tenantIds);
		CALL init_after_group(groupTenantId,tenantIds);

		IF done = -1 THEN
 			ROLLBACK;
    ELSE
      COMMIT;
    END IF;
END