| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace App\Module\Transfer\AdminControllers\Tools;
- use App\Module\Transfer\Logics\TransferLogic;
- use App\Module\Transfer\Models\TransferOrder;
- use Dcat\Admin\Grid\BatchAction;
- use Illuminate\Http\Request;
- /**
- * 重试订单工具
- */
- class RetryOrderTool extends BatchAction
- {
- protected $title = '重试订单';
- /**
- * 处理批量操作
- */
- public function handle(Request $request)
- {
- $orderIds = $this->getKey();
-
- if (empty($orderIds)) {
- return $this->response()->error('请选择要重试的订单');
- }
- $successCount = 0;
- $failCount = 0;
- $errors = [];
- foreach ($orderIds as $orderId) {
- try {
- $order = TransferOrder::find($orderId);
-
- if (!$order) {
- $errors[] = "订单 {$orderId} 不存在";
- $failCount++;
- continue;
- }
- if (!$order->canRetry()) {
- $errors[] = "订单 {$orderId} 状态不允许重试";
- $failCount++;
- continue;
- }
- $result = TransferLogic::retryOrder($orderId);
-
- if ($result) {
- $successCount++;
- } else {
- $errors[] = "订单 {$orderId} 重试失败";
- $failCount++;
- }
- } catch (\Exception $e) {
- $errors[] = "订单 {$orderId} 重试异常: " . $e->getMessage();
- $failCount++;
- }
- }
- // 构建响应消息
- $message = "重试完成:成功 {$successCount} 个,失败 {$failCount} 个";
-
- if (!empty($errors)) {
- $message .= "\n错误详情:\n" . implode("\n", array_slice($errors, 0, 5));
- if (count($errors) > 5) {
- $message .= "\n...还有 " . (count($errors) - 5) . " 个错误";
- }
- }
- if ($failCount > 0) {
- return $this->response()->warning($message);
- } else {
- return $this->response()->success($message);
- }
- }
- /**
- * 确认对话框
- */
- public function confirm()
- {
- return [
- '确认重试选中的订单吗?',
- '重试操作将重新处理失败的订单,请确认操作。',
- ];
- }
- /**
- * 设置按钮样式
- */
- public function html()
- {
- return <<<HTML
- <a class="btn btn-sm btn-warning" style="margin-right: 5px;">
- <i class="fa fa-refresh"></i> {$this->title}
- </a>
- HTML;
- }
- }
|