算力功能改版
This commit is contained in:
@@ -29,7 +29,7 @@ class StatsController extends Controller
|
||||
$where = [
|
||||
['departmentId','=',$this->request->userInfo['companyId']]
|
||||
];
|
||||
if (!empty($this->request->userInfo['isAdmin'])){
|
||||
if (empty($this->request->userInfo['isAdmin'])){
|
||||
$where[] = ['id','=',$this->request->userInfo['s2_accountId']];
|
||||
}
|
||||
$accounts = Db::table('s2_company_account')->where($where)->column('id');
|
||||
@@ -407,7 +407,7 @@ class StatsController extends Controller
|
||||
$where = [
|
||||
['departmentId','=',$companyId]
|
||||
];
|
||||
if (!empty($this->request->userInfo['isAdmin'])){
|
||||
if (empty($this->request->userInfo['isAdmin'])){
|
||||
$where[] = ['id','=',$this->request->userInfo['s2_accountId']];
|
||||
}
|
||||
$accounts = Db::table('s2_company_account')->where($where)->column('id');
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -13,9 +13,12 @@ class PostTransferFriends extends BaseController
|
||||
{
|
||||
$wechatId = $this->request->param('wechatId', '');
|
||||
$inherit = $this->request->param('inherit', '');
|
||||
$greeting = $this->request->param('greeting', '');
|
||||
$firstMessage = $this->request->param('firstMessage', '');
|
||||
$devices = $this->request->param('devices', []);
|
||||
$companyId = $this->getUserInfo('companyId');
|
||||
|
||||
|
||||
if (empty($wechatId)){
|
||||
return ResponseHelper::error('迁移的微信不能为空');
|
||||
}
|
||||
@@ -23,13 +26,16 @@ class PostTransferFriends extends BaseController
|
||||
if (empty($devices)){
|
||||
return ResponseHelper::error('迁移的设备不能为空');
|
||||
}
|
||||
if (empty($greeting)){
|
||||
return ResponseHelper::error('打招呼不能为空');
|
||||
}
|
||||
if (!is_array($devices)){
|
||||
return ResponseHelper::error('迁移的设备必须为数组');
|
||||
}
|
||||
|
||||
$wechat = Db::name('wechat_customer')->alias('wc')
|
||||
->join('wechat_account wa', 'wc.wechatId = wa.wechatId')
|
||||
->where(['wc.wechatId' => $wechatId, 'wc.companyId' => $companyId])
|
||||
->where(['wc.wechatId' => $wechatId])
|
||||
->field('wa.*')
|
||||
->find();
|
||||
|
||||
@@ -53,11 +59,6 @@ class PostTransferFriends extends BaseController
|
||||
'id' => 'poster-3',
|
||||
'name' => '点击咨询',
|
||||
'src' => 'https://hebbkx1anhila5yf.public.blob.vercel-storage.com/%E7%82%B9%E5%87%BB%E5%92%A8%E8%AF%A2-FTiyAMAPop2g9LvjLOLDz0VwPg3KVu.gif'
|
||||
],
|
||||
'$posters' => [
|
||||
'id' => 'poster-3',
|
||||
'name' => '点击咨询',
|
||||
'src' => 'https://hebbkx1anhila5yf.public.blob.vercel-storage.com/%E7%82%B9%E5%87%BB%E5%92%A8%E8%AF%A2-FTiyAMAPop2g9LvjLOLDz0VwPg3KVu.gif'
|
||||
]
|
||||
];
|
||||
$reqConf = [
|
||||
@@ -66,18 +67,38 @@ class PostTransferFriends extends BaseController
|
||||
'endTime' => '18:00',
|
||||
'remarkType' => 'phone',
|
||||
'addFriendInterval' => 60,
|
||||
'greeting' => '您好,我是'. $wechat['nickname'] .'的辅助客服,请通过'
|
||||
'greeting' => !empty($greeting) ? $greeting :'这个是'. $wechat['nickname'] .'的新号,之前那个号没用了,重新加一下您'
|
||||
];
|
||||
|
||||
if (!empty($firstMessage)){
|
||||
$msgConf = [
|
||||
[
|
||||
'day' => 0,
|
||||
'messages' => [
|
||||
[
|
||||
'id' => 1,
|
||||
'type' => 'text',
|
||||
'content' => $firstMessage,
|
||||
'intervalUnit' => 'seconds',
|
||||
'sendInterval' => 5,
|
||||
]
|
||||
]
|
||||
]
|
||||
];
|
||||
}else{
|
||||
$msgConf = [];
|
||||
}
|
||||
|
||||
// 使用容器获取控制器实例,而不是直接实例化
|
||||
$createAddFriendPlan = app('app\cunkebao\controller\plan\PostCreateAddFriendPlanV1Controller');
|
||||
|
||||
$taskId = Db::name('customer_acquisition_task')->insertGetId([
|
||||
'name' => '迁移好友('. $wechat['nickname'] .')',
|
||||
'sceneId' => 10,
|
||||
'sceneConf' => json_encode($sceneConf),
|
||||
'reqConf' => json_encode($reqConf),
|
||||
'sceneConf' => json_encode($sceneConf,256),
|
||||
'reqConf' => json_encode($reqConf,256),
|
||||
'tagConf' => json_encode([]),
|
||||
'msgConf' => json_encode($msgConf,256),
|
||||
'userId' => $this->getUserInfo('id'),
|
||||
'companyId' => $companyId,
|
||||
'status' => 0,
|
||||
|
||||
Reference in New Issue
Block a user