/* * 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 sheets = excelReader.excelExecutor().sheetList(); List 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> 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 ids, @PathVariable String event) { return structureService.changeState(ids, event); } /** * 复制产品结构 * * @param structure 复制产品结构 * @return R */ @ApiOperation(value = "复制产品结构", notes = "复制产品结构") @SysLog("复制产品结构") @PostMapping("/copy") public R copyStructureSave(@RequestBody List 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 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 ids) { return structureService.structureIfsSync(ids); } }