# 看注释！！！！！！

# 将redis中优惠券缓存(tmerclub_coupon目录下的缓存)删除后执行下列操作

# 将nacos中tmerclub-marketing.yml的配置清空，然后重新配置数据库连接参数，注意检查连接配置，重新发布保存
# spring:
#   datasource:
#     url: jdbc:mysql://${MYSQL_HOST:192.168.193.128}:${MYSQL_PORT:3306}/${MYSQL_DATABASE:tmerclub_marketing}?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8&allowMultiQueries=true&allowPublicKeyRetrieval=true&useAffectedRows=true
#     username: ${MYSQL_USERNAME:root}
#     password: ${MYSQL_PASSWORD:hn02le.34lkdLKD}
#   data:
#     mongodb:
#       host: 192.168.193.128
#       port: 27017
#       database: tmerclub
#       username: tmerclub
#       password: tmerclub
#       authentication-database: tmerclub
# redis:
#   multi:
#     enable: true
#     password: hn02le.34lkdLKD
#     database: 0
#     redisAddrs:
#       - 192.168.193.128:6379


# initCouponUserToMongodb,这个定时任务只对2023-12-04更新前将coupon_user存入mysql之后，将数据搬迁到mongodb
# 该定时任务正常执行*成功*一次后(数据已经完全搬迁到mongodb)就可以置为停止或删除
# 随后可以将mysql中的coupon_user删除
# 并且可以将CouponUserMapper以及相关定时任务（initCouponUserToMongodb）代码从代码中删去
USE `tmerclub_job`;
insert into `xxl_job_info`(`job_group`, `job_desc`, `add_time`, `update_time`, `author`, `alarm_email`, `schedule_type`,
                           `schedule_conf`, `misfire_strategy`, `executor_route_strategy`, `executor_handler`,
                           `executor_param`, `executor_block_strategy`, `executor_timeout`, `executor_fail_retry_count`,
                           `glue_type`, `glue_source`, `glue_remark`, `glue_updatetime`, `child_jobid`,
                           `trigger_status`, `trigger_last_time`, `trigger_next_time`)
values (5, '初始化原有领券数据库记录至mongodb', '2023-11-27 10:07:30', '2023-11-27 10:07:30', 'admin', '', 'CRON',
        '0 0 0 1 1 ? *', 'DO_NOTHING', 'FIRST', 'initCouponUserToMongodb', '', 'SERIAL_EXECUTION', 0, 0, 'BEAN',
        '', 'GLUE代码初始化', '2023-11-06 10:07:30', '', 0, 0, 0),
       (5, '解锁优惠券', '2023-11-27 10:07:30', '2023-11-27 10:07:30', 'admin', '', 'CRON',
        '0 5 0 * * ?', 'DO_NOTHING', 'FIRST', 'unLockCouponStock', '', 'SERIAL_EXECUTION', 0, 0, 'BEAN',
        '', 'GLUE代码初始化', '2023-11-06 10:07:30', '', 0, 0, 0);
# 将原本的优惠券库存落库定时任务删除
delete from `xxl_job_info` where `executor_handler` = 'hotCouponStockPlacement';

# 添加领券id，这里max_id调高是防止与之前的id重复，可以参考现有最高的coupon_user_id来限定该值
USE `tmerclub_marketing`;
insert  into `leaf_alloc`(`biz_tag`,`max_id`,`step`,`description`) values
('tmerclub-coupon-user',1000000,1000,'领券记录id');


# 订单支付记录添加字段
USE tmerclub_payment_0;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;

USE tmerclub_payment_1;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;

USE tmerclub_payment_2;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;

USE tmerclub_payment_3;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;


USE tmerclub_payment_4;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;

USE tmerclub_payment_5;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;

USE tmerclub_payment_6;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;

USE tmerclub_payment_7;
DELIMITER //
DROP PROCEDURE IF EXISTS updatePayInfoColumn //
CREATE PROCEDURE updatePayInfoColumn()
BEGIN
    DECLARE i INT;
    SET i = 0;
    WHILE i < 64 DO
        SET @add_column = CONCAT( 'ALTER TABLE pay_info_', i, " ADD COLUMN `biz_user_id` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '第三方系统userid'" );
PREPARE add_column FROM @add_column;
EXECUTE add_column;
SET i = i + 1;
END WHILE;
END //
CALL updatePayInfoColumn() //
DROP  PROCEDURE updatePayInfoColumn //
DELIMITER;