醉梦人间三千年 6 месяцев назад
Родитель
Сommit
0040ae3507

+ 14 - 0
app/Http/Controllers/Frontend/Client/Service/OrderController.php

@@ -63,6 +63,20 @@ class OrderController extends Controller
         return self::success(true);
     }
 
+    public function confirm(Request $request): JsonResponse
+    {
+        $params = $request->all();
+        $res = $this->orderService->confirmOrder($params);
+        return self::success($res);
+    }
+
+    public function compute(Request $request)
+    {
+        $params = $request->all();
+        $res = $this->orderService->computePrice($params);
+        return self::success($res);
+    }
+
 }
 
 

+ 1 - 0
app/Http/Requests/Frontend/Client/Service/OrderRequest.php

@@ -28,6 +28,7 @@ class OrderRequest extends Request
             'orderId' => ['bail', 'string'],
             'payType' => ['bail', 'required', 'string'],
             'addressId' => ['bail', 'required', 'integer'],
+            'useBalance' => ['bail', 'integer'],
         ];
 
         $actionName = last(explode('@', Route::current()->getActionName()));

+ 98 - 18
app/Http/Services/Frontend/Client/Service/OrderService.php

@@ -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);
+
+    }
 }

+ 46 - 0
app/Models/Member/Benefit.php

@@ -0,0 +1,46 @@
+<?php
+
+namespace App\Models\Member;
+
+use Illuminate\Database\Eloquent\Factories\HasFactory;
+use Illuminate\Database\Eloquent\SoftDeletes;
+use Illuminate\Foundation\Auth\User as Authenticatable;
+use Illuminate\Notifications\Notifiable;
+use Laravel\Sanctum\HasApiTokens;
+use Spatie\Permission\Traits\HasRoles;
+
+class Benefit extends Authenticatable
+{
+    use HasFactory, Notifiable;
+
+    protected $table = 'member_benefit';
+
+    /**
+     * The attributes that are mass assignable.
+     *
+     * @var array<int, string>
+     */
+    protected $fillable = [];
+
+    protected $guarded = [];
+
+    /**
+     * The attributes that should be hidden for serialization.
+     *
+     * @var array<int, string>
+     */
+    protected $hidden = [
+        'password'
+    ];
+
+    /**
+     * Get the attributes that should be cast.
+     *
+     * @return array<string, string>
+     */
+    protected function casts(): array
+    {
+        return [];
+    }
+
+}

+ 2 - 1
app/Models/Service/Order.php

@@ -79,7 +79,8 @@ class Order extends Model
         $data['balance_price'] = 0;
         if ($use_balance) {
             // 用户余额
-            $user['balance'] = 0;
+            $memberQuery = \App\Models\Member\User::query();
+            $user = $memberQuery->find($user_id);
             // 余额抵扣金额
             $balance_price = $user['balance'] - $pay_price;
             if ($balance_price >= 0) {

+ 1 - 0
database/migrations/2024_09_04_022856_create_member_users_table.php

@@ -29,6 +29,7 @@ return new class extends Migration {
             $table->timestamp('birthday')->nullable()->comment('出生日期');
             $table->string('mark')->nullable()->comment('会员备注');
             $table->integer('point')->default(0)->comment('用户积分');
+
             $table->string('tag_ids')->nullable()->comment('用户标签编号列表,以逗号分隔');
             $table->bigInteger('level_id')->nullable()->comment('等级编号');
             $table->integer('experience')->default(0)->comment('用户经验');

+ 3 - 2
database/migrations/2024_09_23_025555_create_service_order_table.php

@@ -15,12 +15,13 @@ return new class extends Migration {
             $table->bigInteger('user_id')->comment('用户ID');
             $table->string('order_sn')->comment('订单编号');
             $table->tinyInteger('pay_type')->comment('支付类型');
-            $table->string('transaction_id')->comment('商户订单号');
+            $table->string('transaction_id')->nullable()->comment('商户订单号');
             $table->bigInteger('project_id')->comment('项目ID');
             $table->bigInteger('area_id')->comment('区域ID');
+//            $table->string('city_code');
 
             $table->string('project_name')->comment('项目名称');
-            $table->string('project_icon')->comment('项目图标');
+            $table->string('project_icon')->nullable()->comment('项目图标');
 
             $table->integer('pay_price')->default('0.00')->nullable()->comment('支付金额');
             $table->integer('balance_price')->default('0.00')->nullable()->comment('余额金额');

+ 1 - 1
database/migrations/2024_09_30_025556_create_system_config_table.php

@@ -11,7 +11,7 @@ return new class extends Migration {
     public function up(): void
     {
         Schema::create('system_config', function (Blueprint $table) {
-            $table->string('name')->nullable()->comment('配置名称')->primary();
+            $table->string('name')->comment('配置名称')->primary();
             $table->string('model')->nullable()->comment('配置模块')->index();
             $table->index('model', 'system_model_foreign_key_index');
             $table->string('value')->nullable()->comment('配置值');

+ 33 - 0
database/migrations/2024_10_01_025556_create_member_benefit_table.php

@@ -0,0 +1,33 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration {
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('member_benefit', function (Blueprint $table) {
+            $table->id();
+            $table->bigInteger('user_id')->comment('用户ID');
+            $table->bigInteger('order_id')->nullable()->comment('订单ID');
+            $table->bigInteger('withdraw_id')->nullable()->comment('提现ID');
+            $table->tinyInteger('type')->nullable()->comment('订单类型 1-支付 2-退款 3-扣除 4-提现 5-返还 6-收益 8-赠送');
+            $table->text('remark')->nullable()->comment('备注');
+            $table->integer('benefit')->default(0)->comment('收益');
+            $table->integer('balance')->default(0)->comment('余额');
+            $table->timestamps();
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('member_benefit');
+    }
+};

+ 4 - 4
routes/api.php

@@ -56,7 +56,10 @@ Route::prefix('client')->group(function () {
 
         Route::middleware(['auth:sanctum'])->group(function () {
             # 订单
+            Route::get('order/confirm', [ClientServiceOrderController::class, 'confirm']);
+            Route::get('order/compute', [ClientServiceOrderController::class, 'compute']);
             Route::resource('order', ClientServiceOrderController::class);
+
         });
 
     });
@@ -70,7 +73,7 @@ Route::prefix('client')->group(function () {
     });
 
     Route::prefix('member')->middleware(['auth:sanctum'])->group(function () {
-        Route::get('address/default', [ClientMemberAddressController::class,'default']);
+        Route::get('address/default', [ClientMemberAddressController::class, 'default']);
         Route::resource('address', ClientMemberAddressController::class);
         Route::post('upload', [ClientMemberToolController::class, "upload"]);
         Route::get('/', [ClientMemberUserController::class, 'show']);
@@ -79,9 +82,6 @@ Route::prefix('client')->group(function () {
     });
 
 
-
-
-
 });
 
 # 技师端