Browse Source

update: plan surplus feature

v2board 2 years ago
parent
commit
3f24ba9917

+ 0 - 1
app/Http/Controllers/V1/Admin/ConfigController.php

@@ -101,7 +101,6 @@ class ConfigController extends Controller
             'subscribe' => [
                 'plan_change_enable' => (int)config('v2board.plan_change_enable', 1),
                 'reset_traffic_method' => (int)config('v2board.reset_traffic_method', 0),
-                'surplus_enable' => (int)config('v2board.surplus_enable', 1),
                 'new_order_event_id' => (int)config('v2board.new_order_event_id', 0),
                 'renew_order_event_id' => (int)config('v2board.renew_order_event_id', 0),
                 'change_order_event_id' => (int)config('v2board.change_order_event_id', 0),

+ 0 - 1
app/Http/Requests/Admin/ConfigSave.php

@@ -38,7 +38,6 @@ class ConfigSave extends FormRequest
         // subscribe
         'plan_change_enable' => 'in:0,1',
         'reset_traffic_method' => 'in:0,1,2,3,4',
-        'surplus_enable' => 'in:0,1',
         'new_order_event_id' => 'in:0,1',
         'renew_order_event_id' => 'in:0,1',
         'change_order_event_id' => 'in:0,1',

+ 15 - 52
app/Services/OrderService.php

@@ -95,7 +95,7 @@ class OrderService
         } else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
             if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
             $order->type = 3;
-            if ((int)config('v2board.surplus_enable', 1)) $this->getSurplusValue($user, $order);
+            $this->getSurplusValue($user, $order);
             if ($order->surplus_amount >= $order->total_amount) {
                 $order->refund_amount = $order->surplus_amount - $order->total_amount;
                 $order->total_amount = 0;
@@ -156,63 +156,26 @@ class OrderService
 
     private function getSurplusValue(User $user, Order $order)
     {
-        if ($user->expired_at === NULL) {
-            $this->getSurplusValueByOneTime($user, $order);
-        } else {
-            $this->getSurplusValueByPeriod($user, $order);
-        }
+        $plan = Plan::find($user->plan_id);
+        if (!$plan) return;
+        if ($user->expired_at) $this->getSurplusValueByTime($user, $order, $plan);
+        $this->getSurplusValueByTransfer($user, $order, $plan);
     }
 
-
-    private function getSurplusValueByOneTime(User $user, Order $order)
+    private function getSurplusValueByTime(User $user, Order $order, Plan $plan)
     {
-        $lastOneTimeOrder = Order::where('user_id', $user->id)
-            ->where('period', 'onetime_price')
-            ->where('status', 3)
-            ->orderBy('id', 'DESC')
-            ->first();
-        if (!$lastOneTimeOrder) return;
-        $nowUserTraffic = $user->transfer_enable / 1073741824;
-        if (!$nowUserTraffic) return;
-        $paidTotalAmount = ($lastOneTimeOrder->total_amount + $lastOneTimeOrder->balance_amount);
-        if (!$paidTotalAmount) return;
-        $trafficUnitPrice = $paidTotalAmount / $nowUserTraffic;
-        $notUsedTraffic = $nowUserTraffic - (($user->u + $user->d) / 1073741824);
-        $result = $trafficUnitPrice * $notUsedTraffic;
-        $orderModel = Order::where('user_id', $user->id)->where('period', '!=', 'reset_price')->where('status', 3);
-        $order->surplus_amount = $result > 0 ? $result : 0;
-        $order->surplus_order_ids = array_column($orderModel->get()->toArray(), 'id');
+        if (!$plan['daily_unit_price']) return;
+        $timeLeftDays = $user['expired_at'] - time() / 86400;
+        if (!$timeLeftDays) return;
+        $order->surplus_amount = $order->surplus_amount + ($timeLeftDays * $plan['daily_unit_price']);
     }
 
-    private function getSurplusValueByPeriod(User $user, Order $order)
+    private function getSurplusValueByTransfer(User $user, Order $order, Plan $plan)
     {
-        $orders = Order::where('user_id', $user->id)
-            ->where('period', '!=', 'reset_price')
-            ->where('period', '!=', 'onetime_price')
-            ->where('status', 3)
-            ->get()
-            ->toArray();
-        if (!$orders) return;
-        $orderAmountSum = 0;
-        $orderMonthSum = 0;
-        $lastValidateAt = 0;
-        foreach ($orders as $item) {
-            $period = self::STR_TO_TIME[$item['period']];
-            if (strtotime("+{$period} month", $item['created_at']) < time()) continue;
-            $lastValidateAt = $item['created_at'];
-            $orderMonthSum = $period + $orderMonthSum;
-            $orderAmountSum = $orderAmountSum + ($item['total_amount'] + $item['balance_amount'] + $item['surplus_amount'] - $item['refund_amount']);
-        }
-        if (!$lastValidateAt) return;
-        $expiredAtByOrder = strtotime("+{$orderMonthSum} month", $lastValidateAt);
-        if ($expiredAtByOrder < time()) return;
-        $orderSurplusSecond = $expiredAtByOrder - time();
-        $orderRangeSecond = $expiredAtByOrder - $lastValidateAt;
-        $avgPrice = $orderAmountSum / $orderRangeSecond;
-        $orderSurplusAmount = $avgPrice * $orderSurplusSecond;
-        if (!$orderSurplusSecond || !$orderSurplusAmount) return;
-        $order->surplus_amount = $orderSurplusAmount > 0 ? $orderSurplusAmount : 0;
-        $order->surplus_order_ids = array_column($orders, 'id');
+        if (!$plan['transfer_unit_price']) return;
+        $transferLeft = ($user['transfer_enable'] - $user['u'] + $user['d']) / 1073741824;
+        if (!$transferLeft) return;
+        $order->surplus_amount = $order->surplus_amount + ($transferLeft * $plan['transfer_unit_price']);
     }
 
     public function paid(string $callbackNo)

+ 3 - 2
app/Services/UserService.php

@@ -175,8 +175,9 @@ class UserService
         $statService->setUserStats();
         $statService->setServerStats();
         foreach (array_keys($data) as $userId) {
-            $u = $data[$userId][0];
-            $d = $data[$userId][1];
+            $u = (int)$data[$userId][0] ?? 0;
+            $d = (int)$data[$userId][1] ?? 0;
+            if (!$u && !$d) continue;
             TrafficFetchJob::dispatch($u, $d, $userId, $server, $protocol);
             $statService->statServer($server['id'], $protocol, $u, $d);
             $statService->statUser($server['rate'], $userId, $u, $d);

+ 3 - 2
database/install.sql

@@ -143,7 +143,6 @@ CREATE TABLE `v2_order` (
                             `surplus_amount` int(11) DEFAULT NULL COMMENT '剩余价值',
                             `refund_amount` int(11) DEFAULT NULL COMMENT '退款金额',
                             `balance_amount` int(11) DEFAULT NULL COMMENT '使用余额',
-                            `surplus_order_ids` text COMMENT '折抵订单',
                             `status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待支付1开通中2已取消3已完成4已折抵',
                             `commission_status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待确认1发放中2有效3无效',
                             `commission_balance` int(11) NOT NULL DEFAULT '0',
@@ -196,6 +195,8 @@ CREATE TABLE `v2_plan` (
                            `reset_price` int(11) DEFAULT NULL,
                            `reset_traffic_method` tinyint(1) DEFAULT NULL,
                            `capacity_limit` int(11) DEFAULT NULL,
+                           `daily_unit_price` int(11) DEFAULT NULL,
+                           `transfer_unit_price` int(11) DEFAULT NULL,
                            `created_at` int(11) NOT NULL,
                            `updated_at` int(11) NOT NULL,
                            PRIMARY KEY (`id`)
@@ -469,4 +470,4 @@ CREATE TABLE `v2_user` (
 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
 
--- 2023-07-17 07:38:59
+-- 2023-09-06 07:17:40

+ 7 - 0
database/update.sql

@@ -713,3 +713,10 @@ CREATE TABLE `v2_server_vless` (
 
 ALTER TABLE `v2_server_vless`
     CHANGE `flow` `flow` varchar(64) COLLATE 'utf8mb4_general_ci' NULL AFTER `tls_settings`;
+
+ALTER TABLE `v2_plan`
+    ADD `daily_unit_price` int(11) NULL AFTER `capacity_limit`,
+ADD `transfer_unit_price` int(11) NULL AFTER `daily_unit_price`;
+
+ALTER TABLE `v2_order`
+DROP `surplus_order_ids`;

File diff suppressed because it is too large
+ 0 - 0
public/assets/admin/umi.js


Some files were not shown because too many files changed in this diff