醉梦人间三千年 6 månader sedan
förälder
incheckning
c62f4c1dec

+ 4 - 1
app/Http/Controllers/Frontend/Client/Service/OrderController.php

@@ -70,7 +70,10 @@ class OrderController extends Controller
         return self::success($res);
     }
 
-    public function compute(Request $request)
+    /**
+     * @throws ApiException
+     */
+    public function compute(Request $request): JsonResponse
     {
         $params = $request->all();
         $res = $this->orderService->computePrice($params);

+ 19 - 7
app/Http/Services/Frontend/Client/Service/OrderService.php

@@ -232,16 +232,28 @@ class OrderService extends Service
         ];
     }
 
-    public function computePrice(array $params): void
+    /**
+     * @throws ApiException
+     */
+    public function computePrice(array $params): array
     {
         $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);
+        $addressId = $params['addressId'];
+        $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);
+
+        // 获取地址
+        $address = Address::query()->where('user_id', $user_id)->find($addressId);
+        !$address && self::error('地址数据错误');
         $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);
+        $orderPayInfo = $orderModel->buildPayInfo($user_id, $params['projectId'], $use_balance, $coach_id, $car_type, $coupon_id, $distance);
+
 
+        return $orderPayInfo;
     }
 }

+ 5 - 0
app/Models/Member/User.php

@@ -46,6 +46,11 @@ class User extends Authenticatable
         ];
     }
 
+    public function getBalanceAttribute($value)
+    {
+        return number_format($value / 100, 2);
+    }
+
     public function coach(): \Illuminate\Database\Eloquent\Relations\HasOne
     {
         return $this->hasOne(\App\Models\Coach\User::class, 'user_id');

+ 37 - 17
app/Models/Service/Order.php

@@ -21,7 +21,7 @@ class Order extends Model
         'end_time' => 'datetime'
     ];
 
-    public function buildPayInfo($user_id, $project_id, $area_id, $lat, $lng,$use_balance = 0, $coach_id = null, $car_type = 0, $coupon_id = 0, $order_id = 0): array
+    public function buildPayInfo($user_id, $project_id, $use_balance = 0, $coach_id = null, $car_type = 0, $coupon_id = 0, $order_id = 0, $distance = 0): array
     {
 
         $data['project_id'] = $project_id;
@@ -39,56 +39,76 @@ class Order extends Model
         // 优惠卷
         $data['coupon_id'] = $coupon_id ?: 0;
         // 优惠金额
-        $data['coupon_price'] = 0;
+        $data['coupon_price'] = number_format(0, 2);
 
         // 折扣金额
-        $data['discount_price'] = 0;
+        $data['discount_price'] = number_format(0, 2);
 
         // 物料费
-        $data['material_price'] = $project['material_price'] ?? 0;
+        $data['material_price'] = $project['material_price'] ?? number_format(0, 2);
 
         // 车费默认值
-        $data['car_price'] = 0;
+        $data['car_price'] = number_format(0, 2);
 
         // 选择技师
         if ($coach) {
             $data['coach_id'] = $coach_id;
 
-            if ($lat && $lng) {
-                // 订单距离
+//            if ($lat && $lng) {
+            // 订单距离
 //        $data['distance'] = getDriveDistance($coach['lng'], $coach['lat'], $lng, $lat, $coach['uniacid']);
 
 //        $data['distance'] += $data['car_config']['invented_distance'] * $data['distance'] / 100;
-                //车费
-                $data['car_price'] = 0;
+            //车费
+//                $data['car_price'] = 0;
 //        $data['car_price'] = $this->getCarPrice($data['distance'], $data['car_config'], $car_type);
 
-            }
+//            }
+
+
+
+        }
+
+        // 距离
+        if ($distance) {
+            // 获取技师信息
+            // 获取车费配置
+            $baseCarPrice = 900;// 起步
+            $baseDistance = 3; // 公里
+            $carPrice = 300; // 每公里
+            $freeDistance = 3; // 免费公里
+            $distanceLen = $distance / 1000;
+            $distancePrice = $distanceLen <= $freeDistance ? 0 : ($distanceLen > $baseDistance ? ($distanceLen - $baseDistance) * $carPrice + $baseCarPrice : $baseCarPrice);
+            $data['car_price'] = number_format(round($distancePrice / 100), 2);
         }
+
         // 订单支付价
         // 订单总价格
-        $pay_total_price = $project['price'] + $data['car_price'] + $data['material_price'];
+        $pay_total_price = round($project['price'], 2) + round($data['car_price'], 2) + round($data['material_price'], 2);
+
         // 订单总优惠
-        $coupon_total_price = $data['coupon_price'] + $data['discount_price'];
+        $coupon_total_price = intval($data['coupon_price']) + intval($data['discount_price']);
 
         // 支付金额
         $pay_price = $pay_total_price - $coupon_total_price;
         $pay_price <= 0 && ($pay_price = 0);
-        $data['pay_price'] = $pay_price;
+        $data['pay_price'] = number_format($pay_price, 2);
+
         // 余额支付
         $data['balance_price'] = 0;
         if ($use_balance) {
             // 用户余额
             $memberQuery = \App\Models\Member\User::query();
             $user = $memberQuery->find($user_id);
+
             // 余额抵扣金额
-            $balance_price = $user['balance'] - $pay_price;
+            $balance_price = round($user['balance'], 2) - round($pay_price, 2);
             if ($balance_price >= 0) {
-                $data['balance_price'] = $pay_price;
-                $data['pay_price'] = 0;
+                $data['balance_price'] = number_format($pay_price, 2);
+                $data['pay_price'] = number_format(0, 2);
             } else {
                 $data['balance_price'] = $user['balance'];
-                $data['pay_price'] = abs($balance_price);
+                $data['pay_price'] = number_format(abs($balance_price), 2);
             }
         }
 

+ 17 - 0
app/Models/Service/Project.php

@@ -22,6 +22,23 @@ class Project extends Model
         'isDoor' => 'bool'
     ];
 
+    public function getPriceAttribute($value)
+    {
+        return number_format($value / 100, 2);
+    }
+
+    public function getInitPriceAttribute($value)
+    {
+        return number_format($value / 100, 2);
+    }
+
+    public function getMaterialPriceAttribute($value)
+    {
+        return number_format($value / 100, 2);
+
+    }
+
+
     public function getCoachProjectStatus($coach_id)
     {
         $status = DB::table('service_project_has_coach')->where('project_id', $this->attributes['id'])

+ 5 - 5
database/migrations/2024_09_23_025555_create_service_order_table.php

@@ -23,11 +23,11 @@ return new class extends Migration {
             $table->string('project_name')->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('余额金额');
-            $table->integer('discount_price')->default('0.00')->nullable()->comment('折扣金额');
-            $table->integer('coupon_price')->default('0.00')->nullable()->comment('优惠金额');
-            $table->integer('car_price')->default('0.00')->nullable()->comment('车费金额');
+            $table->integer('pay_price')->default('0')->nullable()->comment('支付金额');
+            $table->integer('balance_price')->default('0')->nullable()->comment('余额金额');
+            $table->integer('discount_price')->default('0')->nullable()->comment('折扣金额');
+            $table->integer('coupon_price')->default('0')->nullable()->comment('优惠金额');
+            $table->integer('car_price')->default('0')->nullable()->comment('车费金额');
             $table->integer('material_price')->default('0.00')->nullable()->comment('物料金额');
             $table->integer('service_price')->default('0.00')->nullable()->comment('服务金额');