算力功能
This commit is contained in:
@@ -16,6 +16,8 @@ class TokensRecordController extends BaseController
|
||||
$limit = $this->request->param('limit', 10);
|
||||
$type = $this->request->param('type', '');
|
||||
$form = $this->request->param('form', '');
|
||||
$startTime = $this->request->param('startTime', '');
|
||||
$endTime = $this->request->param('endTime', '');
|
||||
$userId = $this->getUserInfo('id');
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
|
||||
@@ -32,6 +34,26 @@ class TokensRecordController extends BaseController
|
||||
$where[] = ['form','=',$form];
|
||||
}
|
||||
|
||||
// 时间筛选
|
||||
if (!empty($startTime)) {
|
||||
// 支持时间戳或日期字符串格式
|
||||
$startTimestamp = is_numeric($startTime) ? intval($startTime) : strtotime($startTime);
|
||||
if ($startTimestamp !== false) {
|
||||
$where[] = ['createTime', '>=', $startTimestamp];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($endTime)) {
|
||||
// 支持时间戳或日期字符串格式
|
||||
$endTimestamp = is_numeric($endTime) ? intval($endTime) : strtotime($endTime);
|
||||
if ($endTimestamp !== false) {
|
||||
// 如果是日期字符串,自动设置为当天的23:59:59
|
||||
if (!is_numeric($endTime)) {
|
||||
$endTimestamp = strtotime(date('Y-m-d 23:59:59', $endTimestamp));
|
||||
}
|
||||
$where[] = ['createTime', '<=', $endTimestamp];
|
||||
}
|
||||
}
|
||||
|
||||
$query = TokensRecord::where($where);
|
||||
$total = $query->count();
|
||||
|
||||
@@ -495,7 +495,7 @@ class PaymentService
|
||||
switch ($order['orderType']) {
|
||||
case 1:
|
||||
// 处理购买算力
|
||||
$token = TokensCompany::where(['companyId' => $order->companyId])->find();
|
||||
$token = TokensCompany::where(['companyId' => $order->companyId,'userId' => $order->userId])->find();
|
||||
$goodsSpecs = json_decode($order->goodsSpecs, true);
|
||||
if (!empty($token)) {
|
||||
$token->tokens = $token->tokens + $goodsSpecs['tokens'];
|
||||
@@ -504,6 +504,7 @@ class PaymentService
|
||||
$newTokens = $token->tokens;
|
||||
} else {
|
||||
$tokensCompany = new TokensCompany();
|
||||
$tokensCompany->userId = $order->userId;
|
||||
$tokensCompany->companyId = $order->companyId;
|
||||
$tokensCompany->tokens = $goodsSpecs['tokens'];
|
||||
$tokensCompany->createTime = time();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user