Przeglądaj źródła

API-自动取消订单-完善自动取消订单功能异常处理

sunxiaobiao 5 miesięcy temu
rodzic
commit
d5f7f4a1a7

+ 25 - 12
app/common/repositories/store/order/StoreGroupOrderRepository.php

@@ -178,20 +178,33 @@ class StoreGroupOrderRepository extends BaseRepository
178 178
         }
179 179
 
180 180
         // 3、逻辑处理
181
-        Db::startTrans();
182 181
         try {
183
-            // 软 删除订单组
184
-            $storeGroupOrderEntity->setIsDel(StoreGroupOrderEnum::IS_DEL['Yes']['code']);
185
-            $this->update($storeGroupOrderEntity->getGroupOrderId(), $storeGroupOrderEntity->toUnderlineArray());
186
-
187
-            // 循环关闭订单组内的数据
188
-            foreach ($storeOrderEntityList as $storeOrderEntity) {
189
-                $storeOrderRepository->closeStoreOrder($storeOrderEntity, $uid, $changeMessage, $remark);
190
-            }
182
+            Db::transaction(function () use (
183
+                $storeOrderRepository,
184
+                $storeGroupOrderEntity,
185
+                $storeOrderEntityList,
186
+                $uid,
187
+                $changeMessage,
188
+                $remark
189
+            ) {
190
+                // 软 删除订单组
191
+                $storeGroupOrderEntity->setIsDel(StoreGroupOrderEnum::IS_DEL['Yes']['code']);
192
+                $this->update($storeGroupOrderEntity->getGroupOrderId(), $storeGroupOrderEntity->toUnderlineArray());
191 193
 
192
-            Db::commit();
193
-        } catch (DbException $e) {
194
-            Db::rollback();
194
+                // 循环关闭订单组内的数据
195
+                foreach ($storeOrderEntityList as $storeOrderEntity) {
196
+                    $storeOrderRepository->closeStoreOrder($storeOrderEntity, $uid, $changeMessage, $remark);
197
+                }
198
+            });
199
+        } catch (\Throwable $e) {
200
+            throw new ValidateException('关闭订单失败:' . json_encode([
201
+                    'error_message' => $e->getMessage(),
202
+                    'error_file' => $e->getFile(),
203
+                    'error_line' => $e->getLine(),
204
+                    'error_code' => $e->getCode(),
205
+                    'exception_class' => get_class($e),
206
+                    'trace' => $e->getTraceAsString()
207
+                ], JSON_UNESCAPED_UNICODE));
195 208
         }
196 209
 
197 210
         // $groupOrder = $this->search(['paid' => 0, 'uid' => $uid ?: ''])->where('group_order_id', $id)->with(['orderList'])->find();

+ 9 - 2
app/common/repositories/store/order/StoreOrderRepository.php

@@ -12313,8 +12313,15 @@ class StoreOrderRepository extends BaseRepository
12313 12313
 
12314 12314
             // 5、撤回订单扣除的数据项
12315 12315
             $this->cancellation($userEntity, $storeOrderEntity, $fzonePayTypeEntity);
12316
-        } catch (DbException $e) {
12317
-            throw new ValidateException('支付失败,支付方式错误!');
12316
+        } catch (\Throwable $e) {
12317
+            throw new ValidateException('关闭订单失败:' . json_encode([
12318
+                    'error_message' => $e->getMessage(),
12319
+                    'error_file' => $e->getFile(),
12320
+                    'error_line' => $e->getLine(),
12321
+                    'error_code' => $e->getCode(),
12322
+                    'exception_class' => get_class($e),
12323
+                    'trace' => $e->getTraceAsString()
12324
+                ], JSON_UNESCAPED_UNICODE));
12318 12325
         }
12319 12326
     }
12320 12327
 

+ 19 - 2
app/controller/merchant/store/order/Order.php

@@ -20,6 +20,7 @@ use think\App;
20 20
 use crmeb\basic\BaseController;
21 21
 use app\common\repositories\store\order\StoreOrderRepository as repository;
22 22
 use think\facade\Db;
23
+use think\facade\Log;
23 24
 
24 25
 class Order extends BaseController
25 26
 {
@@ -439,18 +440,34 @@ class Order extends BaseController
439 440
     /**
440 441
      * @param $id
441 442
      * @param StoreGroupOrderRepository $groupOrderRepository
443
+     * @param repository $orderRepository
442 444
      * @return mixed
445
+     * @throws \think\db\exception\DataNotFoundException
446
+     * @throws \think\db\exception\DbException
447
+     * @throws \think\db\exception\ModelNotFoundException
443 448
      * @author xaboy
444 449
      * @day 2020/6/10
445 450
      */
446 451
     public function cancelGroupOrder($id, StoreGroupOrderRepository $groupOrderRepository, StoreOrderRepository $orderRepository)
447 452
     {
448
-        $data = $this->request->params(['change_message','remark']);
453
+        $data = $this->request->params(['change_message', 'remark']);
449 454
         $merId = $this->request->merId();
450 455
         $order = $orderRepository->getWhere(['group_order_id' => $id]);
451 456
         if ($order['mer_id'] != $merId)
452 457
             return app('json')->fail('数据不存在');
453
-        $groupOrderRepository->cancel((int)$id, 0, $data['change_message'], $data['remark']);
458
+        try {
459
+            $groupOrderRepository->cancel((int)$id, 0, $data['change_message'], $data['remark']);
460
+        } catch (\Throwable $e) {
461
+            Log::error('关闭订单失败:' . json_encode([
462
+                    'error_message' => $e->getMessage(),
463
+                    'error_file' => $e->getFile(),
464
+                    'error_line' => $e->getLine(),
465
+                    'error_code' => $e->getCode(),
466
+                    'exception_class' => get_class($e),
467
+                    'trace' => $e->getTraceAsString()
468
+                ], JSON_UNESCAPED_UNICODE));
469
+            return app('json')->fail('关闭订单失败');
470
+        }
454 471
         return app('json')->success('取消成功');
455 472
     }
456 473
 

+ 1 - 0
crmeb/listens/AutoCancelGroupOrderListen.php

@@ -22,6 +22,7 @@ class AutoCancelGroupOrderListen implements ListenerInterface
22 22
 
23 23
     public function handle($event): void
24 24
     {
25
+        /** @var StoreGroupOrderRepository $storeGroupOrderRepository */
25 26
         $storeGroupOrderRepository = app()->make(StoreGroupOrderRepository::class);
26 27
         Timer::tick(60000, function () use ($storeGroupOrderRepository) {
27 28
             $timer = ((int)systemConfig('auto_close_order_timer')) ?: 15;