From 6b485909860d4dc54e3d1b6539c3e19d63629900 Mon Sep 17 00:00:00 2001 From: 76164 <761646706@qq.com> Date: Mon, 29 Jul 2024 09:15:15 +0800 Subject: [PATCH] =?UTF-8?q?=E9=85=8D=E4=BB=B6EXCEL=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bonus/sgzb/base/mapper/ExcelMapper.java | 19 ++ .../base/service/impl/ExcelServiceImpl.java | 164 ++++++++++++++++++ .../resources/mapper/base/ExcelMapper.xml | 29 ++++ 3 files changed, 212 insertions(+) create mode 100644 sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/mapper/ExcelMapper.java create mode 100644 sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java create mode 100644 sgzb-modules/sgzb-base/src/main/resources/mapper/base/ExcelMapper.xml diff --git a/sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/mapper/ExcelMapper.java b/sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/mapper/ExcelMapper.java new file mode 100644 index 00000000..1d127f02 --- /dev/null +++ b/sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/mapper/ExcelMapper.java @@ -0,0 +1,19 @@ +package com.bonus.sgzb.base.mapper; + +import com.bonus.sgzb.base.domain.MaPartType; +import org.apache.ibatis.annotations.Mapper; +import org.apache.ibatis.annotations.Param; + +@Mapper +public interface ExcelMapper { + + + String selectPaName(@Param("firstLevel") String firstLevel); + + + int insertPaName(MaPartType firstLevel); + + void insertPaTwoName(MaPartType maPartType); + + void insertPaThreeName(MaPartType maPartType); +} diff --git a/sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java b/sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java new file mode 100644 index 00000000..764cad59 --- /dev/null +++ b/sgzb-modules/sgzb-base/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java @@ -0,0 +1,164 @@ +package com.bonus.sgzb.base.service.impl;// ExcelServiceImpl.java +import com.bonus.sgzb.base.domain.MaPartType; +import com.bonus.sgzb.base.mapper.ExcelMapper; +import com.bonus.sgzb.base.service.ExcelService; +import com.bonus.sgzb.common.security.utils.SecurityUtils; +import org.apache.poi.ss.usermodel.*; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; +import org.apache.xmlbeans.impl.xb.xsdschema.Public; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.util.*; + +@Service +public class ExcelServiceImpl implements ExcelService { + + @Autowired + ExcelMapper excelMapper; + + + /* + * 将读取的excel文件转为 + * */ + @Override + public Map>> readExcelFile(String filePath) { + + FileInputStream excelFile = null; + Workbook workbook = null; + try { + excelFile = new FileInputStream(new File(filePath)); + workbook = new XSSFWorkbook(excelFile); + Sheet datatypeSheet = workbook.getSheetAt(0); + Iterator iterator = datatypeSheet.iterator(); + String fourthLevel = ""; + String fifthLevel =""; + Long id = null; + Map>> level3Relation = new HashMap<>(); + + while (iterator.hasNext()) { + Row currentRow = iterator.next(); + if (currentRow.getRowNum() == 0) { + // 跳过标题行 + continue; + } + + Cell firstLevelCell = currentRow.getCell(1); + Cell secondLevelCell = currentRow.getCell(2); + Cell thirdLevelCell = currentRow.getCell(3); + Cell fourthLevelCell = currentRow.getCell(4); + Cell fifthLevelCell = currentRow.getCell(5); + + + String firstLevel = getCellValueAsString(firstLevelCell); + String secondLevel = getCellValueAsString(secondLevelCell); + String thirdLevel = getCellValueAsString(thirdLevelCell); + fourthLevel = getCellValueAsString(fourthLevelCell); + fifthLevel = getCellValueAsString(fifthLevelCell); + + level3Relation.computeIfAbsent(firstLevel, k -> new HashMap<>()) + .computeIfAbsent(secondLevel, k -> new ArrayList<>()) + .add(thirdLevel); + } + + // 遍历外层Map + for (Map.Entry>> entry1 : level3Relation.entrySet()) { + String firstLevel = entry1.getKey(); // 获取第一层级的键 + String parentId = excelMapper.selectPaName(firstLevel); + + //产生返回值并且进行判断是否是pa_name列已经有的,并且返回对应的pa_id + if(parentId == null || parentId == ""){ + MaPartType maPartType = new MaPartType(); + maPartType.setPaName(firstLevel); + maPartType.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString()); + excelMapper.insertPaName(maPartType);//如果第一层在数据库中查询不到则插入数据,并且读取到parent_id + id = maPartType.getPaId(); + + } +// + + + + Map> secondLevelMap = entry1.getValue(); // 获取对应于第一层级键的内层Map + + // 遍历内层Map + for (Map.Entry> entry2 : secondLevelMap.entrySet()) { + String secondLevel = entry2.getKey(); // 获取第二层级的键 + String parentIdTwo = excelMapper.selectPaName(secondLevel); + + //产生返回值并且进行判断是否是pa_name列已经有的,并且返回对应的pa_id + if(parentIdTwo == null || parentIdTwo == ""){ + MaPartType maPartType = new MaPartType(); + maPartType.setPaName(secondLevel); + maPartType.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString()); + maPartType.setParentId(id); + excelMapper.insertPaTwoName(maPartType);//第二层在数据库中查询不到则插入数据,则插入时候设置关联关系 + id = maPartType.getPaId(); + } + + + + //从数据库中查询 + String thirdLevel = entry2.getValue().get(0); // 获取对应于第二层级键的值,即第三层级的列表 + + + String parentThreeId = excelMapper.selectPaName(thirdLevel); + if(parentThreeId == null || parentThreeId == ""){ + MaPartType maPartType = new MaPartType(); + maPartType.setPaName(thirdLevel); + maPartType.setCreateBy(SecurityUtils.getLoginUser().getUserid().toString()); + maPartType.setUnitId(fourthLevel); + maPartType.setBuyPrice(fifthLevel); + maPartType.setParentId(id); + excelMapper.insertPaThreeName(maPartType);//第二层在数据库中查询不到则插入数据,则插入时候设置关联关系 + } + } + } + + return level3Relation; + } catch (IOException e) { + e.printStackTrace(); + throw new RuntimeException("Error reading Excel file", e); + } finally { + if (workbook != null) { + try { + workbook.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + if (excelFile != null) { + try { + excelFile.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + } + + private String getCellValueAsString(Cell cell) { + if (cell == null) return ""; + switch (cell.getCellType()) { + case STRING: + return cell.getStringCellValue(); + case NUMERIC: + return String.valueOf(cell.getNumericCellValue()); + case BOOLEAN: + return String.valueOf(cell.getBooleanCellValue()); + case FORMULA: + return cell.getCellFormula(); + case BLANK: + return ""; + default: + return ""; + } + } + + + +} diff --git a/sgzb-modules/sgzb-base/src/main/resources/mapper/base/ExcelMapper.xml b/sgzb-modules/sgzb-base/src/main/resources/mapper/base/ExcelMapper.xml new file mode 100644 index 00000000..645b3d3b --- /dev/null +++ b/sgzb-modules/sgzb-base/src/main/resources/mapper/base/ExcelMapper.xml @@ -0,0 +1,29 @@ + + + + + insert into ma_part_type + (pa_name, status,level,del_flag,create_by,create_time,year) + VALUES + (#{paName}, 0 , 1, 0, #{createBy}, now(),year(now())) + + + insert into ma_part_type + (pa_name, parent_id, status,level,del_flag,create_by,create_time,year) + VALUES + (#{paName}, #{parentId},0 , 2, 0, #{createBy}, now(),year(now())) + + + insert into ma_part_type + (pa_name, parent_id, status,level,del_flag,unit_id,buy_price,create_by,create_time,year) + VALUES + (#{paName}, #{parentId},0 , 3, 0, #{unitId}, #{buyPrice}, #{createBy}, now(),year(now())) + + + + +