云
2026-09-29 f87ce86307056f63f79312178037cc8714eb2b13
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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));
        });
    }
 
}