算力功能改版
This commit is contained in:
@@ -4,10 +4,12 @@ namespace app\cunkebao\controller;
|
||||
|
||||
use app\common\controller\PaymentService;
|
||||
use app\common\model\Order;
|
||||
use app\common\model\User;
|
||||
use app\cunkebao\model\TokensPackage;
|
||||
use app\chukebao\model\TokensCompany;
|
||||
use app\chukebao\model\TokensRecord;
|
||||
use library\ResponseHelper;
|
||||
use think\Db;
|
||||
use think\facade\Env;
|
||||
|
||||
class TokensController extends BaseController
|
||||
@@ -149,6 +151,7 @@ class TokensController extends BaseController
|
||||
|
||||
// 构建查询条件
|
||||
$where = [
|
||||
['userId', '=', $userId],
|
||||
['companyId', '=', $companyId]
|
||||
];
|
||||
|
||||
@@ -257,13 +260,14 @@ class TokensController extends BaseController
|
||||
public function getTokensStatistics()
|
||||
{
|
||||
try {
|
||||
$userId = $this->getUserInfo('id');
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
if (empty($companyId)) {
|
||||
return ResponseHelper::error('公司信息获取失败');
|
||||
}
|
||||
|
||||
// 获取公司算力余额
|
||||
$tokensCompany = TokensCompany::where('companyId', $companyId)->find();
|
||||
$tokensCompany = TokensCompany::where(['companyId' => $companyId,'userId' => $userId])->find();
|
||||
$remainingTokens = $tokensCompany ? intval($tokensCompany->tokens) : 0;
|
||||
|
||||
// 获取今日开始和结束时间戳
|
||||
@@ -276,6 +280,7 @@ class TokensController extends BaseController
|
||||
|
||||
// 统计今日消费(type=0表示消费)
|
||||
$todayUsed = TokensRecord::where([
|
||||
['userId', '=', $userId],
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 0], // 0为减少(消费)
|
||||
['createTime', '>=', $todayStart],
|
||||
@@ -285,6 +290,7 @@ class TokensController extends BaseController
|
||||
|
||||
// 统计本月消费
|
||||
$monthUsed = TokensRecord::where([
|
||||
['userId', '=', $userId],
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 0], // 0为减少(消费)
|
||||
['createTime', '>=', $monthStart],
|
||||
@@ -294,6 +300,7 @@ class TokensController extends BaseController
|
||||
|
||||
// 计算总算力(当前剩余 + 历史总消费)
|
||||
$totalConsumed = TokensRecord::where([
|
||||
['userId', '=', $userId],
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 0]
|
||||
])->sum('tokens');
|
||||
@@ -301,13 +308,14 @@ class TokensController extends BaseController
|
||||
|
||||
// 总充值算力
|
||||
$totalRecharged = TokensRecord::where([
|
||||
['userId', '=', $userId],
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 1] // 1为增加(充值)
|
||||
])->sum('tokens');
|
||||
$totalRecharged = intval($totalRecharged);
|
||||
|
||||
// 计算预计可用天数(基于过去一个月的平均消耗)
|
||||
$estimatedDays = $this->calculateEstimatedDays($companyId, $remainingTokens);
|
||||
$estimatedDays = $this->calculateEstimatedDays($userId,$companyId, $remainingTokens);
|
||||
|
||||
return ResponseHelper::success([
|
||||
'totalTokens' => $totalRecharged, // 总算力(累计充值)
|
||||
@@ -325,11 +333,12 @@ class TokensController extends BaseController
|
||||
|
||||
/**
|
||||
* 计算预计可用天数(基于过去一个月的平均消耗)
|
||||
* @param int $userId 用户ID
|
||||
* @param int $companyId 公司ID
|
||||
* @param int $remainingTokens 当前剩余算力
|
||||
* @return int 预计可用天数,-1表示无法计算(无消耗记录或余额为0)
|
||||
*/
|
||||
private function calculateEstimatedDays($companyId, $remainingTokens)
|
||||
private function calculateEstimatedDays($userId,$companyId, $remainingTokens)
|
||||
{
|
||||
// 如果余额为0或负数,无法计算
|
||||
if ($remainingTokens <= 0) {
|
||||
@@ -340,6 +349,7 @@ class TokensController extends BaseController
|
||||
$oneMonthAgo = time() - (30 * 24 * 60 * 60); // 30天前的时间戳
|
||||
|
||||
$totalConsumed = TokensRecord::where([
|
||||
['userId', '=', $userId],
|
||||
['companyId', '=', $companyId],
|
||||
['type', '=', 0], // 只统计减少的记录
|
||||
['createTime', '>=', $oneMonthAgo]
|
||||
@@ -365,4 +375,161 @@ class TokensController extends BaseController
|
||||
|
||||
return $estimatedDays;
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配token(仅管理员可用)
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function allocateTokens()
|
||||
{
|
||||
try {
|
||||
$userId = $this->getUserInfo('id');
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
$targetUserId = (int)$this->request->param('targetUserId', 0);
|
||||
$tokens = (int)$this->request->param('tokens', 0);
|
||||
$remarks = $this->request->param('remarks', '');
|
||||
|
||||
// 验证参数
|
||||
if (empty($targetUserId)) {
|
||||
return ResponseHelper::error('目标用户ID不能为空');
|
||||
}
|
||||
|
||||
if ($tokens <= 0) {
|
||||
return ResponseHelper::error('分配的token数量必须大于0');
|
||||
}
|
||||
|
||||
if (empty($companyId)) {
|
||||
return ResponseHelper::error('公司信息获取失败');
|
||||
}
|
||||
|
||||
// 验证当前用户是否为管理员
|
||||
$currentUser = User::where([
|
||||
'id' => $userId,
|
||||
'companyId' => $companyId
|
||||
])->find();
|
||||
|
||||
if (empty($currentUser)) {
|
||||
return ResponseHelper::error('用户信息不存在');
|
||||
}
|
||||
|
||||
if (empty($currentUser->isAdmin) || $currentUser->isAdmin != 1) {
|
||||
return ResponseHelper::error('只有管理员才能分配token');
|
||||
}
|
||||
|
||||
// 验证目标用户是否存在且属于同一公司
|
||||
$targetUser = User::where([
|
||||
'id' => $targetUserId,
|
||||
'companyId' => $companyId
|
||||
])->find();
|
||||
|
||||
if (empty($targetUser)) {
|
||||
return ResponseHelper::error('目标用户不存在或不属于同一公司');
|
||||
}
|
||||
|
||||
// 检查分配者的token余额
|
||||
$allocatorTokens = TokensCompany::where([
|
||||
'companyId' => $companyId,
|
||||
'userId' => $userId
|
||||
])->find();
|
||||
|
||||
$allocatorBalance = $allocatorTokens ? intval($allocatorTokens->tokens) : 0;
|
||||
|
||||
if ($allocatorBalance < $tokens) {
|
||||
return ResponseHelper::error('token余额不足,当前余额:' . $allocatorBalance);
|
||||
}
|
||||
|
||||
// 开始事务
|
||||
Db::startTrans();
|
||||
|
||||
try {
|
||||
// 1. 减少分配者的token
|
||||
if (!empty($allocatorTokens)) {
|
||||
$allocatorTokens->tokens = $allocatorBalance - $tokens;
|
||||
$allocatorTokens->updateTime = time();
|
||||
$allocatorTokens->save();
|
||||
$allocatorNewBalance = $allocatorTokens->tokens;
|
||||
} else {
|
||||
// 如果分配者没有记录,创建一条(余额为0)
|
||||
$allocatorTokens = new TokensCompany();
|
||||
$allocatorTokens->userId = $userId;
|
||||
$allocatorTokens->companyId = $companyId;
|
||||
$allocatorTokens->tokens = 0;
|
||||
$allocatorTokens->isAdmin = 1;
|
||||
$allocatorTokens->createTime = time();
|
||||
$allocatorTokens->updateTime = time();
|
||||
$allocatorTokens->save();
|
||||
$allocatorNewBalance = 0;
|
||||
}
|
||||
|
||||
// 2. 记录分配者的减少记录
|
||||
$targetUserAccount = $targetUser->account ?? $targetUser->phone ?? '用户ID[' . $targetUserId . ']';
|
||||
$allocatorRecord = new TokensRecord();
|
||||
$allocatorRecord->companyId = $companyId;
|
||||
$allocatorRecord->userId = $userId;
|
||||
$allocatorRecord->type = 0; // 0为减少
|
||||
$allocatorRecord->form = 1001; // 1001表示分配
|
||||
$allocatorRecord->wechatAccountId = 0;
|
||||
$allocatorRecord->friendIdOrGroupId = $targetUserId;
|
||||
$allocatorRecord->remarks = !empty($remarks) ? $remarks : '分配给' . $targetUserAccount;
|
||||
$allocatorRecord->tokens = $tokens;
|
||||
$allocatorRecord->balanceTokens = $allocatorNewBalance;
|
||||
$allocatorRecord->createTime = time();
|
||||
$allocatorRecord->save();
|
||||
|
||||
// 3. 增加接收者的token
|
||||
$receiverTokens = TokensCompany::where([
|
||||
'companyId' => $companyId,
|
||||
'userId' => $targetUserId
|
||||
])->find();
|
||||
|
||||
if (!empty($receiverTokens)) {
|
||||
$receiverTokens->tokens = intval($receiverTokens->tokens) + $tokens;
|
||||
$receiverTokens->updateTime = time();
|
||||
$receiverTokens->save();
|
||||
$receiverNewBalance = $receiverTokens->tokens;
|
||||
} else {
|
||||
// 如果接收者没有记录,创建一条
|
||||
$receiverTokens = new TokensCompany();
|
||||
$receiverTokens->userId = $targetUserId;
|
||||
$receiverTokens->companyId = $companyId;
|
||||
$receiverTokens->tokens = $tokens;
|
||||
$receiverTokens->isAdmin = (!empty($targetUser->isAdmin) && $targetUser->isAdmin == 1) ? 1 : 0;
|
||||
$receiverTokens->createTime = time();
|
||||
$receiverTokens->updateTime = time();
|
||||
$receiverTokens->save();
|
||||
$receiverNewBalance = $tokens;
|
||||
}
|
||||
|
||||
// 4. 记录接收者的增加记录
|
||||
$adminAccount = $currentUser->account ?? $currentUser->phone ?? '管理员';
|
||||
$receiverRecord = new TokensRecord();
|
||||
$receiverRecord->companyId = $companyId;
|
||||
$receiverRecord->userId = $targetUserId;
|
||||
$receiverRecord->type = 1; // 1为增加
|
||||
$receiverRecord->form = 1001; // 1001表示分配
|
||||
$receiverRecord->wechatAccountId = 0;
|
||||
$receiverRecord->friendIdOrGroupId = $userId;
|
||||
$receiverRecord->remarks = !empty($remarks) ? '管理员分配:' . $remarks : '管理员分配';
|
||||
$receiverRecord->tokens = $tokens;
|
||||
$receiverRecord->balanceTokens = $receiverNewBalance;
|
||||
$receiverRecord->createTime = time();
|
||||
$receiverRecord->save();
|
||||
|
||||
Db::commit();
|
||||
|
||||
return ResponseHelper::success([
|
||||
'allocatorBalance' => $allocatorNewBalance,
|
||||
'receiverBalance' => $receiverNewBalance,
|
||||
'allocatedTokens' => $tokens
|
||||
], '分配成功');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
return ResponseHelper::error('分配失败:' . $e->getMessage());
|
||||
}
|
||||
|
||||
} catch (\Exception $e) {
|
||||
return ResponseHelper::error('分配失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user