| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- <?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 ManualCompleteTool extends BatchAction
- {
- protected $title = '手动完成';
- /**
- * 处理批量操作
- */
- public function handle(Request $request)
- {
- $orderIds = $this->getKey();
- $remark = $request->input('remark', '管理员手动完成');
-
- 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->isFinalStatus()) {
- $errors[] = "订单 {$orderId} 已处于最终状态";
- $failCount++;
- continue;
- }
- // 只允许转出订单手动完成
- if (!$order->isTransferOut()) {
- $errors[] = "订单 {$orderId} 不是转出订单,不允许手动完成";
- $failCount++;
- continue;
- }
- $result = TransferLogic::manualComplete($orderId, $remark);
-
- 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 form()
- {
- $this->textarea('remark', '备注说明')
- ->default('管理员手动完成')
- ->required()
- ->help('请输入手动完成的原因');
- }
- /**
- * 确认对话框
- */
- public function confirm()
- {
- return [
- '确认手动完成选中的订单吗?',
- '手动完成操作将直接将订单状态设置为已完成,请谨慎操作。',
- ];
- }
- /**
- * 设置按钮样式
- */
- public function html()
- {
- return <<<HTML
- <a class="btn btn-sm btn-success" style="margin-right: 5px;">
- <i class="fa fa-check"></i> {$this->title}
- </a>
- HTML;
- }
- }
|