BUGFIX:为design的排序(collection_sort)添加唯一约束

This commit is contained in:
2025-10-13 13:50:51 +08:00
parent 3d67ac71f3
commit 4ce6fb4190
2 changed files with 39 additions and 1 deletions

View File

@@ -2550,6 +2550,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
subAccount.setSystemUser(subUserRole);
subAccount.setLanguage(Language.ENGLISH.name());
subAccount.setValidStartTime(adminAcc.getValidStartTime());
subAccount.setValidEndTime(adminAcc.getValidEndTime());
subAccount.setCreateDate(new Date());
subAccount.setIsTrial(0);
subAccount.setIsBeginner(1);

View File

@@ -1,5 +1,6 @@
package com.ai.da.service.impl;
import com.ai.da.common.config.exception.BusinessException;
import com.ai.da.mapper.primary.CollectionSortMapper;
import com.ai.da.mapper.primary.entity.CollectionSort;
import com.ai.da.model.dto.CollectionSortDTO;
@@ -8,12 +9,14 @@ import com.ai.da.service.CollectionSortService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@Slf4j
@@ -50,7 +53,7 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
return baseMapper.selectOne(qw);
}
@Override
/*@Override
public int getNextSort(Long projectId, Long parentId) {
QueryWrapper<CollectionSort> qw = new QueryWrapper<>();
qw.lambda().eq(CollectionSort::getProjectId, projectId);
@@ -64,6 +67,39 @@ public class CollectionSortServiceImpl extends ServiceImpl<CollectionSortMapper,
return 1;
}
return userLikeSorts.size() + 1;
}*/
// 添加唯一约束
// ALTER TABLE collection_sort ADD UNIQUE KEY sort (project_id, parent_id, sort);
public int getNextSort(Long projectId, Long parentId) {
int retryCount = 3;
while (retryCount-- > 0) {
try {
Integer maxSort = getMaxSortFromDB(projectId, parentId);
return maxSort + 1;
} catch (DuplicateKeyException e) {
// 如果发生唯一约束冲突,重试
if (retryCount == 0) {
throw new BusinessException("获取排序号失败,请重试");
}
}
}
throw new BusinessException("获取排序号失败");
}
private Integer getMaxSortFromDB(Long projectId, Long parentId) {
QueryWrapper<CollectionSort> qw = new QueryWrapper<>();
qw.select("COALESCE(MAX(sort), 0) as maxSort")
.eq("project_id", projectId);
if (null != parentId) {
qw.eq("parent_id", parentId);
} else {
qw.isNull("parent_id");
}
Map<String, Object> result = baseMapper.selectMaps(qw).get(0);
return (Integer) result.get("maxSort");
}
@Override