开发OTA备案工具-导入导出

This commit is contained in:
高嵩
2023-03-27 00:46:37 +08:00
parent 5b71d6d19d
commit 74b8efa197
8 changed files with 297 additions and 94 deletions
@@ -443,7 +443,7 @@ public class SysDictServiceImpl extends ServiceImpl<SysDictMapper, SysDict> impl
@Override
public String queryDictItemByValue(String code, String itemValue, String cut) {
String dictItemValue= sysDictMapper.queryDictTextByKey(code,itemValue);
String dictItemValue= sysDictMapper.queryDictKeyByText(code,itemValue);
return dictItemValue;
}
@@ -18,6 +18,7 @@ import com.jero.common.system.base.controller.JeroController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
@@ -182,4 +183,25 @@ public class OtaBaCxJbxxController extends JeroController<OtaBaCxJbxx, IOtaBaCxJ
return super.importExcel(request, response, OtaBaCxJbxx.class);
}
@ApiOperation(value = "车型备案-车型基本信息-模板下载")
@GetMapping(value = "/exportTemplate")
// @RequiresPermissions("otaBaCx:exportTemplate")
public void exportTemplate(HttpServletResponse response, HttpServletRequest request) throws Exception {
this.otaBaCxJbxxService.exportTemplate(response, request);
}
@ApiOperation(value = "车型备案-车型基本信息-数据导入")
@PostMapping(value = "/importData")
// @RequiresPermissions("otaBaCx:importData")
public Result<?> importData(@RequestParam(value = "file", required = false) MultipartFile file,
@RequestParam(value = "cut", required = false) String cut) throws Exception {
try {
this.otaBaCxJbxxService.importData(file, cut);
} catch (Exception e) {
e.printStackTrace();
return Result.error(e.getMessage());
}
return Result.OK("导入成功");
}
}
@@ -189,7 +189,6 @@ public class OtaBaCxListController extends JeroController<OtaBaCxList, IOtaBaCxL
return Result.OK("备案维护成功!");
}
@AutoLog(value = "车型及功能备案列表-模板下载")
@ApiOperation(value = "车型及功能备案列表-模板下载")
@GetMapping(value = "/exportTemplate")
// @RequiresPermissions("otaBaCx:exportTemplate")
@@ -197,7 +196,6 @@ public class OtaBaCxListController extends JeroController<OtaBaCxList, IOtaBaCxL
this.otaBaCxListService.exportTemplate(response, request);
}
@AutoLog(value = "车型及功能备案列表-数据导入")
@ApiOperation(value = "车型及功能备案列表-数据导入")
@PostMapping(value = "/importData")
// @RequiresPermissions("otaBaCx:importData")
@@ -212,7 +210,6 @@ public class OtaBaCxListController extends JeroController<OtaBaCxList, IOtaBaCxL
return Result.OK("导入成功");
}
@AutoLog(value = "车型及功能备案列表-数据导出")
@ApiOperation(value = "车型及功能备案列表-数据导出")
@GetMapping(value = "/exportData")
// @RequiresPermissions("otaBaCx:exportTemplate")
@@ -2,6 +2,10 @@ package com.jero.modules.ota.service;
import com.jero.modules.ota.entity.OtaBaCxJbxx;
import com.baomidou.mybatisplus.extension.service.IService;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
@@ -60,4 +64,8 @@ public interface IOtaBaCxJbxxService extends IService<OtaBaCxJbxx> {
List<OtaBaCxJbxx> queryList();
OtaBaCxJbxx queryByOtaId(String otaId, String otaIdT);
void exportTemplate(HttpServletResponse response, HttpServletRequest request);
void importData(MultipartFile file, String cut);
}
@@ -1,5 +1,6 @@
package com.jero.modules.ota.service.impl;
import com.aliyuncs.utils.IOUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.modules.ota.entity.OtaBaCxJbxx;
@@ -11,12 +12,21 @@ import com.jero.modules.ota.service.IOtaBaCxJbxxService;
import com.jero.modules.ota.service.IOtaBaCxListService;
import com.jero.modules.ota.service.IOtaBaCxTxjlService;
import com.jero.modules.ota.utils.ComparisonUtil;
import com.jero.modules.ota.utils.ImportFileUtil;
import com.jero.modules.system.service.ISysDictService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@@ -39,6 +49,12 @@ public class OtaBaCxJbxxServiceImpl extends ServiceImpl<OtaBaCxJbxxMapper, OtaBa
@Autowired
private ISysDictService sysDictService;
@Value(value = "${ota.templatePath}")
private String templatePath;
@Value(value = "${ota.uploadPath}")
private String uploadPath;
/**
* 保存
*
@@ -143,4 +159,72 @@ public class OtaBaCxJbxxServiceImpl extends ServiceImpl<OtaBaCxJbxxMapper, OtaBa
queryWrapper.eq(OtaBaCxJbxx::getOtaId, otaId);
return this.getOne(queryWrapper, false);
}
@Override
public void exportTemplate(HttpServletResponse response, HttpServletRequest request) {
ImportFileUtil.exportTemplate(response, templatePath + "车型基本信息.xlsx");
}
@Override
public void importData(MultipartFile file, String cut) {
File upLoadFile = ImportFileUtil.importFile(file, cut, uploadPath, "xlsx");
OtaBaCxJbxx jbxx = parseFile(upLoadFile, cut);
add(jbxx);
}
public static String getCellValue(Cell cell, Workbook workbook) {
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
DataFormatter formatter = new DataFormatter();
//单元格不设置数字格式
cell.setCellStyle(null);
return formatter.formatCellValue(cell, evaluator);
}
private OtaBaCxJbxx parseFile(File file, String cut) {
OtaBaCxJbxx jbxx = new OtaBaCxJbxx();
try {
Workbook wb = ImportFileUtil.readExcel(file);
Sheet s = wb.getSheetAt(0);
Row row = s.getRow(1);
Cell cell = row.getCell(1);
jbxx.setQymc(getCellValue(cell, wb));
row = s.getRow(2);
cell = row.getCell(1);
jbxx.setDjbh(getCellValue(cell, wb));
row = s.getRow(3);
cell = row.getCell(1);
jbxx.setCplb(getCellValue(cell, wb));
row = s.getRow(4);
cell = row.getCell(1);
jbxx.setGgpc(getCellValue(cell, wb));
row = s.getRow(5);
cell = row.getCell(1);
jbxx.setCpsb(getCellValue(cell, wb));
row = s.getRow(6);
cell = row.getCell(1);
jbxx.setCpmc(getCellValue(cell, wb));
row = s.getRow(7);
cell = row.getCell(1);
jbxx.setCpxh(getCellValue(cell, wb));
row = s.getRow(8);
cell = row.getCell(1);
jbxx.setDllx1(getCellValue(cell, wb));
row = s.getRow(9);
cell = row.getCell(1);
jbxx.setDllx2(getCellValue(cell, wb));
row = s.getRow(10);
cell = row.getCell(1);
jbxx.setTxzd(sysDictService.queryDictItemByValue("communication_terminal", getCellValue(cell, wb), cut));
row = s.getRow(11);
cell = row.getCell(1);
jbxx.setCdotasj(getCellValue(cell, wb));
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
}
return jbxx;
}
}
@@ -9,6 +9,7 @@ import com.jero.modules.ota.entity.OtaBaCxLsjl;
import com.jero.modules.ota.mapper.OtaBaCxListMapper;
import com.jero.modules.ota.service.IOtaBaCxListService;
import com.jero.modules.ota.service.IOtaBaCxLsjlService;
import com.jero.modules.ota.utils.ImportFileUtil;
import com.jero.modules.split.common.FileUnZip;
import com.jero.modules.system.util.StringUtils;
import org.apache.commons.io.FileUtils;
@@ -44,8 +45,11 @@ public class OtaBaCxListServiceImpl extends ServiceImpl<OtaBaCxListMapper, OtaBa
private IOtaBaCxLsjlService otaBaCxLsjlService;
@Value(value = "${jero.path.upload}")
private String uploadpath;
@Value(value = "${ota.templatePath}")
private String templatePath;
@Value(value = "${ota.uploadPath}")
private String uploadPath;
/**
* 保存
@@ -146,99 +150,13 @@ public class OtaBaCxListServiceImpl extends ServiceImpl<OtaBaCxListMapper, OtaBa
@Override
public void exportTemplate(HttpServletResponse response, HttpServletRequest request) {
File template = new File("D://opt//ota//车型及功能备案.zip");
BufferedOutputStream os = null;
InputStream in = null;
try {
in = new FileInputStream(template);
response.setHeader("Content-disposition", "attachment;filename=" + template.getName());
response.setContentType("application/octet-stream;charset=UTF-8");
os = new BufferedOutputStream(response.getOutputStream());
int len;
while ((len = in.read()) != -1) {
os.write(len);
os.flush();
}
in.close();
os.close();
} catch (Exception e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(os);
}
ImportFileUtil.exportTemplate(response, templatePath + "车型及功能备案.zip");
}
@Override
public void importData(MultipartFile file, String cut) {
try {
int pos = file.getOriginalFilename().lastIndexOf(".");
String str = file.getOriginalFilename().substring(pos + 1).toLowerCase();
String filenameorg = file.getOriginalFilename().substring(0, pos);
//判断上传文件必须是zip
if (!str.equals("zip") && !str.equals("rar")) {
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("请上传zip格式的文件或rar格式的文件");
} else {
throw new JeroBootException("Please upload a zip file or rar file");
}
}
String path = uploadpath + File.separator + "modal" + File.separator + filenameorg + System.currentTimeMillis();
File saveDirectory = new File(path);
if (!saveDirectory.isDirectory()) {
saveDirectory.mkdir();
}
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + File.separator + file.getOriginalFilename()));
List<File> fileList =ImportFileUtil.importZip(file,cut,uploadPath);
//解压缩
String zipEntryName = "";
if (str.equals("zip")) {
zipEntryName = FileUnZip.unZipFiles(path + File.separator + file.getOriginalFilename(), path);
} else {
// zipEntryName = FileRarUtils.rarUnCompress(path + File.separator + file.getOriginalFilename(), path);
}
//判断压缩包下是否只有一个文件夹
File fileTemp = new File(path);
int length = fileTemp.listFiles().length;
if (length > 2) {
//删除原上传文件
FileUnZip.deleteDir(saveDirectory);
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("压缩包中必须有且仅有一个文件夹,请重新上传");
} else {
throw new JeroBootException("There must be only one folder in the compressed package Please upload it again");
}
}
File fileNew = new File(zipEntryName);
List<File> fileList = new ArrayList<>();
for (File file1 : fileNew.listFiles()) {
log.error("压缩包中的文件:" + file1.getName());
if (file1.getName().contains(".xls") || file1.getName().contains(".xlsx")) {
fileList.add(file1);
}
}
if (fileList.size() == 0) {
//删除原上传文件
FileUnZip.deleteDir(saveDirectory);
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("压缩包根目录下没有excel作为导入数据,请重新上传");
} else {
throw new JeroBootException("Excel does not exist in the root directory of the compressed package Please upload it again");
}
} else if (fileList.size() > 1) {
//删除原上传文件
FileUnZip.deleteDir(saveDirectory);
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("压缩包根目录下仅能存在一个excel为导入数据,请重新上传");
} else {
throw new JeroBootException("Only one Excel file can be imported in the compressed package root directory. Please upload data again");
}
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
@@ -0,0 +1,169 @@
package com.jero.modules.ota.utils;
import com.aliyuncs.utils.IOUtils;
import com.jero.common.constant.enums.CutEnum;
import com.jero.common.exception.JeroBootException;
import com.jero.modules.split.common.FileUnZip;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class ImportFileUtil {
public static File importFile(MultipartFile file, String cut, String uploadPath, String upLoadFileExt) {
File upLoadFile = null;
try {
int pos = file.getOriginalFilename().lastIndexOf(".");
String str = file.getOriginalFilename().substring(pos + 1).toLowerCase();
String fileNameOrg = file.getOriginalFilename().substring(0, pos);
//判断上传文件必须是
if (!str.equals(upLoadFileExt)) {
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("请上传" + upLoadFileExt + "格式的文件");
} else {
throw new JeroBootException("Please upload a " + upLoadFileExt + " file");
}
}
String path = uploadPath + File.separator + "modal" + File.separator + fileNameOrg + System.currentTimeMillis();
File saveDirectory = new File(path);
if (!saveDirectory.isDirectory()) {
saveDirectory.mkdir();
}
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + File.separator + file.getOriginalFilename()));
upLoadFile = new File(path + File.separator + file.getOriginalFilename());
} catch (IOException e) {
e.printStackTrace();
}
return upLoadFile;
}
public static List<File> importZip(MultipartFile file, String cut, String uploadPath) {
List<File> fileList = new ArrayList<>();
try {
int pos = file.getOriginalFilename().lastIndexOf(".");
String str = file.getOriginalFilename().substring(pos + 1).toLowerCase();
String filenameorg = file.getOriginalFilename().substring(0, pos);
//判断上传文件必须是zip
if (!str.equals("zip") && !str.equals("rar")) {
if (CutEnum.CN.getValue().equals(cut)) {
throw new JeroBootException("请上传zip格式的文件或rar格式的文件");
} else {
throw new JeroBootException("Please upload a zip file or rar file");
}
}
String path = uploadPath + File.separator + "modal" + File.separator + filenameorg + System.currentTimeMillis();
File saveDirectory = new File(path);
if (!saveDirectory.isDirectory()) {
saveDirectory.mkdir();
}
FileUtils.copyInputStreamToFile(file.getInputStream(), new File(path + File.separator + file.getOriginalFilename()));
//解压缩
String zipEntryName = "";
if (str.equals("zip") || str.equals("rar")) {
zipEntryName = FileUnZip.unZipFiles(path + File.separator + file.getOriginalFilename(), path);
} else {
// zipEntryName = FileRarUtils.rarUnCompress(path + File.separator + file.getOriginalFilename(), path);
}
//判断压缩包下是否只有一个文件夹
// File fileTemp = new File(path);
// int length = fileTemp.listFiles().length;
// if (length > 2) {
// //删除原上传文件
// FileUnZip.deleteDir(saveDirectory);
// if (CutEnum.CN.getValue().equals(cut)) {
// throw new JeroBootException("压缩包中必须有且仅有一个文件夹,请重新上传");
// } else {
// throw new JeroBootException("There must be only one folder in the compressed package Please upload it again");
// }
//
// }
File fileNew = new File(zipEntryName);
for (File file1 : fileNew.listFiles()) {
if (file1.getName().contains(".xls") || file1.getName().contains(".xlsx") || file1.isDirectory()
|| file1.getName().contains(".docx") || file1.getName().contains(".doc")) {
fileList.add(file1);
}
}
// if (fileList.size() == 0) {
// //删除原上传文件
// FileUnZip.deleteDir(saveDirectory);
// if (CutEnum.CN.getValue().equals(cut)) {
// throw new JeroBootException("压缩包根目录下没有excel作为导入数据,请重新上传");
// } else {
// throw new JeroBootException("Excel does not exist in the root directory of the compressed package Please upload it again");
// }
// } else if (fileList.size() > 1) {
// //删除原上传文件
// FileUnZip.deleteDir(saveDirectory);
// if (CutEnum.CN.getValue().equals(cut)) {
// throw new JeroBootException("压缩包根目录下仅能存在一个excel为导入数据,请重新上传");
// } else {
// throw new JeroBootException("Only one Excel file can be imported in the compressed package root directory. Please upload data again");
// }
// }
} catch (IOException e) {
e.printStackTrace();
}
return fileList;
}
public static void exportTemplate(HttpServletResponse response, String templateFilePath) {
File template = new File(templateFilePath);
BufferedOutputStream os = null;
InputStream in = null;
try {
in = new FileInputStream(template);
response.setHeader("Content-disposition", "attachment;filename=" + template.getName());
response.setContentType("application/octet-stream;charset=UTF-8");
os = new BufferedOutputStream(response.getOutputStream());
int len;
while ((len = in.read()) != -1) {
os.write(len);
os.flush();
}
in.close();
os.close();
} catch (Exception e) {
throw new JeroBootException("下载文件失败,请重试");
} finally {
IOUtils.closeQuietly(in);
IOUtils.closeQuietly(os);
}
}
public static Workbook readExcel(File file) {
Workbook wb = null;
if (file == null) {
return null;
}
String extString = file.getName().substring(file.getName().lastIndexOf("."));
InputStream is = null;
try {
is = new FileInputStream(file);
if (".xls".equals(extString)) {
return wb = new HSSFWorkbook(is);
} else if (".xlsx".equals(extString)) {
return wb = new XSSFWorkbook(is);
}
is.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(is);
}
return wb;
}
}
@@ -475,3 +475,8 @@ Space:
# appSecret: d3bf6e69-0726-42ee-91bd-0441a8f6ccdf
# domainUrl: https://api-plm-portal.nioint.com/api/v1/plm-portal-middle-integration/integration/api/common/queryModelByModule
ota :
#OTA 模版下载路径
templatePath: D://opt//ota//template//
uploadPath: D://opt//ota//upload//