bug修复

This commit is contained in:
wong
2025-09-23 16:21:19 +08:00
parent fbfe0ba1fd
commit a8a9cf76df
2 changed files with 120 additions and 0 deletions

View File

@@ -105,6 +105,36 @@ class AiSettingsController extends BaseController
}
public function getUserTokens()
{
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$tokens = Db::name('users')
->where('id', $userId)
->where('companyId', $companyId)
->value('tokens');
return ResponseHelper::success($tokens, '获取成功');
}
public function getFriend()
{
$friendId = $this->request->param('friendId', '');
$wechatAccountId = $this->request->param('wechatAccountId', '');
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
$aiType = AiFriendSettings::where(['userId' => $userId, 'companyId' => $companyId,'friendId' => $friendId,'wechatAccountId' => $wechatAccountId])->value('type');
if (empty($aiType)) {
$aiType = 0;
}
return ResponseHelper::success($aiType, '获取成功');
}
public function setFriend()
{
@@ -150,4 +180,91 @@ class AiSettingsController extends BaseController
}
public function setAllFriend()
{
$packageId = $this->request->param('packageId', []);
$type = $this->request->param('type', 0);
$isUpdata = $this->request->param('isUpdata', 0);
$userId = $this->getUserInfo('id');
$companyId = $this->getUserInfo('companyId');
if (empty($packageId)) {
return ResponseHelper::error('参数缺失');
}
//列出所有好友
$row = Db::name('traffic_source_package_item')->alias('a')
->join('wechat_friendship f','a.identifier = f.wechatId and f.companyId = '.$companyId)
->join(['s2_wechat_account' => 'wa'],'f.ownerWechatId = wa.wechatId')
->whereIn('a.packageId' , $packageId)
->field('f.id as friendId,wa.id as wechatAccountId')
->group('f.id')
->select();
if (empty($row)) {
return ResponseHelper::error('`好友不存在');
}
// 1000条为一组进行批量处理
$batchSize = 1000;
$totalRows = count($row);
for ($i = 0; $i < $totalRows; $i += $batchSize) {
$batchRows = array_slice($row, $i, $batchSize);
if (!empty($batchRows)) {
// 1. 提取当前批次的phone
$friendIds = array_column($batchRows, 'friendId');
// 2. 批量查询已存在的phone
$existingPhones = [];
if (!empty($friendIds)) {
//强制更新
if(!empty($isUpdata)){
Db::name('ai_friend_settings')->whereIn('friendId',$friendIds)->update(['type' => $type,'updateTime' => time()]);
}
$existing = Db::name('ai_friend_settings')
->where('companyId', $companyId)
->where('friendId', 'in', $friendIds)
->field('friendId')
->select();
$existingPhones = array_column($existing, 'friendId');
}
// 3. 过滤出新数据,批量插入
$newData = [];
foreach ($batchRows as $row) {
if (!empty($friendIds) && !in_array($row['friendId'], $existingPhones)) {
$newData[] = [
'companyId' => $companyId,
'userId' => $userId,
'type' => $type,
'wechatAccountId' => $row['wechatAccountId'],
'friendId' => $row['friendId'],
'createTime' => time(),
'updateTime' => time(),
];
}
}
// 4. 批量插入新数据
if (!empty($newData)) {
Db::name('ai_friend_settings')->insertAll($newData);
}
}
}
try {
return ResponseHelper::success(' ', '配置成功');
} catch (\Exception $e) {
return ResponseHelper::error('配置失败:' . $e->getMessage());
}
}
}