|
@@ -3,15 +3,18 @@
|
|
|
namespace App\Services\Coach;
|
|
|
|
|
|
use App\Enums\OrderGrabRecordStatus;
|
|
|
+use App\Enums\OrderRecordStatus;
|
|
|
use App\Enums\OrderStatus;
|
|
|
use App\Enums\OrderType;
|
|
|
use App\Enums\ProjectStatus;
|
|
|
use App\Enums\TechnicianAuthStatus;
|
|
|
use App\Enums\TechnicianLocationType;
|
|
|
use App\Enums\TechnicianStatus;
|
|
|
+use App\Models\CoachUser;
|
|
|
use App\Models\MemberUser;
|
|
|
use App\Models\Order;
|
|
|
use App\Models\OrderGrabRecord;
|
|
|
+use App\Models\OrderRecord;
|
|
|
use App\Services\SettingItemService;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
@@ -467,4 +470,102 @@ class OrderService
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 技师拒单
|
|
|
+ *
|
|
|
+ * @param int $userId 用户ID
|
|
|
+ * @param int $orderId 订单ID
|
|
|
+ * @param string $reason 拒单原因
|
|
|
+ */
|
|
|
+ public function rejectOrder(int $userId, int $orderId, string $reason): array
|
|
|
+ {
|
|
|
+ return DB::transaction(function () use ($userId, $orderId, $reason) {
|
|
|
+ try {
|
|
|
+ // 获取技师信息(优化关联加载)
|
|
|
+ $user = MemberUser::with([
|
|
|
+ 'coach',
|
|
|
+ 'coach.info',
|
|
|
+ 'coach.real',
|
|
|
+ 'coach.qual',
|
|
|
+ ])->findOrFail($userId);
|
|
|
+
|
|
|
+ // 验证技师信息
|
|
|
+ [$coach, $location] = $this->validateCoach($user);
|
|
|
+
|
|
|
+ // 获取订单信息并加锁
|
|
|
+ $order = Order::lockForUpdate()->findOrFail($orderId);
|
|
|
+
|
|
|
+ // 验证订单状态(修正状态判断)
|
|
|
+ abort_if(! in_array($order->state, [
|
|
|
+ OrderStatus::ASSIGNED->value,
|
|
|
+ OrderStatus::PAID->value,
|
|
|
+ ]), 400, '订单状态异常,无法拒单');
|
|
|
+
|
|
|
+ // 验证订单是否分配给该技师
|
|
|
+ abort_if($order->coach_id !== $coach->id, 403, '该订单未分配给您');
|
|
|
+
|
|
|
+ // 检查拒单次数限制
|
|
|
+ $rejectCount = OrderRecord::where('object_id', $coach->id)
|
|
|
+ ->where('object_type', CoachUser::class)
|
|
|
+ ->where('state', OrderRecordStatus::REJECTED->value)
|
|
|
+ ->whereDate('created_at', today())
|
|
|
+ ->count();
|
|
|
+
|
|
|
+ // 更新订单状态
|
|
|
+ $order->update([
|
|
|
+ 'state' => OrderStatus::REJECTED->value,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 创建订单记录
|
|
|
+ OrderRecord::create([
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'object_id' => $coach->id,
|
|
|
+ 'object_type' => CoachUser::class,
|
|
|
+ 'state' => OrderRecordStatus::REJECTED->value,
|
|
|
+ 'remark' => $reason,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 发送消息通知
|
|
|
+ try {
|
|
|
+ // event(new OrderRejectedEvent($order, $coach, $reason));
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('发送拒单通知失败', [
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'coach_id' => $coach->id,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 记录日志
|
|
|
+ Log::info('技师拒单成功', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'coach_id' => $coach->id,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'order_no' => $order->order_no,
|
|
|
+ 'reason' => $reason,
|
|
|
+ 'reject_count' => $rejectCount + 1,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'message' => '拒单成功',
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'order_no' => $order->order_no,
|
|
|
+ 'reject_count' => $rejectCount + 1,
|
|
|
+ 'max_reject_count' => 5,
|
|
|
+ ];
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('技师拒单失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'reason' => $reason,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'file' => $e->getFile(),
|
|
|
+ 'line' => $e->getLine(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|