mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-22 07:03:53 +08:00
+3
@@ -24,6 +24,9 @@ public class AgentTagRelationEntity {
|
||||
@Schema(description = "标签ID")
|
||||
private String tagId;
|
||||
|
||||
@Schema(description = "排序")
|
||||
private Integer sort;
|
||||
|
||||
@Schema(description = "创建者")
|
||||
private Long creator;
|
||||
|
||||
|
||||
+9
-10
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -104,13 +105,15 @@ public class AgentTagServiceImpl extends BaseServiceImpl<AgentTagDao, AgentTagEn
|
||||
existTags = baseDao.selectByTagNames(newTagNames);
|
||||
}
|
||||
|
||||
List<String> existTagNames = existTags.stream()
|
||||
.map(AgentTagEntity::getTagName)
|
||||
.collect(Collectors.toList());
|
||||
Map<String, AgentTagEntity> existTagMap = existTags.stream()
|
||||
.collect(Collectors.toMap(AgentTagEntity::getTagName, t -> t, (a, b) -> a));
|
||||
|
||||
List<AgentTagEntity> tagsToInsert = new ArrayList<>();
|
||||
for (String tagName : newTagNames) {
|
||||
if (!existTagNames.contains(tagName)) {
|
||||
AgentTagEntity existTag = existTagMap.get(tagName);
|
||||
if (existTag != null) {
|
||||
allTagIds.add(existTag.getId());
|
||||
} else {
|
||||
AgentTagEntity tag = new AgentTagEntity();
|
||||
tag.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||
tag.setTagName(tagName);
|
||||
@@ -129,12 +132,6 @@ public class AgentTagServiceImpl extends BaseServiceImpl<AgentTagDao, AgentTagEn
|
||||
}
|
||||
}
|
||||
|
||||
for (AgentTagEntity existTag : existTags) {
|
||||
if (!allTagIds.contains(existTag.getId())) {
|
||||
allTagIds.add(existTag.getId());
|
||||
}
|
||||
}
|
||||
|
||||
if (tagIds != null && !tagIds.isEmpty()) {
|
||||
List<AgentTagEntity> tagIdEntities = baseDao.selectBatchIds(tagIds);
|
||||
for (AgentTagEntity tag : tagIdEntities) {
|
||||
@@ -152,11 +149,13 @@ public class AgentTagServiceImpl extends BaseServiceImpl<AgentTagDao, AgentTagEn
|
||||
|
||||
List<AgentTagRelationEntity> relations = new ArrayList<>();
|
||||
Date now = new Date();
|
||||
int sort = 0;
|
||||
for (String tagId : allTagIds) {
|
||||
AgentTagRelationEntity relation = new AgentTagRelationEntity();
|
||||
relation.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||
relation.setAgentId(agentId);
|
||||
relation.setTagId(tagId);
|
||||
relation.setSort(sort++);
|
||||
relation.setCreatedAt(now);
|
||||
relation.setUpdatedAt(now);
|
||||
relations.add(relation);
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
-- 给智能体标签关联表添加排序字段
|
||||
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ai_agent_tag_relation' AND COLUMN_NAME = 'sort');
|
||||
SET @sql = IF(@col_exists = 0, 'ALTER TABLE `ai_agent_tag_relation` ADD COLUMN `sort` INT UNSIGNED DEFAULT 0 COMMENT ''排序''', 'SELECT ''Column sort already exists'' AS msg');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 修复现有数据(仅当 sort 字段为默认值 0 时执行,避免覆盖用户已设置的顺序)
|
||||
SET @col_exists = (SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'ai_agent_tag_relation' AND COLUMN_NAME = 'sort');
|
||||
SET @sql = IF(@col_exists > 0 AND (SELECT COUNT(*) FROM ai_agent_tag_relation WHERE sort = 0) > 0,
|
||||
'UPDATE ai_agent_tag_relation r INNER JOIN (SELECT id, ROW_NUMBER() OVER (PARTITION BY agent_id ORDER BY created_at) AS row_num FROM ai_agent_tag_relation) t ON r.id = t.id SET r.sort = t.row_num',
|
||||
'SELECT ''No need to update or column does not exist'' AS msg');
|
||||
PREPARE stmt FROM @sql; EXECUTE stmt; DEALLOCATE PREPARE stmt;
|
||||
@@ -550,3 +550,11 @@ databaseChangeLog:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202602281000.sql
|
||||
- changeSet:
|
||||
id: 202603091051
|
||||
author: rainv123
|
||||
changes:
|
||||
- sqlFile:
|
||||
encoding: utf8
|
||||
path: classpath:db/changelog/202603091051.sql
|
||||
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
FROM ai_agent_tag t
|
||||
INNER JOIN ai_agent_tag_relation r ON t.id = r.tag_id
|
||||
WHERE r.agent_id = #{agentId} AND t.deleted = 0
|
||||
ORDER BY t.sort ASC
|
||||
ORDER BY r.sort ASC, r.created_at ASC
|
||||
</select>
|
||||
|
||||
<select id="selectByAgentIds" resultType="xiaozhi.modules.agent.entity.AgentTagEntity">
|
||||
@@ -19,7 +19,7 @@
|
||||
#{id}
|
||||
</foreach>
|
||||
AND t.deleted = 0
|
||||
ORDER BY t.sort ASC
|
||||
ORDER BY r.sort ASC, r.created_at ASC
|
||||
</select>
|
||||
|
||||
<select id="selectAll" resultType="xiaozhi.modules.agent.entity.AgentTagEntity">
|
||||
|
||||
@@ -7,15 +7,15 @@
|
||||
</delete>
|
||||
|
||||
<insert id="insertRelation" parameterType="xiaozhi.modules.agent.entity.AgentTagRelationEntity">
|
||||
INSERT INTO ai_agent_tag_relation (id, agent_id, tag_id, creator, created_at, updater, updated_at)
|
||||
VALUES (#{id}, #{agentId}, #{tagId}, #{creator}, #{createdAt}, #{updater}, #{updatedAt})
|
||||
INSERT INTO ai_agent_tag_relation (id, agent_id, tag_id, sort, creator, created_at, updater, updated_at)
|
||||
VALUES (#{id}, #{agentId}, #{tagId}, #{sort}, #{creator}, #{createdAt}, #{updater}, #{updatedAt})
|
||||
</insert>
|
||||
|
||||
<insert id="batchInsertRelation" parameterType="java.util.List">
|
||||
INSERT INTO ai_agent_tag_relation (id, agent_id, tag_id, creator, created_at, updater, updated_at)
|
||||
INSERT INTO ai_agent_tag_relation (id, agent_id, tag_id, sort, creator, created_at, updater, updated_at)
|
||||
VALUES
|
||||
<foreach collection="list" item="item" separator=",">
|
||||
(#{item.id}, #{item.agentId}, #{item.tagId}, #{item.creator}, #{item.createdAt}, #{item.updater}, #{item.updatedAt})
|
||||
(#{item.id}, #{item.agentId}, #{item.tagId}, #{item.sort}, #{item.creator}, #{item.createdAt}, #{item.updater}, #{item.updatedAt})
|
||||
</foreach>
|
||||
</insert>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user