ALTER TABLE `wx_offline_activity_item`
    ADD COLUMN `mode_type` smallint(1) NULL DEFAULT NULL COMMENT '活动项模式' AFTER `name`;

update wx_offline_activity_item set mode_type = 2 where type = 1;
update wx_offline_activity_item set mode_type = 2 where type = 2;

update wx_offline_activity_item set mode_type = calculation_mode where type = 3;
update wx_offline_activity_item set mode_type = 2 where type = 3 and mode_type is null;

ALTER TABLE `wx_offline_activity_item`
    MODIFY COLUMN `mode_type` smallint(1) NOT NULL COMMENT '活动项模式' AFTER `name`;

ALTER TABLE `wx_offline_activity_item`
    DROP COLUMN `calculation_mode`;

ALTER TABLE `wx_offline_activity_item`
    ADD COLUMN `business_order_count` int(11) NULL COMMENT '业态订单限制' AFTER `awards_item_price`;

ALTER TABLE `wx_offline_activity_item`
    ADD COLUMN `count_model` smallint(1) NULL COMMENT '联单模式' AFTER `tax_points`;

update wx_offline_activity_item set count_model = 1 where type = 3
                                                      and id in(select distinct activity_item_id from wx_offline_activity_awards_item where `type` = 2);



DROP TABLE IF EXISTS `wx_offline_activity_awards_model`;
CREATE TABLE `wx_offline_activity_awards_model`  (
                                                     `id` bigint(20) NOT NULL COMMENT '主键ID',
                                                     `tenant_id` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '租户ID',
                                                     `parent_tenant_id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '父租户ID',
                                                     `activity_item_id` bigint(20) NOT NULL COMMENT '活动项id',
                                                     `model_type` smallint(1) NOT NULL COMMENT '模式类型',
                                                     `merchant_count` int(11) NULL DEFAULT NULL COMMENT '联单笔数',
                                                     `min_amount` decimal(10, 2) NULL DEFAULT NULL COMMENT '最小金额',
                                                     `max_amount` decimal(10, 2) NULL DEFAULT NULL COMMENT '最大金额',
                                                     `awards_count` int(11) NULL DEFAULT NULL COMMENT '奖项数量',
                                                     `awards_grade` int(11) NULL DEFAULT NULL COMMENT '奖项等级',
                                                     `create_date` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                                     `update_date` datetime NULL DEFAULT CURRENT_TIMESTAMP COMMENT '更新时间',
                                                     PRIMARY KEY (`id`) USING BTREE,
                                                     INDEX `activity_item_id`(`activity_item_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '线下活动奖项模式' ROW_FORMAT = DYNAMIC;


insert into `wx_offline_activity_awards_model`(`id`,`tenant_id`,`parent_tenant_id`,`activity_item_id`,`model_type`,`merchant_count`,`min_amount`,`max_amount`,`awards_count`,`awards_grade`,`create_date`,`update_date`)
select id,tenant_id,parent_tenant_id,activity_item_id,type,null,min_amount,max_amount,null,grade,create_date,update_date from wx_offline_activity_awards_item
where activity_item_id in (select id from wx_offline_activity_item where type = 2);


DROP TABLE IF EXISTS `wx_offline_activity_grant_item_record`;
CREATE TABLE `wx_offline_activity_grant_item_record`  (
                                                          `tenant_id` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '租户ID',
                                                          `parent_tenant_id` varchar(10) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '父租户ID',
                                                          `activity_id` bigint(20) NOT NULL COMMENT '活动id',
                                                          `activity_item_id` bigint(20) NOT NULL COMMENT '活动项id',
                                                          `record_id` bigint(20) NOT NULL COMMENT '参与记录id',
                                                          `grant_id` bigint(20) NOT NULL COMMENT '发放id',
                                                          INDEX `activity_id`(`activity_id`, `record_id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci COMMENT = '活动发放筛选订单金额关系表' ROW_FORMAT = DYNAMIC;


ALTER TABLE `wx_offline_activity_grant_cash_back`
    DROP COLUMN `calculation_mode`;


ALTER TABLE `wx_offline_activity_grant_item`
    ADD COLUMN `grant_awards_mode` smallint(1) NULL COMMENT '发放奖励模式' AFTER `grant_id`;

update wx_offline_activity_grant_item set grant_awards_mode = 2
                                      where award_itam_id in (select id from wx_offline_activity_awards_item where `type` = 2);
update wx_offline_activity_grant_item set grant_awards_mode = 1 where grant_awards_mode is null ;

ALTER TABLE `wx_offline_activity_grant_item`
    MODIFY COLUMN `grant_awards_mode` smallint(1) NOT NULL COMMENT '发放奖励模式' AFTER `grant_id`;


ALTER TABLE `wx_offline_activity_grant_settlement`
    ADD COLUMN `grant_item_id` bigint(20) NULL COMMENT '发放奖励记录id' AFTER `grant_id`,
    ADD COLUMN `grant_awards_mode` smallint(20) NULL COMMENT '发放奖励模式' AFTER `grant_item_id`;

update wx_offline_activity_grant_settlement gs
    left join wx_offline_activity_grant_item gi
    on gs.grant_id = gi.grant_id and gs.activity_item_id = gi.activity_item_id and gs.award_itam_id = gi.award_itam_id
    set gs.grant_item_id = gi.id,gs.grant_awards_mode = gi.grant_awards_mode
    where gi.record_id is null;

update wx_offline_activity_grant_settlement gs
    left join wx_offline_activity_grant_item gi
    on gs.grant_id = gi.grant_id and gs.activity_item_id = gi.activity_item_id and gs.award_itam_id = gi.award_itam_id and gs.record_id = gi.record_id
    set gs.grant_item_id = gi.id,gs.grant_awards_mode = gi.grant_awards_mode
    where gi.record_id is not null;

ALTER TABLE `wx_offline_activity_grant_settlement`
    MODIFY COLUMN `grant_item_id` bigint(20) NOT NULL COMMENT '发放奖励记录id' AFTER `grant_id`,
    MODIFY COLUMN `grant_awards_mode` smallint(20) NOT NULL COMMENT '发放奖励模式' AFTER `grant_item_id`;






