doge 2 tygodni temu
rodzic
commit
5834c09d49

+ 6 - 2
app/Http/Controllers/admin/LhcOrder.php

@@ -134,7 +134,11 @@ class LhcOrder extends Controller
                 'id' => ['required', 'integer'],
                 'id' => ['required', 'integer'],
             ]);
             ]);
             $id = request()->input('id');
             $id = request()->input('id');
-            $info = LhcOrderModel::with(['items'])->where('id', $id)->first();
+            $info = LhcOrderModel::with(['items'])
+                ->leftJoin('users', 'users.member_id', '=', 'lhc_order.member_id')
+                ->where('lhc_order.id', $id)
+                ->select('lhc_order.*', 'users.first_name')
+                ->first();
             if (!$info) throw new Exception('订单不存在');
             if (!$info) throw new Exception('订单不存在');
             $info = $info->toArray();
             $info = $info->toArray();
 
 
@@ -232,4 +236,4 @@ class LhcOrder extends Controller
 
 
     }
     }
 
 
-}
+}

+ 21 - 1
app/Models/LhcOrder.php

@@ -26,6 +26,26 @@ class LhcOrder extends BaseModel
         return $this->hasMany(LhcOrderItem::class, 'ordernum', 'ordernum');
         return $this->hasMany(LhcOrderItem::class, 'ordernum', 'ordernum');
     }
     }
 
 
+    /**
+     * 历史订单主表未写入盈亏时,根据派奖金额和总投注额补齐接口返回值。
+     */
+    protected function getProfitAndLossAttribute($value): string
+    {
+        if ($value !== null && $value !== '') {
+            return (string)$value;
+        }
+
+        $status = (int)($this->attributes['lottery_status'] ?? self::STATUS_STAY);
+        if (!in_array($status, [self::STATUS_LOSS, self::STATUS_WIN], true)) {
+            return '0.00';
+        }
+
+        $winAmount = (string)($this->attributes['win_amount'] ?? 0);
+        $betAmount = $this->attributes['total_amount'] ?? $this->attributes['amount'] ?? 0;
+
+        return bcsub($winAmount, (string)$betAmount, 2);
+    }
+
     public static function getRemark($type)
     public static function getRemark($type)
     {
     {
         if ($type == 1) {
         if ($type == 1) {
@@ -58,4 +78,4 @@ class LhcOrder extends BaseModel
         return '';
         return '';
     }
     }
  
  
-}
+}

+ 47 - 0
tests/Unit/LhcOrderTest.php

@@ -0,0 +1,47 @@
+<?php
+
+namespace Tests\Unit;
+
+use App\Models\LhcOrder;
+use PHPUnit\Framework\TestCase;
+
+class LhcOrderTest extends TestCase
+{
+    public function test_it_preserves_stored_profit_and_loss(): void
+    {
+        $order = new LhcOrder();
+        $order->setRawAttributes([
+            'profit_and_loss' => '12.345',
+            'lottery_status' => LhcOrder::STATUS_WIN,
+        ]);
+
+        $this->assertSame('12.345', $order->profit_and_loss);
+    }
+
+    public function test_it_calculates_missing_profit_and_loss_for_settled_order(): void
+    {
+        $order = new LhcOrder();
+        $order->setRawAttributes([
+            'profit_and_loss' => null,
+            'lottery_status' => LhcOrder::STATUS_WIN,
+            'win_amount' => '150.00',
+            'total_amount' => '100.00',
+            'amount' => '50.00',
+        ]);
+
+        $this->assertSame('50.00', $order->profit_and_loss);
+    }
+
+    public function test_it_returns_zero_for_unsettled_order(): void
+    {
+        $order = new LhcOrder();
+        $order->setRawAttributes([
+            'profit_and_loss' => null,
+            'lottery_status' => LhcOrder::STATUS_STAY,
+            'win_amount' => '0.00',
+            'total_amount' => '100.00',
+        ]);
+
+        $this->assertSame('0.00', $order->profit_and_loss);
+    }
+}