feat(用例管理): 批量删除用例接口

This commit is contained in:
WangXu10 2023-11-07 15:43:16 +08:00 committed by Craftsman
parent 83c3dd2478
commit fd644b81a4
8 changed files with 211 additions and 14 deletions

View File

@ -134,4 +134,14 @@ public class FunctionalCaseController {
StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc");
return PageUtils.setPageInfo(page, functionalCaseService.getFunctionalCasePage(request, false));
}
@PostMapping("/batch/delete-to-gc")
@Operation(summary = "功能用例-批量删除用例")
@RequiresPermissions(PermissionConstants.FUNCTIONAL_CASE_READ_DELETE)
@Log(type = OperationLogType.DELETE, expression = "#msClass.batchDeleteFunctionalCaseLog(#request)", msClass = FunctionalCaseLogService.class)
public void batchDeleteFunctionalCaseToGc(@Validated @RequestBody FunctionalCaseBatchRequest request) {
String userId = SessionUtils.getUserId();
functionalCaseService.batchDeleteFunctionalCaseToGc(request, userId);
}
}

View File

@ -4,6 +4,7 @@ import io.metersphere.functional.domain.FunctionalCase;
import io.metersphere.functional.dto.FunctionalCasePageDTO;
import io.metersphere.functional.dto.FunctionalCaseVersionDTO;
import io.metersphere.functional.request.FunctionalCasePageRequest;
import io.metersphere.system.dto.table.TableBatchProcessDTO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -26,4 +27,12 @@ public interface ExtFunctionalCaseMapper {
List<FunctionalCase> checkCaseByModuleIds(@Param("moduleIds") List<String> deleteIds);
List<FunctionalCasePageDTO> list(@Param("request") FunctionalCasePageRequest request, @Param("deleted") boolean deleted);
List<String> getIds(@Param("request") TableBatchProcessDTO request, @Param("projectId") String projectId, @Param("deleted") boolean deleted);
void batchDelete(@Param("ids") List<String> ids, @Param("userId") String userId);
List<FunctionalCase> getLogInfo(@Param("ids") List<String> ids);
List<String> getRefIds(@Param("ids") List<String> ids);
}

View File

@ -98,7 +98,9 @@
or functional_case.tags like JSON_CONTAINS(tags, concat('["',#{request.keyword},'"]'))
)
</if>
<include refid="filter"/>
<include refid="filters">
<property name="filter" value="request.filter"/>
</include>
<include refid="combine">
<property name="condition" value="request.combine"/>
</include>
@ -107,9 +109,9 @@
</include>
</sql>
<sql id="filter">
<if test="request.filter != null and request.filter.size() > 0">
<foreach collection="request.filter.entrySet()" index="key" item="values">
<sql id="filters">
<if test="${filter} != null and ${filter}.size() > 0">
<foreach collection="${filter}.entrySet()" index="key" item="values">
<if test="values != null and values.size() > 0">
<choose>
<when test="key=='review_status'">
@ -200,4 +202,78 @@
AND ${versionTable}.latest = 0
</if>
</sql>
<select id="getIds" resultType="java.lang.String">
SELECT
id
FROM
functional_case
WHERE
project_id = #{projectId}
and deleted = #{deleted}
<include refid="queryWhereConditionByBaseQueryRequest"/>
</select>
<sql id="queryWhereConditionByBaseQueryRequest">
<where>
<if test="request.condition.combine != null">
<include refid="combine">
<property name="condition" value="request.condition.combine"/>
</include>
</if>
<if test="request.condition.keyword != null">
and (
functional_case.name like concat('%', #{request.keyword},'%')
or functional_case.num like concat('%', #{request.keyword},'%')
or functional_case.tags like JSON_CONTAINS(tags, concat('["',#{request.keyword},'"]'))
)
</if>
<include refid="filters">
<property name="filter" value="request.condition.filter"/>
</include>
</where>
</sql>
<select id="getRefIds" resultType="java.lang.String">
SELECT
ref_id
FROM
functional_case
where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
and deleted = false
group by ref_id
</select>
<update id="batchDelete">
update functional_case
set deleted = 1,
delete_user = #{userId},
delete_time = UNIX_TIMESTAMP()*1000
where ref_id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
and deleted = false
</update>
<select id="getLogInfo" resultType="io.metersphere.functional.domain.FunctionalCase">
SELECT
id,
name,
project_id
from
functional_case
where id in
<foreach collection="ids" item="id" open="(" separator="," close=")">
#{id}
</foreach>
and deleted = false
</select>
</mapper>

View File

@ -0,0 +1,24 @@
package io.metersphere.functional.request;
import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.io.Serializable;
/**
* @author wx
*/
@Data
@EqualsAndHashCode(callSuper = false)
public class FunctionalCaseBatchRequest extends TableBatchProcessDTO implements Serializable {
private static final long serialVersionUID = 1L;
@Schema(description = "项目ID", requiredMode = Schema.RequiredMode.REQUIRED)
private String projectId;
}

View File

@ -1,8 +1,10 @@
package io.metersphere.functional.service;
import io.metersphere.functional.domain.FunctionalCase;
import io.metersphere.functional.mapper.ExtFunctionalCaseMapper;
import io.metersphere.functional.mapper.FunctionalCaseMapper;
import io.metersphere.functional.request.FunctionalCaseAddRequest;
import io.metersphere.functional.request.FunctionalCaseBatchRequest;
import io.metersphere.functional.request.FunctionalCaseDeleteRequest;
import io.metersphere.functional.request.FunctionalCaseEditRequest;
import io.metersphere.sdk.constants.HttpMethodConstants;
@ -12,6 +14,7 @@ import io.metersphere.system.log.constants.OperationLogType;
import io.metersphere.system.log.dto.LogDTO;
import io.metersphere.system.log.service.OperationLogService;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@ -31,6 +34,10 @@ public class FunctionalCaseLogService {
@Resource
private OperationLogService operationLogService;
@Resource
private FunctionalCaseService functionalCaseService;
@Resource
private ExtFunctionalCaseMapper extFunctionalCaseMapper;
//TODO 日志(需要修改)
@ -129,4 +136,29 @@ public class FunctionalCaseLogService {
});
operationLogService.batchAdd(dtoList);
}
public List<LogDTO> batchDeleteFunctionalCaseLog(FunctionalCaseBatchRequest request) {
List<String> ids = functionalCaseService.doSelectIds(request, request.getProjectId());
List<LogDTO> dtoList = new ArrayList<>();
if (CollectionUtils.isNotEmpty(ids)) {
List<FunctionalCase> functionalCases = extFunctionalCaseMapper.getLogInfo(ids);
functionalCases.forEach(functionalCase -> {
LogDTO dto = new LogDTO(
functionalCase.getProjectId(),
null,
functionalCase.getId(),
null,
OperationLogType.DELETE.name(),
OperationLogModule.FUNCTIONAL_CASE,
functionalCase.getName());
dto.setPath("/functional/case/batch/deleteToGc");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(functionalCase));
dtoList.add(dto);
});
}
return dtoList;
}
}

View File

@ -9,10 +9,7 @@ import io.metersphere.functional.mapper.ExtFunctionalCaseMapper;
import io.metersphere.functional.mapper.FunctionalCaseBlobMapper;
import io.metersphere.functional.mapper.FunctionalCaseFollowerMapper;
import io.metersphere.functional.mapper.FunctionalCaseMapper;
import io.metersphere.functional.request.FunctionalCaseAddRequest;
import io.metersphere.functional.request.FunctionalCaseDeleteRequest;
import io.metersphere.functional.request.FunctionalCaseEditRequest;
import io.metersphere.functional.request.FunctionalCasePageRequest;
import io.metersphere.functional.request.*;
import io.metersphere.functional.result.FunctionalCaseResultCode;
import io.metersphere.project.service.ProjectTemplateService;
import io.metersphere.sdk.constants.ApplicationNumScope;
@ -23,6 +20,7 @@ import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.system.dto.sdk.TemplateCustomFieldDTO;
import io.metersphere.system.dto.sdk.TemplateDTO;
import io.metersphere.system.dto.table.TableBatchProcessDTO;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.uid.NumGenerator;
import jakarta.annotation.Resource;
@ -387,4 +385,26 @@ public class FunctionalCaseService {
return functionalCaseLists;
}
public void batchDeleteFunctionalCaseToGc(FunctionalCaseBatchRequest request, String userId) {
List<String> ids = doSelectIds(request, request.getProjectId());
if (CollectionUtils.isNotEmpty(ids)) {
List<String> refId = extFunctionalCaseMapper.getRefIds(ids);
extFunctionalCaseMapper.batchDelete(refId, userId);
}
}
public <T> List<String> doSelectIds(T dto, String projectId) {
TableBatchProcessDTO request = (TableBatchProcessDTO) dto;
if (request.isSelectAll()) {
List<String> ids = extFunctionalCaseMapper.getIds(request, projectId, false);
if (CollectionUtils.isNotEmpty(request.getExcludeIds())) {
ids.removeAll(request.getExcludeIds());
}
return ids;
} else {
return request.getSelectIds();
}
}
}

View File

@ -44,6 +44,7 @@ public class FunctionalCaseControllerTests extends BaseTest {
public static final String FUNCTIONAL_CASE_FOLLOWER_URL = "/functional/case/follower/";
public static final String FUNCTIONAL_CASE_DELETE_URL = "/functional/case/delete";
public static final String FUNCTIONAL_CASE_LIST_URL = "/functional/case/page";
public static final String FUNCTIONAL_CASE_BATCH_DELETE_URL = "/functional/case/batch/delete-to-gc";
@Resource
private NotificationMapper notificationMapper;
@ -266,12 +267,12 @@ public class FunctionalCaseControllerTests extends BaseTest {
Assertions.assertNotNull(resultHolder);
//自定义字段 测试
Map<String,Object> map = new HashMap<>();
map.put("customs",Arrays.asList(new LinkedHashMap(){{
put("id","TEST_FIELD_ID");
put("operator","in");
put("value","222");
put("type","List");
Map<String, Object> map = new HashMap<>();
map.put("customs", Arrays.asList(new LinkedHashMap() {{
put("id", "TEST_FIELD_ID");
put("operator", "in");
put("value", "222");
put("type", "List");
}}));
request.setCombine(map);
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_LIST_URL, request);
@ -297,4 +298,21 @@ public class FunctionalCaseControllerTests extends BaseTest {
request.setDeleteAll(true);
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_DELETE_URL, request);
}
@Test
@Order(7)
public void testBatchDelete() throws Exception {
FunctionalCaseBatchRequest request = new FunctionalCaseBatchRequest();
request.setProjectId(DEFAULT_PROJECT_ID);
request.setSelectAll(false);
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_DELETE_URL, request);
request.setSelectIds(Arrays.asList("TEST_FUNCTIONAL_CASE_ID_5", "TEST_FUNCTIONAL_CASE_ID_7"));
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_DELETE_URL, request);
request.setSelectAll(true);
request.setExcludeIds(Arrays.asList("TEST_FUNCTIONAL_CASE_ID_2"));
this.requestPostWithOkAndReturn(FUNCTIONAL_CASE_BATCH_DELETE_URL, request);
}
}

View File

@ -16,6 +16,14 @@ VALUES ('TEST_FUNCTIONAL_CASE_ID_3', 3, 'TEST_MOUDLE_ID', '100001100001', '10000
INSERT INTO functional_case(id, num, module_id, project_id, template_id, name, review_status, tags, case_edit_type, pos, version_id, ref_id, last_execute_result, deleted, public_case, latest, create_user, update_user, delete_user, create_time, update_time, delete_time)
VALUES ('TEST_FUNCTIONAL_CASE_ID_4', 4, 'TEST_MOUDLE_ID', '100001100001', '100001', 'copy_测试多版本', 'UN_REVIEWED', NULL, 'STEP', 0, 'v3.0.0', 'TEST_REF_ID', 'UN_EXECUTED', b'0', b'0', b'0', 'admin', 'admin', '', 1698058347559, 1698058347559, NULL);
INSERT INTO functional_case(id, num, module_id, project_id, template_id, name, review_status, tags, case_edit_type, pos, version_id, ref_id, last_execute_result, deleted, public_case, latest, create_user, update_user, delete_user, create_time, update_time, delete_time)
VALUES ('TEST_FUNCTIONAL_CASE_ID_5', 5, 'TEST_MOUDLE_ID', '100001100001', '100001', 'copy_测试多版本', 'UN_REVIEWED', NULL, 'STEP', 0, 'v3.0.0', 'TEST_REF_ID_1', 'UN_EXECUTED', b'0', b'0', b'0', 'admin', 'admin', '', 1698058347559, 1698058347559, NULL);
INSERT INTO functional_case(id, num, module_id, project_id, template_id, name, review_status, tags, case_edit_type, pos, version_id, ref_id, last_execute_result, deleted, public_case, latest, create_user, update_user, delete_user, create_time, update_time, delete_time)
VALUES ('TEST_FUNCTIONAL_CASE_ID_6', 6, 'TEST_MOUDLE_ID', '100001100001', '100001', 'copy_测试多版本', 'UN_REVIEWED', NULL, 'STEP', 0, 'v3.0.0', 'TEST_REF_ID_1', 'UN_EXECUTED', b'0', b'0', b'0', 'admin', 'admin', '', 1698058347559, 1698058347559, NULL);
INSERT INTO functional_case(id, num, module_id, project_id, template_id, name, review_status, tags, case_edit_type, pos, version_id, ref_id, last_execute_result, deleted, public_case, latest, create_user, update_user, delete_user, create_time, update_time, delete_time)
VALUES ('TEST_FUNCTIONAL_CASE_ID_7', 7, 'TEST_MOUDLE_ID', '100001100001', '100001', 'copy_测试多版本', 'UN_REVIEWED', NULL, 'STEP', 0, 'v3.0.0', 'TEST_REF_ID_2', 'UN_EXECUTED', b'0', b'0', b'0', 'admin', 'admin', '', 1698058347559, 1698058347559, NULL);
INSERT INTO functional_case_blob(id, steps, text_description, expected_result, prerequisite, description) VALUES ('TEST_FUNCTIONAL_CASE_ID', 'STEP', '1111', NULL, NULL, 'TEST');