feat: 添加文件上传Controller
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
package com.ruoyi.ccdi.project.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.ruoyi.ccdi.project.domain.dto.CcdiFileUploadQueryDTO;
|
||||
import com.ruoyi.ccdi.project.domain.entity.CcdiFileUploadRecord;
|
||||
import com.ruoyi.ccdi.project.domain.vo.CcdiFileUploadStatisticsVO;
|
||||
import com.ruoyi.ccdi.project.service.ICcdiFileUploadService;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
import com.ruoyi.common.core.domain.AjaxResult;
|
||||
import com.ruoyi.common.core.page.PageDomain;
|
||||
import com.ruoyi.common.core.page.TableDataInfo;
|
||||
import com.ruoyi.common.core.page.TableSupport;
|
||||
import com.ruoyi.common.utils.SecurityUtils;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.concurrent.RejectedExecutionException;
|
||||
|
||||
/**
|
||||
* 文件上传 Controller
|
||||
*
|
||||
* @author ruoyi
|
||||
* @date 2026-03-05
|
||||
*/
|
||||
@Slf4j
|
||||
@RestController
|
||||
@RequestMapping("/ccdi/file-upload")
|
||||
@Tag(name = "文件上传管理", description = "项目文件上传相关接口")
|
||||
public class CcdiFileUploadController extends BaseController {
|
||||
|
||||
@Resource
|
||||
private ICcdiFileUploadService fileUploadService;
|
||||
|
||||
/**
|
||||
* 批量上传文件(异步)
|
||||
*/
|
||||
@PostMapping("/batch")
|
||||
@Operation(summary = "批量上传文件", description = "异步批量上传流水文件")
|
||||
public AjaxResult batchUpload(@RequestParam Long projectId,
|
||||
@RequestParam MultipartFile[] files) {
|
||||
// 参数校验
|
||||
if (projectId == null) {
|
||||
return AjaxResult.error("项目ID不能为空");
|
||||
}
|
||||
if (files == null || files.length == 0) {
|
||||
return AjaxResult.error("请选择要上传的文件");
|
||||
}
|
||||
if (files.length > 100) {
|
||||
return AjaxResult.error("单次最多上传100个文件");
|
||||
}
|
||||
|
||||
// 校验文件大小和格式
|
||||
for (MultipartFile file : files) {
|
||||
if (file.isEmpty()) {
|
||||
return AjaxResult.error("文件不能为空");
|
||||
}
|
||||
if (file.getSize() > 50 * 1024 * 1024) {
|
||||
return AjaxResult.error("文件 " + file.getOriginalFilename() + " 超过50MB限制");
|
||||
}
|
||||
String fileName = file.getOriginalFilename();
|
||||
if (!fileName.endsWith(".xlsx") && !fileName.endsWith(".xls")) {
|
||||
return AjaxResult.error("文件 " + fileName + " 格式不支持,仅支持Excel文件");
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
String username = SecurityUtils.getUsername();
|
||||
String batchId = fileUploadService.batchUploadFiles(projectId, files, username);
|
||||
return AjaxResult.success("上传任务已提交", batchId);
|
||||
} catch (RejectedExecutionException e) {
|
||||
log.warn("线程池已满,拒绝上传请求: projectId={}, fileCount={}", projectId, files.length);
|
||||
return AjaxResult.error("系统繁忙,请稍后再试");
|
||||
} catch (Exception e) {
|
||||
log.error("批量上传失败: projectId={}", projectId, e);
|
||||
return AjaxResult.error("上传失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询上传记录列表
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
@Operation(summary = "查询上传记录列表", description = "分页查询文件上传记录")
|
||||
public TableDataInfo list(CcdiFileUploadQueryDTO queryDTO) {
|
||||
PageDomain pageDomain = TableSupport.buildPageRequest();
|
||||
Page<CcdiFileUploadRecord> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
|
||||
Page<CcdiFileUploadRecord> result = fileUploadService.selectPage(page, queryDTO);
|
||||
return getDataTable(result.getRecords(), result.getTotal());
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询上传统计
|
||||
*/
|
||||
@GetMapping("/statistics/{projectId}")
|
||||
@Operation(summary = "查询上传统计", description = "统计各状态的文件数量")
|
||||
public AjaxResult getStatistics(@PathVariable Long projectId) {
|
||||
CcdiFileUploadStatisticsVO statistics = fileUploadService.countByStatus(projectId);
|
||||
return AjaxResult.success(statistics);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询记录详情
|
||||
*/
|
||||
@GetMapping("/detail/{id}")
|
||||
@Operation(summary = "查询记录详情", description = "根据ID查询文件上传记录详情")
|
||||
public AjaxResult getDetail(@PathVariable Long id) {
|
||||
CcdiFileUploadRecord record = fileUploadService.getById(id);
|
||||
return AjaxResult.success(record);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user