|
@@ -2,33 +2,32 @@
|
|
|
|
|
|
namespace App\Services\Client;
|
|
|
|
|
|
-use App\Models\Order;
|
|
|
-use App\Models\OrderRecord;
|
|
|
-use App\Models\WalletRefundRecord;
|
|
|
-
|
|
|
-use App\Models\User;
|
|
|
-use App\Models\MemberAddress;
|
|
|
-use App\Models\CoachUser;
|
|
|
-use App\Models\Project;
|
|
|
-use App\Models\AgentInfo;
|
|
|
use App\Models\AgentConfig;
|
|
|
+use App\Models\AgentInfo;
|
|
|
use App\Models\CoachConfig;
|
|
|
-use App\Models\Wallet;
|
|
|
-use App\Models\Coupon;
|
|
|
use App\Models\CoachSchedule;
|
|
|
+use App\Models\CoachUser;
|
|
|
+use App\Models\Coupon;
|
|
|
use App\Models\MemberUser;
|
|
|
-use App\Models\OrderHistory;
|
|
|
+use App\Models\Order;
|
|
|
use App\Models\OrderGrabRecord;
|
|
|
-
|
|
|
+use App\Models\OrderRecord;
|
|
|
+use App\Models\Project;
|
|
|
+use App\Models\SysConfig;
|
|
|
+use App\Models\User;
|
|
|
+use App\Models\Wallet;
|
|
|
+use App\Models\WalletRefundRecord;
|
|
|
+use Exception;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
-use Exception;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class OrderService
|
|
|
{
|
|
|
protected AgentService $agentService;
|
|
|
+
|
|
|
protected ProjectService $projectService;
|
|
|
-
|
|
|
+
|
|
|
public function __construct(
|
|
|
AgentService $agentService,
|
|
|
ProjectService $projectService
|
|
@@ -37,28 +36,46 @@ class OrderService
|
|
|
$this->projectService = $projectService;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
+ /**
|
|
|
* 订单初始化
|
|
|
*/
|
|
|
- public function initialize($userId, $coachId, $areaCode, $projectId)
|
|
|
+ public function initialize($userId, $data)
|
|
|
{
|
|
|
+ $user = MemberUser::find($userId);
|
|
|
+
|
|
|
+ if (! $user) {
|
|
|
+ throw new Exception('用户不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($user->state != 'enable') {
|
|
|
+ throw new Exception('用户状态异常');
|
|
|
+ }
|
|
|
+
|
|
|
// 查询用户钱包
|
|
|
- $wallet = Wallet::where('user_id', $userId)->first();
|
|
|
+ $wallet = $user->wallet;
|
|
|
|
|
|
// 查询默认地址
|
|
|
- $address = MemberAddress::where('user_id', $userId)
|
|
|
- ->where('is_default', 1)
|
|
|
- ->first();
|
|
|
+ $address = $user->address;
|
|
|
|
|
|
- if ($address) {
|
|
|
- $areaCode = $address->area_code;
|
|
|
- }
|
|
|
+ $areaCode = $address ? $address->area_code : $data['area_code'];
|
|
|
|
|
|
// 查询技师数据
|
|
|
- $coach = CoachUser::where('id', $coachId)
|
|
|
- ->where('state', 'enable')
|
|
|
- ->where('auth_state', 'passed')
|
|
|
- ->first();
|
|
|
+ $coach = CoachUser::where('state', 'enable')
|
|
|
+ ->whereHas('info', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('real', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('qual', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->with(['info:id,nickname,avatar,gender'])
|
|
|
+ ->find($data['coach_id']);
|
|
|
+
|
|
|
+ if (! $coach) {
|
|
|
+ throw new Exception('技师不存在');
|
|
|
+ }
|
|
|
|
|
|
// 查询技师排班
|
|
|
// $schedule = CoachSchedule::where('coach_id', $coachId)
|
|
@@ -71,14 +88,11 @@ class OrderService
|
|
|
// ->where('expire_time', '>', now())
|
|
|
// ->get();
|
|
|
|
|
|
- // 获取代理商
|
|
|
- $agent = $this->agentService->getAgent($areaCode);
|
|
|
-
|
|
|
// 获取项目详情
|
|
|
- $project = $this->projectService->getProjectDetail($projectId, $agent->id);
|
|
|
+ $project = $this->projectService->getProjectDetail($data['project_id'], $areaCode);
|
|
|
|
|
|
// 计算订单金额
|
|
|
- $amounts = $this->calculateOrderAmount($userId, $address->id, $coachId, $projectId, $agent->id);
|
|
|
+ $amounts = $this->calculateOrderAmount($userId, $address?->id, $data['coach_id'], $data['project_id'], $project?->agent_id);
|
|
|
|
|
|
return [
|
|
|
'wallet' => $wallet,
|
|
@@ -91,17 +105,135 @@ class OrderService
|
|
|
];
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 创建订单
|
|
|
+ */
|
|
|
+ public function createOrder($userId, array $data)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ return DB::transaction(function () use ($userId, $data) {
|
|
|
+ // 1. 参数校验
|
|
|
+ $user = MemberUser::where('id', $userId)
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->firstOrFail();
|
|
|
+
|
|
|
+ $address = $user->addresses()
|
|
|
+ ->where('id', $data['address_id'])
|
|
|
+ ->firstOrFail();
|
|
|
+
|
|
|
+ // 查询技师及其认证状态
|
|
|
+ $coach = CoachUser::where('id', $data['coach_id'])
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->whereHas('info', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('qual', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('real', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->firstOrFail();
|
|
|
+
|
|
|
+ $project = Project::where('id', $data['project_id'])
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->firstOrFail();
|
|
|
+
|
|
|
+ // 2. 创建订单
|
|
|
+ $orderType = isset($data['order_id']) ? 'add_time' : 'normal';
|
|
|
+
|
|
|
+ // 计算订单金额
|
|
|
+ $amounts = $this->calculateOrderAmount(
|
|
|
+ $userId,
|
|
|
+ $data['address_id'],
|
|
|
+ $data['coach_id'],
|
|
|
+ $data['project_id'],
|
|
|
+ $project->agent_id,
|
|
|
+ $data['use_balance'] ?? false
|
|
|
+ );
|
|
|
+
|
|
|
+ $order = new Order;
|
|
|
+ $order->user_id = $userId;
|
|
|
+ $order->project_id = $data['project_id'];
|
|
|
+ $order->coach_id = $data['coach_id'];
|
|
|
+ $order->type = $orderType;
|
|
|
+ $order->state = 'wait_pay';
|
|
|
+ $order->source = 'platform';
|
|
|
+ $order->total_amount = $amounts['total_amount'];
|
|
|
+ $order->balance_amount = $amounts['balance_amount'];
|
|
|
+ $order->pay_amount = $amounts['pay_amount'];
|
|
|
+ $order->project_amount = $amounts['project_amount'];
|
|
|
+ $order->traffic_amount = $orderType == 'add_time' ? 0 : $amounts['delivery_fee'];
|
|
|
+ $order->payment_type = ($data['use_balance'] && $amounts['pay_amount'] == 0) ? 'balance' : null;
|
|
|
+ $order->service_time = $data['service_time'];
|
|
|
+
|
|
|
+ // 从用户地址获取位置信息
|
|
|
+ $order->longitude = $address->longitude; // 经度
|
|
|
+ $order->latitude = $address->latitude; // 纬度
|
|
|
+ $order->location = $address->location; // 定位地址
|
|
|
+ $order->address = $address->address; // 详细地址
|
|
|
+ $order->area_code = $address->area_code; // 行政区划代码
|
|
|
+
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ // 3. 创建订单记录
|
|
|
+ OrderRecord::create([
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'object_id' => $userId,
|
|
|
+ 'object_type' => MemberUser::class,
|
|
|
+ 'state' => $orderType == 'add_time' ? 'add_time' : 'create',
|
|
|
+ 'remark' => $orderType == 'add_time' ? '加钟订单' : '创建订单',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 4. 余额支付处理
|
|
|
+ if ($order->payment_type == 'balance') {
|
|
|
+ $order->state = 'wait_receive';
|
|
|
+ $order->save();
|
|
|
+
|
|
|
+ OrderRecord::create([
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'object_id' => $userId,
|
|
|
+ 'object_type' => MemberUser::class,
|
|
|
+ 'state' => 'pay',
|
|
|
+ 'remark' => '余额支付',
|
|
|
+ ]);
|
|
|
+
|
|
|
+ // 创建技师排班
|
|
|
+ // CoachSchedule::create([
|
|
|
+ // 'coach_id' => $data['coach_id'],
|
|
|
+ // 'date' => date('Y-m-d'),
|
|
|
+ // 'state' => 'busy',
|
|
|
+ // ]);
|
|
|
+
|
|
|
+ // TODO: 发送抢单通知
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'order_id' => $order->id,
|
|
|
+ 'payment_type' => $order->payment_type,
|
|
|
+ ];
|
|
|
+ });
|
|
|
+ } catch (Exception $e) {
|
|
|
+ Log::error('创建订单失败:', [
|
|
|
+ 'message' => $e->getMessage(),
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'data' => $data,
|
|
|
+ ]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 结束订单
|
|
|
*/
|
|
|
public function finishOrder($userId, $orderId)
|
|
|
{
|
|
|
- return DB::transaction(function() use ($userId, $orderId) {
|
|
|
+ return DB::transaction(function () use ($userId, $orderId) {
|
|
|
$order = Order::where('user_id', $userId)
|
|
|
->where('id', $orderId)
|
|
|
->first();
|
|
|
|
|
|
- if (!$order) {
|
|
|
+ if (! $order) {
|
|
|
throw new Exception('订单不存在');
|
|
|
}
|
|
|
|
|
@@ -110,11 +242,11 @@ class OrderService
|
|
|
'order_id' => $orderId,
|
|
|
'user_id' => $userId,
|
|
|
'state' => 'finish',
|
|
|
- 'remark' => '服务完成'
|
|
|
+ 'remark' => '服务完成',
|
|
|
]);
|
|
|
|
|
|
// 修改订单状态
|
|
|
- $order->status = 'finished';
|
|
|
+ $order->state = 'finished';
|
|
|
$order->save();
|
|
|
|
|
|
return ['message' => '订单已完成'];
|
|
@@ -126,12 +258,12 @@ class OrderService
|
|
|
*/
|
|
|
public function confirmLeave($userId, $orderId)
|
|
|
{
|
|
|
- return DB::transaction(function() use ($userId, $orderId) {
|
|
|
+ return DB::transaction(function () use ($userId, $orderId) {
|
|
|
$order = Order::where('user_id', $userId)
|
|
|
->where('id', $orderId)
|
|
|
->first();
|
|
|
|
|
|
- if (!$order) {
|
|
|
+ if (! $order) {
|
|
|
throw new Exception('订单不存在');
|
|
|
}
|
|
|
|
|
@@ -140,11 +272,11 @@ class OrderService
|
|
|
'order_id' => $orderId,
|
|
|
'user_id' => $userId,
|
|
|
'state' => 'leave',
|
|
|
- 'remark' => '技师已离开'
|
|
|
+ 'remark' => '技师已离开',
|
|
|
]);
|
|
|
|
|
|
// 修改订单状态
|
|
|
- $order->status = 'allow_leave';
|
|
|
+ $order->state = 'allow_leave';
|
|
|
$order->save();
|
|
|
|
|
|
return ['message' => '已确认技师离开'];
|
|
@@ -156,12 +288,12 @@ class OrderService
|
|
|
*/
|
|
|
public function cancelOrder($userId, $orderId)
|
|
|
{
|
|
|
- return DB::transaction(function() use ($userId, $orderId) {
|
|
|
+ return DB::transaction(function () use ($userId, $orderId) {
|
|
|
$order = Order::where('user_id', $userId)
|
|
|
->where('id', $orderId)
|
|
|
->first();
|
|
|
|
|
|
- if (!$order) {
|
|
|
+ if (! $order) {
|
|
|
throw new Exception('订单不存在');
|
|
|
}
|
|
|
|
|
@@ -170,11 +302,11 @@ class OrderService
|
|
|
'order_id' => $orderId,
|
|
|
'user_id' => $userId,
|
|
|
'state' => 'cancel',
|
|
|
- 'remark' => '用户取消订单'
|
|
|
+ 'remark' => '用户取消订单',
|
|
|
]);
|
|
|
|
|
|
// 修改订单状态
|
|
|
- $order->status = 'cancelled';
|
|
|
+ $order->state = 'cancelled';
|
|
|
$order->save();
|
|
|
|
|
|
return ['message' => '订单已取消'];
|
|
@@ -187,13 +319,13 @@ class OrderService
|
|
|
public function getOrderList()
|
|
|
{
|
|
|
$userId = Auth::id();
|
|
|
-
|
|
|
+
|
|
|
return Order::where('user_id', $userId)
|
|
|
->with([
|
|
|
'project:id,title,cover,price',
|
|
|
'coach:id,name,avatar',
|
|
|
'agent:id,company_name',
|
|
|
- 'address:id,address'
|
|
|
+ 'address:id,address',
|
|
|
])
|
|
|
->orderBy('created_at', 'desc')
|
|
|
->paginate(10);
|
|
@@ -213,9 +345,9 @@ class OrderService
|
|
|
'coach:id,name,avatar,mobile',
|
|
|
'agent:id,company_name',
|
|
|
'address:id,address,latitude,longitude',
|
|
|
- 'records' => function($query) {
|
|
|
+ 'records' => function ($query) {
|
|
|
$query->orderBy('created_at', 'asc');
|
|
|
- }
|
|
|
+ },
|
|
|
])
|
|
|
->firstOrFail();
|
|
|
}
|
|
@@ -227,7 +359,7 @@ class OrderService
|
|
|
{
|
|
|
$userId = Auth::id();
|
|
|
|
|
|
- return DB::transaction(function() use ($orderId, $userId) {
|
|
|
+ return DB::transaction(function () use ($orderId, $userId) {
|
|
|
// 查询并锁定订单
|
|
|
$order = Order::where('id', $orderId)
|
|
|
->where('user_id', $userId)
|
|
@@ -245,7 +377,7 @@ class OrderService
|
|
|
'object_id' => $userId,
|
|
|
'object_type' => 'user',
|
|
|
'state' => 'refund',
|
|
|
- 'remark' => '订单退款'
|
|
|
+ 'remark' => '订单退款',
|
|
|
]);
|
|
|
|
|
|
// 创建退款记录
|
|
@@ -253,14 +385,14 @@ class OrderService
|
|
|
'order_id' => $orderId,
|
|
|
'user_id' => $userId,
|
|
|
'amount' => $order->total_amount,
|
|
|
- 'state' => 'success'
|
|
|
+ 'state' => 'success',
|
|
|
]);
|
|
|
|
|
|
return ['message' => '退款成功'];
|
|
|
});
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
+ /**
|
|
|
* 获取代理商配置
|
|
|
*/
|
|
|
public function getAgentConfig($agentId)
|
|
@@ -299,30 +431,68 @@ class OrderService
|
|
|
/**
|
|
|
* 计算路费金额
|
|
|
*/
|
|
|
- public function calculateDeliveryFee($coachId, $agentId, $distance)
|
|
|
+ public function calculateDeliveryFee($coachId, $projectId, $agentId, $distance)
|
|
|
{
|
|
|
- $coach = CoachUser::where('id', $coachId)
|
|
|
- ->where('state', 'enable')
|
|
|
- ->where('auth_state', 'passed')
|
|
|
- ->firstOrFail();
|
|
|
|
|
|
- $coachConfig = CoachConfig::where('coach_id', $coachId)->firstOrFail();
|
|
|
+ // 查询技师数据
|
|
|
+ $coach = CoachUser::where('state', 'enable')
|
|
|
+ ->whereHas('info', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('real', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('qual', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->find($coachId);
|
|
|
+
|
|
|
+ if (! $coach) {
|
|
|
+ throw new Exception('技师不存在');
|
|
|
+ }
|
|
|
|
|
|
- if (!$coachConfig->charge_delivery_fee) {
|
|
|
+ // 查询技师项目
|
|
|
+ $coachProject = $coach->projects()
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->where('project_id', $projectId)
|
|
|
+ ->first();
|
|
|
+ if (! $coachProject) {
|
|
|
+ throw new Exception('项目不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 技师免收路费
|
|
|
+ if ($coachProject->traffic_fee_type == 'free') {
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
- $agentConfig = AgentConfig::where('agent_id', $agentId)->firstOrFail();
|
|
|
+ // 查询代理商
|
|
|
+ $agent = AgentInfo::where('state', 'enable')->find($agentId);
|
|
|
+ if ($agent) {
|
|
|
+ $agentProject = $agent->projects()
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->where('project_id', $projectId)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if (! $agentProject) {
|
|
|
+ throw new Exception('代理商项目不存在');
|
|
|
+ }
|
|
|
+ $config = $agent->projectConfig;
|
|
|
+
|
|
|
+ } else {
|
|
|
+ // 系统配置
|
|
|
+ $config = SysConfig::where('key', 'delivery_fee')->firstOrFail();
|
|
|
+ dd('暂停处理');
|
|
|
+ }
|
|
|
|
|
|
$fee = 0;
|
|
|
- if ($distance <= $agentConfig->min_distance) {
|
|
|
- $fee = $agentConfig->min_fee;
|
|
|
+ if ($distance <= $config->min_distance) {
|
|
|
+ $fee = $config->min_fee;
|
|
|
} else {
|
|
|
- $extraDistance = $distance - $agentConfig->min_distance;
|
|
|
- $fee = $agentConfig->min_fee + ($extraDistance * $agentConfig->per_km_fee);
|
|
|
+ $extraDistance = $distance - $config->min_distance;
|
|
|
+ $fee = $config->min_fee + ($extraDistance * $config->per_km_fee);
|
|
|
}
|
|
|
|
|
|
- return $coachConfig->delivery_fee_type == 'round_trip' ? $fee * 2 : $fee;
|
|
|
+ return $coachProject->delivery_fee_type == 'round_trip' ? $fee * 2 : $fee;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -331,28 +501,78 @@ class OrderService
|
|
|
public function calculateOrderAmount($userId, $addressId, $coachId, $projectId, $agentId, $useBalance = false)
|
|
|
{
|
|
|
// 参数校验
|
|
|
- $user = MemberUser::where('id', $userId)
|
|
|
- ->where('status', 1)
|
|
|
- ->firstOrFail();
|
|
|
+ $user = MemberUser::find($userId);
|
|
|
+ if (! $user) {
|
|
|
+ throw new Exception('用户不存在');
|
|
|
+ }
|
|
|
|
|
|
- $address = MemberAddress::where('id', $addressId)
|
|
|
- ->where('user_id', $userId)
|
|
|
- ->firstOrFail();
|
|
|
+ if ($user->state != 'enable') {
|
|
|
+ throw new Exception('用户状态异常');
|
|
|
+ }
|
|
|
|
|
|
- $coach = CoachUser::where('id', $coachId)
|
|
|
- ->where('state', 'enable')
|
|
|
- ->where('auth_state', 'passed')
|
|
|
- ->firstOrFail();
|
|
|
+ // 查询地址
|
|
|
+ $address = $user->address()->find($addressId);
|
|
|
+
|
|
|
+ // 查询技师数据
|
|
|
+ $coach = CoachUser::where('state', 'enable')
|
|
|
+ ->whereHas('info', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('real', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->whereHas('qual', function ($query) {
|
|
|
+ $query->where('state', 'approved');
|
|
|
+ })
|
|
|
+ ->with(['info:id,nickname,avatar,gender'])
|
|
|
+ ->find($coachId);
|
|
|
+
|
|
|
+ if (! $coach) {
|
|
|
+ throw new Exception('技师不存在');
|
|
|
+ }
|
|
|
|
|
|
- $project = Project::where('id', $projectId)
|
|
|
+ // 查询技师项目
|
|
|
+ $coachProject = $coach->projects()
|
|
|
->where('state', 'enable')
|
|
|
- ->firstOrFail();
|
|
|
+ ->where('project_id', $projectId)
|
|
|
+ ->first();
|
|
|
+ if (! $coachProject) {
|
|
|
+ throw new Exception('项目不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询项目
|
|
|
+ $project = $coachProject->basicInfo;
|
|
|
+
|
|
|
+ if (! $project) {
|
|
|
+ throw new Exception('项目不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ if ($project->state != 'enable') {
|
|
|
+ throw new Exception('项目状态异常');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询代理商
|
|
|
+ $agent = AgentInfo::where('state', 'enable')->find($agentId);
|
|
|
+ if ($agent) {
|
|
|
+ $agentProject = $agent->projects()
|
|
|
+ ->where('state', 'enable')
|
|
|
+ ->where('project_id', $projectId)
|
|
|
+ ->first();
|
|
|
+
|
|
|
+ if (! $agentProject) {
|
|
|
+ throw new Exception('代理商项目不存在');
|
|
|
+ }
|
|
|
+
|
|
|
+ $project->price = $agentProject->price;
|
|
|
+ $project->duration = $agentProject->duration;
|
|
|
+ $project->distance = $address->distance;
|
|
|
+ }
|
|
|
|
|
|
// 计算金额
|
|
|
$projectAmount = $project->price;
|
|
|
- $deliveryFee = $this->calculateDeliveryFee($coachId, $agentId, $address->distance);
|
|
|
- $tipAmount = request()->has('order_id') ? Order::find(request()->input('order_id'))->tip_amount : 0;
|
|
|
-
|
|
|
+ $deliveryFee = $this->calculateDeliveryFee($coachId, $projectId, $agentId, $address?->distance);
|
|
|
+ // $tipAmount = request()->has('order_id') ? Order::find(request()->input('order_id'))->tip_amount : 0;
|
|
|
+
|
|
|
$couponAmount = 0;
|
|
|
if (request()->has('coupon_id')) {
|
|
|
// $coupon = Coupon::where('id', request()->input('coupon_id'))
|
|
@@ -361,7 +581,7 @@ class OrderService
|
|
|
// $couponAmount = $coupon->amount;
|
|
|
}
|
|
|
|
|
|
- $totalAmount = $projectAmount + $deliveryFee + $tipAmount - $couponAmount;
|
|
|
+ $totalAmount = $projectAmount + $deliveryFee - $couponAmount;
|
|
|
|
|
|
$balanceAmount = 0;
|
|
|
$payAmount = $totalAmount;
|
|
@@ -371,7 +591,7 @@ class OrderService
|
|
|
if ($wallet && $wallet->balance >= $totalAmount) {
|
|
|
$balanceAmount = $totalAmount;
|
|
|
$payAmount = 0;
|
|
|
- } else if ($wallet) {
|
|
|
+ } elseif ($wallet) {
|
|
|
$balanceAmount = $wallet->balance;
|
|
|
$payAmount = $totalAmount - $balanceAmount;
|
|
|
}
|
|
@@ -382,101 +602,12 @@ class OrderService
|
|
|
'balance_amount' => $balanceAmount,
|
|
|
'pay_amount' => $payAmount,
|
|
|
'coupon_amount' => $couponAmount,
|
|
|
- 'tip_amount' => $tipAmount,
|
|
|
+ // 'tip_amount' => $tipAmount,
|
|
|
'project_amount' => $projectAmount,
|
|
|
- 'delivery_fee' => $deliveryFee
|
|
|
+ 'delivery_fee' => $deliveryFee,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 创建订单
|
|
|
- */
|
|
|
- public function createOrder($userId, $projectId, $addressId, $coachId, $useBalance, $orderId = null)
|
|
|
- {
|
|
|
- return DB::transaction(function() use ($userId, $projectId, $addressId, $coachId, $useBalance, $orderId) {
|
|
|
- // 参数校验
|
|
|
- $user = MemberUser::where('id', $userId)
|
|
|
- ->where('status', 1)
|
|
|
- ->firstOrFail();
|
|
|
-
|
|
|
- $address = MemberAddress::where('id', $addressId)
|
|
|
- ->where('user_id', $userId)
|
|
|
- ->firstOrFail();
|
|
|
-
|
|
|
- if (!$orderId) {
|
|
|
- $coach = CoachUser::where('id', $coachId)
|
|
|
- ->where('state', 'enable')
|
|
|
- ->where('auth_state', 'passed')
|
|
|
- ->firstOrFail();
|
|
|
- }
|
|
|
-
|
|
|
- $project = Project::where('id', $projectId)
|
|
|
- ->where('state', 'enable')
|
|
|
- ->firstOrFail();
|
|
|
-
|
|
|
- // 创建订单
|
|
|
- $orderType = $orderId ? 'add_time' : 'normal';
|
|
|
- $amounts = $this->calculateOrderAmount($userId, $addressId, $coachId, $projectId, $project->agent_id, $useBalance);
|
|
|
-
|
|
|
- $order = new Order();
|
|
|
- $order->user_id = $userId;
|
|
|
- $order->project_id = $projectId;
|
|
|
- $order->address_id = $addressId;
|
|
|
- $order->coach_id = $coachId;
|
|
|
- $order->order_type = $orderType == 'normal' ? 0 : 1;
|
|
|
- $order->status = 0;
|
|
|
- $order->total_amount = $amounts['total_amount'];
|
|
|
- $order->balance_amount = $amounts['balance_amount'];
|
|
|
- $order->pay_amount = $amounts['pay_amount'];
|
|
|
- $order->coupon_amount = $amounts['coupon_amount'];
|
|
|
- $order->tip_amount = $amounts['tip_amount'];
|
|
|
- $order->project_amount = $amounts['project_amount'];
|
|
|
- $order->delivery_fee = $orderType == 'add_time' ? 0 : $amounts['delivery_fee'];
|
|
|
- $order->payment_type = $useBalance && $amounts['pay_amount'] == 0 ? 0 : 1;
|
|
|
- $order->save();
|
|
|
-
|
|
|
- // 创建订单历史
|
|
|
- if ($orderType == 'add_time') {
|
|
|
- OrderRecord::create([
|
|
|
- 'order_id' => $order->id,
|
|
|
- 'type' => 'add_time',
|
|
|
- 'user_id' => $userId
|
|
|
- ]);
|
|
|
- } else {
|
|
|
- OrderRecord::create([
|
|
|
- 'order_id' => $order->id,
|
|
|
- 'type' => 'create',
|
|
|
- 'user_id' => $userId
|
|
|
- ]);
|
|
|
- }
|
|
|
-
|
|
|
- // 余额支付处理
|
|
|
- if ($order->payment_type == 'balance') {
|
|
|
- $order->status = 'paid';
|
|
|
- $order->save();
|
|
|
-
|
|
|
- OrderRecord::create([
|
|
|
- 'order_id' => $order->id,
|
|
|
- 'type' => 'pay',
|
|
|
- 'user_id' => $userId
|
|
|
- ]);
|
|
|
-
|
|
|
- // CoachSchedule::create([
|
|
|
- // 'coach_id' => $coachId,
|
|
|
- // 'date' => date('Y-m-d'),
|
|
|
- // 'status' => 'busy'
|
|
|
- // ]);
|
|
|
-
|
|
|
- return $order->id;
|
|
|
- }
|
|
|
-
|
|
|
- // 微信支付处理
|
|
|
- // TODO: 调用支付接口
|
|
|
-
|
|
|
- return $order->id;
|
|
|
- });
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 加钟
|
|
|
*/
|
|
@@ -494,15 +625,15 @@ class OrderService
|
|
|
*/
|
|
|
public function assignCoach($userId, $orderId, $coachId)
|
|
|
{
|
|
|
- return DB::transaction(function() use ($userId, $orderId, $coachId) {
|
|
|
+ return DB::transaction(function () use ($userId, $orderId, $coachId) {
|
|
|
// 参数校验
|
|
|
$user = MemberUser::where('id', $userId)
|
|
|
- ->where('status', 'enable')
|
|
|
+ ->where('state', 'enable')
|
|
|
->firstOrFail();
|
|
|
|
|
|
$order = Order::where('id', $orderId)
|
|
|
->where('user_id', $userId)
|
|
|
- ->whereIn('status', [0, 1, 6])
|
|
|
+ ->whereIn('state', [0, 1, 6])
|
|
|
->firstOrFail();
|
|
|
|
|
|
$coach = CoachUser::where('id', $coachId)
|
|
@@ -512,28 +643,28 @@ class OrderService
|
|
|
|
|
|
// $schedule = CoachSchedule::where('coach_id', $coachId)
|
|
|
// ->where('date', date('Y-m-d'))
|
|
|
- // ->where('status', 'free')
|
|
|
+ // ->where('state', 'free')
|
|
|
// ->firstOrFail();
|
|
|
|
|
|
// 修改订单
|
|
|
$order->coach_id = $coachId;
|
|
|
- if ($order->status == 'created') {
|
|
|
+ if ($order->state == 'created') {
|
|
|
$amounts = $this->calculateOrderAmount($userId, $order->address_id, $coachId, $order->project_id, $order->agent_id, $order->payment_type == 'balance');
|
|
|
-
|
|
|
+
|
|
|
$order->total_amount = $amounts['total_amount'];
|
|
|
$order->balance_amount = $amounts['balance_amount'];
|
|
|
$order->pay_amount = $amounts['pay_amount'];
|
|
|
$order->coupon_amount = $amounts['coupon_amount'];
|
|
|
- $order->tip_amount = $amounts['tip_amount'];
|
|
|
+ // $order->tip_amount = $amounts['tip_amount'];
|
|
|
$order->project_amount = $amounts['project_amount'];
|
|
|
$order->delivery_fee = $amounts['delivery_fee'];
|
|
|
|
|
|
if ($order->payment_type == 'balance') {
|
|
|
- $order->status = 'paid';
|
|
|
+ $order->state = 'paid';
|
|
|
}
|
|
|
}
|
|
|
- if ($order->status == 'paid') {
|
|
|
- $order->status = 'assigned';
|
|
|
+ if ($order->state == 'paid') {
|
|
|
+ $order->state = 'assigned';
|
|
|
}
|
|
|
$order->save();
|
|
|
|
|
@@ -542,7 +673,7 @@ class OrderService
|
|
|
'order_id' => $order->id,
|
|
|
'type' => 'assigned',
|
|
|
'user_id' => $userId,
|
|
|
- 'coach_id' => $coachId
|
|
|
+ 'coach_id' => $coachId,
|
|
|
]);
|
|
|
|
|
|
OrderRecord::create([
|
|
@@ -550,18 +681,18 @@ class OrderService
|
|
|
'type' => 'accepted',
|
|
|
'user_id' => $userId,
|
|
|
'coach_id' => $coachId,
|
|
|
- 'remark' => '抢单成功'
|
|
|
+ 'remark' => '抢单成功',
|
|
|
]);
|
|
|
|
|
|
// 更新抢单池
|
|
|
OrderGrabRecord::where('order_id', $orderId)
|
|
|
- ->update(['status' => 'success', 'coach_id' => $coachId]);
|
|
|
+ ->update(['state' => 'success', 'coach_id' => $coachId]);
|
|
|
|
|
|
// 更新排班
|
|
|
- // $schedule->status = 'busy';
|
|
|
+ // $schedule->state = 'busy';
|
|
|
// $schedule->save();
|
|
|
|
|
|
return true;
|
|
|
});
|
|
|
}
|
|
|
-}
|
|
|
+}
|