@@ -690,6 +690,7 @@ protected function actionCancel($increment_id = '')
690
690
*/
691
691
protected function actionReturnPendingStock ()
692
692
{
693
+ $ logMessage = [];
693
694
$ minute = $ this ->minuteBeforeThatReturnPendingStock ;
694
695
$ begin_time = strtotime (date ('Y-m-d H:i:s ' ). ' - ' .$ minute .' minutes ' );
695
696
@@ -702,8 +703,9 @@ protected function actionReturnPendingStock()
702
703
['order_status ' => $ this ->payment_status_pending ],
703
704
['if_is_return_stock ' => 2 ],
704
705
];
705
- if ($ noRelasePaymentMethod ) {
706
- $ where [] = ['<> ' , 'payment_method ' , $ noRelasePaymentMethod ];
706
+ $ logMessage [] = 'order_updated_at: ' .$ begin_time ;
707
+ if (is_array ($ noRelasePaymentMethod ) && !empty ($ noRelasePaymentMethod )) {
708
+ $ where [] = ['not in ' , 'payment_method ' , $ noRelasePaymentMethod ];
707
709
}
708
710
709
711
$ filter = [
@@ -716,17 +718,54 @@ protected function actionReturnPendingStock()
716
718
$ data = $ this ->coll ($ filter );
717
719
$ coll = $ data ['coll ' ];
718
720
$ count = $ data ['count ' ];
719
-
721
+ $ logMessage [] = ' order count: ' . $ count ;
720
722
if ($ count > 0 ) {
721
723
foreach ($ coll as $ one ) {
722
- $ order_id = $ one ['order_id ' ];
723
- $ product_items = Yii::$ service ->order ->item ->getByOrderId ($ order_id , true );
724
- Yii::$ service ->product ->stock ->returnQty ($ product_items );
725
- $ one ->if_is_return_stock = 1 ;
726
- // 将订单取消掉。取消后的订单不能再次支付。
727
- $ one ->order_status = $ this ->payment_status_canceled ;
728
- $ one ->save ();
724
+ /**
725
+ * service严格上是不允许使用事务的,该方法特殊,是命令行执行的操作。
726
+ * 每一个循环是一个事务。
727
+ */
728
+ $ innerTransaction = Yii::$ app ->db ->beginTransaction ();
729
+ try {
730
+ $ logMessage [] = 'cancel order[begin] increment_id: ' .$ one ['increment_id ' ];
731
+ $ order_id = $ one ['order_id ' ];
732
+
733
+ $ updateComules = $ one ::updateAll (
734
+ [
735
+ 'if_is_return_stock ' => 1 ,
736
+ 'order_status ' => $ this ->payment_status_canceled ,
737
+ ]
738
+ ,
739
+ [
740
+ 'order_id ' => $ one ['order_id ' ],
741
+ 'order_status ' => $ this ->payment_status_pending ,
742
+ 'if_is_return_stock ' => 2
743
+ ]
744
+ );
745
+ /**
746
+ * 取消订单,只能操作一次,因此,我们在更新条件里面加上了order_id, order_status,if_is_return_stock
747
+ * 因为在上面查询和当前执行的时间之间,订单可能被进行其他操作,
748
+ * 如果被其他操作,更改了order_status,那么上面的更新行数就是0行。
749
+ * 那么事务直接回滚。
750
+ */
751
+ if (empty ($ updateComules )) {
752
+ $ innerTransaction ->rollBack ();
753
+ continue ;
754
+ } else {
755
+ $ product_items = Yii::$ service ->order ->item ->getByOrderId ($ order_id , true );
756
+ Yii::$ service ->product ->stock ->returnQty ($ product_items );
757
+ }
758
+ //$one->if_is_return_stock = 1;
759
+ // 将订单取消掉。取消后的订单不能再次支付。
760
+ //$one->order_status = $this->payment_status_canceled;
761
+ //$one->save();
762
+ $ innerTransaction ->commit ();
763
+ $ logMessage [] = 'cancel order[end] increment_id: ' .$ one ['increment_id ' ];
764
+ } catch (Exception $ e ) {
765
+ $ innerTransaction ->rollBack ();
766
+ }
729
767
}
730
768
}
769
+ return $ logMessage ;
731
770
}
732
771
}
0 commit comments