123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- <?php
- /**
- * @Name
- * @Description
- * @Author 刘学玺
- * @Date 2024/9/26 11:11
- */
- 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
- {
- protected array $baseColumn = ['id', 'project_name', 'project_icon', 'time_long', 'pay_type', 'pay_price', 'real_address', 'status', 'created_at'];
- function buildOrderSN(): string
- {
- $random = rand(1, 999999);
- return date('YmdHis') . str_repeat('0', 6 - strlen($random)) . $random;
- }
- /**
- * @throws ApiException
- */
- public function createOrder(array $params)
- {
- $data = ['user_id' => Auth::id()];
- // 用户地址
- $address = Address::query()->where('user_id', $data['user_id'])->find($params['addressId']);
- !$address && self::error('地址信息错误');
- $data['address'] = $address['address'];
- $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'];
- // $coupon_record_model = new CouponRecord();
- // 渠道
- // $channel_model = new UserChannel();
- // 用户渠道
- // $input['channel_id'] = $channel_model->getChannelId($this->_user['id']);
- // 订单号
- $order_id = $params['orderId'] ?? null;
- // 使用余额
- $use_balance = $params['useBalance'] ?? 0;
- // 出行方式
- $car_type = $params['carType'] ?? 0;
- // 技师
- $coach_id = $params['coachId'] ?? null;
- if ($coach_id) {
- $coach_model = new User();
- $coach_info = $coach_model->info($coach_id);
- !$coach_info && self::error('技师不存在');
- !empty($coach_info) && $coach_info['is_work'] == 0 && self::error('该技师未上班');
- !empty($coach_info) && ($coach_info['auth_status'] !== 2 || $coach_info['status'] !== 1) && self::error('该技师已下架');
- }
- // 优惠卷ID
- $coupon_id = $params['couponId'] ?? 0;
- // 加钟订单
- if ($order_id) {
- } else {
- // $address = $address_model->info($params['address_id']);
- // if(empty($address)){
- // $this->errorMsg('请添加地址');
- // }
- }
- $orderModel = new Order();
- 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,
- ];
- // 创建订单
- $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();
- self::error();
- }
- }
- public function getOrderPage(array $data)
- {
- $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]
- };
- // $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();
- 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);
- }
- /**
- * @throws ApiException
- */
- public function updateOrder(array $data): void
- {
- $order_id = $data['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;
- }
- $order->status = $status;
- $order->save();
- }
- public function delOrder(int $order_id): void
- {
- $order = Order::query()->find($order_id);
- $order->user_del = 1;
- $order->save();
- }
- public function confirmOrder(array $params)
- {
- $user_id = Auth::id();
- $projectId = $params['projectId'];
- $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
- ];
- }
- /**
- * @throws ApiException
- */
- public function computePrice(array $params): array
- {
- $user_id = Auth::id();
- $use_balance = $params['useBalance'] ?? 0;
- $coach_id = $params['coachId'] ?? 0;
- $distance = $params['distance'] ?? 0;
- $car_type = $params['carType'] ?? 0;
- $coupon_id = $params['couponId'] ?? 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);
- return $orderPayInfo;
- }
- }
|