update: plan surplus feature

This commit is contained in:
v2board 2023-08-20 03:21:33 +08:00
parent 31c5cf1c2b
commit 3f24ba9917
7 changed files with 29 additions and 59 deletions

View File

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

View File

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

View File

@ -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)) { } 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, '目前不允许更改订阅,请联系客服或提交工单操作'); if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
$order->type = 3; $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) { if ($order->surplus_amount >= $order->total_amount) {
$order->refund_amount = $order->surplus_amount - $order->total_amount; $order->refund_amount = $order->surplus_amount - $order->total_amount;
$order->total_amount = 0; $order->total_amount = 0;
@ -156,63 +156,26 @@ class OrderService
private function getSurplusValue(User $user, Order $order) private function getSurplusValue(User $user, Order $order)
{ {
if ($user->expired_at === NULL) { $plan = Plan::find($user->plan_id);
$this->getSurplusValueByOneTime($user, $order); if (!$plan) return;
} else { if ($user->expired_at) $this->getSurplusValueByTime($user, $order, $plan);
$this->getSurplusValueByPeriod($user, $order); $this->getSurplusValueByTransfer($user, $order, $plan);
}
} }
private function getSurplusValueByTime(User $user, Order $order, Plan $plan)
private function getSurplusValueByOneTime(User $user, Order $order)
{ {
$lastOneTimeOrder = Order::where('user_id', $user->id) if (!$plan['daily_unit_price']) return;
->where('period', 'onetime_price') $timeLeftDays = $user['expired_at'] - time() / 86400;
->where('status', 3) if (!$timeLeftDays) return;
->orderBy('id', 'DESC') $order->surplus_amount = $order->surplus_amount + ($timeLeftDays * $plan['daily_unit_price']);
->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');
} }
private function getSurplusValueByPeriod(User $user, Order $order) private function getSurplusValueByTransfer(User $user, Order $order, Plan $plan)
{ {
$orders = Order::where('user_id', $user->id) if (!$plan['transfer_unit_price']) return;
->where('period', '!=', 'reset_price') $transferLeft = ($user['transfer_enable'] - $user['u'] + $user['d']) / 1073741824;
->where('period', '!=', 'onetime_price') if (!$transferLeft) return;
->where('status', 3) $order->surplus_amount = $order->surplus_amount + ($transferLeft * $plan['transfer_unit_price']);
->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');
} }
public function paid(string $callbackNo) public function paid(string $callbackNo)

View File

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

View File

@ -143,7 +143,6 @@ CREATE TABLE `v2_order` (
`surplus_amount` int(11) DEFAULT NULL COMMENT '剩余价值', `surplus_amount` int(11) DEFAULT NULL COMMENT '剩余价值',
`refund_amount` int(11) DEFAULT NULL COMMENT '退款金额', `refund_amount` int(11) DEFAULT NULL COMMENT '退款金额',
`balance_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已折抵', `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_status` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0待确认1发放中2有效3无效',
`commission_balance` int(11) NOT NULL DEFAULT '0', `commission_balance` int(11) NOT NULL DEFAULT '0',
@ -196,6 +195,8 @@ CREATE TABLE `v2_plan` (
`reset_price` int(11) DEFAULT NULL, `reset_price` int(11) DEFAULT NULL,
`reset_traffic_method` tinyint(1) DEFAULT NULL, `reset_traffic_method` tinyint(1) DEFAULT NULL,
`capacity_limit` int(11) 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, `created_at` int(11) NOT NULL,
`updated_at` int(11) NOT NULL, `updated_at` int(11) NOT NULL,
PRIMARY KEY (`id`) PRIMARY KEY (`id`)
@ -469,4 +470,4 @@ CREATE TABLE `v2_user` (
) ENGINE=InnoDB DEFAULT CHARSET=utf8; ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- 2023-07-17 07:38:59 -- 2023-09-06 07:17:40

View File

@ -713,3 +713,10 @@ CREATE TABLE `v2_server_vless` (
ALTER TABLE `v2_server_vless` ALTER TABLE `v2_server_vless`
CHANGE `flow` `flow` varchar(64) COLLATE 'utf8mb4_general_ci' NULL AFTER `tls_settings`; 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 one or more lines are too long