diff --git a/app/Console/Commands/AutoSaveExp.php b/app/Console/Commands/AutoSaveExp.php index fca5cc9..9a4f401 100644 --- a/app/Console/Commands/AutoSaveExp.php +++ b/app/Console/Commands/AutoSaveExp.php @@ -278,10 +278,8 @@ class AutoSaveExp extends Command */ private function tickDutyLog(User $user, int $roomId): void { + // 无论有无职务,均记录在线流水 $activeUP = $user->activePosition; - if (! $activeUP) { - return; - } // ① 今日未关闭的开放日志 → 刷新时长 $openLog = PositionDutyLog::query() @@ -315,7 +313,7 @@ class AutoSaveExp extends Command PositionDutyLog::create([ 'user_id' => $user->id, - 'user_position_id' => $activeUP->id, + 'user_position_id' => $activeUP?->id, 'login_at' => $loginAt, 'ip_address' => '0.0.0.0', 'room_id' => $roomId, diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 3f5c84d..bda0f2b 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -1049,11 +1049,8 @@ class ChatController extends Controller */ private function tickDutyLog(User $user, int $roomId): void { - // 无在职职务,无需记录 + // 无论有无职务,均记录在线流水 $activeUP = $user->activePosition; - if (! $activeUP) { - return; - } // ① 优先找今日未关闭的开放日志,直接刷新时长 $openLog = PositionDutyLog::query() @@ -1091,7 +1088,7 @@ class ChatController extends Controller PositionDutyLog::create([ 'user_id' => $user->id, - 'user_position_id' => $activeUP->id, + 'user_position_id' => $activeUP?->id, 'login_at' => $loginAt, 'ip_address' => request()->ip(), 'room_id' => $roomId, diff --git a/database/migrations/2026_03_18_214320_make_user_position_id_nullable_on_position_duty_logs_table.php b/database/migrations/2026_03_18_214320_make_user_position_id_nullable_on_position_duty_logs_table.php new file mode 100644 index 0000000..7b0636a --- /dev/null +++ b/database/migrations/2026_03_18_214320_make_user_position_id_nullable_on_position_duty_logs_table.php @@ -0,0 +1,28 @@ +unsignedBigInteger('user_position_id')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('position_duty_logs', function (Blueprint $table) { + $table->unsignedBigInteger('user_position_id')->nullable(false)->change(); + }); + } +};