package cn.iocoder.yudao.module.archive.controller.admin.document;
|
|
import cn.hutool.core.collection.CollUtil;
|
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
|
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
|
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
|
import cn.iocoder.yudao.module.archive.controller.admin.document.vo.ArchiveDocumentPageReqVO;
|
import cn.iocoder.yudao.module.archive.controller.admin.document.vo.ArchiveDocumentRespVO;
|
import cn.iocoder.yudao.module.archive.controller.admin.document.vo.ArchiveDocumentSaveReqVO;
|
import cn.iocoder.yudao.module.archive.dal.dataobject.category.ArchiveCategoryDO;
|
import cn.iocoder.yudao.module.archive.dal.dataobject.document.ArchiveDocumentDO;
|
import cn.iocoder.yudao.module.archive.service.category.ArchiveCategoryService;
|
import cn.iocoder.yudao.module.archive.service.document.ArchiveDocumentService;
|
import cn.iocoder.yudao.module.system.api.storage.StorageAttachmentApi;
|
import cn.iocoder.yudao.module.system.api.storage.dto.StorageBlobRespDTO;
|
import cn.iocoder.yudao.module.system.api.user.AdminUserApi;
|
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
|
import io.swagger.v3.oas.annotations.Operation;
|
import io.swagger.v3.oas.annotations.Parameter;
|
import io.swagger.v3.oas.annotations.tags.Tag;
|
import jakarta.annotation.Resource;
|
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.validation.Valid;
|
import org.springframework.security.access.prepost.PreAuthorize;
|
import org.springframework.validation.annotation.Validated;
|
import org.springframework.web.bind.annotation.*;
|
|
import java.io.IOException;
|
import java.util.Collections;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Set;
|
import java.util.function.Function;
|
import java.util.stream.Collectors;
|
|
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
|
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
import static cn.iocoder.yudao.framework.common.pojo.PageParam.PAGE_SIZE_NONE;
|
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;
|
import static cn.iocoder.yudao.framework.common.util.collection.MapUtils.findAndThen;
|
import static cn.iocoder.yudao.module.archive.service.document.ArchiveDocumentService.DOCUMENT_RECORD_TYPE;
|
|
@Tag(name = "管理后台 - 档案")
|
@RestController
|
@RequestMapping("/archive/document")
|
@Validated
|
public class ArchiveDocumentController {
|
|
@Resource
|
private ArchiveDocumentService documentService;
|
|
@Resource
|
private ArchiveCategoryService categoryService;
|
|
@Resource
|
private AdminUserApi adminUserApi;
|
|
@Resource
|
private StorageAttachmentApi storageAttachmentApi;
|
|
@PostMapping("/create")
|
@Operation(summary = "创建档案")
|
@PreAuthorize("@ss.hasPermission('archive:document:create')")
|
public CommonResult<Long> createDocument(@Valid @RequestBody ArchiveDocumentSaveReqVO createReqVO) {
|
return success(documentService.createDocument(createReqVO));
|
}
|
|
@PutMapping("/update")
|
@Operation(summary = "更新档案(借出中的档案不允许修改)")
|
@PreAuthorize("@ss.hasPermission('archive:document:update')")
|
public CommonResult<Boolean> updateDocument(@Valid @RequestBody ArchiveDocumentSaveReqVO updateReqVO) {
|
documentService.updateDocument(updateReqVO);
|
return success(true);
|
}
|
|
@DeleteMapping("/delete")
|
@Operation(summary = "删除档案")
|
@Parameter(name = "id", description = "编号", required = true)
|
@PreAuthorize("@ss.hasPermission('archive:document:delete')")
|
public CommonResult<Boolean> deleteDocument(@RequestParam("id") Long id) {
|
documentService.deleteDocument(id);
|
return success(true);
|
}
|
|
@GetMapping("/get")
|
@Operation(summary = "获得档案(保密级档案需要 archive:document:sensitive 权限)")
|
@Parameter(name = "id", description = "编号", required = true, example = "1024")
|
@PreAuthorize("@ss.hasPermission('archive:document:query')")
|
public CommonResult<ArchiveDocumentRespVO> getDocument(@RequestParam("id") Long id) {
|
ArchiveDocumentDO document = documentService.getDocument(id);
|
return success(buildDocumentDetail(document));
|
}
|
|
@GetMapping("/page")
|
@Operation(summary = "获得档案分页(无敏感档案权限时自动过滤保密档案)")
|
@PreAuthorize("@ss.hasPermission('archive:document:query')")
|
public CommonResult<PageResult<ArchiveDocumentRespVO>> getDocumentPage(
|
@Valid ArchiveDocumentPageReqVO pageReqVO) {
|
PageResult<ArchiveDocumentDO> pageResult = documentService.getDocumentPage(pageReqVO);
|
return success(new PageResult<>(buildDocumentDetailList(pageResult.getList()), pageResult.getTotal()));
|
}
|
|
@GetMapping("/export-excel")
|
@Operation(summary = "导出档案 Excel")
|
@PreAuthorize("@ss.hasPermission('archive:document:export')")
|
@ApiAccessLog(operateType = EXPORT)
|
public void exportDocumentExcel(@Valid ArchiveDocumentPageReqVO pageReqVO,
|
HttpServletResponse response) throws IOException {
|
pageReqVO.setPageSize(PAGE_SIZE_NONE);
|
List<ArchiveDocumentDO> list = documentService.getDocumentList(pageReqVO);
|
ExcelUtils.write(response, "档案.xls", "数据", ArchiveDocumentRespVO.class, buildDocumentDetailList(list));
|
}
|
|
// ==================== 内部方法 ====================
|
|
private ArchiveDocumentRespVO buildDocumentDetail(ArchiveDocumentDO document) {
|
if (document == null) {
|
return null;
|
}
|
return buildDocumentDetailList(Collections.singletonList(document)).get(0);
|
}
|
|
private List<ArchiveDocumentRespVO> buildDocumentDetailList(List<ArchiveDocumentDO> list) {
|
if (CollUtil.isEmpty(list)) {
|
return Collections.emptyList();
|
}
|
|
// 1.1 分类名称
|
Set<Long> categoryIds = convertSet(list, ArchiveDocumentDO::getCategoryId);
|
Map<Long, ArchiveCategoryDO> categoryMap = categoryIds.isEmpty() ? Collections.emptyMap()
|
: categoryService.getCategoryList().stream()
|
.filter(category -> categoryIds.contains(category.getId()))
|
.collect(Collectors.toMap(ArchiveCategoryDO::getId, Function.identity(), (a, b) -> a));
|
// 1.2 归档人姓名
|
Set<Long> archivistIds = convertSet(list, ArchiveDocumentDO::getArchivistUserId);
|
Map<Long, AdminUserRespDTO> archivistMap = adminUserApi.getUserMap(archivistIds);
|
// 1.3 附件
|
Map<Long, List<StorageBlobRespDTO>> attachmentMap = list.stream()
|
.collect(Collectors.toMap(ArchiveDocumentDO::getId,
|
document -> storageAttachmentApi.listAttachments(DOCUMENT_RECORD_TYPE, document.getId())));
|
|
// 2. 拼接结果
|
return BeanUtils.toBean(list, ArchiveDocumentRespVO.class, vo -> {
|
ArchiveCategoryDO category = categoryMap.get(vo.getCategoryId());
|
if (category != null) {
|
vo.setCategoryName(category.getName());
|
}
|
findAndThen(archivistMap, vo.getArchivistUserId(), user -> vo.setArchivistName(user.getNickname()));
|
List<StorageBlobRespDTO> attachments = attachmentMap.getOrDefault(vo.getId(), Collections.emptyList());
|
vo.setAttachmentList(attachments);
|
vo.setHasAttachment(CollUtil.isNotEmpty(attachments));
|
});
|
}
|
|
}
|