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.qc.template;
 
import cn.hutool.core.collection.CollUtil;
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.module.mes.controller.admin.qc.template.vo.item.MesQcTemplateItemOptionRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.template.vo.item.MesQcTemplateItemPageReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.template.vo.item.MesQcTemplateItemRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.qc.template.vo.item.MesQcTemplateItemSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.mdm.MesMdmItemDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.md.unitmeasure.MesMdUnitMeasureDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateDO;
import cn.iocoder.yudao.module.mes.dal.dataobject.qc.template.MesQcTemplateItemDO;
import cn.iocoder.yudao.module.mes.service.mdm.MesMdmItemService;
import cn.iocoder.yudao.module.mes.service.md.unitmeasure.MesMdUnitMeasureService;
import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateItemService;
import cn.iocoder.yudao.module.mes.service.qc.template.MesQcTemplateService;
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.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
 
import java.util.Collections;
import java.util.List;
import java.util.Map;
 
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.framework.common.pojo.CommonResult.success;
 
@Tag(name = "管理后台 - MES 质检方案-产品关联")
@RestController
@RequestMapping("/mes/qc/template/item")
@Validated
public class MesQcTemplateItemController {
 
    @Resource
    private MesQcTemplateItemService templateItemService;
    @Resource
    private MesQcTemplateService templateService;
    @Resource
    private MesMdmItemService mdmItemService;
    @Resource
    private MesMdUnitMeasureService unitMeasureService;
 
    @PostMapping("/create")
    @Operation(summary = "创建质检方案-产品关联")
    @PreAuthorize("@ss.hasPermission('mes:qc-template:create')")
    public CommonResult<Long> createTemplateItem(@Valid @RequestBody MesQcTemplateItemSaveReqVO createReqVO) {
        return success(templateItemService.createTemplateItem(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新质检方案-产品关联")
    @PreAuthorize("@ss.hasPermission('mes:qc-template:update')")
    public CommonResult<Boolean> updateTemplateItem(@Valid @RequestBody MesQcTemplateItemSaveReqVO updateReqVO) {
        templateItemService.updateTemplateItem(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除质检方案-产品关联")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:qc-template:update')")
    public CommonResult<Boolean> deleteTemplateItem(@RequestParam("id") Long id) {
        templateItemService.deleteTemplateItem(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得质检方案-产品关联")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mes:qc-template:query')")
    public CommonResult<MesQcTemplateItemRespVO> getTemplateItem(@RequestParam("id") Long id) {
        MesQcTemplateItemDO item = templateItemService.getTemplateItem(id);
        return success(buildItemRespVOList(Collections.singletonList(item)).get(0));
    }
 
    @GetMapping("/page")
    @Operation(summary = "获得质检方案-产品关联分页")
    @PreAuthorize("@ss.hasPermission('mes:qc-template:query')")
    public CommonResult<PageResult<MesQcTemplateItemRespVO>> getTemplateItemPage(
            @Valid MesQcTemplateItemPageReqVO pageReqVO) {
        PageResult<MesQcTemplateItemDO> pageResult = templateItemService.getTemplateItemPage(pageReqVO);
        return success(new PageResult<>(buildItemRespVOList(pageResult.getList()), pageResult.getTotal()));
    }
 
    @GetMapping("/list-by-item-and-type")
    @Operation(summary = "根据物料或工单/任务和检测种类获取可用质检方案")
    @PreAuthorize("@ss.hasPermission('mes:qc-template:query')")
    public CommonResult<List<MesQcTemplateItemOptionRespVO>> getTemplateItemOptionList(
            @RequestParam(value = "qcType", required = false) Integer qcType,
            @RequestParam(value = "itemId", required = false) Long itemId,
            @RequestParam(value = "workOrderId", required = false) Long workOrderId,
            @RequestParam(value = "taskId", required = false) Long taskId) {
        List<MesQcTemplateItemDO> list = templateItemService.getTemplateItemList(
                itemId, qcType, workOrderId, taskId);
        if (CollUtil.isEmpty(list)) {
            return success(Collections.emptyList());
        }
        Map<Long, MesQcTemplateDO> templateMap = templateService.getTemplateMap(
                convertSet(list, MesQcTemplateItemDO::getTemplateId));
        List<MesQcTemplateItemOptionRespVO> result = BeanUtils.toBean(list,
                MesQcTemplateItemOptionRespVO.class, vo -> {
                    MesQcTemplateDO template = templateMap.get(vo.getTemplateId());
                    if (template != null) {
                        vo.setTemplateCode(template.getCode()).setTemplateName(template.getName());
                    }
                });
        return success(result);
    }
 
    // ==================== 拼接 VO ====================
 
    private List<MesQcTemplateItemRespVO> buildItemRespVOList(List<MesQcTemplateItemDO> list) {
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        // 批量查询物料(itemId 存的是 mdm_item_id,关联 mdm_item 表)
        Map<Long, MesMdmItemDO> itemMap = mdmItemService.getItemMap(
                convertSet(list, MesQcTemplateItemDO::getItemId));
        // 批量查询计量单位
        Map<Long, MesMdUnitMeasureDO> unitMeasureMap = unitMeasureService.getUnitMeasureMap(
                convertSet(itemMap.values(), MesMdmItemDO::getUnitMeasureId));
        // 拼装 VO
        return BeanUtils.toBean(list, MesQcTemplateItemRespVO.class, vo -> {
            findAndThen(itemMap, vo.getItemId(), item -> {
                vo.setItemCode(item.getCode()).setItemName(item.getName()).setSpecification(item.getSpecification());
                findAndThen(unitMeasureMap, item.getUnitMeasureId(),
                        unit -> vo.setUnitMeasureName(unit.getName()));
            });
        });
    }
 
}