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
package cn.iocoder.yudao.module.mes.controller.admin.dv.machinery;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
 
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.type.MesDvMachineryTypeListReqVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.type.MesDvMachineryTypeRespVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.type.MesDvMachineryTypeSaveReqVO;
import cn.iocoder.yudao.module.mes.dal.dataobject.dv.machinery.MesDvMachineryTypeDO;
import cn.iocoder.yudao.module.mes.service.dv.machinery.MesDvMachineryTypeService;
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.List;
 
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import org.springframework.web.multipart.MultipartFile;
import io.swagger.v3.oas.annotations.Parameters;
import java.util.Collections;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.type.MesDvMachineryTypeImportExcelVO;
import cn.iocoder.yudao.module.mes.controller.admin.dv.machinery.vo.type.MesDvMachineryTypeImportRespVO;
 
@Tag(name = "管理后台 - MES 设备类型")
@RestController
@RequestMapping("/mes/dv/machinery-type")
@Validated
public class MesDvMachineryTypeController {
 
    @Resource
    private MesDvMachineryTypeService machineryTypeService;
 
    @PostMapping("/create")
    @Operation(summary = "创建设备类型")
    @PreAuthorize("@ss.hasPermission('mes:dv-machinery-type:create')")
    public CommonResult<Long> createMachineryType(@Valid @RequestBody MesDvMachineryTypeSaveReqVO createReqVO) {
        return success(machineryTypeService.createMachineryType(createReqVO));
    }
 
    @PutMapping("/update")
    @Operation(summary = "更新设备类型")
    @PreAuthorize("@ss.hasPermission('mes:dv-machinery-type:update')")
    public CommonResult<Boolean> updateMachineryType(@Valid @RequestBody MesDvMachineryTypeSaveReqVO updateReqVO) {
        machineryTypeService.updateMachineryType(updateReqVO);
        return success(true);
    }
 
    @DeleteMapping("/delete")
    @Operation(summary = "删除设备类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mes:dv-machinery-type:delete')")
    public CommonResult<Boolean> deleteMachineryType(@RequestParam("id") Long id) {
        machineryTypeService.deleteMachineryType(id);
        return success(true);
    }
 
    @GetMapping("/get")
    @Operation(summary = "获得设备类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MesDvMachineryTypeRespVO> getMachineryType(@RequestParam("id") Long id) {
        MesDvMachineryTypeDO machineryType = machineryTypeService.getMachineryType(id);
        return success(BeanUtils.toBean(machineryType, MesDvMachineryTypeRespVO.class));
    }
 
    @GetMapping("/list")
    @Operation(summary = "获得设备类型列表")
    public CommonResult<List<MesDvMachineryTypeRespVO>> getMachineryTypeList(@Valid MesDvMachineryTypeListReqVO listReqVO) {
        List<MesDvMachineryTypeDO> list = machineryTypeService.getMachineryTypeList(listReqVO);
        return success(BeanUtils.toBean(list, MesDvMachineryTypeRespVO.class));
    }
 
    @GetMapping("/simple-list")
    @Operation(summary = "获得设备类型精简列表", description = "只包含被开启的类型,主要用于前端的下拉选项")
    public CommonResult<List<MesDvMachineryTypeRespVO>> getMachineryTypeSimpleList() {
        List<MesDvMachineryTypeDO> list = machineryTypeService.getMachineryTypeList(
                new MesDvMachineryTypeListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(convertList(list, machineryType -> new MesDvMachineryTypeRespVO()
                .setId(machineryType.getId()).setName(machineryType.getName()).setParentId(machineryType.getParentId())
                .setCode(machineryType.getCode()).setRemark(machineryType.getRemark())));
    }
 
 
    @GetMapping("/get-import-template")
    @Operation(summary = "获得设备类型导入模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<MesDvMachineryTypeImportExcelVO> list = Collections.singletonList(
                MesDvMachineryTypeImportExcelVO.builder()
                        .code("MT-001").name("数控机床").parentCode("")
                        .status(0).sort(0).remark("")
                        .build()
        );
        // 输出
        ExcelUtils.write(response, "设备类型导入模板.xls", "设备类型列表", MesDvMachineryTypeImportExcelVO.class, list);
    }
 
    @PostMapping("/import")
    @Operation(summary = "导入设备类型")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新,默认为 false", example = "true")
    })
    @PreAuthorize("@ss.hasPermission('mes:dv-machinery-type:import')")
    public CommonResult<MesDvMachineryTypeImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                           @RequestParam(value = "updateSupport", required = false,
                                                                   defaultValue = "false") Boolean updateSupport) throws Exception {
        List<MesDvMachineryTypeImportExcelVO> list = ExcelUtils.read(file, MesDvMachineryTypeImportExcelVO.class);
        return success(machineryTypeService.importMachineryTypeList(list, updateSupport));
    }
 
}