CREATE PROCEDURE `pro_cancel_order`( IN orderId INT , IN userId INT , OUT resultStatus INT ) COMMENT 'Cancel item order' BEGIN ######Parameter Description####### #orderId order id #extras Extended Records -- exception handling DECLARE CONTINUE HANDLER FOR 1062 SET resultStatus=500;-- exception SET resultStatus=0; START TRANSACTION; #declare transaction start #Modify the product order form #UPDATE `tos_goods_order` SET `order_status` =-1,`cancel_status` =2,`deleted` =1,`update_time` =now() UPDATE `tos_goods_order` SET `order_status` =-1,`cancel_status` =2,`update_time` =now() WHERE `id` = orderId; #Modify the order item table UPDATE `tos_goods_order_goods` SET `deleted` =1,`update_time` =now() WHERE `order_id` = orderId; #Order log table insert record INSERT INTO `tos_goods_order_log`(`order_id` ,`user_id` ,`old_status` ,`new_status` , `remark`,`create_time` ) VALUES(orderId,userId,1,-1,'Cancel order',now()); SET resultStatus=200;-- Order canceled successfully COMMIT; # rollback IF resultStatus=500 THEN ROLLBACK; ELSE COMMIT; END IF; SELECT resultStatus;#output parameters END
Stored procedure traversal:
CREATE PROCEDURE `pro_time_cancel_order`() COMMENT 'Timely cancellation of product orders' BEGIN ########TODO You need to query all unpaid orders over 15 minutes and cancel them ############ DECLARE orderId INT;# order id DECLARE userId INT;# user id DECLARE goodsId INT;# product id DECLARE goodsNum INT;# number of goods ## Traverse the data end flag DECLARE done INT DEFAULT FALSE; ##Declaring the cursor## DECLARE cur_order CURSOR FOR SELECT g.`id`,g.`user_id`,g1.`goods_id`,g1.`num` FROM `tos_goods_order` g JOIN `tos_goods_order_goods` g1 ON(g.`id`=g1.`order_id`) WHERE g.`order_status`=1 AND g.`deleted`=0 AND NOW()>DATE_ADD(g.`create_time`,INTERVAL 15 MINUTE); ##Exception handling DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE; ##Open the cursor OPEN cur_order; ##assignment FETCH NEXT FROM cur_order INTO orderId,userId,goodsId,goodsNum; #traverse REPEAT IF NOT Done THEN ##Release commodity inventory UPDATE `tos_goods` SET `store_count` = `store_count` +goodsNum WHERE `id`=goodsId; #Modify the product order form UPDATE `tos_goods_order` SET `order_status` =-1,`cancel_status` =2,`update_time` =now() WHERE `id` = orderId; #Order log table insert record INSERT INTO `tos_goods_order_log`(`order_id` ,`user_id` ,`old_status` ,`new_status` , `remark`,`create_time` ) VALUES(orderId,userId,1,-1,'Cancel order',now()); #Record cancellation order update time UPDATE `tos_sys_time_task` SET `update_time`=now() WHERE `id`=1; COMMIT; END IF; FETCH NEXT from cur_order INTO orderId,userId,goodsId,goodsNum; UNTIL Done END REPEAT; ##Close the cursor CLOSE cur_order; END
Events to execute the stored procedure periodically:
Check if event is enabled: show variables like '%sche%';
Turn on event scheduling: set global event_scheduler=1;
Close the event task: alter event e_test ON COMPLETION PRESERVE DISABLE;
Account opening event task: alter event e_test ON COMPLETION PRESERVE ENABLE;
drop event `tosuser`.`e_time_cancel_order`; CREATE EVENT `tosuser`.`e_time_cancel_order` ON SCHEDULE EVERY 60 SECOND STARTS '2017-06-02 15:00:53' ON COMPLETION PRESERVE ENABLE COMMENT 'Timely cancel order event' DO begin /**Timely cancel the product order**/ call pro_time_cancel_order(); end