|
@@ -15,16 +15,20 @@ use App\Models\Coach\User;
|
|
|
use App\Models\Member\Address;
|
|
|
use App\Models\Member\Benefit;
|
|
|
use App\Models\Service\Order;
|
|
|
+use App\Models\Service\OrderGrab;
|
|
|
use App\Models\Service\Project;
|
|
|
use Endroid\QrCode\Builder\Builder;
|
|
|
use Endroid\QrCode\Writer\PngWriter;
|
|
|
+use Illuminate\Support\Carbon;
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
+use Illuminate\Support\Str;
|
|
|
|
|
|
class OrderService extends Service
|
|
|
{
|
|
|
- protected array $baseColumn = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at'];
|
|
|
+ protected array $baseColumn = ['id', 'order_sn', 'user_name', 'mobile', 'coach_id', 'project_name', 'project_icon', 'service_price', 'time_long', 'pay_type', 'pay_price', 'address', 'real_address', 'status', 'created_at'];
|
|
|
+ protected array $infoColumn = ['distance', 'remark', 'car_price', 'material_price', 'coupon_price', 'discount_price', 'balance_price', 'pay_time', 'start_time'];
|
|
|
|
|
|
function buildOrderSN(): string
|
|
|
{
|
|
@@ -38,6 +42,8 @@ class OrderService extends Service
|
|
|
public function createOrder(array $params)
|
|
|
{
|
|
|
$data = ['user_id' => Auth::id()];
|
|
|
+ // 备注
|
|
|
+ $data['remark'] = $params['remark'] ?? '';
|
|
|
// 用户地址
|
|
|
$address = Address::query()->where('user_id', $data['user_id'])->find($params['addressId']);
|
|
|
!$address && self::error('地址信息错误');
|
|
@@ -45,13 +51,18 @@ class OrderService extends Service
|
|
|
$data['real_address'] = $address['addressInfo'];
|
|
|
$data['address_lat'] = $address['lat'];
|
|
|
$data['address_lng'] = $address['lng'];
|
|
|
+ $data['mobile'] = $address['mobile'];
|
|
|
+ $data['adcode'] = $address['adcode'];
|
|
|
+ $data['user_name'] = $address['user_name'];
|
|
|
$data['area_id'] = 0;
|
|
|
|
|
|
// 获取项目信息
|
|
|
$project = Project::query()->find($params['projectId']);
|
|
|
$data['project_name'] = $project->title;
|
|
|
+ $data['project_sub_title'] = $project->sub_title;
|
|
|
$data['project_icon'] = $project->cover;
|
|
|
|
|
|
+
|
|
|
// 优惠卷
|
|
|
// if ($params['couponId']) $data['coupon_id'] = $params['couponId'];
|
|
|
// $coupon_record_model = new CouponRecord();
|
|
@@ -101,100 +112,156 @@ class OrderService extends Service
|
|
|
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);
|
|
|
+ $orderPayInfo = $orderModel->buildPayInfo($data['user_id'], $params['projectId'], $use_balance, $coach_id, $car_type, $coupon_id, $order_id);
|
|
|
|
|
|
//默认微信
|
|
|
$payType = $params['pay_type'] ?? 1;
|
|
|
- $use_balance && !$orderPayInfo['pay_price'] && ($payType = 0);
|
|
|
+
|
|
|
+ $use_balance && !round($orderPayInfo['pay_price'], 2) && ($payType = 0);
|
|
|
|
|
|
// 订单状态
|
|
|
- $orderStatus = $use_balance && !$orderPayInfo['pay_price'] ? 1 : 0;
|
|
|
-
|
|
|
- $orderData = [
|
|
|
- 'order_sn' => $this->buildOrderSN(),
|
|
|
- 'pay_type' => $payType,
|
|
|
- 'status' => $orderStatus,
|
|
|
- ...$data,
|
|
|
- ...$orderPayInfo,
|
|
|
- //备注
|
|
|
+ $orderStatus = 0;
|
|
|
+ if ($use_balance && !round($orderPayInfo['pay_price'], 2)) {
|
|
|
+ $orderStatus = 1;
|
|
|
+ $orderPayInfo['pay_time'] = time();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ $orderData = array_merge($data, $orderPayInfo,
|
|
|
+ [
|
|
|
+ 'order_sn' => $this->buildOrderSN(),
|
|
|
+ 'pay_type' => $payType,
|
|
|
+ 'status' => $orderStatus
|
|
|
+ ]);
|
|
|
+
|
|
|
+ //备注
|
|
|
// 'text' => $params['text'] ?: '',
|
|
|
// 'car_type' => $car_type,
|
|
|
// 'channel_id' => $params['channel_id'] ?: 0,
|
|
|
|
|
|
- //目的地地址
|
|
|
- //加钟
|
|
|
+ //目的地地址
|
|
|
+ //加钟
|
|
|
// 'add_pid' => $order_id,
|
|
|
// 'is_add' => $order_id ? 1 : 0,
|
|
|
- ];
|
|
|
+
|
|
|
+ $orderData['coupon_price'] = $orderData['coupon_price'] * 100;
|
|
|
+ $orderData['discount_price'] = $orderData['discount_price'] * 100;
|
|
|
+ $orderData['material_price'] = $orderData['material_price'] * 100;
|
|
|
+ $orderData['car_price'] = $orderData['car_price'] * 100;
|
|
|
+ $orderData['pay_price'] = $orderData['pay_price'] * 100;
|
|
|
+ $orderData['balance_price'] = $orderData['balance_price'] * 100;
|
|
|
+ // 服务价格
|
|
|
+ $orderData['service_price'] = $orderData['service_price'] * 100;
|
|
|
+
|
|
|
+
|
|
|
// 创建订单
|
|
|
$orderId = $orderModel->newQuery()->create($orderData)->id;
|
|
|
|
|
|
// 变更用户余额
|
|
|
- if ($use_balance && $orderPayInfo['balance_price']) {
|
|
|
+ if ($use_balance && round($orderPayInfo['balance_price'], 2) > 0) {
|
|
|
+
|
|
|
Benefit::query()->create([
|
|
|
'user_id' => $data['user_id'],
|
|
|
'order_id' => $orderId,
|
|
|
'type' => 1,
|
|
|
- 'benefit' => $orderPayInfo['balance_price'] * -1,
|
|
|
- 'balance' => $member['balance']
|
|
|
+ 'benefit' => round($orderPayInfo['balance_price'], 2) * -100,
|
|
|
+ 'balance' => round($member['balance'], 2) * 100
|
|
|
]);
|
|
|
- $member->decrement('balance', $orderPayInfo['balance_price']);
|
|
|
+ $member->decrement('balance', round($orderPayInfo['balance_price'], 2) * 100);
|
|
|
}
|
|
|
|
|
|
DB::commit();
|
|
|
return $orderId;
|
|
|
} catch (\Exception $e) {
|
|
|
DB::rollBack();
|
|
|
- self::error();
|
|
|
+ self::error($e->getMessage());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- public function getOrderPage(array $data)
|
|
|
+ public function getOrderPage(array $data): array
|
|
|
{
|
|
|
$user_id = Auth::id();
|
|
|
$status = intval($data['status'] ?? 0);
|
|
|
$where = ['user_id' => $user_id, 'user_del' => 0];
|
|
|
$statusRange = match ($status) {
|
|
|
- 1 => [0, 1, 2, 3],
|
|
|
- 2 => [4, 5],
|
|
|
- 3 => [5],
|
|
|
- 4 => [6, 7],
|
|
|
- default => [0, 1, 2, 3, 4, 5, 6, 7]
|
|
|
+// 1 => [0, 1, 2, 3],
|
|
|
+// 2 => [4, 5],
|
|
|
+// 3 => [5],
|
|
|
+// 4 => [6, 7],
|
|
|
+// default => [0, 1, 2, 3, 4, 5, 6, 7]
|
|
|
+// 0:未支付 1:已支付(待接单) 2:已接单 3:已出发 4:已到达 5:开始服务(服务中) 6:服务结束 7:已撤离 8:已评价 9:取消订单(退款中) 10:已退款
|
|
|
+ // 对应景工UI
|
|
|
+ 1 => [0],
|
|
|
+ 2 => [1, 2, 3, 4, 5],
|
|
|
+ 3 => [6, 7, 8],
|
|
|
+ 4 => [7],
|
|
|
+ 5 => [9, 10],
|
|
|
+ default => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
|
|
};
|
|
|
-// $select = ['id', 'project_name', 'project_icon', 'duration', 'paid_type', 'price', 'real_address', 'status', 'created_at', 'refunded_at'];
|
|
|
- $select = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at'];
|
|
|
- $orderPage = Order::query()->where($where)->whereIn('status', $statusRange)->select($select)->latest()->paginate();
|
|
|
+ $orderPage = Order::query()->with('coach', function ($query) {
|
|
|
+ $query->select('id', 'nickname', 'avatar');
|
|
|
+ })->where($where)->whereIn('status', $statusRange)->select($this->baseColumn)->latest()->paginate(10);
|
|
|
return ['list' => $orderPage->items(), 'total' => $orderPage->total()];
|
|
|
}
|
|
|
|
|
|
public function getOrder(int $order_id)
|
|
|
{
|
|
|
$user_id = Auth::id();
|
|
|
- return Order::query()->where('user_id', $user_id)->select($this->baseColumn)->find($order_id);
|
|
|
+ return Order::query()->with('coach', function ($query) {
|
|
|
+ $query->select(['id', 'nickname', 'avatar']);
|
|
|
+ })->with('grab.coach', function ($query) {
|
|
|
+ $query->select(['id', 'nickname', 'avatar']);
|
|
|
+ })->where('user_id', $user_id)->select([...$this->baseColumn, ...$this->infoColumn])->find($order_id);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @throws ApiException
|
|
|
*/
|
|
|
- public function updateOrder(array $data): void
|
|
|
+ public function updateOrder(array $data,int $id): void
|
|
|
{
|
|
|
- $order_id = $data['id'];
|
|
|
+ $user_id = Auth::id();
|
|
|
+ $order_id = $id;
|
|
|
$order = Order::query()->find($order_id);
|
|
|
- $status = $data['status'];
|
|
|
- switch ($status) {
|
|
|
- // 结束订单
|
|
|
- case 6:
|
|
|
- $order->status !== 5 && self::error('订单状态有误');
|
|
|
- // 查询是否存在加钟订单
|
|
|
- // 判断加钟服务是否完成
|
|
|
- $order->user_end = 1;
|
|
|
- $order->end_time = time();
|
|
|
- break;
|
|
|
- case 9:
|
|
|
- !in_array($order->status, [0, 1, 2, 3]) && self::error('订单状态有误');
|
|
|
- break;
|
|
|
+ if ($order->status === 0) {
|
|
|
+
|
|
|
+ // 用户地址
|
|
|
+ if (isset($data['addressId'])) {
|
|
|
+ $address = Address::query()->where('user_id', $user_id)->find($data['addressId']);
|
|
|
+ !$address && self::error('地址信息错误!');
|
|
|
+ $order['address'] = $address['address'];
|
|
|
+ $order['real_address'] = $address['addressInfo'];
|
|
|
+ $order['address_lat'] = $address['lat'];
|
|
|
+ $order['address_lng'] = $address['lng'];
|
|
|
+ $order['mobile'] = $address['mobile'];
|
|
|
+ $order['adcode'] = $address['adcode'];
|
|
|
+ $order['user_name'] = $address['user_name'];
|
|
|
+ }
|
|
|
+ $orderModel = new Order();
|
|
|
+ $orderPayInfo = $orderModel->buildPayInfo($user_id, $order['project_id'], $data['useBalance'] ?? 0, $data['coachId'], $data['carType'] ?? 0, $data['couponId'] ?? 0);
|
|
|
+ $order->service_price = $orderPayInfo['service_price'] * 100;
|
|
|
+ $order->coupon_price = $orderPayInfo['coupon_price'] * 100;
|
|
|
+ $order->discount_price = $orderPayInfo['discount_price'] * 100;
|
|
|
+ $order->material_price = $orderPayInfo['material_price'] * 100;
|
|
|
+ $order->car_price = $orderPayInfo['car_price'] * 100;
|
|
|
+ $order->pay_price = $orderPayInfo['pay_price'] * 100;
|
|
|
+ $order->balance_price = $orderPayInfo['balance_price'] * 100;
|
|
|
+ } else {
|
|
|
+ $status = $data['status'];
|
|
|
+ switch ($status) {
|
|
|
+ // 结束订单
|
|
|
+ case 6:
|
|
|
+ $order->status !== 5 && self::error('订单状态有误');
|
|
|
+ // 查询是否存在加钟订单
|
|
|
+ // 判断加钟服务是否完成
|
|
|
+ $order->user_end = 1;
|
|
|
+ $order->end_time = time();
|
|
|
+ break;
|
|
|
+ case 9:
|
|
|
+ !in_array($order->status, [0, 1, 2, 3]) && self::error('订单状态有误');
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ $order->status = $status;
|
|
|
}
|
|
|
- $order->status = $status;
|
|
|
$order->save();
|
|
|
}
|
|
|
|
|
@@ -205,7 +272,13 @@ class OrderService extends Service
|
|
|
$order->save();
|
|
|
}
|
|
|
|
|
|
- public function confirmOrder(array $params)
|
|
|
+ /**
|
|
|
+ * Notes :
|
|
|
+ * Method : 确认下单
|
|
|
+ * @param array $params
|
|
|
+ * @return array
|
|
|
+ */
|
|
|
+ public function confirmOrder(array $params): array
|
|
|
{
|
|
|
$user_id = Auth::id();
|
|
|
$projectId = $params['projectId'];
|
|
@@ -215,45 +288,141 @@ class OrderService extends Service
|
|
|
$project = Project::query()->select($projectSelect)->find($projectId);
|
|
|
|
|
|
// 查询用户余额
|
|
|
- $balance = \App\Models\Member\User::query()->where('id', $user_id)->pluck('balance');
|
|
|
+ $balance = \App\Models\Member\User::query()->where('id', $user_id)->value('balance');
|
|
|
// 车费
|
|
|
// 判断技师是否收取车费
|
|
|
// $coach = User::query()->find($coachId);
|
|
|
// 获取技师定位
|
|
|
- $coachSite = null;
|
|
|
+ $coachUser = null;
|
|
|
if ($coachId) {
|
|
|
- $coachSelect = ['latitude', 'longitude'];
|
|
|
- $coachSite = Site::query()->select($coachSelect)->where('coach_id', $coachId)->first();
|
|
|
+ $coachSelect = ['id', 'nickname', 'avatar'];
|
|
|
+ $coachSiteSelect = ['latitude', 'longitude'];
|
|
|
+ $coachUser = User::query()->where('id', $coachId)->select($coachSelect)->first();
|
|
|
+ $coachUser['site'] = Site::query()->select($coachSiteSelect)->where('coach_id', $coachId)->first();
|
|
|
}
|
|
|
|
|
|
return [
|
|
|
'balance' => $balance,
|
|
|
'project' => $project,
|
|
|
- 'coach' => $coachSite
|
|
|
+ 'coach' => $coachUser
|
|
|
];
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @throws ApiException
|
|
|
+ * Notes :
|
|
|
+ * Method : 计算金额
|
|
|
+ * @param array $params
|
|
|
+ * @return array
|
|
|
*/
|
|
|
public function computePrice(array $params): array
|
|
|
{
|
|
|
$user_id = Auth::id();
|
|
|
+ // 存在订单
|
|
|
+ $order_id = $params['orderId'] ?? 0;
|
|
|
+
|
|
|
$use_balance = $params['useBalance'] ?? 0;
|
|
|
$coach_id = $params['coachId'] ?? 0;
|
|
|
$distance = $params['distance'] ?? 0;
|
|
|
$car_type = $params['carType'] ?? 0;
|
|
|
$coupon_id = $params['couponId'] ?? 0;
|
|
|
+ $project_id = $params['projectId'] ?? 0;
|
|
|
|
|
|
// $member = $memberQuery->find($user_id);
|
|
|
|
|
|
$orderModel = new Order();
|
|
|
- $orderPayInfo = $orderModel->buildPayInfo($user_id, $params['projectId'], $use_balance, $coach_id, $car_type, $coupon_id, $distance);
|
|
|
-
|
|
|
+ $orderPayInfo = $orderModel->buildPayInfo($user_id, $project_id, $use_balance, $coach_id, $car_type, $coupon_id, $order_id, $distance);
|
|
|
|
|
|
return $orderPayInfo;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Notes :
|
|
|
+ * Method : 取消订单
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ public function cancelOrder(int $order_id)
|
|
|
+ {
|
|
|
+ $userId = Auth::id();
|
|
|
+ $order = Order::query()->find($order_id);
|
|
|
+ !$order && self::error('无效订单!');
|
|
|
+ $member = \App\Models\Member\User::query()->lockForUpdate()->find($userId);
|
|
|
+ $balance_price = $order['balance_price'] * 100;
|
|
|
+ $pay_price = $order['pay_price'] * 100;
|
|
|
+ $car_price = $order['car_price'] * 100;
|
|
|
+// $discount_price = $order['discount_price'] * 100;
|
|
|
+// $coupon_price = $order['coupon_price'] * 100;
|
|
|
+// $material_price = $order['material_price'] * 100;
|
|
|
+ $service_price = $order['service_price'] * 100;
|
|
|
+
|
|
|
+ // 退款金额
|
|
|
+// $benefit = $balance_price + $pay_price + $car_price;
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ $benefitData = [
|
|
|
+ 'user_id' => $userId,
|
|
|
+ 'order_id' => $order_id,
|
|
|
+ 'type' => 2,
|
|
|
+ 'remark' => '取消订单退款',
|
|
|
+ 'benefit' => 0,
|
|
|
+ 'balance' => $member['balance'] * 100
|
|
|
+ ];
|
|
|
+ // 订单状态 0:未支付 1:已支付(待接单) 2:已接单 3:已出发 4:已到达 5:开始服务(服务中) 6:服务结束 7:已撤离 8:已评价 9:取消订单(退款中) 10:已退款
|
|
|
+ switch ($order->status) {
|
|
|
+ case 1: // 已支付
|
|
|
+ $benefit = $balance_price + $pay_price;
|
|
|
+ $benefitData['benefit'] = $benefit;
|
|
|
+ Benefit::query()->create($benefitData);
|
|
|
+ $member->increment('balance', $benefit);
|
|
|
+ break;
|
|
|
+ case 2: // 已接单
|
|
|
+ $benefit = $balance_price + $pay_price;
|
|
|
+ $benefitData['benefit'] = $benefit;
|
|
|
+ Benefit::query()->create($benefitData);
|
|
|
+ $member->increment('balance', $benefit);
|
|
|
+
|
|
|
+ // 扣除20%服务费
|
|
|
+ $deduct_benefit = $service_price * 0.2;
|
|
|
+ $benefitData['type'] = 3;
|
|
|
+ $benefitData['remark'] = '技师已接单,取消订单扣除20%服务费';
|
|
|
+ $benefitData['benefit'] = $deduct_benefit;
|
|
|
+ Benefit::query()->create($benefitData);
|
|
|
+ $member->decrement('balance', $deduct_benefit);
|
|
|
+ break;
|
|
|
+ case 3: // 已出发
|
|
|
+ $benefit = $balance_price + $pay_price;
|
|
|
+ $benefitData['benefit'] = $benefit;
|
|
|
+ Benefit::query()->create($benefitData);
|
|
|
+ $member->increment('balance', $benefit);
|
|
|
+
|
|
|
+ // 扣除50%服务费
|
|
|
+ $deduct_benefit = $service_price * 0.5;
|
|
|
+ $benefitData['type'] = 3;
|
|
|
+ $benefitData['remark'] = '技师已出发,取消订单扣除50%服务费';
|
|
|
+ $benefitData['benefit'] = $deduct_benefit;
|
|
|
+ Benefit::query()->create($benefitData);
|
|
|
+ $member->decrement('balance', $deduct_benefit);
|
|
|
+
|
|
|
+ // 扣除车费
|
|
|
+ if ($car_price) {
|
|
|
+ $benefitData['type'] = 3;
|
|
|
+ $benefitData['remark'] = '技师已出发,扣除交通费用';
|
|
|
+ $benefitData['benefit'] = $car_price;
|
|
|
+ Benefit::query()->create($benefitData);
|
|
|
+ $member->decrement('balance', $car_price);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ self::error('订单状态错误!');
|
|
|
+ }
|
|
|
+ $order->status = 10;
|
|
|
+ $order->save();
|
|
|
+ DB::commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ self::error($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public function qrCode(array $params)
|
|
|
{
|
|
|
$result = Builder::create()
|
|
@@ -264,4 +433,34 @@ class OrderService extends Service
|
|
|
->build();
|
|
|
return $result;
|
|
|
}
|
|
|
+
|
|
|
+ public function getGrabList(int $id): array
|
|
|
+ {
|
|
|
+ $orderGrabPage = OrderGrab::query()->with('coach', function ($query) {
|
|
|
+ $query->select('id', 'nickname', 'avatar');
|
|
|
+ })->where('order_id', $id)->select(['id', 'coach_id', 'status', 'created_at'])->paginate(10);
|
|
|
+ return ['list' => $orderGrabPage->items(), 'total' => $orderGrabPage->total()];
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @throws ApiException
|
|
|
+ */
|
|
|
+ public function setGrab(int $id, int $coach_id): void
|
|
|
+ {
|
|
|
+ DB::beginTransaction();
|
|
|
+ try {
|
|
|
+ OrderGrab::query()->where('order_id', $id)->update(['status' => 0]);
|
|
|
+ $grabCoach = OrderGrab::query()->where('order_id', $id)->where('coach_id', $coach_id)->first();
|
|
|
+ $grabCoach->status = 1;
|
|
|
+ $grabCoach->save();
|
|
|
+// $order = Order::query()->lockForUpdate()->find($id);
|
|
|
+// $order['coach_id'] && self::error('已选择技师');
|
|
|
+// $order['coach_id'] = $coach_id;
|
|
|
+// $order->save();
|
|
|
+ DB::commit();
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ DB::rollBack();
|
|
|
+ self::error($e->getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|