mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-27 01:23:55 +08:00
fix: 修复智能体标签顺序错乱问题
This commit is contained in:
+3
@@ -24,6 +24,9 @@ public class AgentTagRelationEntity {
|
|||||||
@Schema(description = "标签ID")
|
@Schema(description = "标签ID")
|
||||||
private String tagId;
|
private String tagId;
|
||||||
|
|
||||||
|
@Schema(description = "排序")
|
||||||
|
private Integer sort;
|
||||||
|
|
||||||
@Schema(description = "创建者")
|
@Schema(description = "创建者")
|
||||||
private Long creator;
|
private Long creator;
|
||||||
|
|
||||||
|
|||||||
+9
-10
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -104,13 +105,15 @@ public class AgentTagServiceImpl extends BaseServiceImpl<AgentTagDao, AgentTagEn
|
|||||||
existTags = baseDao.selectByTagNames(newTagNames);
|
existTags = baseDao.selectByTagNames(newTagNames);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<String> existTagNames = existTags.stream()
|
Map<String, AgentTagEntity> existTagMap = existTags.stream()
|
||||||
.map(AgentTagEntity::getTagName)
|
.collect(Collectors.toMap(AgentTagEntity::getTagName, t -> t, (a, b) -> a));
|
||||||
.collect(Collectors.toList());
|
|
||||||
|
|
||||||
List<AgentTagEntity> tagsToInsert = new ArrayList<>();
|
List<AgentTagEntity> tagsToInsert = new ArrayList<>();
|
||||||
for (String tagName : newTagNames) {
|
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();
|
AgentTagEntity tag = new AgentTagEntity();
|
||||||
tag.setId(UUID.randomUUID().toString().replace("-", ""));
|
tag.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||||
tag.setTagName(tagName);
|
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()) {
|
if (tagIds != null && !tagIds.isEmpty()) {
|
||||||
List<AgentTagEntity> tagIdEntities = baseDao.selectBatchIds(tagIds);
|
List<AgentTagEntity> tagIdEntities = baseDao.selectBatchIds(tagIds);
|
||||||
for (AgentTagEntity tag : tagIdEntities) {
|
for (AgentTagEntity tag : tagIdEntities) {
|
||||||
@@ -152,11 +149,13 @@ public class AgentTagServiceImpl extends BaseServiceImpl<AgentTagDao, AgentTagEn
|
|||||||
|
|
||||||
List<AgentTagRelationEntity> relations = new ArrayList<>();
|
List<AgentTagRelationEntity> relations = new ArrayList<>();
|
||||||
Date now = new Date();
|
Date now = new Date();
|
||||||
|
int sort = 0;
|
||||||
for (String tagId : allTagIds) {
|
for (String tagId : allTagIds) {
|
||||||
AgentTagRelationEntity relation = new AgentTagRelationEntity();
|
AgentTagRelationEntity relation = new AgentTagRelationEntity();
|
||||||
relation.setId(UUID.randomUUID().toString().replace("-", ""));
|
relation.setId(UUID.randomUUID().toString().replace("-", ""));
|
||||||
relation.setAgentId(agentId);
|
relation.setAgentId(agentId);
|
||||||
relation.setTagId(tagId);
|
relation.setTagId(tagId);
|
||||||
|
relation.setSort(sort++);
|
||||||
relation.setCreatedAt(now);
|
relation.setCreatedAt(now);
|
||||||
relation.setUpdatedAt(now);
|
relation.setUpdatedAt(now);
|
||||||
relations.add(relation);
|
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:
|
- sqlFile:
|
||||||
encoding: utf8
|
encoding: utf8
|
||||||
path: classpath:db/changelog/202602281000.sql
|
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
|
FROM ai_agent_tag t
|
||||||
INNER JOIN ai_agent_tag_relation r ON t.id = r.tag_id
|
INNER JOIN ai_agent_tag_relation r ON t.id = r.tag_id
|
||||||
WHERE r.agent_id = #{agentId} AND t.deleted = 0
|
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>
|
||||||
|
|
||||||
<select id="selectByAgentIds" resultType="xiaozhi.modules.agent.entity.AgentTagEntity">
|
<select id="selectByAgentIds" resultType="xiaozhi.modules.agent.entity.AgentTagEntity">
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
#{id}
|
#{id}
|
||||||
</foreach>
|
</foreach>
|
||||||
AND t.deleted = 0
|
AND t.deleted = 0
|
||||||
ORDER BY t.sort ASC
|
ORDER BY r.sort ASC, r.created_at ASC
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select id="selectAll" resultType="xiaozhi.modules.agent.entity.AgentTagEntity">
|
<select id="selectAll" resultType="xiaozhi.modules.agent.entity.AgentTagEntity">
|
||||||
|
|||||||
@@ -7,15 +7,15 @@
|
|||||||
</delete>
|
</delete>
|
||||||
|
|
||||||
<insert id="insertRelation" parameterType="xiaozhi.modules.agent.entity.AgentTagRelationEntity">
|
<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)
|
INSERT INTO ai_agent_tag_relation (id, agent_id, tag_id, sort, creator, created_at, updater, updated_at)
|
||||||
VALUES (#{id}, #{agentId}, #{tagId}, #{creator}, #{createdAt}, #{updater}, #{updatedAt})
|
VALUES (#{id}, #{agentId}, #{tagId}, #{sort}, #{creator}, #{createdAt}, #{updater}, #{updatedAt})
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
<insert id="batchInsertRelation" parameterType="java.util.List">
|
<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
|
VALUES
|
||||||
<foreach collection="list" item="item" separator=",">
|
<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>
|
</foreach>
|
||||||
</insert>
|
</insert>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user