update: order service

This commit is contained in:
v2board 2023-02-25 20:35:18 +08:00
parent 7c7f7288d4
commit 986acf67d0

View File

@ -188,21 +188,26 @@ class OrderService
{ {
$orders = Order::where('user_id', $user->id) $orders = Order::where('user_id', $user->id)
->where('period', '!=', 'reset_price') ->where('period', '!=', 'reset_price')
->where('period', '!=', 'onetime_price')
->where('status', 3) ->where('status', 3)
->get() ->get()
->toArray(); ->toArray();
if (!$orders) return; if (!$orders) return;
$orderAmountSum = 0; $orderAmountSum = 0;
$orderMonthSum = 0; $orderMonthSum = 0;
$lastValidateAt = 0;
foreach ($orders as $item) { foreach ($orders as $item) {
if ($item['period'] === 'onetime_price') continue; $period = self::STR_TO_TIME[$item['period']];
$orderMonthSum = self::STR_TO_TIME[$item['period']] + $orderMonthSum; 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']); $orderAmountSum = $orderAmountSum + ($item['total_amount'] + $item['balance_amount'] + $item['surplus_amount'] - $item['refund_amount']);
} }
$expiredAtByOrder = strtotime("+{$orderMonthSum} month", $orders[0]['created_at']); if (!$lastValidateAt) return;
$expiredAtByOrder = strtotime("+{$orderMonthSum} month", $lastValidateAt);
if ($expiredAtByOrder < time()) return; if ($expiredAtByOrder < time()) return;
$orderSurplusSecond = $expiredAtByOrder - time(); $orderSurplusSecond = $expiredAtByOrder - time();
$orderRangeSecond = $expiredAtByOrder - $orders[0]['created_at']; $orderRangeSecond = $expiredAtByOrder - $lastValidateAt;
$avgPrice = $orderAmountSum / $orderRangeSecond; $avgPrice = $orderAmountSum / $orderRangeSecond;
$orderSurplusAmount = $avgPrice * $orderSurplusSecond; $orderSurplusAmount = $avgPrice * $orderSurplusSecond;
if (!$orderSurplusSecond || !$orderSurplusAmount) return; if (!$orderSurplusSecond || !$orderSurplusAmount) return;