|
@@ -65,7 +65,12 @@ class OrderService
|
|
|
);
|
|
|
|
|
|
// 需要加载关联数据
|
|
|
- $items = $paginator->items();
|
|
|
+ $items = collect($paginator->items())->map(function ($order) {
|
|
|
+ $order->type_text = OrderType::from($order->type)->label();
|
|
|
+ $order->state_text = OrderStatus::from($order->state)->label();
|
|
|
+
|
|
|
+ return $order;
|
|
|
+ });
|
|
|
$query->with(['project']); // 加载项目信息以便返回project_name等字段
|
|
|
|
|
|
return [
|
|
@@ -568,4 +573,157 @@ class OrderService
|
|
|
}
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 技师出发
|
|
|
+ *
|
|
|
+ * @param int $userId 技师用户ID
|
|
|
+ * @param int $orderId 订单ID
|
|
|
+ */
|
|
|
+ public function depart(int $userId, int $orderId): array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ return DB::transaction(function () use ($userId, $orderId) {
|
|
|
+
|
|
|
+ // 获取技师信息
|
|
|
+ $user = MemberUser::with(['coach'])->findOrFail($userId);
|
|
|
+
|
|
|
+ // 获取订单信息
|
|
|
+ $order = Order::query()->where('id', $orderId)->lockForUpdate()->first();
|
|
|
+
|
|
|
+ // 检查订单是否存在
|
|
|
+ abort_if(! $order, 404, '订单不存在');
|
|
|
+
|
|
|
+ // 检查是否是该技师的订单
|
|
|
+ abort_if($order->coach_id !== $user->coach->id, 403, '无权操作此订单');
|
|
|
+
|
|
|
+ // 检查订单状态是否为已分配技师
|
|
|
+ abort_if($order->status !== OrderRecordStatus::ASSIGNED->value, 400, '订单状态不正确');
|
|
|
+
|
|
|
+ // 更新订单状态为技师出发
|
|
|
+ $order->status = OrderStatus::DEPARTED->value;
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ // 记录订单状态变更日志
|
|
|
+ OrderRecord::create([
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'status' => OrderRecordStatus::DEPARTED->value,
|
|
|
+ 'operator_id' => $user->coach->id,
|
|
|
+ 'operator_type' => CoachUser::class,
|
|
|
+ 'remark' => '技师已出发',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 发送通知给用户
|
|
|
+ // TODO: 发送通知
|
|
|
+ // event(new TechnicianDepartedEvent($order));
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'status' => true,
|
|
|
+ 'message' => '操作成功',
|
|
|
+ 'data' => [
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'status' => $order->status,
|
|
|
+ 'created_at' => $order->created_at,
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ });
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ \Log::error('技师出发失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 技师到达
|
|
|
+ *
|
|
|
+ * @param int $userId 技师用户ID
|
|
|
+ * @param int $orderId 订单ID
|
|
|
+ */
|
|
|
+ public function arrive(int $userId, int $orderId): array
|
|
|
+ {
|
|
|
+ return DB::transaction(function () use ($userId, $orderId) {
|
|
|
+ try {
|
|
|
+ // 获取技师信息
|
|
|
+ $user = MemberUser::with(['coach'])->findOrFail($userId);
|
|
|
+ $coach = $user->coach;
|
|
|
+ abort_if(! $coach, 404, '技师信息不存在');
|
|
|
+
|
|
|
+ // 获取订单信息
|
|
|
+ $order = Order::query()->where('id', $orderId)->lockForUpdate()->first();
|
|
|
+ abort_if(! $order, 404, '订单不存在');
|
|
|
+
|
|
|
+ // 检查是否是该技师的订单
|
|
|
+ abort_if($order->coach_id !== $coach->id, 403, '无权操作此订单');
|
|
|
+ dd(in_array($order->status, [
|
|
|
+ OrderStatus::DEPARTED->value,
|
|
|
+ ]));
|
|
|
+ // 检查订单状态
|
|
|
+ abort_if(! in_array($order->status, [
|
|
|
+ OrderStatus::DEPARTED->value,
|
|
|
+ ]), 400, '订单状态不正确');
|
|
|
+
|
|
|
+ $now = now();
|
|
|
+
|
|
|
+ // 更新订单状态为技师到达
|
|
|
+ $order->status = OrderRecordStatus::ARRIVED->value;
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ // 记录订单状态变更日志
|
|
|
+ OrderRecord::create([
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'status' => OrderRecordStatus::ARRIVED->value,
|
|
|
+ 'operator_id' => $coach->id,
|
|
|
+ 'operator_type' => CoachUser::class,
|
|
|
+ 'remark' => '技师已到达',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 更新技师当前位置到Redis GEO
|
|
|
+ try {
|
|
|
+ Redis::geoadd(
|
|
|
+ 'coach_locations',
|
|
|
+ $order->longitude,
|
|
|
+ $order->latitude,
|
|
|
+ $coach->id.'_'.TechnicianLocationType::CURRENT->value
|
|
|
+ );
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('更新技师位置失败', [
|
|
|
+ 'coach_id' => $coach->id,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO: 发送通知给用户
|
|
|
+ // event(new TechnicianArrivedEvent($order));
|
|
|
+
|
|
|
+ Log::info('技师到达成功', [
|
|
|
+ 'coach_id' => $coach->id,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'arrived_at' => $now,
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'status' => true,
|
|
|
+ 'message' => '操作成功',
|
|
|
+ 'data' => [
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'status' => $order->status,
|
|
|
+ 'arrived_at' => $now,
|
|
|
+ ],
|
|
|
+ ];
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('技师到达失败', [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'trace' => $e->getTraceAsString(),
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|