李林
2023-10-07 658d4927d468c47208fd012d9128b09249c07eff
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
/*
 *    Copyright (c) 2018-2025, ztt All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: ztt
 */
 
package com.chinaztt.mes.technology.controller;
 
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinaztt.mes.basic.util.DictUtils;
import com.chinaztt.mes.common.wrapper.QueryWrapperUtil;
import com.chinaztt.mes.technology.excel.*;
import com.chinaztt.mes.technology.dto.StructureDTO;
import com.chinaztt.mes.technology.entity.StructureComponent;
import com.chinaztt.mes.technology.service.StructureService;
import com.chinaztt.ztt.admin.api.entity.SysDictItem;
import com.chinaztt.ztt.admin.api.feign.RemoteDictService;
import com.chinaztt.ztt.common.core.util.R;
import com.chinaztt.ztt.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
 
 
/**
 * 产品结构
 *
 * @author zhangxy
 * @date 2020-08-19 10:48:07
 */
@RestController
@AllArgsConstructor
@RequestMapping("/structure")
@Api(value = "structure", tags = "产品结构管理")
public class StructureController {
 
    private final RemoteDictService remoteDictService;
    private final StructureService structureService;
    private final DictUtils dictUtils;
 
    /**
     * 横着导入的导入方式
     *
     * @param file
     * @return
     */
    @PostMapping("/uploadExt")
    public R uploadExt(@RequestParam("file") MultipartFile file) {
        ExcelReader excelReader = null;
        try {
            ExcelReaderBuilder excelReaderBuilder = EasyExcel.read(file.getInputStream());
            excelReader = excelReaderBuilder.build();
            List<ReadSheet> sheets = excelReader.excelExecutor().sheetList();
            List<ReadSheet> readSheetList = new ArrayList<>();
            for (ReadSheet sheet : sheets) {
                ReadSheet readSheet = EasyExcel.readSheet(sheet.getSheetName()).registerReadListener(new StructureUploadExtListener(structureService)).build();
                readSheetList.add(readSheet);
            }
            excelReader.read(readSheetList);
        } catch (IOException e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
                excelReader.finish();
            }
        }
        return R.ok();
    }
 
    /**
     * 竖着导入
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    public R simpleRead(@RequestParam("file") MultipartFile file) {
        ExcelReader excelReader = null;
        try {
            excelReader = EasyExcel.read(file.getInputStream(), StructureData.class, new StructureUploadListener(structureService, dictUtils)).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
        } catch (IOException e) {
            e.printStackTrace();
            return R.failed(e.getMessage());
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭,读的时候会创建临时文件,到时磁盘会崩的
                excelReader.finish();
            }
        }
        return R.ok();
    }
 
    /**
     * 根据零件号查询默认BOM
     *
     * @param partId
     * @return
     */
    @ApiOperation(value = "根据零件号查询默认BOM", notes = "根据零件号查询默认BOM")
    @GetMapping("/master/{partId}")
    public R getMasterTreeByPartId(@PathVariable("partId") Long partId) {
        return R.ok(structureService.getMasterTreeByPartId(partId));
    }
 
    /**
     * 结构类型
     *
     * @return
     */
    @ApiOperation(value = "结构类型", notes = "结构类型")
    @GetMapping("/type")
    public R<List<SysDictItem>> getStructurePage() {
        return remoteDictService.getDictByType("structure_type");
    }
 
 
    /**
     * 分页查询
     *
     * @param page      分页对象
     * @param structure 产品结构
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('technology_structure_view')")
    public R getStructurePage(Page page, StructureDTO structure) {
        return R.ok(structureService.getPage(page, QueryWrapperUtil.gen(structure)));
    }
 
 
    /**
     * 通过id查询产品结构
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('technology_structure_view')")
    public R getById(@PathVariable("id") Long id) {
        return R.ok(structureService.getFullById(id));
    }
 
 
    /**
     * 通过id查询完整树结构
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "通过id查询完整树结构", notes = "通过id查询完整树结构")
    @GetMapping("/tree/{id}")
    @PreAuthorize("@pms.hasPermission('technology_structure_view')")
    public R getTreeById(@PathVariable("id") Long id) {
        return R.ok(structureService.getTree(id));
    }
 
 
    /**
     * 新增产品结构
     *
     * @param structure 产品结构
     * @return R
     */
    @ApiOperation(value = "新增产品结构", notes = "新增产品结构")
    @SysLog("新增产品结构")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('technology_structure_add')")
    public R save(@RequestBody StructureDTO structure) {
        return structureService.fullSave(structure);
    }
 
    /**
     * 修改产品结构
     *
     * @param structure 产品结构
     * @return R
     */
    @ApiOperation(value = "修改产品结构", notes = "修改产品结构")
    @SysLog("修改产品结构")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('technology_structure_edit')")
    public R updateById(@RequestBody StructureDTO structure) {
        return structureService.fullUpdate(structure);
    }
 
    /**
     * 通过id删除产品结构
     *
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除产品结构", notes = "通过id删除产品结构")
    @SysLog("通过id删除产品结构")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('technology_structure_del')")
    public R removeById(@PathVariable Long id) {
        return R.ok(structureService.removeById(id));
    }
 
 
    /**
     * 批准、作废
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "更改状态", notes = "更改状态")
    @SysLog("更改状态")
    @PostMapping("/state/{event}")
    @PreAuthorize("@pms.hasPermission('technology_structure_edit')")
    public R changeState(@RequestBody List<Long> ids, @PathVariable String event) {
        return structureService.changeState(ids, event);
    }
 
    /**
     * 复制产品结构
     *
     * @param structure 复制产品结构
     * @return R
     */
    @ApiOperation(value = "复制产品结构", notes = "复制产品结构")
    @SysLog("复制产品结构")
    @PostMapping("/copy")
    public R copyStructureSave(@RequestBody List<StructureDTO> structure) {
        return structureService.copyStructureSave(structure);
    }
 
    /**
     * 新增组件
     *
     * @param component
     * @return
     */
    @PostMapping("/component")
    public R addComponent(@RequestBody StructureComponent component) {
        return structureService.addComponent(component);
    }
 
    /**
     * 更新组件
     *
     * @param component
     * @return
     */
    @PutMapping("/component")
    public R updateComponent(@RequestBody StructureComponent component) {
        return structureService.updateComponent(component);
    }
 
    /**
     * 新增组件
     *
     * @param structureComponents
     * @return
     */
    @PostMapping("/batchComponent")
    public R batchComponent(@RequestBody List<StructureComponent> structureComponents) {
        return structureService.batchComponent(structureComponents);
    }
 
    /**
     * 删除组件
     *
     * @param compId
     * @return
     */
    @DeleteMapping("/component/{compId}")
    public R removeComponent(@PathVariable Long compId) {
        return structureService.removeComponent(compId);
    }
 
    /**
     * 导入模板下载
     *
     * @return
     */
    @ApiOperation(value = "导入模板下载", notes = "导入模板下载")
    @GetMapping("/exportModel")
    public void exportModel(HttpServletResponse response) throws IOException {
        structureService.exportModel(response);
    }
 
    /**
     * 批量对接IFS
     *
     * @param ids
     * @return R
     */
    @ApiOperation(value = "批量对接IFS", notes = "批量对接IFS")
    @PostMapping("/structureIfsSync")
    public R structureIfsSync(@RequestBody List<Long> ids) {
        return structureService.structureIfsSync(ids);
    }
 
 
}