From 992a07f15454507ec961775b84c2d67653d05c7c Mon Sep 17 00:00:00 2001 From: OTTO <731554297@qq.com> Date: Wed, 9 Apr 2025 14:33:26 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E9=A1=B9=E7=9B=AE=E7=AE=A1=E7=90=86?= =?UTF-8?q?=E7=9F=A5=E8=AF=86=E5=BA=93=E4=BC=98=E5=8C=96):=20=E7=9F=A5?= =?UTF-8?q?=E8=AF=86=E5=BA=93=E7=9B=B4=E6=8E=A5=E5=88=9B=E5=BB=BA=E7=9A=84?= =?UTF-8?q?=E6=96=87=E6=A1=A3=E9=9C=80=E8=A6=81=E8=83=BD=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: OTTO <731554297@qq.com> --- .../controller/PmsDocumentController.java | 40 ++++----- .../document/service/IPmsDocumentService.java | 3 + .../service/impl/PmsDocumentServiceImpl.java | 82 +++++++++++++------ 3 files changed, 81 insertions(+), 44 deletions(-) diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/controller/PmsDocumentController.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/controller/PmsDocumentController.java index 225c79ab5..0712f6382 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/controller/PmsDocumentController.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/controller/PmsDocumentController.java @@ -20,7 +20,7 @@ import java.util.List; /** * 项目管理-文档库Controller - * + * * @author wanjia * @date 2024-01-08 */ @@ -30,8 +30,7 @@ import java.util.List; @ApiImplicitParams({ @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String") }) -public class PmsDocumentController extends BaseController -{ +public class PmsDocumentController extends BaseController { @Autowired private IPmsDocumentService pmsDocumentService; @@ -46,8 +45,7 @@ public class PmsDocumentController extends BaseController @ApiImplicitParam(name = "orderByColumn", value = "排序列(更新时间:updateTime,创建时间:createTime)", paramType = "query", dataType = "String"), @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String") }) - public GenericsTableDataInfo list( @Validated PmsDocumentSearchVo pmsDocumentSearchVo) - { + public GenericsTableDataInfo list(@Validated PmsDocumentSearchVo pmsDocumentSearchVo) { return pmsDocumentService.selectPmsDocumentVoList(pmsDocumentSearchVo); } @@ -58,8 +56,7 @@ public class PmsDocumentController extends BaseController @Log(title = "项目管理-文档库", businessType = BusinessType.EXPORT) @PostMapping("/export") @ApiOperation(value = "导出项目管理-文档库列表", hidden = true) - public void export(HttpServletResponse response, PmsDocument pmsDocument) - { + public void export(HttpServletResponse response, PmsDocument pmsDocument) { List list = pmsDocumentService.selectPmsDocumentList(pmsDocument); ExcelUtil util = new ExcelUtil(PmsDocument.class); util.exportExcel(response, list, "项目管理-文档库数据"); @@ -71,8 +68,7 @@ public class PmsDocumentController extends BaseController // @RequiresPermissions("pms:document:query") @GetMapping(value = "/{id}") @ApiOperation(value = "获取项目管理-文档库详细信息") - public AjaxResult getInfo(@PathVariable("id") Long id, @ApiParam("企业id") @RequestParam(name = "enterpriseId") Long enterpriseId) - { + public AjaxResult getInfo(@PathVariable("id") Long id, @ApiParam("企业id") @RequestParam(name = "enterpriseId") Long enterpriseId) { return success(pmsDocumentService.selectPmsDocumentDetailVoByIdAndEnterpriseId(id, enterpriseId)); } @@ -83,8 +79,7 @@ public class PmsDocumentController extends BaseController @Log(title = "项目管理-文档库", businessType = BusinessType.INSERT) @PostMapping @ApiOperation(value = "新增项目管理-文档库") - public AjaxResult add(@RequestBody @Validated PmsDocumentInputVo pmsDocumentInputVo) - { + public AjaxResult add(@RequestBody @Validated PmsDocumentInputVo pmsDocumentInputVo) { return toAjax(pmsDocumentService.insertPmsDocument(pmsDocumentInputVo.toPmsDocument())); } @@ -95,8 +90,7 @@ public class PmsDocumentController extends BaseController @Log(title = "新增附件类型文档", businessType = BusinessType.INSERT) @PostMapping("/fileType") @ApiOperation(value = "新增附件类型文档") - public AjaxResult addFileTypeDoc(@RequestBody @Validated PmsFileTypeDocumentInputVo pmsFileTypeDocumentInputVo) - { + public AjaxResult addFileTypeDoc(@RequestBody @Validated PmsFileTypeDocumentInputVo pmsFileTypeDocumentInputVo) { return toAjax(pmsDocumentService.insertPmsFileTypeDocument(pmsFileTypeDocumentInputVo)); } @@ -107,8 +101,7 @@ public class PmsDocumentController extends BaseController @Log(title = "项目管理-文档库", businessType = BusinessType.UPDATE) @PutMapping @ApiOperation(value = "修改项目管理-文档库") - public AjaxResult edit(@RequestBody PmsDocumentUpdateVo pmsDocumentUpdateVo) - { + public AjaxResult edit(@RequestBody PmsDocumentUpdateVo pmsDocumentUpdateVo) { return toAjax(pmsDocumentService.updatePmsDocument(pmsDocumentUpdateVo.toPmsDocument())); } @@ -117,12 +110,11 @@ public class PmsDocumentController extends BaseController */ // @RequiresPermissions("pms:document:remove") @Log(title = "项目管理-文档库", businessType = BusinessType.DELETE) - @DeleteMapping("/{ids}") + @DeleteMapping("/{ids}") @ApiOperation(value = "删除项目管理-文档库") public AjaxResult remove(@PathVariable Long[] ids, @ApiParam("企业id") @RequestParam(name = "enterpriseId") Long enterpriseId, - @ApiParam("项目id") @RequestParam(name = "pmsProjectId", defaultValue = "0") Long pmsProjectId) - { + @ApiParam("项目id") @RequestParam(name = "pmsProjectId", defaultValue = "0") Long pmsProjectId) { return toAjax(pmsDocumentService.deletePmsDocumentByIds(ids, enterpriseId, pmsProjectId)); } @@ -131,8 +123,16 @@ public class PmsDocumentController extends BaseController */ @GetMapping(value = "/optionalWikiRepoList") @ApiOperation(value = "导入wiki时可选的仓库列表") - public AjaxResult getOptionalWikiRepoList(@Validated OptionalWikiReposSearchVo optionalWikiReposSearchVo) - { + public AjaxResult getOptionalWikiRepoList(@Validated OptionalWikiReposSearchVo optionalWikiReposSearchVo) { return success(pmsDocumentService.getOptionalWikiRepoList(optionalWikiReposSearchVo)); } + + /** + * 下载文档类型文件 + */ + @GetMapping(value = "/downloadDoc/{docId}") + @ApiOperation(value = "下载文档类型文件") + public void downloadDoc(@PathVariable String enterpriseIdentifier, @PathVariable Long docId, HttpServletResponse response) { + pmsDocumentService.downloadDoc(response, enterpriseIdentifier, docId); + } } diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/IPmsDocumentService.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/IPmsDocumentService.java index 9f1ac213a..a58a1881b 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/IPmsDocumentService.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/IPmsDocumentService.java @@ -5,6 +5,7 @@ import com.microservices.common.core.web.page.GenericsTableDataInfo; import com.microservices.pms.document.domain.PmsDocument; import com.microservices.pms.document.domain.vo.*; +import javax.servlet.http.HttpServletResponse; import java.util.List; /** @@ -93,4 +94,6 @@ public interface IPmsDocumentService * @return */ JSONObject getOptionalWikiRepoList(OptionalWikiReposSearchVo optionalWikiReposSearchVo); + + void downloadDoc(HttpServletResponse response, String enterpriseIdentifier, Long docId); } diff --git a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/impl/PmsDocumentServiceImpl.java b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/impl/PmsDocumentServiceImpl.java index 24d28a868..cdc1723e3 100644 --- a/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/impl/PmsDocumentServiceImpl.java +++ b/microservices-modules/microservices-modules-pms/src/main/java/com/microservices/pms/document/service/impl/PmsDocumentServiceImpl.java @@ -5,6 +5,7 @@ import com.microservices.common.core.constant.HttpStatus; import com.microservices.common.core.exception.ServiceException; import com.microservices.common.core.utils.DateUtils; import com.microservices.common.core.utils.StringUtils; +import com.microservices.common.core.utils.file.FileUtils; import com.microservices.common.core.web.page.Breadcrumb; import com.microservices.common.core.web.page.GenericsTableDataInfo; import com.microservices.common.httpClient.util.GitLinkRequestHelper; @@ -21,6 +22,7 @@ import com.microservices.pms.enums.PmsDocumentType; import com.microservices.pms.project.mapper.PmsProjectRepositoryMapper; import com.microservices.pms.project.service.IPmsProjectService; import com.microservices.pms.utils.PmsGitLinkRequestUrl; +import lombok.extern.slf4j.Slf4j; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeanUtils; @@ -28,7 +30,12 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; import java.net.URISyntaxException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.*; import java.util.stream.Collectors; @@ -38,13 +45,13 @@ import static com.microservices.common.httpClient.constant.GitLinkConstants.DATA /** * 项目管理-文档库Service业务层处理 - * + * * @author wanjia * @date 2024-01-08 */ +@Slf4j @Service -public class PmsDocumentServiceImpl implements IPmsDocumentService -{ +public class PmsDocumentServiceImpl implements IPmsDocumentService { private static final Logger logger = LoggerFactory.getLogger(PmsDocumentServiceImpl.class); @Resource private PmsDocumentMapper pmsDocumentMapper; @@ -69,13 +76,12 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService /** * 查询项目管理-文档库 - * + * * @param id 项目管理-文档库主键 * @return 项目管理-文档库 */ @Override - public PmsDocument selectPmsDocumentByIdAndEnterpriseId(Long id, Long enterpriseId) - { + public PmsDocument selectPmsDocumentByIdAndEnterpriseId(Long id, Long enterpriseId) { boolean isEnterpriseAdmin = pmsCommonService.hasDocsManageRole(enterpriseId); return pmsDocumentMapper.selectPmsDocumentByIdAndEnterpriseId(id, enterpriseId, isEnterpriseAdmin, SecurityUtils.getUsername()); } @@ -97,13 +103,12 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService /** * 查询项目管理-文档库列表 - * + * * @param pmsDocument 项目管理-文档库 * @return 项目管理-文档库 */ @Override - public List selectPmsDocumentList(PmsDocument pmsDocument) - { + public List selectPmsDocumentList(PmsDocument pmsDocument) { checkAuthAndReturnAdminStatus(pmsDocument.getPmsEnterpriseId()); return pmsDocumentMapper.selectPmsDocumentList(pmsDocument); } @@ -116,7 +121,7 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService pmsDocumentSearchVo.setCurrentUserName(currentUserName); startPage(); List pmsDocumentList = pmsDocumentMapper.selectPmsDocumentListBySearchInput(pmsDocumentSearchVo); - GenericsTableDataInfo page = toPage(pmsDocumentList,this::buildPmsDocumentVo); + GenericsTableDataInfo page = toPage(pmsDocumentList, this::buildPmsDocumentVo); //构建面包屑列表 if (pmsDocumentSearchVo.getParentId() != 0) { List breadcrumbs = buildBreadcrumbs(pmsDocumentSearchVo.getParentId()); @@ -132,21 +137,53 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService PmsEnterprise pmsEnterprise = pmsEnterpriseService.selectPmsEnterpriseById(enterpriseId); Map queryParams = buildRepoListQueryParams(optionalWikiReposSearchVo); try { - return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_ORGANIZATION_REPO_LIST(pmsEnterprise.getEnterpriseIdentifier(), queryParams)); + return gitLinkRequestHelper.doGet(PmsGitLinkRequestUrl.GET_ORGANIZATION_REPO_LIST(pmsEnterprise.getEnterpriseIdentifier(), queryParams)); } catch (URISyntaxException e) { throw new RuntimeException(e); } } + @Override + public void downloadDoc(HttpServletResponse response, String enterpriseIdentifier, Long docId) { + PmsEnterprise enterprise = pmsEnterpriseService.selectPmsEnterpriseByIdentifier(enterpriseIdentifier); + PmsDocument pmsDocument = selectPmsDocumentByIdAndEnterpriseId(docId, enterprise.getId()); + if (!pmsDocument.getDocType().equals(PmsDocumentType.DOCUMENT.getKey())) { + throw new ServiceException("当前接口仅允许下载文档类型"); + } + Path tempFilePath = null; + try { + String docFileName = pmsDocument.getName() + ".md"; + FileUtils.setAttachmentResponseHeader(response, docFileName); + // 创建临时文件 + tempFilePath = Files.createTempFile(pmsDocument.getName(), ".md"); + String content = ""; + if (pmsDocument.getContent() != null) { + content = pmsDocument.getContent(); + } + Files.write(tempFilePath, content.getBytes(StandardCharsets.UTF_8)); + FileUtils.writeBytes(tempFilePath.toString(), response.getOutputStream()); + } catch (IOException e) { + log.error("文档下载失败:{}", e.getMessage()); + throw new ServiceException("文档下载失败"); + } finally { + if (tempFilePath != null) { + try { + Files.deleteIfExists(tempFilePath); + } catch (IOException e) { + log.error("删除临时文件失败:{}", e.getMessage()); + } + } + } + } + /** * 新增项目管理-文档库 - * + * * @param pmsDocument 项目管理-文档库 * @return 结果 */ @Override - public int insertPmsDocument(PmsDocument pmsDocument) - { + public int insertPmsDocument(PmsDocument pmsDocument) { checkPmsProjectById(pmsDocument.getPmsProjectId()); checkAuthAndReturnAdminStatus(pmsDocument.getPmsEnterpriseId()); String currentUserName = SecurityUtils.getUsername(); @@ -169,13 +206,12 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService /** * 修改项目管理-文档库 - * + * * @param pmsDocument 项目管理-文档库 * @return 结果 */ @Override - public int updatePmsDocument(PmsDocument pmsDocument) - { + public int updatePmsDocument(PmsDocument pmsDocument) { checkPmsProjectById(pmsDocument.getPmsProjectId()); String currentUserName = SecurityUtils.getUsername(); boolean isEnterpriseAdmin = checkAuthAndReturnAdminStatus(pmsDocument.getPmsEnterpriseId()); @@ -195,8 +231,7 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService * @return 结果 */ @Override - public int deletePmsDocumentByIds(Long[] ids, Long enterpriseId, Long projectId) - { + public int deletePmsDocumentByIds(Long[] ids, Long enterpriseId, Long projectId) { checkPmsProjectById(projectId); String currentUserName = SecurityUtils.getUsername(); boolean isEnterpriseAdmin = checkAuthAndReturnAdminStatus(enterpriseId); @@ -207,13 +242,12 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService /** * 删除项目管理-文档库信息 - * + * * @param id 项目管理-文档库主键 * @return 结果 */ @Override - public int deletePmsDocumentById(Long id, Long enterpriseId, Long projectId) - { + public int deletePmsDocumentById(Long id, Long enterpriseId, Long projectId) { checkPmsProjectById(projectId); String currentUserName = SecurityUtils.getUsername(); boolean isEnterpriseAdmin = checkAuthAndReturnAdminStatus(enterpriseId); @@ -303,7 +337,7 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService throw new ServiceException("文件类型的文档名称个数与文件标识个数不匹配"); } List pmsDocumentList = new ArrayList<>(); - for (int i=0; i < names.length; i++) { + for (int i = 0; i < names.length; i++) { PmsDocument pmsDocument = new PmsDocument(); BeanUtils.copyProperties(pmsFileTypeDocumentInputVo, pmsDocument); pmsDocument.setName(names[i]); @@ -373,7 +407,7 @@ public class PmsDocumentServiceImpl implements IPmsDocumentService queryParams.put("pm_project_repository_ids", unlinkedRepoIdsString); } queryParams.put("search", optionalWikiReposSearchVo.getSearch()); - queryParams.put("page" , String.valueOf(optionalWikiReposSearchVo.getPage())); + queryParams.put("page", String.valueOf(optionalWikiReposSearchVo.getPage())); queryParams.put("limit", String.valueOf(optionalWikiReposSearchVo.getLimit())); queryParams.put("sort_by", optionalWikiReposSearchVo.getSortBy()); queryParams.put("sort_direction", "desc");