package cn.iocoder.yudao.module.mes.controller.admin.pd.project; 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.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.project.vo.MesPdProjectPageReqVO; import cn.iocoder.yudao.module.mes.controller.admin.pd.project.vo.MesPdProjectRespVO; import cn.iocoder.yudao.module.mes.controller.admin.pd.project.vo.MesPdProjectSaveReqVO; import cn.iocoder.yudao.module.mes.dal.dataobject.pd.project.MesPdProjectDO; import cn.iocoder.yudao.module.mes.service.pd.project.MesPdProjectService; 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.stream.Stream; 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; import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSetByFlatMap; import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId; @Tag(name = "管理后台 - MES 设计项目") @RestController @RequestMapping("/mes/pd/project") @Validated public class MesPdProjectController { @Resource private MesPdProjectService projectService; @Resource private AdminUserApi adminUserApi; @PostMapping("/create") @Operation(summary = "创建设计项目") @PreAuthorize("@ss.hasPermission('mes:pd-project:create')") public CommonResult createProject(@Valid @RequestBody MesPdProjectSaveReqVO createReqVO) { return success(projectService.createProject(createReqVO)); } @PutMapping("/update") @Operation(summary = "更新设计项目") @PreAuthorize("@ss.hasPermission('mes:pd-project:update')") public CommonResult updateProject(@Valid @RequestBody MesPdProjectSaveReqVO updateReqVO) { projectService.updateProject(updateReqVO); return success(true); } @DeleteMapping("/delete") @Operation(summary = "删除设计项目") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pd-project:delete')") public CommonResult deleteProject(@RequestParam("id") Long id) { projectService.deleteProject(id); return success(true); } @GetMapping("/get") @Operation(summary = "获得设计项目") @Parameter(name = "id", description = "编号", required = true, example = "1024") @PreAuthorize("@ss.hasPermission('mes:pd-project:query')") public CommonResult getProject(@RequestParam("id") Long id) { MesPdProjectDO project = projectService.getProject(id); if (project == null) { return success(null); } return success(buildProjectRespVOList(Collections.singletonList(project)).get(0)); } @GetMapping("/page") @Operation(summary = "获得设计项目分页") @PreAuthorize("@ss.hasPermission('mes:pd-project:query')") public CommonResult> getProjectPage(@Valid MesPdProjectPageReqVO pageReqVO) { PageResult pageResult = projectService.getProjectPage(pageReqVO); return success(new PageResult<>(buildProjectRespVOList(pageResult.getList()), pageResult.getTotal())); } @GetMapping("/export-excel") @Operation(summary = "导出设计项目 Excel") @PreAuthorize("@ss.hasPermission('mes:pd-project:export')") @ApiAccessLog(operateType = EXPORT) public void exportProjectExcel(@Valid MesPdProjectPageReqVO pageReqVO, HttpServletResponse response) throws IOException { pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE); List list = projectService.getProjectPage(pageReqVO).getList(); // 导出 Excel ExcelUtils.write(response, "设计项目.xls", "数据", MesPdProjectRespVO.class, buildProjectRespVOList(list)); } @PutMapping("/publish") @Operation(summary = "发布设计项目(待发布→待上传)") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pd-project:update')") public CommonResult publishProject(@RequestParam("id") Long id) { projectService.publishProject(id); return success(true); } @PutMapping("/upload") @Operation(summary = "上传资料(待上传→待审核),同时自动创建资料审核记录") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pd-project:update')") public CommonResult uploadProject(@RequestParam("id") Long id) { projectService.uploadProject(id, getLoginUserId()); return success(true); } @PutMapping("/audit") @Operation(summary = "审核设计项目(待审核→待归档)") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pd-project:update')") public CommonResult auditProject(@RequestParam("id") Long id) { projectService.auditProject(id); return success(true); } @PutMapping("/archive") @Operation(summary = "归档设计项目(待归档→已归档),同时自动创建归档台账") @Parameter(name = "id", description = "编号", required = true) @PreAuthorize("@ss.hasPermission('mes:pd-project:update')") public CommonResult archiveProject(@RequestParam("id") Long id) { projectService.archiveProject(id, getLoginUserId()); return success(true); } // ==================== 拼接 VO ==================== private List buildProjectRespVOList(List list) { if (CollUtil.isEmpty(list)) { return Collections.emptyList(); } // 1.1 批量获取关联数据(主设计师 + 审核人) Map userMap = adminUserApi.getUserMap(convertSetByFlatMap(list, project -> Stream.of(project.getChiefDesigner(), project.getReviewer()))); // 2. 拼接 VO return BeanUtils.toBean(list, MesPdProjectRespVO.class, vo -> { MapUtils.findAndThen(userMap, vo.getChiefDesigner(), user -> vo.setChiefDesignerNickname(user.getNickname())); MapUtils.findAndThen(userMap, vo.getReviewer(), user -> vo.setReviewerNickname(user.getNickname())); }); } }