refactor(项目设置): 文件管理的表格数据查询和模块数量统计分成两个接口

This commit is contained in:
song-tianyang 2023-10-19 15:36:17 +08:00 committed by 建国
parent ac903a0124
commit 491e58d3ca
13 changed files with 97 additions and 104 deletions

View File

@ -1,11 +1,12 @@
package io.metersphere.project.controller;
import io.metersphere.project.dto.FileTableResult;
import io.metersphere.project.dto.FileInformationDTO;
import io.metersphere.project.request.filemanagement.*;
import io.metersphere.project.service.FileManagementService;
import io.metersphere.project.service.FileMetadataService;
import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.utils.SessionUtils;
import io.swagger.v3.oas.annotations.Operation;
@ -17,6 +18,9 @@ import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.Map;
@Tag(name = "项目管理-文件管理-文件")
@RestController
@RequestMapping("/project/file")
@ -30,10 +34,17 @@ public class FileManagementController {
@PostMapping("/page")
@Operation(summary = "项目管理-文件管理-表格分页查询文件")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ)
public FileTableResult page(@Validated @RequestBody FileMetadataTableRequest request) {
public Pager<List<FileInformationDTO>> page(@Validated @RequestBody FileMetadataTableRequest request) {
return fileMetadataService.page(request);
}
@PostMapping("/module/count")
@Operation(summary = "项目管理-文件管理-表格分页查询文件")
@RequiresPermissions(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ)
public Map<String, Integer> moduleCount(@Validated @RequestBody FileMetadataTableRequest request) {
return fileMetadataService.moduleCount(request);
}
@PostMapping("/upload")
@Operation(summary = "项目管理-文件管理-上传文件")

View File

@ -1,18 +0,0 @@
package io.metersphere.project.dto;
import io.metersphere.sdk.util.Pager;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Data
public class FileTableResult {
@Schema(description = "表格数据")
Pager<List<FileInformationDTO>> tableData;
@Schema(description = "模块统计")
Map<String, Long> moduleCount = new HashMap<>();
}

View File

@ -5,5 +5,5 @@ import lombok.Data;
@Data
public class ModuleCountDTO {
private String moduleId;
private long dataCount;
private int dataCount;
}

View File

@ -7,9 +7,9 @@ import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface ExtFileMetadataMapper {
List<FileMetadata> selectByKeywordAndFileType(@Param("projectId") String projectId, @Param("keyword") String keyword, @Param("moduleIds") List<String> moduleIds, @Param("fileTypes") List<String> fileTypes, @Param("isRefId") boolean isRefId);
List<FileMetadata> selectByKeywordAndFileType(@Param("projectId") String projectId, @Param("keyword") String keyword, @Param("moduleIds") List<String> moduleIds, @Param("fileType") String fileType, @Param("isRefId") boolean isRefId);
List<ModuleCountDTO> countModuleIdByKeywordAndFileType(@Param("projectId") String projectId, @Param("keyword") String keyword, @Param("moduleIds") List<String> moduleIds, @Param("fileTypes") List<String> fileTypes);
List<ModuleCountDTO> countModuleIdByKeywordAndFileType(@Param("projectId") String projectId, @Param("keyword") String keyword, @Param("moduleIds") List<String> moduleIds, @Param("fileType") String fileType);
List<String> selectIdByRefIdList(@Param("refIdList") List<String> refIdList);

View File

@ -21,30 +21,43 @@
</if>
FROM file_metadata f
INNER JOIN user u ON f.update_user = u.id
WHERE latest = true
AND f.project_id = #{projectId}
<if test="keyword != null and keyword != ''">
AND f.name = #{keyword}
</if>
<if test="moduleIds != null and moduleIds.size() != 0">
AND f.module_id IN
<foreach collection="moduleIds" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="fileTypes != null and fileTypes.size() != 0 ">
AND f.type IN
<foreach collection="fileTypes" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<include refid="file_page_request"/>
</select>
<select id="countModuleIdByKeywordAndFileType" resultType="io.metersphere.project.dto.ModuleCountDTO">
SELECT f.module_id AS moduleId, count(f.id) AS dataCount
FROM file_metadata f
INNER JOIN user u ON f.update_user = u.id
<include refid="file_page_request"/>
GROUP BY f.module_id
</select>
<sql id="file_page_request">
<where>
f.latest = true
AND f.project_id = #{projectId}
<if test="keyword != null and keyword != ''">
AND f.name = #{keyword}
</if>
<if test="moduleIds != null and moduleIds.size() != 0">
AND f.module_id IN
<foreach collection="moduleIds" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="fileType != null and fileType != ''">
AND f.type = #{fileType}
</if>
</where>
</sql>
<select id="selectIdByRefIdList" resultType="java.lang.String">
SELECT id FROM file_metadata WHERE ref_id IN
<foreach collection="refIdList" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</select>
<select id="selectDeleteFileInfoByIds" resultType="io.metersphere.project.domain.FileMetadata">
SELECT
f.id,
@ -55,29 +68,7 @@
#{item}
</foreach>
</select>
<select id="countModuleIdByKeywordAndFileType" resultType="io.metersphere.project.dto.ModuleCountDTO">
SELECT f.module_id AS moduleId, count(f.id) AS dataCount
FROM file_metadata f
INNER JOIN user u ON f.update_user = u.id
WHERE latest = true
AND f.project_id = #{projectId}
<if test="keyword != null and keyword != ''">
AND f.name = #{keyword}
</if>
<if test="moduleIds != null and moduleIds.size() != 0">
AND f.module_id IN
<foreach collection="moduleIds" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
<if test="fileTypes != null and fileTypes.size() != 0 ">
AND f.type IN
<foreach collection="fileTypes" item="item" open="(" separator="," close=")">
#{item}
</foreach>
</if>
GROUP BY f.module_id
</select>
<select id="selectRefIdByIds" resultType="io.metersphere.project.domain.FileMetadata">
SELECT DISTINCT f.ref_id FROM file_metadata f
WHERE

View File

@ -15,7 +15,7 @@ public class FileBatchProcessDTO extends TableBatchProcessDTO {
private String projectId;
@Schema(description = "文件类型")
private List<String> fileTypes;
private String fileType;
@Schema(description = "模块ID")
private List<String> moduleIds;

View File

@ -13,7 +13,7 @@ public class FileMetadataTableRequest extends BasePageRequest {
private List<String> moduleIds;
@Schema(description = "文件类型")
private List<String> fileTypes;
private String fileType;
@Schema(description = "项目ID")
@NotBlank(message = "{id must not be blank}")

View File

@ -76,7 +76,7 @@ public class FileManagementService {
List<String> processIds = request.getSelectIds();
List<FileMetadata> refFileList = new ArrayList<>();
if (request.isSelectAll()) {
refFileList = extFileMetadataMapper.selectByKeywordAndFileType(request.getProjectId(), request.getCondition().getKeyword(), request.getModuleIds(), request.getFileTypes(), true);
refFileList = extFileMetadataMapper.selectByKeywordAndFileType(request.getProjectId(), request.getCondition().getKeyword(), request.getModuleIds(), request.getFileType(), true);
if (CollectionUtils.isNotEmpty(request.getExcludeIds())) {
refFileList = refFileList.stream().filter(fileMetadata -> !request.getExcludeIds().contains(fileMetadata.getId())).collect(Collectors.toList());
}
@ -97,7 +97,7 @@ public class FileManagementService {
List<String> processIds = request.getSelectIds();
List<FileMetadata> refFileList = new ArrayList<>();
if (request.isSelectAll()) {
refFileList = extFileMetadataMapper.selectByKeywordAndFileType(request.getProjectId(), request.getCondition().getKeyword(), request.getModuleIds(), request.getFileTypes(), false);
refFileList = extFileMetadataMapper.selectByKeywordAndFileType(request.getProjectId(), request.getCondition().getKeyword(), request.getModuleIds(), request.getFileType(), false);
if (CollectionUtils.isNotEmpty(request.getExcludeIds())) {
refFileList = refFileList.stream().filter(fileMetadata -> !request.getExcludeIds().contains(fileMetadata.getId())).collect(Collectors.toList());
}

View File

@ -5,7 +5,6 @@ import com.github.pagehelper.PageHelper;
import io.metersphere.project.domain.FileMetadata;
import io.metersphere.project.domain.FileMetadataExample;
import io.metersphere.project.dto.FileInformationDTO;
import io.metersphere.project.dto.FileTableResult;
import io.metersphere.project.dto.ModuleCountDTO;
import io.metersphere.project.mapper.ExtFileMetadataMapper;
import io.metersphere.project.mapper.FileMetadataMapper;
@ -55,7 +54,7 @@ public class FileMetadataService {
public List<FileInformationDTO> list(FileMetadataTableRequest request) {
List<FileInformationDTO> returnList = new ArrayList<>();
List<FileMetadata> fileMetadataList = extFileMetadataMapper.selectByKeywordAndFileType(request.getProjectId(), request.getKeyword(), request.getModuleIds(), request.getFileTypes(), false);
List<FileMetadata> fileMetadataList = extFileMetadataMapper.selectByKeywordAndFileType(request.getProjectId(), request.getKeyword(), request.getModuleIds(), request.getFileType(), false);
fileMetadataList.forEach(fileMetadata -> {
FileInformationDTO fileInformationDTO = new FileInformationDTO(fileMetadata);
if (FilePreviewUtils.isImage(fileMetadata.getType())) {
@ -238,17 +237,10 @@ public class FileMetadataService {
}
}
public FileTableResult page(FileMetadataTableRequest request) {
FileTableResult dto = new FileTableResult();
public Pager<List<FileInformationDTO>> page(FileMetadataTableRequest request) {
Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(),
StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "update_time desc");
dto.setTableData(PageUtils.setPageInfo(page, this.list(request)));
//获取模块统计
List<ModuleCountDTO> moduleCountDTOList = extFileMetadataMapper.countModuleIdByKeywordAndFileType(request.getProjectId(), request.getKeyword(), request.getModuleIds(), request.getFileTypes());
Map<String, Long> moduleCountMap = moduleCountDTOList.stream().collect(Collectors.toMap(ModuleCountDTO::getModuleId, ModuleCountDTO::getDataCount));
dto.setModuleCount(moduleCountMap);
return dto;
return PageUtils.setPageInfo(page, this.list(request));
}
public ResponseEntity<byte[]> batchDownload(FileBatchProcessDTO request) {
@ -293,4 +285,10 @@ public class FileMetadataService {
throw new MSException(Translator.get("file.size.is.too.large"));
}
}
//获取模块统计
public Map<String, Integer> moduleCount(FileMetadataTableRequest request) {
List<ModuleCountDTO> moduleCountDTOList = extFileMetadataMapper.countModuleIdByKeywordAndFileType(request.getProjectId(), request.getKeyword(), request.getModuleIds(), request.getFileType());
return moduleCountDTOList.stream().collect(Collectors.toMap(ModuleCountDTO::getModuleId, ModuleCountDTO::getDataCount));
}
}

View File

@ -1,7 +1,7 @@
package io.metersphere.project.controller.filemanagement;
import io.metersphere.project.domain.*;
import io.metersphere.project.dto.FileTableResult;
import io.metersphere.project.dto.FileInformationDTO;
import io.metersphere.project.mapper.FileMetadataMapper;
import io.metersphere.project.mapper.FileModuleMapper;
import io.metersphere.project.mapper.ProjectMapper;
@ -15,6 +15,7 @@ import io.metersphere.sdk.constants.StorageType;
import io.metersphere.sdk.dto.BaseTreeNode;
import io.metersphere.sdk.dto.request.NodeMoveRequest;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.log.constants.OperationLogType;
@ -101,14 +102,14 @@ public class FileManagementControllerTests extends BaseTest {
this.setPageSize(10);
this.setProjectId(project.getId());
}};
MvcResult mvcResult = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_PAGE, request);
String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
MvcResult pageResult = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_PAGE, request);
String returnData = pageResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class);
FileTableResult result = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), FileTableResult.class);
Pager<List<FileInformationDTO>> result = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class);
//返回值的页码和当前页码相同
Assertions.assertEquals(result.getTableData().getCurrent(), request.getCurrent());
Assertions.assertEquals(result.getCurrent(), request.getCurrent());
//返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(result.getTableData().getList())).size() <= request.getPageSize());
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(result.getList())).size() <= request.getPageSize());
}
@Test
@ -584,9 +585,7 @@ public class FileManagementControllerTests extends BaseTest {
this.setCurrent(1);
this.setPageSize(10);
this.setProjectId(project.getId());
this.setFileTypes(new ArrayList<>() {{
this.add("JPG");
}});
this.setFileType("JPG");
}};
this.filePageRequestAndCheck(request, true);
@ -595,9 +594,7 @@ public class FileManagementControllerTests extends BaseTest {
this.setCurrent(1);
this.setPageSize(10);
this.setProjectId(project.getId());
this.setFileTypes(new ArrayList<>() {{
this.add("JpG");
}});
this.setFileType("JpG");
}};
this.filePageRequestAndCheck(request, true);
@ -606,9 +603,7 @@ public class FileManagementControllerTests extends BaseTest {
this.setCurrent(1);
this.setPageSize(10);
this.setProjectId(project.getId());
this.setFileTypes(new ArrayList<>() {{
this.add("fire");
}});
this.setFileType("fire");
}};
this.filePageRequestAndCheck(request, false);
}
@ -1111,10 +1106,14 @@ public class FileManagementControllerTests extends BaseTest {
private void filePageRequestAndCheck(FileMetadataTableRequest request, Boolean hasData) throws Exception {
MvcResult mvcResult = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_PAGE, request);
FileTableResult result = JSON.parseObject(JSON.toJSONString(
Pager<List<FileInformationDTO>> pageResult = JSON.parseObject(JSON.toJSONString(
JSON.parseObject(mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData()),
FileTableResult.class);
FileManagementBaseUtils.checkFilePage(result, request, hasData);
Pager.class);
MvcResult moduleCountMvcResult = this.requestPostWithOkAndReturn(FileManagementRequestUtils.URL_FILE_MODULE_COUNT, request);
Map<String, Integer> moduleCountResult = JSON.parseObject(JSON.toJSONString(
JSON.parseObject(moduleCountMvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8), ResultHolder.class).getData()),
Map.class);
FileManagementBaseUtils.checkFilePage(pageResult, moduleCountResult, request, hasData);
}
private void preliminaryData() throws Exception {

View File

@ -92,6 +92,15 @@ public class FileManagementPermissionControllerTests extends BaseTest {
this.requestPostPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ, FileManagementRequestUtils.URL_FILE_PAGE, request);
}
@Test
void moduleCountTestSuccess() throws Exception {
FileMetadataTableRequest request = new FileMetadataTableRequest() {{
this.setCurrent(1);
this.setPageSize(10);
this.setProjectId(DEFAULT_PROJECT_ID);
}};
this.requestPostPermissionTest(PermissionConstants.PROJECT_FILE_MANAGEMENT_READ, FileManagementRequestUtils.URL_FILE_MODULE_COUNT, request);
}
@Test
public void fileDeleteSuccess() throws Exception {

View File

@ -1,11 +1,11 @@
package io.metersphere.project.utils;
import io.metersphere.project.dto.FileInformationDTO;
import io.metersphere.project.dto.FileTableResult;
import io.metersphere.project.request.filemanagement.FileMetadataTableRequest;
import io.metersphere.sdk.dto.BaseTreeNode;
import io.metersphere.sdk.util.FilePreviewUtils;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
import org.apache.commons.collections4.CollectionUtils;
import org.junit.jupiter.api.Assertions;
import org.testcontainers.shaded.org.apache.commons.lang3.StringUtils;
@ -15,6 +15,7 @@ import java.io.FileInputStream;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.List;
import java.util.Map;
public class FileManagementBaseUtils {
public static BaseTreeNode getNodeByName(List<BaseTreeNode> preliminaryTreeNodes, String nodeName) {
@ -96,12 +97,12 @@ public class FileManagementBaseUtils {
}
}
public static void checkFilePage(FileTableResult result, FileMetadataTableRequest request, boolean hasData) {
public static void checkFilePage(Pager<List<FileInformationDTO>> tableData, Map<String, Integer> moduleCount, FileMetadataTableRequest request, boolean hasData) {
//返回值的页码和当前页码相同
Assertions.assertEquals(result.getTableData().getCurrent(), request.getCurrent());
Assertions.assertEquals(tableData.getCurrent(), request.getCurrent());
//返回的数据量不超过规定要返回的数据量相同
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(result.getTableData().getList())).size() <= request.getPageSize());
List<FileInformationDTO> fileInformationDTOList = JSON.parseArray(JSON.toJSONString(result.getTableData().getList()), FileInformationDTO.class);
Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(tableData.getList())).size() <= request.getPageSize());
List<FileInformationDTO> fileInformationDTOList = JSON.parseArray(JSON.toJSONString(tableData.getList()), FileInformationDTO.class);
for (FileInformationDTO fileInformationDTO : fileInformationDTOList) {
if (FilePreviewUtils.isImage(fileInformationDTO.getFileType())) {
//检查是否有预览文件
@ -113,11 +114,11 @@ public class FileManagementBaseUtils {
//判断返回的节点统计总量是否和表格总量匹配
long allResult = 0;
for (Long countByModuleId : result.getModuleCount().values()) {
for (int countByModuleId : moduleCount.values()) {
allResult += countByModuleId;
}
Assertions.assertEquals(allResult, result.getTableData().getTotal());
Assertions.assertEquals(request.getPageSize(), result.getTableData().getPageSize());
Assertions.assertEquals(allResult, tableData.getTotal());
Assertions.assertEquals(request.getPageSize(), tableData.getPageSize());
if (hasData) {
Assertions.assertTrue(allResult > 0);
} else {

View File

@ -16,6 +16,8 @@ public class FileManagementRequestUtils {
public static final String URL_FILE_UPLOAD = "/project/file/upload";
//文件列表查询
public static final String URL_FILE_PAGE = "/project/file/page";
//文件列表查询对应的模块统计
public static final String URL_FILE_MODULE_COUNT = "/project/file/module/count";
//文件重传
public static final String URL_FILE_RE_UPLOAD = "/project/file/re-upload";
//文件下载