|
@@ -10,10 +10,15 @@ namespace App\Http\Services\Frontend\Client\Service;
|
|
|
|
|
|
use App\Exceptions\ApiException;
|
|
|
use App\Http\Services\Service;
|
|
|
+use App\Models\Coach\Site;
|
|
|
use App\Models\Coach\User;
|
|
|
use App\Models\Member\Address;
|
|
|
+use App\Models\Member\Benefit;
|
|
|
use App\Models\Service\Order;
|
|
|
+use App\Models\Service\Project;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
+use Illuminate\Support\Facades\DB;
|
|
|
+use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class OrderService extends Service
|
|
|
{
|
|
@@ -38,6 +43,12 @@ class OrderService extends Service
|
|
|
$data['real_address'] = $address['addressInfo'];
|
|
|
$data['address_lat'] = $address['lat'];
|
|
|
$data['address_lng'] = $address['lng'];
|
|
|
+ $data['area_id'] = 0;
|
|
|
+
|
|
|
+ // 获取项目信息
|
|
|
+ $project = Project::query()->find($params['projectId']);
|
|
|
+ $data['project_name'] = $project->title;
|
|
|
+ $data['project_icon'] = $project->cover;
|
|
|
|
|
|
// 优惠卷
|
|
|
// if ($params['couponId']) $data['coupon_id'] = $params['couponId'];
|
|
@@ -84,30 +95,57 @@ class OrderService extends Service
|
|
|
}
|
|
|
|
|
|
$orderModel = new Order();
|
|
|
- $orderPayInfo = $orderModel->buildPayInfo($data['user_id'], $params['projectId'], $address['city_code'], $address['lat'], $address['lng'], $use_balance, $coach_id, $car_type, $coupon_id, $order_id);
|
|
|
-
|
|
|
-
|
|
|
- //默认微信
|
|
|
- $payType = $params['pay_type'] ?? 1;
|
|
|
- $use_balance && !$orderPayInfo['pay_price'] && ($payType = 0);
|
|
|
-
|
|
|
- $orderData = [
|
|
|
- 'order_sn' => $this->buildOrderSN(),
|
|
|
- 'pay_type' => $payType,
|
|
|
- ...$data,
|
|
|
- ...$orderPayInfo,
|
|
|
- //备注
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $memberQuery = \App\Models\Member\User::query();
|
|
|
+ $member = $memberQuery->lockForUpdate()->find($data['user_id']);
|
|
|
+ $orderPayInfo = $orderModel->buildPayInfo($data['user_id'], $params['projectId'], $address['city_code'], $address['lat'], $address['lng'], $use_balance, $coach_id, $car_type, $coupon_id, $order_id);
|
|
|
+
|
|
|
+ //默认微信
|
|
|
+ $payType = $params['pay_type'] ?? 1;
|
|
|
+ $use_balance && !$orderPayInfo['pay_price'] && ($payType = 0);
|
|
|
+
|
|
|
+ // 订单状态
|
|
|
+ $orderStatus = $use_balance && !$orderPayInfo['pay_price'] ? 1 : 0;
|
|
|
+
|
|
|
+ $orderData = [
|
|
|
+ 'order_sn' => $this->buildOrderSN(),
|
|
|
+ 'pay_type' => $payType,
|
|
|
+ 'status' => $orderStatus,
|
|
|
+ ...$data,
|
|
|
+ ...$orderPayInfo,
|
|
|
+ //备注
|
|
|
// 'text' => $params['text'] ?: '',
|
|
|
// 'car_type' => $car_type,
|
|
|
// 'channel_id' => $params['channel_id'] ?: 0,
|
|
|
|
|
|
- //目的地地址
|
|
|
- //加钟
|
|
|
+ //目的地地址
|
|
|
+ //加钟
|
|
|
// 'add_pid' => $order_id,
|
|
|
// 'is_add' => $order_id ? 1 : 0,
|
|
|
- ];
|
|
|
- // 创建订单
|
|
|
- return $orderModel->newQuery()->create($orderData)->id;
|
|
|
+ ];
|
|
|
+ // 创建订单
|
|
|
+ $orderId = $orderModel->newQuery()->create($orderData)->id;
|
|
|
+
|
|
|
+ // 变更用户余额
|
|
|
+ if ($use_balance && $orderPayInfo['balance_price']) {
|
|
|
+ Benefit::query()->create([
|
|
|
+ 'user_id' => $data['user_id'],
|
|
|
+ 'order_id' => $orderId,
|
|
|
+ 'type' => 1,
|
|
|
+ 'benefit' => $orderPayInfo['balance_price'] * -1,
|
|
|
+ 'balance' => $member['balance']
|
|
|
+ ]);
|
|
|
+ $member->decrement('balance', $orderPayInfo['balance_price']);
|
|
|
+ }
|
|
|
+
|
|
|
+ DB::commit();
|
|
|
+ return $orderId;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ Log::error('Transaction failed: ' . $e->getMessage());
|
|
|
+ self::error('执行错误!');
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
public function getOrderPage(array $data)
|
|
@@ -164,4 +202,46 @@ class OrderService extends Service
|
|
|
$order->user_del = 1;
|
|
|
$order->save();
|
|
|
}
|
|
|
+
|
|
|
+ public function confirmOrder(array $params)
|
|
|
+ {
|
|
|
+ $user_id = Auth::id();
|
|
|
+ $projectId = $params['projectId'];
|
|
|
+ $addressId = $params['addressId'];
|
|
|
+ $coachId = $params['coachId'] ?? 0;
|
|
|
+ // 查询地区项目
|
|
|
+ $projectSelect = ['title', 'sub_title', 'cover', 'price', 'init_price', 'total_sale', 'time_long', 'is_store', 'is_door'];
|
|
|
+ $project = Project::query()->select($projectSelect)->find($projectId);
|
|
|
+
|
|
|
+ // 查询用户余额
|
|
|
+ $balance = \App\Models\Member\User::query()->where('id', $user_id)->pluck('balance');
|
|
|
+ // 车费
|
|
|
+ // 判断技师是否收取车费
|
|
|
+// $coach = User::query()->find($coachId);
|
|
|
+ // 获取技师定位
|
|
|
+ $coachSite = null;
|
|
|
+ if ($coachId) {
|
|
|
+ $coachSelect = ['latitude', 'longitude'];
|
|
|
+ $coachSite = Site::query()->select($coachSelect)->where('coach_id', $coachId)->first();
|
|
|
+ }
|
|
|
+
|
|
|
+ return [
|
|
|
+ 'balance' => $balance,
|
|
|
+ 'project' => $project,
|
|
|
+ 'coach' => $coachSite
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ public function computePrice(array $params): void
|
|
|
+ {
|
|
|
+ $user_id = Auth::id();
|
|
|
+ $address = $params['addressId'];
|
|
|
+ $use_balance = $params['useBalance'];
|
|
|
+ $coach_id = $params['coachId'];
|
|
|
+ $memberQuery = \App\Models\Member\User::query();
|
|
|
+ $member = $memberQuery->find($user_id);
|
|
|
+ $orderModel = new Order();
|
|
|
+ $orderPayInfo = $orderModel->buildPayInfo($user_id, $params['projectId'], $address['city_code'], $address['lat'], $address['lng'], $use_balance, $coach_id, $car_type, $coupon_id, $order_id);
|
|
|
+
|
|
|
+ }
|
|
|
}
|