Procházet zdrojové kódy

在数据库加了another字段,another值为1则表示为博物馆文件,做了两个接口在上传和查询时插入1到another

hyy před 6 měsíci
rodič
revize
762132b192

+ 20 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java

@@ -46,6 +46,17 @@ public class FileController {
         return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream())));
     }
 
+    //自加的
+    @PostMapping("/museums-upload")
+    @Operation(summary = "上传博物馆文件", description = "模式二:后端上传文件,包含另一个字段")
+    public CommonResult<String> uploadFileWithAnother(FileUploadReqVO uploadReqVO) throws Exception {
+        MultipartFile file = uploadReqVO.getFile();
+        String path = uploadReqVO.getPath();
+        Boolean another = uploadReqVO.getAnother(); // 获取新的字段
+        return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()), another));
+    }
+
+
     @GetMapping("/presigned-url")
     @Operation(summary = "获取文件预签名地址", description = "模式二:前端上传文件:用于前端直接上传七牛、阿里云 OSS 等文件存储器")
     public CommonResult<FilePresignedUrlRespVO> getFilePresignedUrl(@RequestParam("path") String path) throws Exception {
@@ -100,4 +111,13 @@ public class FileController {
         return success(BeanUtils.toBean(pageResult, FileRespVO.class));
     }
 
+    //自加的
+    @GetMapping("/page/another")
+    @Operation(summary = "获得另一个字段为1的文件分页")
+    @PreAuthorize("@ss.hasPermission('infra:file:query')")
+    public CommonResult<PageResult<FileRespVO>> getFilePageWithAnotherOne(@Valid FilePageReqVO pageVO) {
+        PageResult<FileDO> pageResult = fileService.getFilePageWithAnotherOne(pageVO);
+        return success(BeanUtils.toBean(pageResult, FileRespVO.class));
+    }
+
 }

+ 3 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileCreateReqVO.java

@@ -31,4 +31,7 @@ public class FileCreateReqVO {
     @Schema(description = "文件大小", example = "2048", requiredMode = Schema.RequiredMode.REQUIRED)
     private Integer size;
 
+    @Schema(description = "博物馆文件文件", example = "1", requiredMode = Schema.RequiredMode.REQUIRED)
+    private Boolean another;
+
 }

+ 3 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/FileUploadReqVO.java

@@ -17,4 +17,7 @@ public class FileUploadReqVO {
     @Schema(description = "文件附件", example = "yudaoyuanma.png")
     private String path;
 
+    @Schema(description = "文件附件", example = "yudaoyuanma.png")
+    private Boolean another;
+
 }

+ 4 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/FileDO.java

@@ -51,5 +51,9 @@ public class FileDO extends BaseDO {
      * 文件大小
      */
     private Integer size;
+    /**
+     * 文件大小
+     */
+    private Boolean another;
 
 }

+ 15 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/FileMapper.java

@@ -7,6 +7,8 @@ import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO
 import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
 import org.apache.ibatis.annotations.Mapper;
 
+import java.util.List;
+
 /**
  * 文件操作 Mapper
  *
@@ -23,4 +25,17 @@ public interface FileMapper extends BaseMapperX<FileDO> {
                 .orderByDesc(FileDO::getId));
     }
 
+    default PageResult<FileDO> selectPageWithAnotherOne(FilePageReqVO reqVO) {
+        return selectPage(reqVO, new LambdaQueryWrapperX<FileDO>()
+                .eq(FileDO::getAnother, 1) // 只选择 another = 1 的文件
+                .likeIfPresent(FileDO::getPath, reqVO.getPath())
+                .likeIfPresent(FileDO::getType, reqVO.getType())
+                .betweenIfPresent(FileDO::getCreateTime, reqVO.getCreateTime())
+                .orderByDesc(FileDO::getId));
+    }
+
+
+    List<FileDO> findByAnother(Integer another);
+
+
 }

+ 21 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java

@@ -21,6 +21,14 @@ public interface FileService {
      */
     PageResult<FileDO> getFilePage(FilePageReqVO pageReqVO);
 
+    /**
+     * 获得另一个字段值为1的文件分页
+     *
+     * @param pageReqVO 分页查询
+     * @return 文件分页
+     */
+    PageResult<FileDO> getFilePageWithAnotherOne(FilePageReqVO pageReqVO);
+
     /**
      * 保存文件,并返回文件的访问路径
      *
@@ -31,6 +39,19 @@ public interface FileService {
      */
     String createFile(String name, String path, byte[] content);
 
+    //自加的
+    /**
+     * 保存文件,并返回文件的访问路径
+     *
+     * @param name    文件名称
+     * @param path    文件路径
+     * @param content 文件内容
+     * @param another 另一个字段,用于标识
+     * @return 文件路径
+     */
+    String createFile(String name, String path, byte[] content, Boolean another);
+
+
     /**
      * 创建文件
      *

+ 40 - 0
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java

@@ -40,6 +40,13 @@ public class FileServiceImpl implements FileService {
         return fileMapper.selectPage(pageReqVO);
     }
 
+    @Override
+    public PageResult<FileDO> getFilePageWithAnotherOne(FilePageReqVO pageReqVO) {
+        // 可以直接调用 mapper 的 selectPage 方法,添加 another 字段的条件
+        return fileMapper.selectPageWithAnotherOne(pageReqVO);
+    }
+
+
     @Override
     @SneakyThrows
     public String createFile(String name, String path, byte[] content) {
@@ -70,6 +77,39 @@ public class FileServiceImpl implements FileService {
         return url;
     }
 
+    //自加的
+    @Override
+    @SneakyThrows
+    public String createFile(String name, String path, byte[] content, Boolean another) {
+        // 计算默认的 path 名
+        String type = FileTypeUtils.getMineType(content, name);
+        if (StrUtil.isEmpty(path)) {
+            path = FileUtils.generatePath(content, name);
+        }
+        // 如果 name 为空,则使用 path 填充
+        if (StrUtil.isEmpty(name)) {
+            name = path;
+        }
+
+        // 上传到文件存储器
+        FileClient client = fileConfigService.getMasterFileClient();
+        Assert.notNull(client, "客户端(master) 不能为空");
+        String url = client.upload(content, path, type);
+
+        // 保存到数据库
+        FileDO file = new FileDO();
+        file.setConfigId(client.getId());
+        file.setName(name);
+        file.setPath(path);
+        file.setUrl(url);
+        file.setType(type);
+        file.setSize(content.length);
+        file.setAnother(another); // 新增字段的设置
+        fileMapper.insert(file);
+        return url;
+    }
+
+
     @Override
     public Long createFile(FileCreateReqVO createReqVO) {
         FileDO file = BeanUtils.toBean(createReqVO, FileDO.class);