package com.whyc.controller; import com.whyc.dto.Response; import com.whyc.pojo.DocUser; import com.whyc.pojo.Material; import com.whyc.pojo.MaterialApproving; import com.whyc.service.MaterialService; import com.whyc.util.ActionUtil; import com.whyc.util.CommonUtil; import com.whyc.util.FileUtil; import com.whyc.util.Zip4jUtil; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; import org.apache.poi.ss.usermodel.*; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.LinkedList; import java.util.List; @Api(tags = "物料管理") @RestController @RequestMapping("material") public class MaterialController { @Autowired private MaterialService service; /*@ApiOperation(value = "查询所有的散装件信息分页(带上关联的子件信息,status状态不做处理))") @GetMapping("getAllMaterial") public Response getAllMaterial(@RequestParam(required = false) String category,@RequestParam(required = false) String subCode,@RequestParam(required = false) String subName, @RequestParam(required = false) String subModel,@RequestParam int pageCurr, @RequestParam int pageSize){ return service.getAllMaterial(category,subCode,subName,subModel,pageCurr,pageSize); }*/ /* @ApiOperation(tags = "产品管理",value = "查询所有的散装件信息不分页(不包含被最新版本关联的散装件,status=1可用)") @GetMapping("getMaterialWithoutSub") public Response getMaterialWithoutSub(@RequestParam String parentModel, @RequestParam String subName, @RequestParam int version){ return service.getMaterialWithoutSub(parentModel,subName,version); }*/ /*@ApiOperation(value = "删除散装件(将散装件标识为不可用)") @GetMapping("deleteMaterial") public Response deleteMaterial(@RequestParam int id){ return service.deleteMaterial(id); }*/ @ApiOperation(value = "查询所有物料(分页,模糊查询)",notes = "8.17修改后使用") @GetMapping("getMaterialLimit") public Response getMaterialLimit(@RequestParam(required = false) String subCode,@RequestParam(required = false) String subName, @RequestParam(required = false) String subModel,@RequestParam int pageCurr, @RequestParam int pageSize){ return service.getMaterialLimit(subCode,subName,subModel,pageCurr,pageSize); } @PostMapping @ApiOperation(value = "新增",notes = "解析返回的绝对路径,需要回传到字段fileUrl") public Response add(@RequestBody List materialList){ return service.add(materialList); } @PostMapping("zipParse") @ApiOperation("zip解析") public Response zipParse(MultipartFile file) throws IOException, InvalidFormatException { List list = new LinkedList<>(); Response response = new Response(); //检查是否为zip boolean isZip = Zip4jUtil.checkZipFileParam(file); if(!isZip){ return response.set(1,false,"上传的文件格式不是zip"); } //zip存储路径:doc_file/material_submit/${username}/{dateFormat}/${timeStamp} String rootFile = CommonUtil.getRootFile(); DocUser user = ActionUtil.getUser(); String dateFormat = new SimpleDateFormat("YYYY-MM").format(new Date()); long timeStamp = System.currentTimeMillis(); String filePath = rootFile + File.separator + "material_submit" + File.separator + user.getName() + File.separator + dateFormat+ File.separator + timeStamp; File parentFile = new File(filePath); String originalFilename = file.getOriginalFilename(); File zipFile = new File(filePath+File.separator+ originalFilename); if(!zipFile.exists()){ zipFile.mkdirs(); } file.transferTo(zipFile); //解压文件夹 Zip4jUtil.unPackZip(zipFile,null,filePath); //遍历解压后的文件夹路径,解析excel System.out.println(filePath); List fileList = new ArrayList<>(); fileList = FileUtil.getStaticFilePath(parentFile,fileList); for (int i = 0; i < fileList.size(); i++) { String fileTempUrl = fileList.get(i); //查询到xls数据 if(fileTempUrl.contains(".xls")){ File fileTemp = new File(fileTempUrl); Workbook workbook = null; workbook = WorkbookFactory.create(fileTemp); List allPictures = workbook.getAllPictures(); //取第一个sheet表 Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); //取第三行,并以第三行开始 Row rowTemp = sheet.getRow(1); short lastCellNum = rowTemp.getLastCellNum(); //先单独获取产品型号和版本号/分类 Row row = sheet.getRow(1); String type = row.getCell(1).getStringCellValue(); Row row2 = sheet.getRow(2); String productModel = row2.getCell(5).getStringCellValue(); Row row3 = sheet.getRow(3); String productVersion = row3.getCell(5).getStringCellValue(); for (int l = 8; l < lastRowNum-1; l++) { Cell cellTemp = sheet.getRow(l).getCell(1); cellTemp.setCellType(Cell.CELL_TYPE_STRING); if(cellTemp.getStringCellValue().equals("")){ break; } MaterialApproving approving = new MaterialApproving(); approving.setCreateDate(new Date()); for (int m = 2; m < lastCellNum; m++) { row = sheet.getRow(l); Cell cell = row.getCell(m); String cellValue = null; int cellValueInt = 0; Double cellValueDouble = null; if(m == 3){ //cellValueDouble = cell.getNumericCellValue(); //DecimalFormat decimalFormat = new DecimalFormat("0"); //cellValue = decimalFormat.format(cellValueDouble); cell.setCellType(Cell.CELL_TYPE_STRING); cellValue = cell.getStringCellValue(); } else if(m ==7){ cellValueDouble = cell.getNumericCellValue(); } else { cellValue = cell.getStringCellValue(); } switch (m){ case 2:{approving.setCategory(cellValue);}break; case 3:{approving.setSubCode(cellValue);}break; case 4:{approving.setSubName(cellValue);}break; case 5:{approving.setSubModel(cellValue);}break; case 6:{approving.setUnit(cellValue);}break; case 7:{approving.setQuantity(cellValueDouble.intValue());}break; case 8:{approving.setProducer(cellValue);}break; case 9:{approving.setNotes(cellValue);}break; } } list.add(approving); } } } for (MaterialApproving MaterialApproving : list) { if(MaterialApproving.getSubName()==null ||MaterialApproving.getSubName().equals("")){ return response.set(1,false,"拒绝解析,excel文件中记录包含名称为空的记录"); } } return response.setII(1,true,list,filePath); } }