fix: 导入创建功能用例ID重复

--bug=1009981 --user=陈建星 导入用例ID重复 https://www.tapd.cn/55049933/s/1098516
This commit is contained in:
chenjianxing 2022-01-25 21:40:57 +08:00 committed by zhangdahai112
parent 79ef990069
commit 875f711eee
4 changed files with 40 additions and 38 deletions

View File

@ -56,8 +56,7 @@
a.protocol, a.protocol,
t.status execResult, t.status execResult,
a.user_id, a.user_id,
a.version_id versionId, a.version_id versionId
project_version.name versionName
from from
test_plan_api_case t test_plan_api_case t
inner join inner join
@ -70,7 +69,7 @@
api_definition a api_definition a
on on
c.api_definition_id = a.id c.api_definition_id = a.id
inner join project_version on a.version_id = project_version.id and a.project_id = project_version.project_id where 1
<if test="request.protocol != null and request.protocol!=''"> <if test="request.protocol != null and request.protocol!=''">
and a.protocol = #{request.protocol} and a.protocol = #{request.protocol}
</if> </if>
@ -91,7 +90,6 @@
and t.status = #{request.status} and t.status = #{request.status}
</otherwise> </otherwise>
</choose> </choose>
where 1
<if test="request.ids != null and request.ids.size() > 0"> <if test="request.ids != null and request.ids.size() > 0">
<if test="request.projectId != null and request.projectId!=''"> <if test="request.projectId != null and request.projectId!=''">
and a.projectId = #{request.projectId} and a.projectId = #{request.projectId}

View File

@ -47,7 +47,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
/** /**
* 每隔2000条存储数据库然后清理list 方便内存回收 * 每隔2000条存储数据库然后清理list 方便内存回收
*/ */
protected static final int BATCH_COUNT = 2000; protected static final int BATCH_COUNT = 5000;
private TestCaseService testCaseService; private TestCaseService testCaseService;
@ -158,7 +158,7 @@ public class TestCaseNoModelDataListener extends AnalysisEventListener<Map<Integ
if (data.getCustomNum() == null) { if (data.getCustomNum() == null) {
stringBuilder.append(Translator.get("id_required") + ";"); stringBuilder.append(Translator.get("id_required") + ";");
} else { } else {
String customId = data.getCustomNum().toString(); String customId = data.getCustomNum();
if (StringUtils.isEmpty(customId)) { if (StringUtils.isEmpty(customId)) {
stringBuilder.append(Translator.get("id_required") + ";"); stringBuilder.append(Translator.get("id_required") + ";");
} else if (customIds.contains(customId)) { } else if (customIds.contains(customId)) {

View File

@ -164,6 +164,9 @@ public class TestCaseService {
@Resource @Resource
private ProjectVersionMapper projectVersionMapper; private ProjectVersionMapper projectVersionMapper;
private ThreadLocal<Integer> importCreateNum = new ThreadLocal<>();
private ThreadLocal<Integer> beforeImportCreateNum = new ThreadLocal<>();
private void setNode(TestCaseWithBLOBs testCase) { private void setNode(TestCaseWithBLOBs testCase) {
if (StringUtils.isEmpty(testCase.getNodeId()) || "default-module".equals(testCase.getNodeId())) { if (StringUtils.isEmpty(testCase.getNodeId()) || "default-module".equals(testCase.getNodeId())) {
TestCaseNodeExample example = new TestCaseNodeExample(); TestCaseNodeExample example = new TestCaseNodeExample();
@ -251,16 +254,11 @@ public class TestCaseService {
} }
private void checkCustomNumExist(TestCaseWithBLOBs testCase) { private void checkCustomNumExist(TestCaseWithBLOBs testCase) {
String id = testCase.getId();
TestCaseWithBLOBs testCaseWithBLOBs = testCaseMapper.selectByPrimaryKey(id);
TestCaseExample example = new TestCaseExample(); TestCaseExample example = new TestCaseExample();
TestCaseExample.Criteria criteria = example.createCriteria(); example.createCriteria()
criteria.andCustomNumEqualTo(testCase.getCustomNum()) .andCustomNumEqualTo(testCase.getCustomNum())
.andProjectIdEqualTo(testCase.getProjectId()) .andProjectIdEqualTo(testCase.getProjectId())
.andIdNotEqualTo(testCase.getId()); .andIdNotEqualTo(testCase.getId());
if (testCaseWithBLOBs != null && StringUtils.isNotBlank(testCaseWithBLOBs.getRefId())) {
criteria.andRefIdNotEqualTo(testCaseWithBLOBs.getRefId());
}
List<TestCase> list = testCaseMapper.selectByExample(example); List<TestCase> list = testCaseMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(list)) {
MSException.throwException(Translator.get("custom_num_is_exist")); MSException.throwException(Translator.get("custom_num_is_exist"));
@ -729,6 +727,9 @@ public class TestCaseService {
if (StringUtils.isBlank(request.getVersionId()) && StringUtils.equals(request.getImportType(), FunctionCaseImportEnum.Create.name())) { if (StringUtils.isBlank(request.getVersionId()) && StringUtils.equals(request.getImportType(), FunctionCaseImportEnum.Create.name())) {
// 创建如果没选版本就创建最新版本更新时没选就更新最近版本的用例 // 创建如果没选版本就创建最新版本更新时没选就更新最近版本的用例
request.setVersionId(extProjectVersionMapper.getDefaultVersion(request.getProjectId())); request.setVersionId(extProjectVersionMapper.getDefaultVersion(request.getProjectId()));
int nextNum = getNextNum(request.getProjectId());
importCreateNum.set(nextNum);
beforeImportCreateNum.set(nextNum);
} }
if (multipartFile.getOriginalFilename().endsWith(".xmind")) { if (multipartFile.getOriginalFilename().endsWith(".xmind")) {
return testCaseXmindImport(multipartFile, request, httpRequest); return testCaseXmindImport(multipartFile, request, httpRequest);
@ -917,36 +918,38 @@ public class TestCaseService {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
Project project = projectService.getProjectById(projectId); Project project = projectService.getProjectById(projectId);
TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class); TestCaseMapper mapper = sqlSession.getMapper(TestCaseMapper.class);
try { try {
Long nextOrder = ServiceUtils.getNextOrder(projectId, extTestCaseMapper::getLastOrder);
if (!testCases.isEmpty()) { if (!testCases.isEmpty()) {
AtomicInteger sort = new AtomicInteger(); Integer num = importCreateNum.get();
AtomicInteger num = new AtomicInteger(); Integer beforeInsertId = beforeImportCreateNum.get();
num.set(getNextNum(projectId) + testCases.size());
for (TestCaseWithBLOBs testcase : testCases) { for (int i = testCases.size() - 1; i > - 1; i--) { // 反向遍历保持和文件顺序一致
testcase.setId(UUID.randomUUID().toString()); TestCaseWithBLOBs testCase = testCases.get(i);
testcase.setCreateUser(SessionUtils.getUserId()); testCase.setId(UUID.randomUUID().toString());
testcase.setCreateTime(System.currentTimeMillis()); testCase.setCreateUser(SessionUtils.getUserId());
testcase.setUpdateTime(System.currentTimeMillis()); testCase.setCreateTime(System.currentTimeMillis());
testcase.setNodeId(nodePathMap.get(testcase.getNodePath())); testCase.setUpdateTime(System.currentTimeMillis());
testcase.setSort(sort.getAndIncrement()); testCase.setNodeId(nodePathMap.get(testCase.getNodePath()));
int number = num.incrementAndGet(); testCase.setNum(num);
testcase.setNum(number); if (project.getCustomNum() && StringUtils.isBlank(testCase.getCustomNum())) {
if (project.getCustomNum() && StringUtils.isBlank(testcase.getCustomNum())) { testCase.setCustomNum(String.valueOf(num));
testcase.setCustomNum(String.valueOf(number));
} }
testcase.setReviewStatus(TestCaseReviewStatus.Prepare.name()); num++;
testcase.setStatus(TestCaseReviewStatus.Prepare.name()); testCase.setReviewStatus(TestCaseReviewStatus.Prepare.name());
testcase.setOrder(nextOrder); testCase.setStatus(TestCaseReviewStatus.Prepare.name());
testcase.setRefId(testcase.getId()); testCase.setOrder(new Long(testCases.size() - (num - beforeInsertId)) * ServiceUtils.ORDER_STEP);
testcase.setVersionId(request.getVersionId()); testCase.setRefId(testCase.getId());
testcase.setLatest(true); testCase.setVersionId(request.getVersionId());
mapper.insert(testcase); testCase.setLatest(true);
nextOrder += ServiceUtils.ORDER_STEP; mapper.insert(testCase);
} }
importCreateNum.set(num);
} }
sqlSession.flushStatements();
} finally { } finally {
sqlSession.commit();
sqlSession.clearCache();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory); SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
} }
} }
@ -1671,7 +1674,6 @@ public class TestCaseService {
} }
} }
/** /**
* 导入用例前检查数据库是否存在此用例 * 导入用例前检查数据库是否存在此用例
* *

View File

@ -80,6 +80,7 @@ public class TestPlanApiCaseService {
request.setProjectId(null); request.setProjectId(null);
request.setOrders(ServiceUtils.getDefaultSortOrder(request.getOrders())); request.setOrders(ServiceUtils.getDefaultSortOrder(request.getOrders()));
List<TestPlanApiCaseDTO> apiTestCases = extTestPlanApiCaseMapper.list(request); List<TestPlanApiCaseDTO> apiTestCases = extTestPlanApiCaseMapper.list(request);
ServiceUtils.buildVersionInfo(apiTestCases);
if (CollectionUtils.isEmpty(apiTestCases)) { if (CollectionUtils.isEmpty(apiTestCases)) {
return apiTestCases; return apiTestCases;
} }
@ -244,6 +245,7 @@ public class TestPlanApiCaseService {
ApiTestCaseRequest selectReq = new ApiTestCaseRequest(); ApiTestCaseRequest selectReq = new ApiTestCaseRequest();
selectReq.setIds(ids); selectReq.setIds(ids);
List<TestPlanApiCaseDTO> returnList = extTestPlanApiCaseMapper.list(selectReq); List<TestPlanApiCaseDTO> returnList = extTestPlanApiCaseMapper.list(selectReq);
ServiceUtils.buildVersionInfo(returnList);
return returnList; return returnList;
} }