liyong
5 天以前 274dd8b724dd4485658e1d2a3f825a007f625bd7
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
package cn.iocoder.yudao.module.mes.controller.admin.pd.processparamtemplate;
 
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.processparamtemplate.vo.MesPdProcessParamTemplatePageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.processparamtemplate.vo.MesPdProcessParamTemplateRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.pd.processparamtemplate.vo.MesPdProcessParamTemplateSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.unitmeasure.MesMdUnitMeasureDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.processparamtemplate.MesPdProcessParamTemplateDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.pd.processparamtemplate.MesPdProcessParamTemplateDetailDO;
import cn.iocoder.yudao.module.mes.service.md.unitmeasure.MesMdUnitMeasureService;
import cn.iocoder.yudao.module.mes.service.pd.processparamtemplate.MesPdProcessParamTemplateService;
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.*;
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.util.collection.CollectionUtils.convertSet;
 
@Tag(name = "管理后台 - MES 工艺参数模板")
@RestController
@RequestMapping("/mes/pd/process-param-template")
@Validated
public class MesPdProcessParamTemplateController {
 
    @Resource
    private MesPdProcessParamTemplateService templateService;
 
    @Resource
    private MesMdUnitMeasureService unitMeasureService;
 
    @PostMapping("/create")
    @Operation(summary = "创建工艺参数模板")
    @PreAuthorize("@ss.hasPermission('mes:pd-process-param-template:create')")
    public CommonResult<Long> createProcessParamTemplate(@Valid @RequestBody MesPdProcessParamTemplateSaveReqVO createReqVO) {
        return success(templateService.createProcessParamTemplate(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新工艺参数模板")
    @PreAuthorize("@ss.hasPermission('mes:pd-process-param-template:update')")
    public CommonResult<Boolean> updateProcessParamTemplate(@Valid @RequestBody MesPdProcessParamTemplateSaveReqVO updateReqVO) {
        templateService.updateProcessParamTemplate(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除工艺参数模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:pd-process-param-template:delete')")
    public CommonResult<Boolean> deleteProcessParamTemplate(@RequestParam("id") Long id) {
        templateService.deleteProcessParamTemplate(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得工艺参数模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:pd-process-param-template:query')")
    public CommonResult<MesPdProcessParamTemplateRespVO> getProcessParamTemplate(@RequestParam("id") Long id) {
        MesPdProcessParamTemplateDO template = templateService.getProcessParamTemplate(id);
        if (template == null) {
            return success(null);
        }
        return success(buildTemplateRespVO(template));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得工艺参数模板分页")
    @PreAuthorize("@ss.hasPermission('mes:pd-process-param-template:query')")
    public CommonResult<PageResult<MesPdProcessParamTemplateRespVO>> getProcessParamTemplatePage(@Valid MesPdProcessParamTemplatePageReqVO pageReqVO) {
        PageResult<MesPdProcessParamTemplateDO> pageResult = templateService.getProcessParamTemplatePage(pageReqVO);
        List<MesPdProcessParamTemplateRespVO> voList = buildTemplateRespVOList(pageResult.getList());
        return success(new PageResult<>(voList, pageResult.getTotal()));
    }
 
    @GetMapping("/export-excel")
    @Operation(summary = "导出工艺参数模板 Excel")
    @PreAuthorize("@ss.hasPermission('mes:pd-process-param-template:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProcessParamTemplateExcel(@Valid MesPdProcessParamTemplatePageReqVO pageReqVO,
                                                 HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MesPdProcessParamTemplateDO> list = templateService.getProcessParamTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "工艺参数模板.xls", "数据", MesPdProcessParamTemplateRespVO.class,
                buildTemplateRespVOList(list));
    }
 
    // ==================== 拼接 VO ====================
 
    private MesPdProcessParamTemplateRespVO buildTemplateRespVO(MesPdProcessParamTemplateDO template) {
        return buildTemplateRespVOList(Collections.singletonList(template)).get(0);
    }
 
    private List<MesPdProcessParamTemplateRespVO> buildTemplateRespVOList(List<MesPdProcessParamTemplateDO> list) {
        if (CollUtil.isEmpty(list)) {
            return Collections.emptyList();
        }
        // 1. 批量获取所有模板的明细
        Map<Long, List<MesPdProcessParamTemplateDetailDO>> detailMap = new HashMap<>();
        Set<Long> allUnitMeasureIds = new HashSet<>();
        for (MesPdProcessParamTemplateDO template : list) {
            List<MesPdProcessParamTemplateDetailDO> details =
                    templateService.getProcessParamTemplateDetailListByTemplateId(template.getId());
            detailMap.put(template.getId(), details);
            for (MesPdProcessParamTemplateDetailDO detail : details) {
                if (detail.getUnitMeasureId() != null) {
                    allUnitMeasureIds.add(detail.getUnitMeasureId());
                }
            }
        }
        // 2. 批量获取计量单位
        Map<Long, MesMdUnitMeasureDO> unitMeasureMap = unitMeasureService.getUnitMeasureMap(allUnitMeasureIds);
        // 3. 拼接 VO
        return BeanUtils.toBean(list, MesPdProcessParamTemplateRespVO.class, vo -> {
            List<MesPdProcessParamTemplateDetailDO> details = detailMap.get(vo.getId());
            if (CollUtil.isNotEmpty(details)) {
                List<MesPdProcessParamTemplateRespVO.DetailResp> detailResps = BeanUtils.toBean(
                        details, MesPdProcessParamTemplateRespVO.DetailResp.class, detailResp ->
                                MapUtils.findAndThen(unitMeasureMap, detailResp.getUnitMeasureId(),
                                        unitMeasure -> detailResp.setUnitMeasureName(unitMeasure.getName()))
                );
                vo.setDetails(detailResps);
            }
        });
    }
 
}