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

View File

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

View File

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

View File

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