diff --git a/tool-tech-admin/pom.xml b/tool-tech-admin/pom.xml index 6a1b9d0..3b63802 100644 --- a/tool-tech-admin/pom.xml +++ b/tool-tech-admin/pom.xml @@ -85,6 +85,18 @@ lombok + + + com.documents4j + documents4j-local + 1.0.3 + + + com.documents4j + documents4j-transformer-msoffice-word + 1.0.3 + + diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/controller/common/CommonController.java b/tool-tech-admin/src/main/java/com/rzdata/web/controller/common/CommonController.java index 78a0fc5..51ca39f 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/controller/common/CommonController.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/controller/common/CommonController.java @@ -7,9 +7,14 @@ import com.rzdata.common.utils.StringUtils; import com.rzdata.common.utils.file.FileUploadUtils; import com.rzdata.common.utils.file.FileUtils; import com.rzdata.framework.config.ServerConfig; +import com.rzdata.web.domain.Attachment; +import com.rzdata.web.domain.FileOperationRequest; +import com.rzdata.web.service.IAttachmentService; +import com.rzdata.web.service.impl.AttachmentServiceImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; @@ -37,9 +42,13 @@ public class CommonController @Autowired private ServerConfig serverConfig; + @Autowired + private IAttachmentService attachmentService; + private static final String FILE_DELIMETER = ","; + /** * 通用下载请求 * @@ -166,6 +175,21 @@ public class CommonController } + + /** + * 预览下载 + * txt、pdf、word(doc、docx格式) + */ + @PostMapping("/preview/download") + public ResponseEntity resourceDownload(@RequestBody Attachment attachment) + { + byte[] bytes = attachmentService.loadFileAsBytes(attachment); + return ResponseEntity.ok() + .contentType(MediaType.parseMediaType("application/octet-stream")) + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + attachment.getFileOldName() + "\"") + .body(bytes); + } + @PostMapping("/initUpload") public ResponseEntity> initUpload(@RequestParam("fileName") String fileName) { // 上传文件路径 diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/controller/document/DocumentController.java b/tool-tech-admin/src/main/java/com/rzdata/web/controller/document/DocumentController.java index d0c6a50..8986f4d 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/controller/document/DocumentController.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/controller/document/DocumentController.java @@ -45,7 +45,7 @@ public class DocumentController extends BaseController * 导出【文档资源信息】列表 */ @Log(title = "【文档资源信息】", businessType = BusinessType.EXPORT) - @PreAuthorize("@ss.hasPermi('document:export')") + @PreAuthorize("@ss.hasPermi('document:list')") @PostMapping("/export") public void export(HttpServletResponse response, Document document) { @@ -122,8 +122,8 @@ public class DocumentController extends BaseController } - @GetMapping("/all/list") - public AjaxResult allList(Document Document) + @PostMapping("/export/word/list") + public AjaxResult exportWordList(Document Document) { List list = documentService.selectAllList(Document); return AjaxResult.success(list); diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/controller/tool/ToolController.java b/tool-tech-admin/src/main/java/com/rzdata/web/controller/tool/ToolController.java index 7d17169..db06763 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/controller/tool/ToolController.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/controller/tool/ToolController.java @@ -94,6 +94,7 @@ public class ToolController extends BaseController * 导出工具信息列表 */ @Log(title = "工具信息", businessType = BusinessType.EXPORT) + @PreAuthorize("@ss.hasPermi('tool:list')") @PostMapping("/export") public void export(HttpServletResponse response, Tool tTool) { @@ -108,7 +109,8 @@ public class ToolController extends BaseController * 导出工具信息列表 */ @Log(title = "工具信息", businessType = BusinessType.EXPORT) - @GetMapping("/export/word/list") + @PreAuthorize("@ss.hasPermi('tool:list')") + @PostMapping("/export/word/list") public AjaxResult exportWordList(Tool tool) { List list = toolService.selectAllList(tool); @@ -194,8 +196,6 @@ public class ToolController extends BaseController //办结 if(RecordStatusEnum.DONE.getCode().equals(tTool.getRecordStatus())){ - //更新文档状态 - toolService.updateDocPushStatus(tTool); //给消息中心发送消息 toolService.sendTzMessage(tTool); } diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/domain/Attachment.java b/tool-tech-admin/src/main/java/com/rzdata/web/domain/Attachment.java index f51155a..efd3e0b 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/domain/Attachment.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/domain/Attachment.java @@ -32,6 +32,10 @@ public class Attachment extends BaseEntity @Excel(name = "业务类型") private String bizType; + /** 文件路径 */ + @Excel(name = "文件路径") + private String filePath; + /** 文件URL */ @Excel(name = "文件URL") private String fileUrl; diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/domain/Document.java b/tool-tech-admin/src/main/java/com/rzdata/web/domain/Document.java index 85669f7..21469c7 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/domain/Document.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/domain/Document.java @@ -37,9 +37,11 @@ public class Document extends BaseEntity { private String docType; /** 文档负责人 */ - @Excel(name = "负责人") private String docPrincipals; + /** 文档负责人名称 */ + private String docPrincipalsName; + /** 归属单位 **/ @Excel(name = "归属单位") private String docRespDeptName; @@ -109,4 +111,8 @@ public class Document extends BaseEntity { private String docSourceName; private String statusName; private String createNowTime; + + private List attachmentList; + /** 主键 **/ + private List docIdList; } diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/domain/FileOperationRequest.java b/tool-tech-admin/src/main/java/com/rzdata/web/domain/FileOperationRequest.java new file mode 100644 index 0000000..a9d917b --- /dev/null +++ b/tool-tech-admin/src/main/java/com/rzdata/web/domain/FileOperationRequest.java @@ -0,0 +1,20 @@ +package com.rzdata.web.domain; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class FileOperationRequest { + private String id; + private String name; + private Boolean isLocal; + /** preview:预览,download:下载 **/ + private String type; + /** + * 模板下载类型 + * 1: 导入模板 + * 2:示例请求模板 + */ + private String templateDownloadType; +} diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/domain/Tool.java b/tool-tech-admin/src/main/java/com/rzdata/web/domain/Tool.java index f098ea1..3b73df0 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/domain/Tool.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/domain/Tool.java @@ -111,4 +111,6 @@ public class Tool extends BaseEntity private String toolTypeName; /** 状态名称 **/ private String statusName; + /** 主键 **/ + private List toolIdList; } diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/service/IAttachmentService.java b/tool-tech-admin/src/main/java/com/rzdata/web/service/IAttachmentService.java index 4b2f2b1..e7180da 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/service/IAttachmentService.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/service/IAttachmentService.java @@ -1,6 +1,7 @@ package com.rzdata.web.service; import com.rzdata.web.domain.Attachment; +import com.rzdata.web.domain.FileOperationRequest; import java.util.List; @@ -74,4 +75,6 @@ public interface IAttachmentService * @return */ public int batchInsert(List batch); + + byte[] loadFileAsBytes(Attachment attachment); } diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/AttachmentServiceImpl.java b/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/AttachmentServiceImpl.java index 71f5e3c..84c5cd7 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/AttachmentServiceImpl.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/AttachmentServiceImpl.java @@ -1,10 +1,21 @@ package com.rzdata.web.service.impl; +import java.io.*; import java.util.List; +import cn.hutool.core.util.ObjectUtil; +import com.documents4j.api.DocumentType; +import com.documents4j.api.IConverter; +import com.documents4j.job.LocalConverter; +import com.rzdata.common.config.JaConfig; +import com.rzdata.common.constant.Constants; +import com.rzdata.common.exception.ServiceException; +import com.rzdata.common.utils.StringUtils; import com.rzdata.web.domain.Attachment; +import com.rzdata.web.domain.FileOperationRequest; import com.rzdata.web.mapper.AttachmentMapper; import com.rzdata.web.service.IAttachmentService; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -14,6 +25,7 @@ import org.springframework.stereotype.Service; * @author panchichun * @date 2024-08-28 */ +@Slf4j @Service public class AttachmentServiceImpl implements IAttachmentService { @@ -94,7 +106,7 @@ public class AttachmentServiceImpl implements IAttachmentService /** * 批量删除附件 * - * @param ids 需要删除的附件主键 + * @param businessIds 需要删除的附件主键 * @return 结果 */ @Override @@ -119,4 +131,64 @@ public class AttachmentServiceImpl implements IAttachmentService public int batchInsert(List batch) { return attachmentMapper.batchInsert(batch); } + + @Override + public byte[] loadFileAsBytes(Attachment attachment) { + // 本地资源路径 + String localPath = JaConfig.getProfile(); + // 全路径 + String uploadPath = localPath + StringUtils.substringAfter(attachment.getFilePath(), Constants.RESOURCE_PREFIX); + String fileName = attachment.getFileOldName(); + File file = null; + + if (fileName.endsWith(Constants.FILE_TYPE_DOC)) { + //获取doc名称转换为pdf名称 + String fileNameDocx = fileName.substring(0, fileName.lastIndexOf(Constants.SYMBOL_POINT)); + fileNameDocx += Constants.FILE_TYPE_PDF; + //获取转换的路径 + String pathDocx = uploadPath.substring(0, uploadPath.lastIndexOf('/') + 1) + fileNameDocx; + file = new File(pathDocx); + if (!file.exists()) { + convertDocToPdf(uploadPath,pathDocx); + // 再次检查转换后的文件是否存在 + if (!file.exists()) { + throw new ServiceException("转换后的文件不存在"); + } + } + }else{ + file = new File(uploadPath); + } + + byte[] result = null; + try (FileInputStream fis = new FileInputStream(file); + ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);) { + byte[] b = new byte[1000]; + int n; + while ((n = fis.read(b)) != -1) { + bos.write(b, 0, n); + } + result = bos.toByteArray(); + } catch (Exception ex) { + log.error("AttachmentServiceImpl-->loadFileAsBytes----ex###", ex); + } + return result; + } + + /** + * word文件doc格式转换为pdf文件 + * @param docPath + * @param pdfPath + */ + public static void convertDocToPdf(String docPath, String pdfPath){ + File file = new File(docPath); + File outputFile = new File(pdfPath); + try (InputStream docxInputStream = new FileInputStream(file); + OutputStream outputStream = new FileOutputStream(outputFile)) { + IConverter converter = LocalConverter.builder().build(); + converter.convert(docxInputStream).as(DocumentType.DOCX).to(outputStream).as(DocumentType.PDF).execute(); + } catch (IOException e) { + log.error("AttachmentServiceImpl-->convertDocToPdf----e###", e); + throw new ServiceException("转换异常"); + } + } } diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/DocumentServiceImpl.java b/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/DocumentServiceImpl.java index 9ee4e17..9729fb4 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/DocumentServiceImpl.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/DocumentServiceImpl.java @@ -2,6 +2,7 @@ package com.rzdata.web.service.impl; import cn.hutool.core.collection.CollUtil; import cn.hutool.core.date.DateUtil; +import cn.hutool.core.lang.Snowflake; import cn.hutool.core.util.BooleanUtil; import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.ObjectUtil; @@ -24,6 +25,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Collectors; /** @@ -52,6 +54,9 @@ public class DocumentServiceImpl implements IDocumentService @Autowired private ISysDeptService sysDeptService; + @Autowired + private Snowflake snowflake; + /** * 查询【请填写功能名称】 * @@ -166,22 +171,45 @@ public class DocumentServiceImpl implements IDocumentService document.setIsDeleted(Constants.STR_ZERO); document.setCreateTime(new Date()); int result = documentMapper.insertDocument(document); - if(ObjectUtil.isNotEmpty(document.getAttachment())){ - Attachment attachment = document.getAttachment(); - attachment.setId(IdUtil.simpleUUID()); - attachment.setCreateBy(SecurityUtils.getLoginUser().getUsername()); - attachment.setBusinessId(docId); - attachment.setBizType(Constants.DOC_TYPE_DOC); - attachment.setDel(Constants.STR_ZERO); - attachment.setCreateDate(new Date()); - attachmentService.insertAttachment(attachment); - } + addFileList(document); if(result > 0){ return docId; } return null; } + + @Transactional(rollbackFor = Exception.class) + public void delFile(Document document) { + List toolIds = Collections.singletonList(document.getDocId()); + attachmentService.deleteAttachmentByBusinessId(toolIds); + } + + @Transactional(rollbackFor = Exception.class) + public void addFileList(Document document) { + List attachmentList = document.getAttachmentList(); + if(CollUtil.isEmpty(attachmentList)){ + return; + } + for (Attachment attachment : attachmentList) { + attachment.setId(String.valueOf(snowflake.nextId())); + attachment.setBusinessId(document.getDocId()); + attachment.setBizType(Constants.DOC_TYPE_DOC); + attachment.setDel(Constants.STR_ZERO); + attachment.setCreateBy(String.valueOf(SecurityUtils.getLoginUser().getUserId())); + attachment.setCreateDate(new Date()); + } + // 使用AtomicInteger来跟踪索引 + AtomicInteger counter = new AtomicInteger(0); + // 使用Stream API进行分组 + Map> grouped = attachmentList.stream() + .collect(Collectors.groupingBy( + e -> counter.getAndIncrement() / 500 + )); + // 对每个子列表进行批量插入 + grouped.values().forEach(batch -> attachmentService.batchInsert(batch)); + } + /** * 修改【请填写功能名称】 * @@ -196,24 +224,8 @@ public class DocumentServiceImpl implements IDocumentService document.setUpdateById(String.valueOf(SecurityUtils.getLoginUser().getUserId())); document.setUpdateTime(new Date()); int result = documentMapper.updateDocument(document); - - Attachment attParam = document.getAttachment(); - if(ObjectUtil.isNotEmpty(attParam) && StrUtil.isBlank(attParam.getId())){ - Attachment att = new Attachment(); - att.setBusinessId(document.getDocId()); - att.setDel(Constants.STR_ONE); - attachmentService.updateAttachmentByBusinessId(att); - - Attachment attachment = document.getAttachment(); - attachment.setId(IdUtil.simpleUUID()); - attachment.setCreateBy(SecurityUtils.getLoginUser().getUsername()); - attachment.setBusinessId(document.getDocId()); - attachment.setBizType(Constants.DOC_TYPE_DOC); - attachment.setDel(Constants.STR_ZERO); - attachment.setCreateDate(new Date()); - attachmentService.insertAttachment(attachment); - } - + delFile(document); + addFileList(document); return result; } diff --git a/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/ToolServiceImpl.java b/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/ToolServiceImpl.java index 602a620..2783b2f 100644 --- a/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/ToolServiceImpl.java +++ b/tool-tech-admin/src/main/java/com/rzdata/web/service/impl/ToolServiceImpl.java @@ -137,8 +137,8 @@ public class ToolServiceImpl implements IToolService tool.setUpdateBy(SecurityUtils.getUserId().toString()); tool.setUpdateTime(DateUtils.getNowDate()); int result = toolMapper.updateTool(tool); - delFile(tool); - addFileList(tool); + /* delFile(tool); + addFileList(tool);*/ return result; } diff --git a/tool-tech-admin/src/main/resources/mapper/AttachmentMapper.xml b/tool-tech-admin/src/main/resources/mapper/AttachmentMapper.xml index 0faba97..d55df7f 100644 --- a/tool-tech-admin/src/main/resources/mapper/AttachmentMapper.xml +++ b/tool-tech-admin/src/main/resources/mapper/AttachmentMapper.xml @@ -8,6 +8,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" + @@ -25,7 +26,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - select id, biz_type, file_url, file_old_name, file_new_name, suffix_type, + select id, biz_type, file_path, file_url, file_old_name, file_new_name, suffix_type, file_size, business_id, sorts, remark, del, create_by, create_date, update_by, update_date, failure_time, file_old_name as file_name from t_attachment @@ -35,6 +36,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" and biz_type = #{bizType} + and file_path = #{filePath} and file_url = #{fileUrl} and file_old_name like concat('%', #{fileOldName}, '%') and file_new_name like concat('%', #{fileNewName}, '%') @@ -65,6 +67,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" id, biz_type, + file_path, file_url, file_old_name, file_new_name, @@ -83,6 +86,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" #{id}, #{bizType}, + #{filePath}, #{fileUrl}, #{fileOldName}, #{fileNewName}, @@ -105,6 +109,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" biz_type = #{bizType}, file_url = #{fileUrl}, + file_path = #{filePath}, file_old_name = #{fileOldName}, file_new_name = #{fileNewName}, suffix_type = #{suffixType}, @@ -128,6 +133,7 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" biz_type = #{bizType}, file_url = #{fileUrl}, + file_path = #{filePath}, file_old_name = #{fileOldName}, file_new_name = #{fileNewName}, suffix_type = #{suffixType}, @@ -164,10 +170,10 @@ PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" - INSERT INTO t_attachment (id, biz_type, file_url, file_old_name, file_new_name, suffix_type, file_size, business_id, sorts, remark, del, create_by, create_date) + INSERT INTO t_attachment (id, biz_type, file_path, file_url, file_old_name, file_new_name, suffix_type, file_size, business_id, sorts, remark, del, create_by, create_date) VALUES - #{item.id}, #{item.bizType}, #{item.fileUrl}, #{item.fileOldName}, #{item.fileNewName}, #{item.suffixType}, #{item.fileSize}, #{item.businessId}, #{item.sorts}, #{item.remark}, #{item.del}, #{item.createBy}, #{item.createDate} + #{item.id}, #{item.bizType}, #{item.filePath}, #{item.fileUrl}, #{item.fileOldName}, #{item.fileNewName}, #{item.suffixType}, #{item.fileSize}, #{item.businessId}, #{item.sorts}, #{item.remark}, #{item.del}, #{item.createBy}, #{item.createDate} \ No newline at end of file diff --git a/tool-tech-admin/src/main/resources/mapper/DocumentMapper.xml b/tool-tech-admin/src/main/resources/mapper/DocumentMapper.xml index e4222b6..6befa39 100644 --- a/tool-tech-admin/src/main/resources/mapper/DocumentMapper.xml +++ b/tool-tech-admin/src/main/resources/mapper/DocumentMapper.xml @@ -10,6 +10,7 @@ + @@ -30,7 +31,7 @@ select doc_id, doc_code, doc_name, doc_type, doc_principals, doc_resp_dept, - doc_source, doc_status, doc_url, create_by, create_time, + doc_source, doc_status, doc_url, create_by, create_time,doc_principals_name, update_by, update_time, remark, doc_category_id,is_deleted,tool_id from t_document @@ -58,6 +59,13 @@ AND (td.create_by_id = #{createById} or td.doc_status = 'yfb') + + and td.doc_id in + + #{id} + + + and td.is_deleted = '0' order by td.create_time desc @@ -76,6 +84,7 @@ doc_name, doc_type, doc_principals, + doc_principals_name, doc_resp_dept, doc_source, doc_status, @@ -97,6 +106,7 @@ #{docName}, #{docType}, #{docPrincipals}, + #{docPrincipalsName}, #{docRespDept}, #{docSource}, #{docStatus}, @@ -121,6 +131,7 @@ doc_name = #{docName}, doc_type = #{docType}, doc_principals = #{docPrincipals}, + doc_principals_name = #{docPrincipalsName}, doc_resp_dept = #{docRespDept}, doc_source = #{docSource}, doc_status = #{docStatus}, diff --git a/tool-tech-admin/src/main/resources/mapper/ToolMapper.xml b/tool-tech-admin/src/main/resources/mapper/ToolMapper.xml index d722bfc..360d0d1 100644 --- a/tool-tech-admin/src/main/resources/mapper/ToolMapper.xml +++ b/tool-tech-admin/src/main/resources/mapper/ToolMapper.xml @@ -178,6 +178,12 @@ AND u.record_status = #{recordStatus} + + and u.tool_id in + + #{id} + + ${params.dataScope} order by create_time desc diff --git a/tool-tech-common/src/main/java/com/rzdata/common/constant/Constants.java b/tool-tech-common/src/main/java/com/rzdata/common/constant/Constants.java index 2d0ed14..612835f 100644 --- a/tool-tech-common/src/main/java/com/rzdata/common/constant/Constants.java +++ b/tool-tech-common/src/main/java/com/rzdata/common/constant/Constants.java @@ -214,4 +214,14 @@ public class Constants /** 申请查看素哟偶数据 **/ public static final String APPLY_VIEW_ALL_PERMISSION = "system:apply:all:view"; + + public static final String FILE_DOWNLOAD = "download"; + public static final String FILE_PREVIEW = "preview"; + public static final String FILE_TYPE_DOC = ".doc"; + public static final String FILE_TYPE_PDF = ".pdf"; + public static final String SYMBOL_POINT = "."; + /** + * URL路径符 + */ + public final static String SEPARATOR ="/"; }