Commit 9fb61cd3 authored by wujipeng's avatar wujipeng
Browse files

导入修复数据

parent e72ab383
package com.example.pipelinenetwork.controller;
import com.example.pipelinenetwork.result.Result;
import com.example.pipelinenetwork.service.RepairService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.IOException;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/23 14:35
* @Version: 1.0
*/
@RestController
@RequestMapping("/repair")
public class RepairController {
@Resource
private RepairService repairService;
@PostMapping("/import")
public Result<Boolean> importDefectData(@RequestBody MultipartFile file) throws IOException {
return Result.success(repairService.importDefectData(file));
}
}
package com.example.pipelinenetwork.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.pipelinenetwork.pojo.entity.RepairEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/23 14:33
* @Version: 1.0
*/
@Mapper
public interface RepairMapper extends BaseMapper<RepairEntity> {
}
...@@ -14,22 +14,22 @@ public class DefectEntity { ...@@ -14,22 +14,22 @@ public class DefectEntity {
private Long id; private Long id;
/** /**
* 缺陷类型 * 类型
*/ */
private String type; private String type;
/** /**
* 缺陷名称 * 缺称
*/ */
private String name; private String name;
/** /**
* 缺陷等级 * 等级
*/ */
private String level; private String level;
/** /**
* 缺陷数量 * 数量
*/ */
private Integer num; private Integer num;
......
package com.example.pipelinenetwork.pojo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/23 14:32
* @Version: 1.0
*/
@Data
@TableName("repair")
public class RepairEntity {
@TableId(type = IdType.AUTO)
private Long id;
/**
* 类型
*/
private String type;
/**
* 缺称
*/
private String name;
/**
* 等级
*/
private String level;
/**
* 数量
*/
private Integer num;
@TableField(value = "rel_id")
private String relId;
}
package com.example.pipelinenetwork.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.pipelinenetwork.pojo.entity.RepairEntity;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/23 14:34
* @Version: 1.0
*/
public interface RepairService extends IService<RepairEntity> {
Boolean importDefectData(MultipartFile file) throws IOException;
}
...@@ -92,20 +92,4 @@ public class DefectServiceImpl extends ServiceImpl<DefectMapper, DefectEntity> i ...@@ -92,20 +92,4 @@ public class DefectServiceImpl extends ServiceImpl<DefectMapper, DefectEntity> i
return null; return null;
} }
} }
/**
* 检查行是否为空
*/
private boolean isRowEmpty(Row row) {
if (row == null) {
return true;
}
for (int c = row.getFirstCellNum(); c < row.getLastCellNum(); c++) {
Cell cell = row.getCell(c);
if (cell != null && cell.getCellType() != CellType.BLANK) {
return false;
}
}
return true;
}
} }
package com.example.pipelinenetwork.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.pipelinenetwork.mapper.RepairMapper;
import com.example.pipelinenetwork.pojo.entity.RepairEntity;
import com.example.pipelinenetwork.service.RepairService;
import org.apache.poi.ss.usermodel.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: wujipeng
* @Description: TODO
* @Date: 2026/7/23 14:34
* @Version: 1.0
*/
@Service
public class RepairServiceImpl extends ServiceImpl<RepairMapper, RepairEntity> implements RepairService {
@Override
@Transactional
public Boolean importDefectData(MultipartFile file) throws IOException {
List<RepairEntity> repairEntities = new ArrayList<>();
try (InputStream inputStream = file.getInputStream();
Workbook workbook = WorkbookFactory.create(inputStream)) {
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i <= sheet.getLastRowNum(); i++) {
RepairEntity repair = new RepairEntity();
repair.setType(getCellStringValue(sheet.getRow(i).getCell(0)));
repair.setName(getCellStringValue(sheet.getRow(i).getCell(1)));
repair.setLevel(getCellStringValue(sheet.getRow(i).getCell(2)));
repair.setNum(getCellIntValue(sheet.getRow(i).getCell(3)));
repair.setRelId(getCellStringValue(sheet.getRow(i).getCell(4)));
repairEntities.add(repair);
}
return saveBatch(repairEntities);
}
}
/**
* 安全获取字符串单元格值
*/
private String getCellStringValue(Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case STRING:
return cell.getStringCellValue().trim();
case NUMERIC:
// 处理数字转字符串的情况
double value = cell.getNumericCellValue();
if (value == Math.floor(value) && !Double.isInfinite(value)) {
return String.valueOf((long) value);
}
return String.valueOf(value);
case BOOLEAN:
return String.valueOf(cell.getBooleanCellValue());
case FORMULA:
try {
return cell.getStringCellValue();
} catch (IllegalStateException e) {
return String.valueOf(cell.getNumericCellValue());
}
default:
return null;
}
}
/**
* 安全获取整数单元格值
*/
private Integer getCellIntValue(Cell cell) {
if (cell == null) {
return null;
}
switch (cell.getCellType()) {
case NUMERIC:
return (int) cell.getNumericCellValue();
case STRING:
try {
return Integer.parseInt(cell.getStringCellValue().trim());
} catch (NumberFormatException e) {
return null;
}
default:
return null;
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment