| @@ -0,0 +1,63 @@ | |||||
| CREATE DEFINER=`root`@`%` PROCEDURE `user_credit_clear`(IN `cutOffDate` VARCHAR(50),IN `finalTableIndex` VARCHAR(50)) | |||||
| BEGIN | |||||
| DECLARE done INT DEFAULT 0; | |||||
| DECLARE CONTINUE HANDLER FOR SQLEXCEPTION SET done = -1; | |||||
| select concat(finalTableIndex,'_',date_format(now(),'%Y%m%d%H%i')) into @tableSuffix; | |||||
| #START TRANSACTION; | |||||
| set @backUpSql1_0 = concat('CREATE TABLE `wx_credit_clear_md',@tableSuffix,'` (' | |||||
| ,'`id` bigint(20) NOT NULL,' | |||||
| ,'`user_id` bigint(20) DEFAULT NULL,' | |||||
| ,'`final_tenant_id` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,' | |||||
| ,'`credit` int(11) DEFAULT NULL COMMENT \'用户当前总积分\',' | |||||
| ,'`sum_credit` int(11) DEFAULT NULL COMMENT \'用户当前清零截止时间后总积分\',' | |||||
| ,'`credit_amount` int(11) DEFAULT NULL COMMENT \'用户清零扣减积分\',' | |||||
| ,'`cut_off_date` datetime(0) DEFAULT NULL COMMENT \'清零截止时间\',' | |||||
| ,'PRIMARY KEY (`id`) USING BTREE' | |||||
| ,') ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci ROW_FORMAT = Dynamic;'); | |||||
| PREPARE back_up_sql1_0 FROM @backUpSql1_0; | |||||
| EXECUTE back_up_sql1_0; | |||||
| DEALLOCATE PREPARE back_up_sql1_0; | |||||
| INSERT into zz_process_log VALUES(NOW(),'user_credit_clear()','创建积分清零数据处理备份表',@backUpSql1_0); | |||||
| set @backUpSql1_1 = concat('insert into `wx_credit_clear_md',@tableSuffix,'`' | |||||
| ,'(`id`, `user_id`, `final_tenant_id`, `credit`, `sum_credit`, `credit_amount`, `cut_off_date`)' | |||||
| ,' select (select unix_timestamp(now()) + CEILING(RAND()*90000+10000)) id,wcubi.id user_id,wcubi.final_tenant_id,wcubi.credit,ifnull(wch.sum_credit,0) sum_credit,(ifnull(wch.sum_credit,0)-wcubi.credit) credit_amount,\'',cutOffDate,'\'' | |||||
| ,' from wx_c_user_basic_info',finalTableIndex,' wcubi' | |||||
| ,' left join (select c_user_id,sum(credit_num) sum_credit from wx_credit_history',finalTableIndex,' where credit_num > 0 and create_date >= \'',cutOffDate,'\' group by c_user_id) wch on wch.c_user_id = wcubi.id' | |||||
| ,' where wcubi.credit > ifnull(wch.sum_credit,0);'); | |||||
| PREPARE back_up_sql1_1 FROM @backUpSql1_1; | |||||
| EXECUTE back_up_sql1_1; | |||||
| DEALLOCATE PREPARE back_up_sql1_1; | |||||
| INSERT into zz_process_log VALUES(NOW(),'user_credit_clear()','添加积分清零数据处理',@backUpSql1_1); | |||||
| set @backUpSql1_2 = concat('insert into `wx_credit_history',finalTableIndex,'`' | |||||
| ,'(`id`, `tenant_id`, `c_user_id`, `credit_amount`, `credit_num`, `credit_type`, `create_date`, `operator_type`, `operator_id`) ' | |||||
| ,'select id,final_tenant_id,user_id,sum_credit,credit_amount,14,now(),4,0' | |||||
| ,' from `wx_credit_clear_md',@tableSuffix,'`;'); | |||||
| PREPARE back_up_sql1_2 FROM @backUpSql1_2; | |||||
| EXECUTE back_up_sql1_2; | |||||
| DEALLOCATE PREPARE back_up_sql1_2; | |||||
| INSERT into zz_process_log VALUES(NOW(),'user_credit_clear()','添加积分历史',@backUpSql1_2); | |||||
| set @backUpSql1_3 = concat('update wx_c_user_basic_info',finalTableIndex,' wcubi ' | |||||
| ,'left join `wx_credit_clear_md',@tableSuffix,'` wccm on wcubi.id = wccm.user_id' | |||||
| ,' set wcubi.credit = (wcubi.credit + wccm.credit_amount), wcubi.update_date = now()' | |||||
| ,' where wccm.id is not null;'); | |||||
| PREPARE back_up_sql1_3 FROM @backUpSql1_3; | |||||
| EXECUTE back_up_sql1_3; | |||||
| DEALLOCATE PREPARE back_up_sql1_3; | |||||
| INSERT into zz_process_log VALUES(NOW(),'user_credit_clear()','修改用户积分',@backUpSql1_3); | |||||
| #IF done = -1 THEN | |||||
| # ROLLBACK; | |||||
| # ELSE | |||||
| # COMMIT; | |||||
| # END IF; | |||||
| END | |||||