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

This commit is contained in:
高嵩
2023-03-27 01:56:13 +08:00
parent a09ae32b60
commit e5ff47278e
5 changed files with 167 additions and 40 deletions
@@ -1,26 +1,27 @@
package com.jero.modules.ota.controller; package com.jero.modules.ota.controller;
import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jero.common.api.vo.Result;
import com.jero.common.system.query.QueryGenerator;
import com.jero.modules.ota.entity.OtaBaCxZxsjyq;
import com.jero.modules.ota.service.IOtaBaCxZxsjyqService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j; import com.jero.common.api.vo.Result;
import com.jero.common.aspect.annotation.AutoLog;
import com.jero.common.system.base.controller.JeroController; import com.jero.common.system.base.controller.JeroController;
import com.jero.common.system.query.QueryGenerator;
import com.jero.modules.ota.entity.OtaBaCxZxsjyq;
import com.jero.modules.ota.service.IOtaBaCxZxsjyqService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import javax.servlet.http.HttpServletRequest;
import com.jero.common.aspect.annotation.AutoLog; import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
/** /**
@@ -183,4 +184,27 @@ public class OtaBaCxZxsjyqController extends JeroController<OtaBaCxZxsjyq, IOtaB
return super.importExcel(request, response, OtaBaCxZxsjyq.class); return super.importExcel(request, response, OtaBaCxZxsjyq.class);
} }
@ApiOperation(value = "车型备案-在线升级要求-模板下载")
@GetMapping(value = "/exportTemplate")
// @RequiresPermissions("otaBaCx:exportTemplate")
public void exportTemplate(HttpServletResponse response, HttpServletRequest request) throws Exception {
this.otaBaCxZxsjyqService.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,
@RequestParam(value = "otaId", required = false) String otaId) throws Exception {
OtaBaCxZxsjyq res;
try {
res = this.otaBaCxZxsjyqService.importData(file, otaId, cut);
} catch (Exception e) {
e.printStackTrace();
return Result.error(e.getMessage());
}
return Result.OK(res);
}
} }
@@ -1,8 +1,12 @@
package com.jero.modules.ota.service; package com.jero.modules.ota.service;
import com.jero.modules.ota.entity.OtaBaCxJbxx;
import com.jero.modules.ota.entity.OtaBaCxZxsjyq; import com.jero.modules.ota.entity.OtaBaCxZxsjyq;
import com.baomidou.mybatisplus.extension.service.IService; 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; import java.util.List;
/** /**
@@ -61,4 +65,8 @@ public interface IOtaBaCxZxsjyqService extends IService<OtaBaCxZxsjyq> {
List<OtaBaCxZxsjyq> queryList(); List<OtaBaCxZxsjyq> queryList();
OtaBaCxZxsjyq queryByOtaId(String otaId, String otaIdT); OtaBaCxZxsjyq queryByOtaId(String otaId, String otaIdT);
void exportTemplate(HttpServletResponse response, HttpServletRequest request);
OtaBaCxZxsjyq importData(MultipartFile file, String otaId, String cut) throws Exception;
} }
@@ -174,14 +174,6 @@ public class OtaBaCxJbxxServiceImpl extends ServiceImpl<OtaBaCxJbxxMapper, OtaBa
return add(jbxx); return add(jbxx);
} }
public static String getCellValue(Cell cell, Workbook workbook) throws Exception {
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
DataFormatter formatter = new DataFormatter();
//单元格不设置数字格式
cell.setCellStyle(null);
return formatter.formatCellValue(cell, evaluator);
}
private OtaBaCxJbxx parseFile(File file, String cut) throws Exception { private OtaBaCxJbxx parseFile(File file, String cut) throws Exception {
OtaBaCxJbxx jbxx = new OtaBaCxJbxx(); OtaBaCxJbxx jbxx = new OtaBaCxJbxx();
Workbook wb = ImportFileUtil.readExcel(file); Workbook wb = ImportFileUtil.readExcel(file);
@@ -189,39 +181,39 @@ public class OtaBaCxJbxxServiceImpl extends ServiceImpl<OtaBaCxJbxxMapper, OtaBa
Row row = s.getRow(1); Row row = s.getRow(1);
Cell cell = row.getCell(1); Cell cell = row.getCell(1);
jbxx.setQymc(getCellValue(cell, wb)); jbxx.setQymc(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(2); row = s.getRow(2);
cell = row.getCell(1); cell = row.getCell(1);
String cpbh = getCellValue(cell, wb); String cpbh = ImportFileUtil.getCellValue(cell, wb);
if (StringUtils.isEmpty(cpbh)) { if (StringUtils.isEmpty(cpbh)) {
throw new JeroBootException("产品编号不能为空"); throw new JeroBootException("产品编号不能为空");
} }
jbxx.setDjbh(cpbh); jbxx.setDjbh(cpbh);
row = s.getRow(3); row = s.getRow(3);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setCplb(getCellValue(cell, wb)); jbxx.setCplb(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(4); row = s.getRow(4);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setGgpc(getCellValue(cell, wb)); jbxx.setGgpc(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(5); row = s.getRow(5);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setCpsb(getCellValue(cell, wb)); jbxx.setCpsb(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(6); row = s.getRow(6);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setCpmc(getCellValue(cell, wb)); jbxx.setCpmc(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(7); row = s.getRow(7);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setCpxh(getCellValue(cell, wb)); jbxx.setCpxh(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(8); row = s.getRow(8);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setDllx1(getCellValue(cell, wb)); jbxx.setDllx1(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(9); row = s.getRow(9);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setDllx2(getCellValue(cell, wb)); jbxx.setDllx2(ImportFileUtil.getCellValue(cell, wb));
row = s.getRow(10); row = s.getRow(10);
cell = row.getCell(1); cell = row.getCell(1);
String txzdStr = getCellValue(cell, wb); String txzdStr = ImportFileUtil.getCellValue(cell, wb);
if (StringUtils.isNotEmpty(txzdStr)) { if (StringUtils.isNotEmpty(txzdStr)) {
String[] txzdStrs = txzdStr.split(","); String[] txzdStrs = txzdStr.split(",");
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@@ -235,7 +227,7 @@ public class OtaBaCxJbxxServiceImpl extends ServiceImpl<OtaBaCxJbxxMapper, OtaBa
jbxx.setTxzd(sb.toString()); jbxx.setTxzd(sb.toString());
row = s.getRow(11); row = s.getRow(11);
cell = row.getCell(1); cell = row.getCell(1);
jbxx.setCdotasj(getCellValue(cell, wb)); jbxx.setCdotasj(ImportFileUtil.getCellValue(cell, wb));
} }
return jbxx; return jbxx;
} }
@@ -1,10 +1,10 @@
package com.jero.modules.ota.service.impl; package com.jero.modules.ota.service.impl;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jero.common.exception.JeroBootException;
import com.jero.modules.ota.entity.AttachmentEO; import com.jero.modules.ota.entity.AttachmentEO;
import com.jero.modules.ota.entity.OtaBaCxJbxx;
import com.jero.modules.ota.entity.OtaBaCxTxjl; import com.jero.modules.ota.entity.OtaBaCxTxjl;
import com.jero.modules.ota.entity.OtaBaCxZxsjyq; import com.jero.modules.ota.entity.OtaBaCxZxsjyq;
import com.jero.modules.ota.enums.OtaModuleEnum; import com.jero.modules.ota.enums.OtaModuleEnum;
@@ -12,16 +12,24 @@ import com.jero.modules.ota.mapper.OtaBaCxZxsjyqMapper;
import com.jero.modules.ota.service.IOtaBaCxTxjlService; import com.jero.modules.ota.service.IOtaBaCxTxjlService;
import com.jero.modules.ota.service.IOtaBaCxZxsjyqService; import com.jero.modules.ota.service.IOtaBaCxZxsjyqService;
import com.jero.modules.ota.utils.ComparisonUtil; import com.jero.modules.ota.utils.ComparisonUtil;
import com.jero.modules.ota.utils.ImportFileUtil;
import com.jero.modules.system.service.ISysDictService; import com.jero.modules.system.service.ISysDictService;
import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.Date; import java.util.Date;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
/** /**
* @Description: 车型备案-在线升级要求 * @Description: 车型备案-在线升级要求
@@ -32,6 +40,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@Service @Service
public class OtaBaCxZxsjyqServiceImpl extends ServiceImpl<OtaBaCxZxsjyqMapper, OtaBaCxZxsjyq> implements IOtaBaCxZxsjyqService { public class OtaBaCxZxsjyqServiceImpl extends ServiceImpl<OtaBaCxZxsjyqMapper, OtaBaCxZxsjyq> implements IOtaBaCxZxsjyqService {
@Value(value = "${ota.templatePath}")
private String templatePath;
@Value(value = "${ota.uploadPath}")
private String uploadPath;
@Autowired @Autowired
private IOtaBaCxTxjlService otaBaCxTxjlService; private IOtaBaCxTxjlService otaBaCxTxjlService;
@@ -150,4 +163,70 @@ public class OtaBaCxZxsjyqServiceImpl extends ServiceImpl<OtaBaCxZxsjyqMapper, O
queryWrapper.eq(OtaBaCxZxsjyq::getOtaId, otaId); queryWrapper.eq(OtaBaCxZxsjyq::getOtaId, otaId);
return this.getOne(queryWrapper, false); return this.getOne(queryWrapper, false);
} }
@Override
public void exportTemplate(HttpServletResponse response, HttpServletRequest request) {
ImportFileUtil.exportTemplate(response, templatePath + "在线升级要求.zip");
}
@Override
public OtaBaCxZxsjyq importData(MultipartFile file, String otaId, String cut) throws Exception {
List<File> upLoadFiles = ImportFileUtil.importZip(file, cut, uploadPath);
File attFile = null;
File upFile = null;
for (File f : upLoadFiles) {
if (f.isDirectory() && f.getName().equals("在线升级要求")) {
attFile = f;
} else if (f.getName().equals("在线升级要求.xlsx")) {
upFile = f;
}
}
if (null == upFile || null == attFile) {
throw new JeroBootException("请上传正确的文件");
}
OtaBaCxZxsjyq otaBaCxZxsjyq = parseFile(upFile, cut);
otaBaCxZxsjyq.setOtaId(otaId);
add(otaBaCxZxsjyq);
return otaBaCxZxsjyq;
}
private OtaBaCxZxsjyq parseFile(File upFile, String cut) throws Exception {
OtaBaCxZxsjyq otaBaCxZxsjyq = new OtaBaCxZxsjyq();
Workbook wb = ImportFileUtil.readExcel(upFile);
Sheet s = wb.getSheetAt(0);
Row row = s.getRow(2);
Cell cell = row.getCell(2);
otaBaCxZxsjyq.setBhsjb(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(3);
cell = row.getCell(2);
otaBaCxZxsjyq.setBhclbb(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(4);
cell = row.getCell(2);
otaBaCxZxsjyq.setClgxnl(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(5);
cell = row.getCell(2);
otaBaCxZxsjyq.setClsjtj(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(6);
cell = row.getCell(2);
otaBaCxZxsjyq.setClsjdl(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(7);
cell = row.getCell(2);
otaBaCxZxsjyq.setSjbhaq(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(8);
cell = row.getCell(2);
otaBaCxZxsjyq.setSjantj(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(9);
cell = row.getCell(2);
otaBaCxZxsjyq.setSjsbaq(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(10);
cell = row.getCell(2);
otaBaCxZxsjyq.setSjgg(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(11);
cell = row.getCell(2);
otaBaCxZxsjyq.setSjzt(ImportFileUtil.getCellValueYesOrNo(cell, wb));
row = s.getRow(2);
cell = row.getCell(3);
otaBaCxZxsjyq.setZmcl(ImportFileUtil.getCellValue(cell, wb));
return otaBaCxZxsjyq;
}
} }
@@ -5,10 +5,10 @@ import com.jero.common.constant.enums.CutEnum;
import com.jero.common.exception.JeroBootException; import com.jero.common.exception.JeroBootException;
import com.jero.modules.split.common.FileUnZip; import com.jero.modules.split.common.FileUnZip;
import org.apache.commons.io.FileUtils; import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -166,4 +166,28 @@ public class ImportFileUtil {
} }
return wb; return wb;
} }
public static String getCellValue(Cell cell, Workbook workbook) throws Exception {
FormulaEvaluator evaluator = workbook.getCreationHelper().createFormulaEvaluator();
DataFormatter formatter = new DataFormatter();
//单元格不设置数字格式
if (ObjectUtils.isNotEmpty(cell)) {
cell.setCellStyle(null);
return formatter.formatCellValue(cell, evaluator);
}
return "";
}
public static String getCellValueYesOrNo(Cell cell, Workbook workbook) throws Exception {
String res = "";
String str = getCellValue(cell, workbook);
if (StringUtils.isNotEmpty(str)) {
if (str.equals("")) {
res = "1";
} else if (str.equals("")) {
res = "2";
}
}
return res;
}
} }