v2board/app/Services/OrderService.php

311 lines
10 KiB
PHP
Raw Normal View History

2020-03-17 19:00:33 +08:00
<?php
namespace App\Services;
2021-09-04 15:31:25 +08:00
use App\Jobs\OrderHandleJob;
2020-03-17 19:00:33 +08:00
use App\Models\Order;
2020-04-22 16:57:00 +08:00
use App\Models\Plan;
use App\Models\User;
2022-04-10 18:08:26 +08:00
use App\Utils\CacheKey;
use Illuminate\Support\Facades\Cache;
2020-03-17 20:00:46 +08:00
use Illuminate\Support\Facades\DB;
2020-03-17 19:00:33 +08:00
class OrderService
{
2020-10-11 01:22:43 +08:00
CONST STR_TO_TIME = [
2020-09-16 20:31:30 +08:00
'month_price' => 1,
'quarter_price' => 3,
'half_year_price' => 6,
'year_price' => 12,
'two_year_price' => 24,
'three_year_price' => 36
];
2020-03-17 19:00:33 +08:00
public $order;
2021-07-01 21:04:22 +08:00
public $user;
2020-03-17 19:00:33 +08:00
public function __construct(Order $order)
{
$this->order = $order;
}
2020-10-04 14:21:09 +08:00
public function open()
{
$order = $this->order;
2021-07-01 21:04:22 +08:00
$this->user = User::find($order->user_id);
2020-10-04 14:21:09 +08:00
$plan = Plan::find($order->plan_id);
if ($order->refund_amount) {
2021-07-01 21:04:22 +08:00
$this->user->balance = $this->user->balance + $order->refund_amount;
2020-10-04 14:21:09 +08:00
}
DB::beginTransaction();
if ($order->surplus_order_ids) {
try {
2021-08-06 00:43:01 +08:00
Order::whereIn('id', $order->surplus_order_ids)->update([
2020-10-04 14:21:09 +08:00
'status' => 4
]);
} catch (\Exception $e) {
DB::rollback();
abort(500, '开通失败');
}
}
2021-12-28 01:49:33 +08:00
switch ((string)$order->period) {
2020-10-04 14:21:09 +08:00
case 'onetime_price':
2021-07-01 21:04:22 +08:00
$this->buyByOneTime($plan);
2020-10-04 14:21:09 +08:00
break;
case 'reset_price':
2021-07-01 21:04:22 +08:00
$this->buyByResetTraffic();
2020-10-04 14:21:09 +08:00
break;
default:
2021-12-28 01:49:33 +08:00
$this->buyByPeriod($order, $plan);
2020-10-04 14:21:09 +08:00
}
2020-10-11 00:28:09 +08:00
2021-07-01 21:04:22 +08:00
switch ((int)$order->type) {
case 1:
$this->openEvent(config('v2board.new_order_event_id', 0));
break;
case 2:
$this->openEvent(config('v2board.renew_order_event_id', 0));
break;
case 3:
$this->openEvent(config('v2board.change_order_event_id', 0));
break;
}
2020-10-11 00:28:09 +08:00
2022-11-26 22:19:19 +08:00
$this->setSpeedLimit($plan->speed_limit);
2021-07-01 21:04:22 +08:00
if (!$this->user->save()) {
2020-10-04 14:21:09 +08:00
DB::rollBack();
abort(500, '开通失败');
}
$order->status = 3;
if (!$order->save()) {
DB::rollBack();
abort(500, '开通失败');
}
DB::commit();
}
2020-04-09 13:35:27 +08:00
2020-04-22 16:57:00 +08:00
public function setOrderType(User $user)
{
$order = $this->order;
2021-12-28 01:49:33 +08:00
if ($order->period === 'reset_price') {
2020-04-25 19:44:47 +08:00
$order->type = 4;
2020-09-10 13:01:55 +08:00
} else if ($user->plan_id !== NULL && $order->plan_id !== $user->plan_id && ($user->expired_at > time() || $user->expired_at === NULL)) {
2020-04-25 19:44:47 +08:00
if (!(int)config('v2board.plan_change_enable', 1)) abort(500, '目前不允许更改订阅,请联系客服或提交工单操作');
2020-04-22 16:57:00 +08:00
$order->type = 3;
if ((int)config('v2board.surplus_enable', 1)) $this->getSurplusValue($user, $order);
2020-04-22 16:57:00 +08:00
if ($order->surplus_amount >= $order->total_amount) {
$order->refund_amount = $order->surplus_amount - $order->total_amount;
$order->total_amount = 0;
} else {
$order->total_amount = $order->total_amount - $order->surplus_amount;
}
2020-07-13 17:09:13 +08:00
} else if ($user->expired_at > time() && $order->plan_id == $user->plan_id) { // 用户订阅未过期且购买订阅与当前订阅相同 === 续费
2020-04-22 16:57:00 +08:00
$order->type = 2;
2020-07-13 17:09:13 +08:00
} else { // 新购
2020-04-22 16:57:00 +08:00
$order->type = 1;
}
}
public function setVipDiscount(User $user)
{
$order = $this->order;
if ($user->discount) {
$order->discount_amount = $order->discount_amount + ($order->total_amount * ($user->discount / 100));
}
$order->total_amount = $order->total_amount - $order->discount_amount;
}
public function setInvite(User $user):void
2020-04-22 16:57:00 +08:00
{
$order = $this->order;
2022-03-04 22:48:44 +08:00
if ($user->invite_user_id && ($order->total_amount <= 0)) return;
$order->invite_user_id = $user->invite_user_id;
$inviter = User::find($user->invite_user_id);
2022-03-05 12:40:11 +08:00
if (!$inviter) return;
2022-03-04 22:48:44 +08:00
$isCommission = false;
switch ((int)$inviter->commission_type) {
case 0:
$commissionFirstTime = (int)config('v2board.commission_first_time_enable', 1);
$isCommission = (!$commissionFirstTime || ($commissionFirstTime && !$this->haveValidOrder($user)));
break;
case 1:
$isCommission = true;
break;
case 2:
$isCommission = !$this->haveValidOrder($user);
break;
}
2022-03-05 12:40:11 +08:00
if (!$isCommission) return;
2022-03-04 22:48:44 +08:00
if ($inviter && $inviter->commission_rate) {
$order->commission_balance = $order->total_amount * ($inviter->commission_rate / 100);
} else {
$order->commission_balance = $order->total_amount * (config('v2board.invite_commission', 10) / 100);
2020-04-22 16:57:00 +08:00
}
}
2020-11-17 16:34:10 +08:00
private function haveValidOrder(User $user)
{
return Order::where('user_id', $user->id)
2021-03-13 00:03:53 +08:00
->whereNotIn('status', [0, 2])
2020-11-17 16:34:10 +08:00
->first();
}
2020-04-22 16:57:00 +08:00
private function getSurplusValue(User $user, Order $order)
{
if ($user->expired_at === NULL) {
$this->getSurplusValueByOneTime($user, $order);
} else {
2021-12-28 01:49:33 +08:00
$this->getSurplusValueByPeriod($user, $order);
2020-04-22 16:57:00 +08:00
}
}
private function getSurplusValueByOneTime(User $user, Order $order)
{
2022-02-26 00:42:14 +08:00
$lastOneTimeOrder = Order::where('user_id', $user->id)
2022-04-09 16:05:53 +08:00
->where('period', 'onetime_price')
2022-02-26 00:42:14 +08:00
->where('status', 3)
->orderBy('id', 'DESC')
->first();
if (!$lastOneTimeOrder) return;
2022-04-29 16:56:22 +08:00
$nowUserTraffic = $user->transfer_enable / 1073741824;
2022-04-30 02:38:22 +08:00
if (!$nowUserTraffic) return;
$paidTotalAmount = ($lastOneTimeOrder->total_amount + $lastOneTimeOrder->balance_amount);
if (!$paidTotalAmount) return;
$trafficUnitPrice = $paidTotalAmount / $nowUserTraffic;
2022-04-29 16:56:22 +08:00
$notUsedTraffic = $nowUserTraffic - (($user->u + $user->d) / 1073741824);
2020-09-11 01:02:31 +08:00
$result = $trafficUnitPrice * $notUsedTraffic;
2021-12-28 01:49:33 +08:00
$orderModel = Order::where('user_id', $user->id)->where('period', '!=', 'reset_price')->where('status', 3);
2020-04-22 16:57:00 +08:00
$order->surplus_amount = $result > 0 ? $result : 0;
2021-08-06 00:43:01 +08:00
$order->surplus_order_ids = array_column($orderModel->get()->toArray(), 'id');
2020-04-22 16:57:00 +08:00
}
2021-12-28 01:49:33 +08:00
private function getSurplusValueByPeriod(User $user, Order $order)
2020-04-22 16:57:00 +08:00
{
2020-04-30 16:08:26 +08:00
$orderModel = Order::where('user_id', $user->id)
2021-12-28 01:49:33 +08:00
->where('period', '!=', 'reset_price')
2020-04-30 16:08:26 +08:00
->where('status', 3);
2020-09-16 20:31:30 +08:00
$orders = $orderModel->get();
2023-01-19 12:44:07 +08:00
$orderAmountSum = 0;
$orderMonthSum = 0;
2020-09-16 20:31:30 +08:00
foreach ($orders as $k => $item) {
2021-12-28 01:49:33 +08:00
if ($item->period === 'onetime_price') continue;
2023-01-19 12:44:07 +08:00
$orderMonthSum = self::STR_TO_TIME[$item->period] + $orderMonthSum;
$orderAmountSum = $orderAmountSum + ($item['total_amount'] + $item['balance_amount'] + $item['surplus_amount'] - $item['refund_amount']);
2020-05-02 14:29:04 +08:00
}
2023-01-19 12:44:07 +08:00
$expiredAtByOrder = strtotime("+{$orderMonthSum} month", $orders[0]->created_at);
if ($expiredAtByOrder < time()) return;
$orderSurplusSecond = $expiredAtByOrder - time();
$orderRangeSecond = $expiredAtByOrder - $orders[0]->created_at;
$avgPrice = $orderAmountSum / $orderRangeSecond;
$orderSurplusAmount = $avgPrice * $orderSurplusSecond;
if (!$orderSurplusSecond || !$orderSurplusAmount) return;
2020-06-11 20:47:02 +08:00
$order->surplus_amount = $orderSurplusAmount > 0 ? $orderSurplusAmount : 0;
2021-08-06 00:43:01 +08:00
$order->surplus_order_ids = array_column($orders->toArray(), 'id');
2020-04-22 16:57:00 +08:00
}
2020-06-04 18:00:33 +08:00
2021-08-28 14:11:59 +08:00
public function paid(string $callbackNo)
2020-06-04 18:00:33 +08:00
{
$order = $this->order;
2021-08-28 14:11:59 +08:00
if ($order->status !== 0) return true;
2020-06-04 18:00:33 +08:00
$order->status = 1;
2021-07-31 01:21:49 +08:00
$order->paid_at = time();
2020-06-04 18:00:33 +08:00
$order->callback_no = $callbackNo;
2021-09-04 15:31:25 +08:00
if (!$order->save()) return false;
OrderHandleJob::dispatch($order->trade_no);
return true;
2020-06-04 18:00:33 +08:00
}
2020-10-04 14:21:09 +08:00
2021-08-28 14:11:59 +08:00
public function cancel():bool
{
$order = $this->order;
DB::beginTransaction();
$order->status = 2;
if (!$order->save()) {
DB::rollBack();
return false;
}
if ($order->balance_amount) {
$userService = new UserService();
if (!$userService->addBalance($order->user_id, $order->balance_amount)) {
DB::rollBack();
return false;
}
}
DB::commit();
return true;
}
2020-10-04 14:21:09 +08:00
2022-11-26 22:19:19 +08:00
private function setSpeedLimit($speedLimit)
{
$this->user->speed_limit = $speedLimit;
}
2021-07-01 21:04:22 +08:00
private function buyByResetTraffic()
2020-10-04 14:21:09 +08:00
{
2021-07-01 21:04:22 +08:00
$this->user->u = 0;
$this->user->d = 0;
2020-10-04 14:21:09 +08:00
}
2021-12-28 01:49:33 +08:00
private function buyByPeriod(Order $order, Plan $plan)
2020-10-04 14:21:09 +08:00
{
// change plan process
if ((int)$order->type === 3) {
2021-07-01 21:04:22 +08:00
$this->user->expired_at = time();
2020-10-04 14:21:09 +08:00
}
2021-07-01 21:04:22 +08:00
$this->user->transfer_enable = $plan->transfer_enable * 1073741824;
2020-10-11 00:28:09 +08:00
// 从一次性转换到循环
2021-07-01 21:04:22 +08:00
if ($this->user->expired_at === NULL) $this->buyByResetTraffic();
2020-10-04 14:21:09 +08:00
// 新购
2021-07-01 21:04:22 +08:00
if ($order->type === 1) $this->buyByResetTraffic();
$this->user->plan_id = $plan->id;
$this->user->group_id = $plan->group_id;
2021-12-28 01:49:33 +08:00
$this->user->expired_at = $this->getTime($order->period, $this->user->expired_at);
2020-10-04 14:21:09 +08:00
}
2021-07-01 21:04:22 +08:00
private function buyByOneTime(Plan $plan)
2020-10-04 14:21:09 +08:00
{
2021-07-01 21:04:22 +08:00
$this->buyByResetTraffic();
$this->user->transfer_enable = $plan->transfer_enable * 1073741824;
$this->user->plan_id = $plan->id;
$this->user->group_id = $plan->group_id;
$this->user->expired_at = NULL;
2020-10-04 14:21:09 +08:00
}
private function getTime($str, $timestamp)
{
if ($timestamp < time()) {
$timestamp = time();
}
switch ($str) {
case 'month_price':
return strtotime('+1 month', $timestamp);
case 'quarter_price':
return strtotime('+3 month', $timestamp);
case 'half_year_price':
return strtotime('+6 month', $timestamp);
case 'year_price':
return strtotime('+12 month', $timestamp);
case 'two_year_price':
return strtotime('+24 month', $timestamp);
case 'three_year_price':
return strtotime('+36 month', $timestamp);
}
}
2021-07-01 21:04:22 +08:00
private function openEvent($eventId)
{
switch ((int) $eventId) {
case 0:
break;
case 1:
$this->buyByResetTraffic();
break;
}
}
2020-03-17 19:00:33 +08:00
}