package com.irm.jd.service;
import com.irm.jd.entity.Sit;
import com.irm.jd.mapper.SitMapper;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
@Service
public class SitService {
@Autowired
private SitMapper sitMapper;
@Async
public void upload(MultipartFile file) throws IOException {
// 执行文件上传
String originalFilename = file.getOriginalFilename();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
UUID uuid = UUID.randomUUID();
String fileName = uuid + fileExtension;
String dir = "D:/uploads/" + fileName;
File willUploadFile = new File(dir);
file.transferTo(willUploadFile);
try {
FileInputStream fileInputStream = new FileInputStream(willUploadFile);
XSSFWorkbook xssfWorkbook = new XSSFWorkbook(fileInputStream);
if (xssfWorkbook != null) {
XSSFSheet sheet = xssfWorkbook.getSheetAt(0);
List<Sit> sits = new ArrayList<>();
int count = 0;
for (int i = 0; i < sheet.getLastRowNum(); i++) {
XSSFRow row = sheet.getRow(i);
if (row != null) {
Sit sit = new Sit();
for (int k = 0; k < row.getLastCellNum(); k++) {
if (row.getCell(0) != null) {
if (!row.getCell(0).toString().equals("null") && !row.getCell(0).toString().equals("")) {
sit.setGongwei(row.getCell(0).toString());
}
}
if (row.getCell(1) != null) {
if (!row.getCell(1).toString().equals("null") && !row.getCell(1).toString().equals("")) {
sit.setFloor(row.getCell(1).toString());
}
}
if (row.getCell(2) != null) {
if (!row.getCell(2).toString().equals("null") && !row.getCell(2).toString().equals("")) {
sit.setTelephone_ip(row.getCell(2).toString());
}
}
if (row.getCell(3) != null) {
if (!row.getCell(3).toString().equals("null") && !row.getCell(3).toString().equals("")) {
sit.setComputer_ip(row.getCell(3).toString());
}
}
if (row.getCell(4) != null) {
if (!row.getCell(4).toString().equals("null") && !row.getCell(4).toString().equals("")) {
sit.setArea(row.getCell(4).toString());
}
}
}
sits.add(sit);
} else {
System.out.println("null");
}
}
sitMapper.insertSite(sits);
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}