From a8a9cf76df8aa988ac4fab4cca63baf5fac008d3 Mon Sep 17 00:00:00 2001 From: wong <106998207@qq.com> Date: Tue, 23 Sep 2025 16:21:19 +0800 Subject: [PATCH] =?UTF-8?q?bug=E4=BF=AE=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Server/application/chukebao/config/route.php | 3 + .../controller/AiSettingsController.php | 117 ++++++++++++++++++ 2 files changed, 120 insertions(+) diff --git a/Server/application/chukebao/config/route.php b/Server/application/chukebao/config/route.php index 5ae4b0fc..202537e4 100644 --- a/Server/application/chukebao/config/route.php +++ b/Server/application/chukebao/config/route.php @@ -50,10 +50,13 @@ Route::group('v1/', function () { //好友配置 Route::group('friend/', function () { Route::post('set', 'app\chukebao\controller\AiSettingsController@setFriend'); + Route::get('get', 'app\chukebao\controller\AiSettingsController@getFriend'); + Route::post('setAll', 'app\chukebao\controller\AiSettingsController@setAllFriend'); }); //ai对话 + Route::get('getUserTokens', 'app\chukebao\controller\AiSettingsController@getUserTokens'); Route::post('chat', 'app\chukebao\controller\AiChatController@index'); }); diff --git a/Server/application/chukebao/controller/AiSettingsController.php b/Server/application/chukebao/controller/AiSettingsController.php index 64161ddc..e9c9b1ae 100644 --- a/Server/application/chukebao/controller/AiSettingsController.php +++ b/Server/application/chukebao/controller/AiSettingsController.php @@ -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()); + } + + } + } \ No newline at end of file