配件EXCEL导入功能

This commit is contained in:
76164 2024-07-29 09:15:15 +08:00
parent 2e8690f37f
commit 6b48590986
3 changed files with 212 additions and 0 deletions

View File

@ -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);
}

View File

@ -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<String, Map<String, List<String>>> 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<Row> iterator = datatypeSheet.iterator();
String fourthLevel = "";
String fifthLevel ="";
Long id = null;
Map<String, Map<String, List<String>>> 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<String, Map<String, List<String>>> 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<String, List<String>> secondLevelMap = entry1.getValue(); // 获取对应于第一层级键的内层Map
// 遍历内层Map
for (Map.Entry<String, List<String>> 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 "";
}
}
}

View File

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bonus.sgzb.base.mapper.ExcelMapper">
<insert id="insertPaName" keyProperty="paId" useGeneratedKeys="true">
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>
<insert id="insertPaTwoName" keyProperty="paId" useGeneratedKeys="true">
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>
<insert id="insertPaThreeName">
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()))
</insert>
<select id="selectPaName" resultType="java.lang.String">
SELECT pa_id FROM ma_part_type WHERE pa_name = #{firstLevel}
</select>
</mapper>