From aa760b14a2949aea5cae1b4a8d1a11428ddb0337 Mon Sep 17 00:00:00 2001 From: lkddi Date: Sat, 28 Mar 2026 21:28:14 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E7=99=BE=E5=AE=B6=E4=B9=90AI=E9=A2=84?= =?UTF-8?q?=E6=B5=8B):=20ai=5Fusage=5Flogs=20=E4=BD=BF=E7=94=A8=20AI?= =?UTF-8?q?=E5=B0=8F=E7=8F=AD=E9=95=BF=E7=9C=9F=E5=AE=9E=20user=5Fid?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 resolveAiUserId() 按 username 查询 AI小班长 ID(惰性/缓存) - 原先硬编码 null,改为正确关联到 AI小班长用户记录 --- app/Services/BaccaratPredictionService.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/app/Services/BaccaratPredictionService.php b/app/Services/BaccaratPredictionService.php index 1bd9f8e..2c37f88 100644 --- a/app/Services/BaccaratPredictionService.php +++ b/app/Services/BaccaratPredictionService.php @@ -37,6 +37,11 @@ class BaccaratPredictionService */ private const PREFERRED_MODEL = 'glm-5.1-free'; + /** + * AI小班长的用户 ID(惰性加载,首次使用时查询一次) + */ + private ?int $aiUserId = null; + /** * 调用 AI 接口预测百家乐下注方向 * @@ -248,7 +253,7 @@ PROMPT; ): void { try { AiUsageLog::create([ - 'user_id' => null, // AI 系统行为,无对应用户 + 'user_id' => $this->resolveAiUserId(), 'provider' => $config->provider, 'model' => $config->model, 'action' => 'baccarat_predict', @@ -263,4 +268,19 @@ PROMPT; Log::error('百家乐 AI 预测日志记录失败', ['error' => $e->getMessage()]); } } + + /** + * 查询 AI小班长的用户 ID + * + * 加载结果以属性形式缓存,同一个服务实例内只查一次。 + * 查不到时返回 null,避免破坏外键约束。 + */ + private function resolveAiUserId(): ?int + { + if ($this->aiUserId === null) { + $this->aiUserId = \App\Models\User::where('username', 'AI小班长')->value('id'); + } + + return $this->aiUserId; + } }