算力功能

This commit is contained in:
wong
2025-12-06 17:13:07 +08:00
parent f06eb0e9d7
commit a557319b3e
13 changed files with 3100 additions and 735 deletions

View File

@@ -72,7 +72,7 @@ class TokensController extends BaseController
} else {
//获取配置的tokens比例
$tokens_multiple = Env::get('payment.tokens_multiple', 28);
$tokens_multiple = Env::get('payment.tokens_multiple', 20);
$specs = [
'id' => 0,
'name' => '自定义购买算力',
@@ -119,7 +119,7 @@ class TokensController extends BaseController
return ResponseHelper::success($order, '订单已支付');
} else {
$errorMsg = !empty($order['payInfo']) ? $order['payInfo'] : '订单未支付';
return ResponseHelper::success($order,$errorMsg,400);
return ResponseHelper::success($order,$errorMsg);
}
} else {
return ResponseHelper::success($order, '订单已支付');
@@ -140,6 +140,7 @@ class TokensController extends BaseController
$status = $this->request->param('status', ''); // 订单状态筛选
$keyword = $this->request->param('keyword', ''); // 关键词搜索(订单号)
$orderType = $this->request->param('orderType', ''); // 订单类型筛选
$payType = $this->request->param('payType', ''); // 支付类型筛选
$startTime = $this->request->param('startTime', ''); // 开始时间
$endTime = $this->request->param('endTime', ''); // 结束时间
@@ -166,6 +167,11 @@ class TokensController extends BaseController
$where[] = ['orderType', '=', $orderType];
}
// 支付类型筛选
if($payType !== '') {
$where[] = ['payType', '=', $payType];
}
// 时间范围筛选
if (!empty($startTime)) {
$where[] = ['createTime', '>=', strtotime($startTime)];
@@ -300,16 +306,63 @@ class TokensController extends BaseController
])->sum('tokens');
$totalRecharged = intval($totalRecharged);
// 计算预计可用天数(基于过去一个月的平均消耗)
$estimatedDays = $this->calculateEstimatedDays($companyId, $remainingTokens);
return ResponseHelper::success([
'totalTokens' => $totalRecharged, // 总算力(累计充值)
'todayUsed' => $todayUsed, // 今日使用
'monthUsed' => $monthUsed, // 本月使用
'remainingTokens' => $remainingTokens, // 剩余算力
'totalConsumed' => $totalConsumed, // 累计消费
'estimatedDays' => $estimatedDays, // 预计可用天数
], '获取成功');
} catch (\Exception $e) {
return ResponseHelper::error('获取算力统计失败:' . $e->getMessage());
}
}
/**
* 计算预计可用天数(基于过去一个月的平均消耗)
* @param int $companyId 公司ID
* @param int $remainingTokens 当前剩余算力
* @return int 预计可用天数,-1表示无法计算无消耗记录或余额为0
*/
private function calculateEstimatedDays($companyId, $remainingTokens)
{
// 如果余额为0或负数无法计算
if ($remainingTokens <= 0) {
return -1;
}
// 计算过去30天的消耗总量只统计减少的记录type=0
$oneMonthAgo = time() - (30 * 24 * 60 * 60); // 30天前的时间戳
$totalConsumed = TokensRecord::where([
['companyId', '=', $companyId],
['type', '=', 0], // 只统计减少的记录
['createTime', '>=', $oneMonthAgo]
])->sum('tokens');
$totalConsumed = intval($totalConsumed);
// 如果过去30天没有消耗记录无法计算
if ($totalConsumed <= 0) {
return -1;
}
// 计算平均每天消耗量
$avgDailyConsumption = $totalConsumed / 30;
// 如果平均每天消耗为0无法计算
if ($avgDailyConsumption <= 0) {
return -1;
}
// 计算预计可用天数 = 当前余额 / 平均每天消耗量
$estimatedDays = floor($remainingTokens / $avgDailyConsumption);
return $estimatedDays;
}
}