diff --git a/app/Models/AbstractModel.php b/app/Models/AbstractModel.php index 4feaafff2..82f626217 100644 --- a/app/Models/AbstractModel.php +++ b/app/Models/AbstractModel.php @@ -210,8 +210,8 @@ class AbstractModel extends Model /** * 数据库更新或插入 * @param $where - * @param array $update 存在时更新的内容 - * @param array $insert 不存在时插入的内容,如果没有则插入更新内容 + * @param array|\Closure $update 存在时更新的内容 + * @param array|\Closure $insert 不存在时插入的内容,如果没有则插入更新内容 * @param bool $isInsert 是否是插入数据 * @return AbstractModel|\Illuminate\Database\Eloquent\Builder|Model|object|static|null */ @@ -220,6 +220,12 @@ class AbstractModel extends Model $row = static::where($where)->first(); if (empty($row)) { $row = new static; + if ($update instanceof \Closure) { + $update = $update(); + } + if ($insert instanceof \Closure) { + $insert = $insert(); + } $array = array_merge($where, $insert ?: $update); if (isset($array[$row->primaryKey])) { unset($array[$row->primaryKey]); @@ -227,6 +233,9 @@ class AbstractModel extends Model $row->updateInstance($array); $isInsert = true; } elseif ($update) { + if ($update instanceof \Closure) { + $update = $update(); + } $row->updateInstance($update); $isInsert = false; } diff --git a/app/Models/Project.php b/app/Models/Project.php index d3dcf78d7..599f08b20 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -219,7 +219,11 @@ class Project extends AbstractModel 'userid' => $userid, ], [ 'important' => 1 - ]); + ], function () use ($userid) { + return [ + 'bot' => User::isBot($userid) ? 1 : 0, + ]; + }); } WebSocketDialogUser::whereDialogId($this->dialog_id)->whereNotIn('userid', $userids)->whereImportant(1)->remove(); }); diff --git a/app/Models/ProjectTask.php b/app/Models/ProjectTask.php index 85927efb1..5f542ed5f 100644 --- a/app/Models/ProjectTask.php +++ b/app/Models/ProjectTask.php @@ -1180,7 +1180,11 @@ class ProjectTask extends AbstractModel 'userid' => $userid, ], [ 'important' => 1 - ]); + ], function () use ($userid) { + return [ + 'bot' => User::isBot($userid) ? 1 : 0, + ]; + }); } WebSocketDialogUser::whereDialogId($this->dialog_id)->whereNotIn('userid', $userids)->whereImportant(1)->remove(); }); diff --git a/app/Models/WebSocketDialog.php b/app/Models/WebSocketDialog.php index 082b134c9..1d31b16a1 100644 --- a/app/Models/WebSocketDialog.php +++ b/app/Models/WebSocketDialog.php @@ -423,7 +423,11 @@ class WebSocketDialog extends AbstractModel WebSocketDialogUser::updateInsert([ 'dialog_id' => $this->id, 'userid' => $value, - ], $updateData, [], $isInsert); + ], $updateData, function() use ($value) { + return [ + 'bot' => User::isBot($value) ? 1 : 0, + ]; + }, $isInsert); if ($isInsert) { WebSocketDialogMsg::sendMsg(null, $this->id, 'notice', [ 'notice' => User::userid2nickname($value) . " 已加入群组" diff --git a/database/migrations/2025_01_02_125600_add_bot_to_web_socket_dialog_users_table.php b/database/migrations/2025_01_02_125600_add_bot_to_web_socket_dialog_users_table.php new file mode 100644 index 000000000..1b071440c --- /dev/null +++ b/database/migrations/2025_01_02_125600_add_bot_to_web_socket_dialog_users_table.php @@ -0,0 +1,50 @@ +tinyInteger('bot')->nullable()->default(0)->after('userid')->comment('是否机器人'); + $table->index('bot'); + } + }); + + // 获取表前缀 + $prefix = DB::getTablePrefix(); + + // 使用原生SQL更新数据 + /** @noinspection SqlNoDataSourceInspection */ + DB::statement(" + UPDATE {$prefix}web_socket_dialog_users du + INNER JOIN {$prefix}users u ON u.userid = du.userid + SET du.bot = 1 + WHERE u.bot = 1 + "); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('web_socket_dialog_users', function (Blueprint $table) { + if (Schema::hasColumn('web_socket_dialog_users', 'bot')) { + $table->dropIndex('bot'); + $table->dropColumn('bot'); + } + }); + } +}