mirror of
https://github.com/v2board/v2board.git
synced 2025-06-15 14:17:48 +08:00
update: plan surplus feature
This commit is contained in:
@ -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),
|
||||
|
@ -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',
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
Reference in New Issue
Block a user