New file |
| | |
| | | package com.whyc.service; |
| | | |
| | | import com.whyc.dto.Response; |
| | | import com.whyc.mapper.ECRMapper; |
| | | import com.whyc.pojo.ECR; |
| | | import com.whyc.util.CommonUtil; |
| | | import com.whyc.util.DateUtil; |
| | | import org.apache.poi.openxml4j.exceptions.InvalidFormatException; |
| | | import org.apache.poi.ss.usermodel.Sheet; |
| | | import org.apache.poi.ss.usermodel.Workbook; |
| | | import org.apache.poi.ss.usermodel.WorkbookFactory; |
| | | import org.springframework.beans.BeanUtils; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.text.ParseException; |
| | | import java.util.Date; |
| | | import java.util.LinkedList; |
| | | import java.util.List; |
| | | |
| | | @Service |
| | | public class ECRService { |
| | | |
| | | @Resource |
| | | private ECRMapper mapper; |
| | | |
| | | public Response ecrImportByExcel(InputStream inputStream) throws IOException, InvalidFormatException, ParseException { |
| | | Workbook workbook = null; |
| | | workbook = WorkbookFactory.create(inputStream); |
| | | inputStream.close(); |
| | | //解析excel,获取相关数据 |
| | | Sheet sheet = workbook.getSheetAt(0); |
| | | List<ECR> ecrList = new LinkedList<>(); |
| | | ECR ecr = new ECR(); |
| | | String numberCellValue = sheet.getRow(1).getCell(5).getStringCellValue(); |
| | | String number = numberCellValue.substring(numberCellValue.indexOf("BG")); |
| | | |
| | | String codeCellValue = sheet.getRow(2).getCell(0).getStringCellValue(); |
| | | //变更料号:xxx |
| | | String codeStr = codeCellValue.substring(5).trim(); |
| | | String[] codeSplit; |
| | | if(codeStr == null || codeStr.equals("")){ |
| | | return new Response().set(1,false,"变更料号不能为空!"); |
| | | }else{ |
| | | codeSplit = codeStr.split("/"); |
| | | } |
| | | |
| | | String modelCellValue = sheet.getRow(2).getCell(5).getStringCellValue(); |
| | | //变更名称/型号:xxx |
| | | String modelStr = modelCellValue.substring(8).trim(); |
| | | String[] modelSplit; |
| | | if(modelStr == null || modelStr.equals("")){ |
| | | return new Response().set(1,false,"变更型号不能为空!"); |
| | | }else{ |
| | | modelSplit = modelStr.split("/"); |
| | | } |
| | | |
| | | String proposeDateCellValue = sheet.getRow(2).getCell(14).getStringCellValue(); |
| | | //申请日期:xxx |
| | | String proposeDateStr = proposeDateCellValue.substring(5).trim(); |
| | | Date proposeDate = DateUtil.YYYY_MM_DD_UNION.parse(proposeDateStr); |
| | | |
| | | String proposerCellValue = sheet.getRow(2).getCell(22).getStringCellValue(); |
| | | //申请人:xxx |
| | | String proposer = proposerCellValue.substring(4).trim(); |
| | | |
| | | String changeDescriptionCellValue = sheet.getRow(4).getCell(0).getStringCellValue(); |
| | | |
| | | //申请变更归类:index从16-22,被选中的标识为■D.品质改善 |
| | | StringBuilder changeTypeBuilder = new StringBuilder(); |
| | | String changeTypeA = sheet.getRow(16).getCell(0).getStringCellValue(); |
| | | String changeTypeB = sheet.getRow(17).getCell(0).getStringCellValue(); |
| | | String changeTypeC = sheet.getRow(18).getCell(0).getStringCellValue(); |
| | | String changeTypeD = sheet.getRow(19).getCell(0).getStringCellValue(); |
| | | String changeTypeE = sheet.getRow(20).getCell(0).getStringCellValue(); |
| | | String changeTypeF = sheet.getRow(21).getCell(0).getStringCellValue(); |
| | | String changeTypeG = sheet.getRow(22).getCell(0).getStringCellValue(); |
| | | if(changeTypeA.contains("■")){ |
| | | changeTypeBuilder.append(changeTypeA.substring(3)); |
| | | } |
| | | if(changeTypeB.contains("■")){ |
| | | if (changeTypeBuilder.length() == 0) { |
| | | changeTypeBuilder.append(changeTypeB.substring(3)); |
| | | } else { |
| | | changeTypeBuilder.append(",").append(changeTypeB.substring(3)); |
| | | } |
| | | } |
| | | if(changeTypeC.contains("■")){ |
| | | if (changeTypeBuilder.length() == 0) { |
| | | changeTypeBuilder.append(changeTypeC.substring(3)); |
| | | } else { |
| | | changeTypeBuilder.append(",").append(changeTypeC.substring(3)); |
| | | } |
| | | } |
| | | if(changeTypeD.contains("■")){ |
| | | if (changeTypeBuilder.length() == 0) { |
| | | changeTypeBuilder.append(changeTypeD.substring(3)); |
| | | } |
| | | else { |
| | | changeTypeBuilder.append(",").append(changeTypeD.substring(3)); |
| | | } |
| | | } |
| | | if(changeTypeE.contains("■")){ |
| | | if (changeTypeBuilder.length() == 0) { |
| | | changeTypeBuilder.append(changeTypeE.substring(3)); |
| | | } |
| | | else { |
| | | changeTypeBuilder.append(",").append(changeTypeE.substring(3)); |
| | | } |
| | | } |
| | | if(changeTypeF.contains("■")){ |
| | | if (changeTypeBuilder.length() == 0) { |
| | | changeTypeBuilder.append(changeTypeF.substring(3)); |
| | | } |
| | | else { |
| | | changeTypeBuilder.append(",").append(changeTypeF.substring(3)); |
| | | } |
| | | } |
| | | if(changeTypeG.contains("■")){ |
| | | if (changeTypeBuilder.length() == 0) { |
| | | changeTypeBuilder.append(changeTypeG.substring(3)); |
| | | } |
| | | else { |
| | | changeTypeBuilder.append(",").append(changeTypeG.substring(3)); |
| | | } |
| | | } |
| | | String changeType = changeTypeBuilder.toString(); |
| | | String changeDescription = changeDescriptionCellValue + "\n"+changeType; |
| | | |
| | | //处理方式,处理方式: |
| | | //□1.报废. □2. 用完为止 □3. 重工(变) |
| | | //■4.与库存品无关. □5. 转 成品使用 |
| | | //□6.其它处理方式: |
| | | String solutionCellValue = sheet.getRow(23).getCell(0).getStringCellValue(); |
| | | String solution = null; |
| | | if(solutionCellValue.contains("■")){ |
| | | String solutionStr = solutionCellValue.split("■")[1]; |
| | | solution = solutionStr.split("\\.")[1]; |
| | | } |
| | | |
| | | //先将共同属性赋值给基础对象 |
| | | ecr.setNumber(number); |
| | | ecr.setProposeDate(proposeDate); |
| | | ecr.setProposer(proposer); |
| | | ecr.setChangeDescription(changeDescription); |
| | | ecr.setSolution(solution); |
| | | ecr.setCreateTime(new Date()); |
| | | //赋值给具体对象 |
| | | for (int i = 0; i < codeSplit.length; i++) { |
| | | ECR ecrTemp = new ECR(); |
| | | BeanUtils.copyProperties(ecr,ecrTemp); |
| | | ecrTemp.setCode(CommonUtil.codeAutoFill(codeSplit[i])); |
| | | ecrTemp.setModel(modelSplit[i]); |
| | | ecrList.add(ecrTemp); |
| | | } |
| | | mapper.insertBatchSomeColumn(ecrList); |
| | | |
| | | return new Response().set(1,true,"导入完成"); |
| | | } |
| | | } |