From eb9fcf94c70a669914d9c7d93b2866c9f579892e Mon Sep 17 00:00:00 2001 From: Echo <4759156@qq.com> Date: Thu, 11 Apr 2024 18:14:10 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=9A=E5=8A=A1=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E7=9A=84=E9=99=84=E4=BB=B6=E4=B8=8A=E4=BC=A0=EF=BC=8C?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=AE=9E=E4=BE=8BID=EF=BC=8C=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/admin/file/FileController.java | 31 +++++- .../file/vo/file/BusinessFileUploadReqVO.java | 23 +++++ .../dal/dataobject/file/BusinessFileDO.java | 67 +++++++++++++ .../dal/mysql/file/BusinessFileMapper.java | 26 +++++ .../infra/service/file/FileService.java | 26 +++++ .../infra/service/file/FileServiceImpl.java | 97 ++++++++++++++++++- 6 files changed, 265 insertions(+), 5 deletions(-) create mode 100644 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/BusinessFileUploadReqVO.java create mode 100644 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/BusinessFileDO.java create mode 100644 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/BusinessFileMapper.java diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java index 9d65e075..de905c33 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java @@ -8,11 +8,9 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.util.object.BeanUtils; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.BpmFileUploadReqVO; -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileUploadReqVO; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.*; import cn.iocoder.yudao.module.infra.dal.dataobject.file.BpmFileDO; +import cn.iocoder.yudao.module.infra.dal.dataobject.file.BusinessFileDO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.service.file.FileService; import io.swagger.v3.oas.annotations.Operation; @@ -117,4 +115,29 @@ public class FileController { return success(true); } + + //add by yj 2024 04-11 Begin + @PostMapping("/businessUpload") + @Operation(summary = "上传业务类型附件【如:工作日/周报附件】") + @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 + public CommonResult businessUpload(@RequestParam("uploadFiles") MultipartFile file,@RequestParam("businessType")Long businessType) throws Exception { + return success(fileService.createBusinessReturnFile(file,businessType)); + } + + @DeleteMapping("/deleteBusinessFile") + @Operation(summary = "删除业务流附件【如:工作日/周报附件】") + @Parameter(name = "url", description = "附件URL地址", required = true) + public CommonResult deleteBusinessFile(@RequestParam("url") String url) throws Exception { + fileService.deleteBusinessFile(url); + return success(true); + } + + @PostMapping("/uploadBusinessFileProcessInstanceId") + @Operation(summary = "更新文件的实例ID【如:工作日/周报附件】") + @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 + public String uploadBusinessFileProcessInstanceId(@Valid @RequestBody BusinessFileUploadReqVO reqVO) throws Exception { + fileService.uploadBusinessFileProcessInstanceId(reqVO); + return "success" ; + } + //add by yj 2024 04-11 End } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/BusinessFileUploadReqVO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/BusinessFileUploadReqVO.java new file mode 100644 index 00000000..49ab2591 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/vo/file/BusinessFileUploadReqVO.java @@ -0,0 +1,23 @@ +package cn.iocoder.yudao.module.infra.controller.admin.file.vo.file; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +import javax.validation.constraints.NotNull; +import java.util.List; + +@Schema(description = "管理后台 - 上传业务文件 Request VO") +@Data +public class BusinessFileUploadReqVO { + + @Schema(description = "附件地址", requiredMode = Schema.RequiredMode.REQUIRED) + @NotNull(message = "附件地址不能为空") + private List urls; + + @Schema(description = "流程实例编号", example = "7c45f44f-b775-11ee-aba7-5ea7f9635f60") + @NotNull(message = "流程实例编号不能为空") + private String businessInstanceId; + + + +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/BusinessFileDO.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/BusinessFileDO.java new file mode 100644 index 00000000..0f0ccbd1 --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/dataobject/file/BusinessFileDO.java @@ -0,0 +1,67 @@ +package cn.iocoder.yudao.module.infra.dal.dataobject.file; + +import cn.iocoder.yudao.framework.mybatis.core.dataobject.BaseDO; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.*; + +/** + * 文件表 + * 每次文件上传,都会记录一条记录到该表中 + * + + */ +@TableName("business_file") +@Data +@EqualsAndHashCode(callSuper = true) +@ToString(callSuper = true) +@Builder +@NoArgsConstructor +@AllArgsConstructor +public class BusinessFileDO extends BaseDO { + + /** + * 编号,数据库自增 + */ + private Long id; + + /** + * 业务类型 1:日志 2.暂定 3.暂定 4... + */ + private Long businessType; + /** + * 配置编号 + * + * 关联 {@link FileConfigDO#getId()} + */ + private Long configId; + /** + * 原文件名 + */ + private String name; + /** + * 路径,即文件名 + */ + private String path; + /** + * 访问地址 + */ + private String url; + /** + * 文件的 MIME 类型,例如 "application/octet-stream" + */ + private String type; + /** + * 文件大小 + */ + private Integer size; + + /** + * 上传人ID + */ + private Long uploadUserId ; + + /** + * 流程实例编号 + */ + private String businessInstanceId ; +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/BusinessFileMapper.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/BusinessFileMapper.java new file mode 100644 index 00000000..99bec22c --- /dev/null +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/dal/mysql/file/BusinessFileMapper.java @@ -0,0 +1,26 @@ +package cn.iocoder.yudao.module.infra.dal.mysql.file; + +import cn.iocoder.yudao.framework.common.pojo.PageResult; +import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; +import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; +import cn.iocoder.yudao.module.infra.dal.dataobject.file.BpmFileDO; +import cn.iocoder.yudao.module.infra.dal.dataobject.file.BusinessFileDO; +import org.apache.ibatis.annotations.Mapper; + +/** + * business文件操作 Mapper + * + + */ +@Mapper +public interface BusinessFileMapper extends BaseMapperX { + + default PageResult selectPage(FilePageReqVO reqVO) { + return selectPage(reqVO, new LambdaQueryWrapperX() + .likeIfPresent(BusinessFileDO::getPath, reqVO.getPath()) + .likeIfPresent(BusinessFileDO::getType, reqVO.getType()) + .betweenIfPresent(BusinessFileDO::getCreateTime, reqVO.getCreateTime()) + .orderByDesc(BusinessFileDO::getId)); + } +} diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java index 6d7cb7e7..7a23f5ce 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileService.java @@ -1,9 +1,11 @@ package cn.iocoder.yudao.module.infra.service.file; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.BpmFileUploadReqVO; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.BusinessFileUploadReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.module.infra.dal.dataobject.file.BpmFileDO; +import cn.iocoder.yudao.module.infra.dal.dataobject.file.BusinessFileDO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import org.springframework.web.multipart.MultipartFile; @@ -77,4 +79,28 @@ public interface FileService { * @throws Exception */ void uploadBpmFileProcessInstanceId(BpmFileUploadReqVO reqVO) throws Exception ; + + /** + * 保存业务类型文件,并返回文件对象 + * + * @param file 上传文件对象 + * @param businessType 业务类型 + * @return 文件路径 + */ + BusinessFileDO createBusinessReturnFile(MultipartFile file, Long businessType); + + /** + * 删除bpm文件 + * @param url + * @throws Exception + */ + void deleteBusinessFile(String url) throws Exception; + + /** + * 更新文件的流程实例ID + * @param reqVO + * @throws Exception + */ + void uploadBusinessFileProcessInstanceId(BusinessFileUploadReqVO reqVO) throws Exception ; + } diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java index 03cf1b99..7e30b564 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/service/file/FileServiceImpl.java @@ -10,10 +10,13 @@ import cn.iocoder.yudao.framework.file.core.client.FileClient; import cn.iocoder.yudao.framework.file.core.utils.FileTypeUtils; import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.BpmFileUploadReqVO; +import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.BusinessFileUploadReqVO; import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.BpmFileDO; +import cn.iocoder.yudao.module.infra.dal.dataobject.file.BusinessFileDO; import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; import cn.iocoder.yudao.module.infra.dal.mysql.file.BpmFileMapper; +import cn.iocoder.yudao.module.infra.dal.mysql.file.BusinessFileMapper; import cn.iocoder.yudao.module.infra.dal.mysql.file.FileMapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import lombok.SneakyThrows; @@ -40,12 +43,14 @@ public class FileServiceImpl implements FileService { @Resource private BpmFileMapper bpmFileMapper; + @Resource + private BusinessFileMapper businessFileMapper; + @Override public PageResult getFilePage(FilePageReqVO pageReqVO) { return fileMapper.selectPage(pageReqVO); } - @Override @SneakyThrows public BpmFileDO createReturnFile(MultipartFile file) { @@ -206,4 +211,94 @@ public class FileServiceImpl implements FileService { // 调用 MyBatis Plus 的 update 方法执行批量更新 bpmFileMapper.update(null, lambdaUpdateWrapper); } + + @Override + @SneakyThrows + public BusinessFileDO createBusinessReturnFile(MultipartFile file, Long businessType) { + Long userId = SecurityFrameworkUtils.getLoginUserId(); + long timestamp = System.currentTimeMillis(); + + String[] fileInfo = new String[4]; + String name = file.getOriginalFilename(); + String path = null; + byte[] content = IoUtil.readBytes(file.getInputStream()); + + // 计算默认的 path 名 + String type = FileTypeUtils.getMineType(content, name); + + StrUtil.isEmpty(path);//path = FileUtils.generatePath(content, name); + path = userId + "_" + timestamp; + String beginPath = name.replace(".", "_"); + path = beginPath + "_" + path + "." + FileNameUtil.extName(name); + // 如果 name 为空,则使用 path 填充 + if (StrUtil.isEmpty(name)) { + name = path; + } + + // 上传到文件存储器 + FileClient client = fileConfigService.getMasterFileClient(); + Assert.notNull(client, "客户端(master) 不能为空"); + String url = client.upload(content, path, type); + + // 保存到数据库 + BusinessFileDO fileDo = new BusinessFileDO(); + fileDo.setConfigId(client.getId()); + fileDo.setName(name); + fileDo.setPath(path); + fileDo.setUrl(url); + fileDo.setType(type); + fileDo.setSize(content.length); + fileDo.setUploadUserId(userId); + fileDo.setBusinessType(businessType) ; + businessFileMapper.insert(fileDo); + + fileInfo[0] = name; + fileInfo[1] = fileDo.getUrl(); + fileInfo[2] = fileDo.getType(); + return fileDo; + } + + /** + * 删除bpm文件 + * @param url + * @throws Exception + */ + @Override + @SneakyThrows + public void deleteBusinessFile(String url) { + String path = url.substring(url.lastIndexOf("/") + 1); + PageResult fileDOPageResult = businessFileMapper.selectPage(new FilePageReqVO().setPath(path)); + BusinessFileDO file = null; + if (fileDOPageResult != null && fileDOPageResult.getTotal() > 0) { + file = fileDOPageResult.getList().get(0); + } + if (file == null) { + throw exception(FILE_NOT_EXISTS); + } + + // 从文件存储器中删除 + FileClient client = fileConfigService.getFileClient(file.getConfigId()); + Assert.notNull(client, "客户端({}) 不能为空", file.getConfigId()); + client.delete(file.getPath()); + + // 删除记录 + businessFileMapper.deleteById(file.getId()); + } + + /** + * 更新文件的流程实例ID + * @param reqVO + * @throws Exception + */ + @Override + @SneakyThrows + public void uploadBusinessFileProcessInstanceId(BusinessFileUploadReqVO reqVO) { + String businessInstanceId = reqVO.getBusinessInstanceId(); + List urls = reqVO.getUrls(); + LambdaUpdateWrapper lambdaUpdateWrapper = new LambdaUpdateWrapper<>(); + lambdaUpdateWrapper.in(BusinessFileDO::getUrl, urls); + lambdaUpdateWrapper.set(BusinessFileDO::getBusinessInstanceId, businessInstanceId); // 假设 bid 是要更新的 bid 值 + // 调用 MyBatis Plus 的 update 方法执行批量更新 + businessFileMapper.update(null, lambdaUpdateWrapper); + } }