Skip to content

Commit fdc918e

Browse files
committed
Merge branch 'feature/optimize-super-magic' into merge/optimize-super-magic-master-01
2 parents 8c6eeca + 8d2c892 commit fdc918e

File tree

18 files changed

+699
-570
lines changed

18 files changed

+699
-570
lines changed

backend/magic-service/app/Application/Chat/Service/MagicChatMessageAppService.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,21 @@ public function pullRecentMessage(MagicUserAuthorization $userAuthorization, Mes
155155
public function getConversations(MagicUserAuthorization $userAuthorization, ConversationListQueryDTO $queryDTO): ConversationsPageResponseDTO
156156
{
157157
$dataIsolation = $this->createDataIsolation($userAuthorization);
158-
return $this->magicConversationDomainService->getConversations($dataIsolation, $queryDTO);
158+
$result = $this->magicConversationDomainService->getConversations($dataIsolation, $queryDTO);
159+
$filterAccountEntity = $this->magicUserDomainService->getByAiCode($dataIsolation, 'SUPER_MAGIC');
160+
if (! empty($filterAccountEntity) && count($result->getItems()) > 0) {
161+
$filterItems = [];
162+
foreach ($result->getItems() as $item) {
163+
/**
164+
* @var MagicConversationEntity $item
165+
*/
166+
if ($item->getReceiveId() !== $filterAccountEntity->getUserId()) {
167+
$filterItems[] = $item;
168+
}
169+
}
170+
$result->setItems($filterItems);
171+
}
172+
return $result;
159173
}
160174

161175
public function getUserGroupConversation(UserGroupConversationQueryDTO $queryDTO): ?MagicConversationEntity
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Copyright (c) The Magic , Distributed under the software license
6+
*/
7+
8+
use Hyperf\Database\Migrations\Migration;
9+
use Hyperf\Database\Schema\Blueprint;
10+
use Hyperf\Database\Schema\Schema;
11+
12+
class AddIsHiddenToMagicSuperAgentTaskFilesTable extends Migration
13+
{
14+
/**
15+
* Run the migrations.
16+
*/
17+
public function up(): void
18+
{
19+
Schema::table('magic_super_agent_task_files', function (Blueprint $table) {
20+
$table->tinyInteger('is_hidden')->default(0)->comment('是否为隐藏文件:0-否,1-是');
21+
});
22+
}
23+
24+
/**
25+
* Reverse the migrations.
26+
*/
27+
public function down(): void
28+
{
29+
Schema::table('magic_super_agent_task_files', function (Blueprint $table) {
30+
$table->dropColumn('is_hidden');
31+
});
32+
}
33+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Copyright (c) The Magic , Distributed under the software license
6+
*/
7+
use Hyperf\Database\Migrations\Migration;
8+
use Hyperf\Database\Schema\Blueprint;
9+
use Hyperf\Database\Schema\Schema;
10+
use Hyperf\DbConnection\Db;
11+
12+
return new class extends Migration {
13+
/**
14+
* Run the migrations.
15+
*/
16+
public function up(): void
17+
{
18+
if (!Schema::hasTable('magic_super_agent_message')) {
19+
return;
20+
}
21+
22+
Schema::table('magic_super_agent_message', function (Blueprint $table) {
23+
// Check if idx_id index exists
24+
if (!$this->indexExists('magic_super_agent_message', 'idx_id')) {
25+
$table->index(['id'], 'idx_id');
26+
}
27+
28+
// Check if idx_topic_show_deleted index exists
29+
if (!$this->indexExists('magic_super_agent_message', 'idx_topic_show_deleted')) {
30+
$table->index(['topic_id', 'show_in_ui', 'deleted_at'], 'idx_topic_show_deleted');
31+
}
32+
});
33+
}
34+
35+
/**
36+
* Reverse the migrations.
37+
*/
38+
public function down(): void
39+
{
40+
if (!Schema::hasTable('magic_super_agent_message')) {
41+
return;
42+
}
43+
44+
Schema::table('magic_super_agent_message', function (Blueprint $table) {
45+
// Drop indexes if they exist
46+
if ($this->indexExists('magic_super_agent_message', 'idx_id')) {
47+
$table->dropIndex('idx_id');
48+
}
49+
50+
if ($this->indexExists('magic_super_agent_message', 'idx_topic_show_deleted')) {
51+
$table->dropIndex('idx_topic_show_deleted');
52+
}
53+
});
54+
}
55+
56+
/**
57+
* Check if index exists on table
58+
*/
59+
private function indexExists(string $tableName, string $indexName): bool
60+
{
61+
$database = config('databases.default.database');
62+
63+
$result = Db::select("
64+
SELECT COUNT(*) as count
65+
FROM information_schema.statistics
66+
WHERE table_schema = ?
67+
AND table_name = ?
68+
AND index_name = ?
69+
", [$database, $tableName, $indexName]);
70+
71+
return $result[0]->count > 0;
72+
}
73+
};
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* Copyright (c) The Magic , Distributed under the software license
6+
*/
7+
use Hyperf\Database\Migrations\Migration;
8+
use Hyperf\Database\Schema\Schema;
9+
use Hyperf\DbConnection\Db;
10+
11+
return new class extends Migration {
12+
/**
13+
* Run the migrations.
14+
*/
15+
public function up(): void
16+
{
17+
// 只有当表存在时才执行索引操作
18+
if (Schema::hasTable('magic_super_agent_message')) {
19+
// 检查并创建 idx_message_id 索引
20+
$this->createIndexIfNotExists(
21+
'magic_super_agent_message',
22+
'idx_message_id',
23+
'CREATE INDEX idx_message_id ON `magic_super_agent_message` (message_id)'
24+
);
25+
}
26+
}
27+
28+
/**
29+
* Reverse the migrations.
30+
*/
31+
public function down(): void
32+
{
33+
if (Schema::hasTable('magic_super_agent_message')) {
34+
// 删除索引
35+
$this->dropIndexIfExists('magic_super_agent_message', 'idx_message_id');
36+
}
37+
}
38+
39+
/**
40+
* 检查索引是否存在,如果不存在则创建索引.
41+
*
42+
* @param string $table 表名
43+
* @param string $indexName 索引名称
44+
* @param string $createStatement 创建索引的SQL语句
45+
*/
46+
private function createIndexIfNotExists(string $table, string $indexName, string $createStatement): void
47+
{
48+
// 检查索引是否存在
49+
$indexExists = Db::select(
50+
"SHOW INDEX FROM `{$table}` WHERE Key_name = ?",
51+
[$indexName]
52+
);
53+
54+
// 只有当索引不存在时才创建
55+
if (empty($indexExists)) {
56+
// 创建索引
57+
Db::statement($createStatement);
58+
}
59+
}
60+
61+
/**
62+
* 如果索引存在则删除.
63+
*
64+
* @param string $table 表名
65+
* @param string $indexName 索引名称
66+
*/
67+
private function dropIndexIfExists(string $table, string $indexName): void
68+
{
69+
// 检查索引是否存在
70+
$indexExists = Db::select(
71+
"SHOW INDEX FROM `{$table}` WHERE Key_name = ?",
72+
[$indexName]
73+
);
74+
75+
if (! empty($indexExists)) {
76+
// 删除现有索引
77+
Db::statement("DROP INDEX `{$indexName}` ON `{$table}`");
78+
}
79+
}
80+
};

0 commit comments

Comments
 (0)