|
@@ -2,6 +2,7 @@
|
|
|
|
|
|
namespace App\Services\Client;
|
|
|
|
|
|
+use App\Enums\ProjectStatus;
|
|
|
use App\Models\AgentConfig;
|
|
|
use App\Models\AgentInfo;
|
|
|
use App\Models\CoachConfig;
|
|
@@ -66,7 +67,6 @@ class OrderService
|
|
|
|
|
|
// 查询技师数据
|
|
|
$coach = $this->validateCoach($data['coach_id']);
|
|
|
-
|
|
|
// 获取项目详情
|
|
|
$project = $this->projectService->getProjectDetail($data['project_id'], $areaCode);
|
|
|
abort_if(! $project, 400, '项目不存在');
|
|
@@ -77,7 +77,10 @@ class OrderService
|
|
|
$address?->id ?? 0,
|
|
|
$data['coach_id'],
|
|
|
$data['project_id'],
|
|
|
- $project->agent_id
|
|
|
+ $project->agent_id,
|
|
|
+ false,
|
|
|
+ $data['latitude'],
|
|
|
+ $data['longitude']
|
|
|
);
|
|
|
|
|
|
return [
|
|
@@ -111,7 +114,7 @@ class OrderService
|
|
|
->firstOrFail();
|
|
|
|
|
|
$project = Project::where('id', $data['project_id'])
|
|
|
- ->where('state', 'enable')
|
|
|
+ ->where('state', ProjectStatus::OPEN->value())
|
|
|
->firstOrFail();
|
|
|
|
|
|
// 2. 订单类型判断
|
|
@@ -119,25 +122,27 @@ class OrderService
|
|
|
|
|
|
// 关键操作:验证必要参数
|
|
|
abort_if(empty($data['project_id']), 400, '项目ID不能为空');
|
|
|
- abort_if(empty($data['service_time']), 400, '服务时间不能为空');
|
|
|
abort_if($orderType == 'normal' && empty($data['coach_id']), 400, '技师ID不能为空');
|
|
|
abort_if($orderType == 'normal' && empty($data['address_id']), 400, '地址ID不能为空');
|
|
|
|
|
|
- // 3. 验证地址
|
|
|
- $address = $user->addresses()
|
|
|
- ->where('id', $data['address_id'])
|
|
|
- ->firstOrFail();
|
|
|
-
|
|
|
+ // 3. 验证技师
|
|
|
// 4. 根据订单类型处理
|
|
|
if ($orderType == 'normal') {
|
|
|
- $coach = $this->validateCoach($data['coach_id']);
|
|
|
+ $this->validateCoach($data['coach_id']);
|
|
|
} else {
|
|
|
$originalOrder = $this->getOriginalOrder($user, $data['order_id']);
|
|
|
- $coach = $this->validateCoach($originalOrder->coach_id);
|
|
|
+
|
|
|
+ $data['address_id'] = $originalOrder->address_id;
|
|
|
+
|
|
|
+ $this->validateCoach($originalOrder->coach_id);
|
|
|
abort_if(! in_array($originalOrder->state, ['service_ing', 'service_end']), 400, '原订单状态不允许加钟');
|
|
|
$data = $this->prepareAddTimeData($originalOrder, $data);
|
|
|
}
|
|
|
|
|
|
+ $address = $user->addresses()
|
|
|
+ ->where('id', $data['address_id'])
|
|
|
+ ->firstOrFail();
|
|
|
+
|
|
|
// 5. 计算订单金额
|
|
|
$amounts = $this->calculateOrderAmount(
|
|
|
$userId,
|
|
@@ -150,11 +155,11 @@ class OrderService
|
|
|
|
|
|
// 6. 验证金额和余额
|
|
|
abort_if($amounts['total_amount'] <= 0, 400, '订单金额异常');
|
|
|
+
|
|
|
if ($data['payment_type'] == 'balance') {
|
|
|
$wallet = $user->wallet;
|
|
|
abort_if($wallet->available_balance < $amounts['balance_amount'], 400, '可用余额不足');
|
|
|
}
|
|
|
-
|
|
|
// 7. 创建订单记录
|
|
|
$order = $this->createOrderRecord($userId, $data, $orderType, $address, $data['payment_type'], (object) $amounts);
|
|
|
|
|
@@ -175,9 +180,9 @@ class OrderService
|
|
|
{
|
|
|
$coach = CoachUser::where('id', $coachId)
|
|
|
->where('state', 'enable')
|
|
|
- ->whereHas('info', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('qual', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('real', fn($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('info', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('qual', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('real', fn ($q) => $q->where('state', 'approved'))
|
|
|
->firstOrFail();
|
|
|
|
|
|
return $coach;
|
|
@@ -730,10 +735,10 @@ class OrderService
|
|
|
try {
|
|
|
// 1. 校验技师
|
|
|
$coach = CoachUser::where('state', 'enable')
|
|
|
- ->whereHas('info', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('real', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->whereHas('qual', fn($q) => $q->where('state', 'approved'))
|
|
|
- ->with(['projects' => fn($q) => $q->where('project_id', $projectId)])
|
|
|
+ ->whereHas('info', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('real', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->whereHas('qual', fn ($q) => $q->where('state', 'approved'))
|
|
|
+ ->with(['projects' => fn ($q) => $q->where('project_id', $projectId)])
|
|
|
->find($coachId);
|
|
|
|
|
|
abort_if(! $coach, 404, '技师不存在或状态异常');
|
|
@@ -761,7 +766,7 @@ class OrderService
|
|
|
? bcmul($fee, '2', 2)
|
|
|
: $fee;
|
|
|
} catch (Exception $e) {
|
|
|
- Log::error(__CLASS__ . '->' . __FUNCTION__ . '计算路费失败:', [
|
|
|
+ Log::error(__CLASS__.'->'.__FUNCTION__.'计算路费失败:', [
|
|
|
'message' => $e->getMessage(),
|
|
|
'coach_id' => $coachId,
|
|
|
'project_id' => $projectId,
|
|
@@ -820,6 +825,8 @@ class OrderService
|
|
|
* @param int $agentId 代理商ID
|
|
|
* @param bool $useBalance 是否使用余额
|
|
|
* @param float $distance 距离
|
|
|
+ * @param int $lat 纬度
|
|
|
+ * @param int $lng 经度
|
|
|
*
|
|
|
* @throws Exception
|
|
|
*/
|
|
@@ -830,9 +837,12 @@ class OrderService
|
|
|
int $projectId,
|
|
|
?int $agentId = null,
|
|
|
bool $useBalance = false,
|
|
|
- float $distance = 0
|
|
|
+ float $distance = 0,
|
|
|
+ int $lat = 0,
|
|
|
+ int $lng = 0
|
|
|
): array {
|
|
|
try {
|
|
|
+
|
|
|
// 1. 参数校验
|
|
|
$user = MemberUser::find($userId);
|
|
|
abort_if(! $user || $user->state != 'enable', 404, '用户不存在或状态异常');
|
|
@@ -841,26 +851,27 @@ class OrderService
|
|
|
$coach = $this->validateCoach($coachId);
|
|
|
|
|
|
abort_if(! $coach, 404, '技师不存在或状态异常');
|
|
|
-
|
|
|
$coachProject = $coach->projects()
|
|
|
->where('state', 'enable')
|
|
|
->where('project_id', $projectId)
|
|
|
->first();
|
|
|
|
|
|
abort_if(! $coachProject, 404, '技师项目不存在');
|
|
|
-
|
|
|
// 3. 查询基础项目
|
|
|
$project = Project::where('id', $projectId)
|
|
|
- ->where('state', 'enable')
|
|
|
+ ->where('state', ProjectStatus::OPEN->value())
|
|
|
->first();
|
|
|
|
|
|
abort_if(! $project, 404, '项目不存在或状态异常');
|
|
|
|
|
|
// 4. 计算距离
|
|
|
- if ($distance <= 0) {
|
|
|
- $address = $user->addresses()->findOrFail($addressId);
|
|
|
+ if (floatval($distance) <= 0) {
|
|
|
+
|
|
|
+ $address = $user->addresses()->find($addressId) ?? ['latitude' => $lat, 'longitude' => $lng];
|
|
|
+
|
|
|
$coachService = app(CoachService::class);
|
|
|
- $coachDetail = $coachService->getCoachDetail($coachId, $address->latitude, $address->longitude);
|
|
|
+
|
|
|
+ $coachDetail = $coachService->getCoachDetail($coachId, $address['latitude'], $address['longitude']);
|
|
|
$distance = $coachDetail['distance'] ?? 0;
|
|
|
}
|
|
|
|
|
@@ -904,7 +915,7 @@ class OrderService
|
|
|
'delivery_fee' => $deliveryFee,
|
|
|
];
|
|
|
} catch (Exception $e) {
|
|
|
- Log::error(__CLASS__ . '->' . __FUNCTION__ . '计算订单金额失败:', [
|
|
|
+ Log::error(__CLASS__.'->'.__FUNCTION__.'计算订单金额失败:', [
|
|
|
'message' => $e->getMessage(),
|
|
|
'user_id' => $userId,
|
|
|
'project_id' => $projectId,
|
|
@@ -977,6 +988,47 @@ class OrderService
|
|
|
return [$balanceAmount, $payAmount];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取订单抢单池列表
|
|
|
+ *
|
|
|
+ * @param int $orderId 订单ID
|
|
|
+ * @return array 抢单池列表
|
|
|
+ */
|
|
|
+ public function getOrderGrabList(int $orderId): array
|
|
|
+ {
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 查询订单信息
|
|
|
+ $order = Order::where('id', $orderId)
|
|
|
+ ->whereIn('state', ['wait_pay', 'wait_service'])
|
|
|
+ ->firstOrFail();
|
|
|
+ // 查询抢单池列表
|
|
|
+ $grabList = $order->grabRecords()->with(['coach.info'])->get();
|
|
|
+
|
|
|
+ // 格式化返回数据
|
|
|
+ $result = [];
|
|
|
+ foreach ($grabList as $grab) {
|
|
|
+ $coach = $grab->coach;
|
|
|
+ $result[] = [
|
|
|
+ 'id' => $grab->id,
|
|
|
+ 'coach_id' => $coach->id,
|
|
|
+ 'nickname' => $coach->info->nickname,
|
|
|
+ 'avatar' => $coach->info->avatar,
|
|
|
+ 'created_at' => $grab->created_at->format('Y-m-d H:i:s'),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $result;
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('获取订单抢单池列表失败', [
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 指定技师
|
|
|
*/
|
|
@@ -1044,7 +1096,7 @@ class OrderService
|
|
|
WalletPaymentRecord::create([
|
|
|
'order_id' => $order->id,
|
|
|
'wallet_id' => $wallet->id,
|
|
|
- 'payment_no' => 'balance_' . $order->id,
|
|
|
+ 'payment_no' => 'balance_'.$order->id,
|
|
|
'payment_method' => 'balance',
|
|
|
'total_amount' => $order->balance_amount,
|
|
|
'actual_amount' => 0,
|