liu
2 天以前 104db2c4423867a7fa4f41343213f45985ae3110
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
package cn.iocoder.yudao.module.mes.controller.admin.pd.document;
 
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.document.vo.MesPdDocumentSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.document.MesPdDocumentDO;
import cn.iocoder.yudao.module.mes.service.pd.document.MesPdDocumentService;
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 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.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - MES 设计资料")
@RestController
@RequestMapping("/mes/pd/document")
@Validated
public class MesPdDocumentController {
 
    @Resource
    private MesPdDocumentService documentService;
 
    @Resource
    private AdminUserApi adminUserApi;
    @Resource
    private StorageAttachmentApi storageAttachmentApi;
 
    @PostMapping("/create")
    @Operation(summary = "创建设计资料")
    @PreAuthorize("@ss.hasPermission('mes:pd-document:create')")
    public CommonResult<Long> createDocument(@Valid @RequestBody MesPdDocumentSaveReqVO createReqVO) {
        return success(documentService.createDocument(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新设计资料")
    @PreAuthorize("@ss.hasPermission('mes:pd-document:update')")
    public CommonResult<Boolean> updateDocument(@Valid @RequestBody MesPdDocumentSaveReqVO updateReqVO) {
        documentService.updateDocument(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除设计资料")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:pd-document:delete')")
    public CommonResult<Boolean> deleteDocument(@RequestParam("id") Long id) {
        documentService.deleteDocument(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得设计资料")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:pd-document:query')")
    public CommonResult<MesPdDocumentRespVO> getDocument(@RequestParam("id") Long id) {
        MesPdDocumentDO document = documentService.getDocument(id);
        if (document == null) {
            return success(null);
        }
        MesPdDocumentRespVO respVO = buildDocumentRespVOList(Collections.singletonList(document)).get(0);
        respVO.setAttachmentList(storageAttachmentApi.listAttachments("mes_pd_document", id));
        return success(respVO);
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得设计资料分页")
    @PreAuthorize("@ss.hasPermission('mes:pd-document:query')")
    public CommonResult<PageResult<MesPdDocumentRespVO>> getDocumentPage(@Valid MesPdDocumentPageReqVO pageReqVO) {
        PageResult<MesPdDocumentDO> pageResult = documentService.getDocumentPage(pageReqVO);
        return success(new PageResult<>(buildDocumentRespVOList(pageResult.getList()), pageResult.getTotal()));
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出设计资料 Excel")
    @PreAuthorize("@ss.hasPermission('mes:pd-document:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDocumentExcel(@Valid MesPdDocumentPageReqVO pageReqVO,
                                    HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MesPdDocumentDO> list = documentService.getDocumentPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设计资料.xls", "数据", MesPdDocumentRespVO.class,
                buildDocumentRespVOList(list));
    }
 
    // ==================== 拼接 VO ====================
 
    private List<MesPdDocumentRespVO> buildDocumentRespVOList(List<MesPdDocumentDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 1.1 批量获取关联数据(上传人)
        Map<Long, AdminUserRespDTO> userMap = adminUserApi.getUserMap(convertSet(list, MesPdDocumentDO::getUploadUserId));
        // 2. 拼接 VO
        List<MesPdDocumentRespVO> voList = BeanUtils.toBean(list, MesPdDocumentRespVO.class, vo -> {
            MapUtils.findAndThen(userMap, vo.getUploadUserId(),
                    user -> vo.setUploadUserNickname(user.getNickname()));
        });
        // 3. 拼接文件信息:优先取第一个附件的地址作为 fileUrl,并在未填文件名时自动补全
        for (MesPdDocumentRespVO vo : voList) {
            List<StorageBlobRespDTO> attachments = storageAttachmentApi.listAttachments("mes_pd_document", vo.getId());
            if (CollUtil.isNotEmpty(attachments)) {
                StorageBlobRespDTO first = attachments.get(0);
                vo.setFileUrl(first.getUrl());
                if (StrUtil.isEmpty(vo.getFileName())) {
                    vo.setFileName(first.getName());
                }
            }
        }
        return voList;
    }
 
}