Skip to content

Commit a330af3

Browse files
处理pending订单,返还库存的脚本的优化
1 parent d24b897 commit a330af3

File tree

6 files changed

+60
-16
lines changed

6 files changed

+60
-16
lines changed

app/console/modules/Order/controllers/ProductController.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ class ProductController extends Controller
2020
{
2121
public function actionReturnpendingstock()
2222
{
23-
Yii::$service->order->returnPendingStock();
23+
$logMessage = Yii::$service->order->returnPendingStock();
24+
foreach ($logMessage as $msg) {
25+
echo $msg."\n";
26+
}
2427
}
2528
}

config/services/Payment.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
'payment' => [
1010
'class' => 'fecshop\services\Payment',
1111
/*
12-
'noRelasePaymentMethod' => 'check_money', # 不需要释放库存的支付方式。譬如货到付款,在系统中
12+
'noRelasePaymentMethod' => ['check_money'], # 不需要释放库存的支付方式。譬如货到付款,在系统中
1313
# pending订单,如果一段时间未付款,会释放产品库存,但是货到付款类型的订单不会释放,
1414
# 如果需要释放产品库存,客服在后台取消订单即可释放产品库存。
1515
'paymentConfig' => [

services/Order.php

+49-10
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,7 @@ protected function actionCancel($increment_id = '')
690690
*/
691691
protected function actionReturnPendingStock()
692692
{
693+
$logMessage = [];
693694
$minute = $this->minuteBeforeThatReturnPendingStock;
694695
$begin_time = strtotime(date('Y-m-d H:i:s'). ' -'.$minute.' minutes ');
695696

@@ -702,8 +703,9 @@ protected function actionReturnPendingStock()
702703
['order_status' => $this->payment_status_pending],
703704
['if_is_return_stock' => 2],
704705
];
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];
707709
}
708710

709711
$filter = [
@@ -716,17 +718,54 @@ protected function actionReturnPendingStock()
716718
$data = $this->coll($filter);
717719
$coll = $data['coll'];
718720
$count = $data['count'];
719-
721+
$logMessage[] = 'order count: '.$count;
720722
if ($count > 0) {
721723
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+
}
729767
}
730768
}
769+
return $logMessage;
731770
}
732771
}

services/Payment.php

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Payment extends Service
2020
{
2121
public $paymentConfig;
2222
/**
23+
* Array
2324
* 不需要释放库存的支付方式。譬如货到付款,在系统中
2425
* pending订单,如果一段时间未付款,会释放产品库存,但是货到付款类型的订单不会释放,
2526
* 如果需要释放产品库存,客服在后台取消订单即可释放产品库存。

services/payment/Alipay.php

+1
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ public function receiveIpn()
231231
*/
232232
protected function paymentSuccess($increment_id,$trade_no,$sendEmail = true)
233233
{
234+
Yii::$service->store->currentLangCode = 'zh';
234235
if (!$this->_order) {
235236
$this->_order = Yii::$service->order->getByIncrementId($increment_id);
236237
Yii::$service->payment->setPaymentMethod($this->_order['payment_method']);

shell/urlRewrite.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ $Cur_Dir/../../../../yii product/search/initindex
55
# get now update timestamp.
66
nowtime=`$Cur_Dir/../../../../yii helper/urlrewrite/nowtime`
77

8-
###### 1.Sync Section : Sync Product Serach Collection
8+
# 1.Product 遍历所有产品,把product的url_key 写入到url rewrite
99
# get product all count.
1010
count=`$Cur_Dir/../../../../yii helper/urlrewrite/productcount`
1111
pagenum=`$Cur_Dir/../../../../yii helper/urlrewrite/productpagenum`
@@ -21,7 +21,7 @@ do
2121
done
2222

2323

24-
#Category
24+
# 2.Category 遍历所有的分类,把category的url_key 写入到 url rewrite
2525

2626
count=`$Cur_Dir/../../../../yii helper/urlrewrite/categorycount`
2727
pagenum=`$Cur_Dir/../../../../yii helper/urlrewrite/categorypagenum`
@@ -37,8 +37,8 @@ do
3737
done
3838

3939

40-
#delete all search data that sync_updated_at $gt $nowtime.
41-
$Cur_Dir/../../../../yii helper/urlrewrite/clearnoactive $nowtime
40+
# delete all search data that sync_updated_at $gt $nowtime.
41+
# $Cur_Dir/../../../../yii helper/urlrewrite/clearnoactive $nowtime
4242

4343
###### 1.Sync Section End
4444

0 commit comments

Comments
 (0)