RetryOrderTool.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Module\Transfer\AdminControllers\Tools;
  3. use App\Module\Transfer\Logics\TransferLogic;
  4. use App\Module\Transfer\Models\TransferOrder;
  5. use Dcat\Admin\Grid\BatchAction;
  6. use Illuminate\Http\Request;
  7. /**
  8. * 重试订单工具
  9. */
  10. class RetryOrderTool extends BatchAction
  11. {
  12. protected $title = '重试订单';
  13. /**
  14. * 处理批量操作
  15. */
  16. public function handle(Request $request)
  17. {
  18. $orderIds = $this->getKey();
  19. if (empty($orderIds)) {
  20. return $this->response()->error('请选择要重试的订单');
  21. }
  22. $successCount = 0;
  23. $failCount = 0;
  24. $errors = [];
  25. foreach ($orderIds as $orderId) {
  26. try {
  27. $order = TransferOrder::find($orderId);
  28. if (!$order) {
  29. $errors[] = "订单 {$orderId} 不存在";
  30. $failCount++;
  31. continue;
  32. }
  33. if (!$order->canRetry()) {
  34. $errors[] = "订单 {$orderId} 状态不允许重试";
  35. $failCount++;
  36. continue;
  37. }
  38. $result = TransferLogic::retryOrder($orderId);
  39. if ($result) {
  40. $successCount++;
  41. } else {
  42. $errors[] = "订单 {$orderId} 重试失败";
  43. $failCount++;
  44. }
  45. } catch (\Exception $e) {
  46. $errors[] = "订单 {$orderId} 重试异常: " . $e->getMessage();
  47. $failCount++;
  48. }
  49. }
  50. // 构建响应消息
  51. $message = "重试完成:成功 {$successCount} 个,失败 {$failCount} 个";
  52. if (!empty($errors)) {
  53. $message .= "\n错误详情:\n" . implode("\n", array_slice($errors, 0, 5));
  54. if (count($errors) > 5) {
  55. $message .= "\n...还有 " . (count($errors) - 5) . " 个错误";
  56. }
  57. }
  58. if ($failCount > 0) {
  59. return $this->response()->warning($message);
  60. } else {
  61. return $this->response()->success($message);
  62. }
  63. }
  64. /**
  65. * 确认对话框
  66. */
  67. public function confirm()
  68. {
  69. return [
  70. '确认重试选中的订单吗?',
  71. '重试操作将重新处理失败的订单,请确认操作。',
  72. ];
  73. }
  74. /**
  75. * 设置按钮样式
  76. */
  77. public function html()
  78. {
  79. return <<<HTML
  80. <a class="btn btn-sm btn-warning" style="margin-right: 5px;">
  81. <i class="fa fa-refresh"></i> {$this->title}
  82. </a>
  83. HTML;
  84. }
  85. }