diff --git a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysDictServiceImpl.java b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysDictServiceImpl.java index b97383d94..24788b2a5 100644 --- a/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysDictServiceImpl.java +++ b/jero-boot/jero-boot-module-system/src/main/java/com/jero/modules/system/service/impl/SysDictServiceImpl.java @@ -443,7 +443,7 @@ public class SysDictServiceImpl extends ServiceImpl 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; } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxJbxxController.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxJbxxController.java index ee7e0292b..4bacf2384 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxJbxxController.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxJbxxController.java @@ -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 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("导入成功"); + } + } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java index 299c6a205..6aee82684 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/controller/OtaBaCxListController.java @@ -189,7 +189,6 @@ public class OtaBaCxListController extends JeroController { List queryList(); OtaBaCxJbxx queryByOtaId(String otaId, String otaIdT); + + void exportTemplate(HttpServletResponse response, HttpServletRequest request); + + void importData(MultipartFile file, String cut); } diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java index 7dfe8bf32..672d2e660 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/service/impl/OtaBaCxJbxxServiceImpl.java @@ -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 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 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 diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/utils/ImportFileUtil.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/utils/ImportFileUtil.java new file mode 100644 index 000000000..c33a92dbd --- /dev/null +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/ota/utils/ImportFileUtil.java @@ -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 importZip(MultipartFile file, String cut, String uploadPath) { + List 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; + } +} diff --git a/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml b/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml index 7f35fb03c..c8baa8421 100644 --- a/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml +++ b/jero-boot/jero-boot-single-startup/src/main/resources/application-dev.yml @@ -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// +