From e33f2f7673babb26f9ab75e0accd19496ea1b119 Mon Sep 17 00:00:00 2001 From: 76164 <761646706@qq.com> Date: Thu, 8 Aug 2024 17:36:26 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=BA=86=20=E9=85=8D=E4=BB=B6?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=AF=BC=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../sgzb/common/core/constant/Constants.java | 8 +- .../base/controller/MaPartTypeController.java | 106 ++++++++++-------- .../base/service/impl/ExcelServiceImpl.java | 40 +++++-- 3 files changed, 99 insertions(+), 55 deletions(-) diff --git a/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/Constants.java b/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/Constants.java index e4b0ccb..e282522 100644 --- a/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/Constants.java +++ b/sgzb-common/sgzb-common-core/src/main/java/com/bonus/sgzb/common/core/constant/Constants.java @@ -2,7 +2,7 @@ package com.bonus.sgzb.common.core.constant; /** * 通用常量信息 - * + * * @author ruoyi */ public class Constants @@ -56,6 +56,12 @@ public class Constants * 失败标记 */ public static final Integer FAIL = 500; + /* + * 区分系统标记 + * */ + public static final String UPLOAD_DIR="/data/sgzb/"; + + public static final String UPLOAD_DIR_WIN="D:/data/sgzb"; /** * 登录成功状态 diff --git a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaPartTypeController.java b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaPartTypeController.java index f17785a..a72c618 100644 --- a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaPartTypeController.java +++ b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/controller/MaPartTypeController.java @@ -4,6 +4,7 @@ import com.bonus.sgzb.base.domain.MaPartType; import com.bonus.sgzb.base.domain.MapType; import com.bonus.sgzb.base.service.ExcelService; import com.bonus.sgzb.base.service.IPartTypeService; +import com.bonus.sgzb.common.core.constant.Constants; import com.bonus.sgzb.common.core.utils.poi.ExcelUtil; import com.bonus.sgzb.common.core.web.controller.BaseController; import com.bonus.sgzb.common.core.web.domain.AjaxResult; @@ -33,7 +34,7 @@ import java.util.Map; * @since 2023-11-27 16:44:02 */ @RestController -@RequestMapping("/base/maPartType") +@RequestMapping("maPartType") public class MaPartTypeController extends BaseController { /** * 服务对象 @@ -44,8 +45,6 @@ public class MaPartTypeController extends BaseController { @Resource private ExcelService excelService; - private static final String UPLOAD_DIR = "D:/work/wcy"; - /** * 查询配件类型列表 @@ -89,21 +88,34 @@ public class MaPartTypeController extends BaseController { @PostMapping("/readExcel") public AjaxResult readExcelFile(@RequestParam("file") MultipartFile file) throws IOException { + String os = System.getProperty("os.name").toLowerCase(); + boolean isLinux = false; + if (os.contains("nix") || os.contains("nux") || os.contains("linux")) { + isLinux = true; + } // 创建上传目录,如果不存在的话 - File dir = new File(UPLOAD_DIR); + File dir = null; + if (isLinux){ + dir = new File(Constants.UPLOAD_DIR); + }else { + dir = new File(Constants.UPLOAD_DIR_WIN); + } + if (!dir.exists()) { dir.mkdirs(); } - // 获取文件名 String fileName = file.getOriginalFilename(); - + Path filePath; // 定义文件路径 - Path filePath = Paths.get(UPLOAD_DIR, fileName); + if (isLinux){ + filePath = Paths.get(Constants.UPLOAD_DIR, fileName); + }else { + filePath = Paths.get(Constants.UPLOAD_DIR_WIN, fileName); + } // 保存文件 file.transferTo(filePath.toFile()); - Map>> stringMapMap = excelService.readExcelFile(filePath.toString()); return AjaxResult.success(); } @@ -115,45 +127,45 @@ public class MaPartTypeController extends BaseController { } - /** - * poi - * 删除配件管理类型 - * - * @param paId - * @return - */ - @DeleteMapping("/{paId}") - public AjaxResult delete (@PathVariable("paId") Long paId){ - if (maPartTypeService.hasChildBypaId(paId)) { - return warn("存在下级仓库列表,不允许删除"); - } - return toAjax(maPartTypeService.deletePaById(paId)); + /** + * poi + * 删除配件管理类型 + * + * @param paId + * @return + */ + @DeleteMapping("/{paId}") + public AjaxResult delete (@PathVariable("paId") Long paId){ + if (maPartTypeService.hasChildBypaId(paId)) { + return warn("存在下级仓库列表,不允许删除"); } - - /** - * 根据id获取数据 - * - * @param paId - * @return - */ - @GetMapping("/{paId}") - public AjaxResult getById (@PathVariable("paId") Long paId){ - MaPartType bean = maPartTypeService.getById(paId); - return AjaxResult.success(bean); - } - - /** - * 根据id修改数据 - * - * @param maPartType - * @return - */ - @PostMapping("/updateById") - public AjaxResult updateById (@RequestBody MaPartType maPartType){ - maPartType.setUpdateBy(SecurityUtils.getUsername()); - return toAjax(maPartTypeService.updateById(maPartType)); - } - - + return toAjax(maPartTypeService.deletePaById(paId)); } + /** + * 根据id获取数据 + * + * @param paId + * @return + */ + @GetMapping("/{paId}") + public AjaxResult getById (@PathVariable("paId") Long paId){ + MaPartType bean = maPartTypeService.getById(paId); + return AjaxResult.success(bean); + } + + /** + * 根据id修改数据 + * + * @param maPartType + * @return + */ + @PostMapping("/updateById") + public AjaxResult updateById (@RequestBody MaPartType maPartType){ + maPartType.setUpdateBy(SecurityUtils.getUsername()); + return toAjax(maPartTypeService.updateById(maPartType)); + } + + +} + diff --git a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java index fb46153..af0e5fb 100644 --- a/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java +++ b/sgzb-modules/sgzb-material/src/main/java/com/bonus/sgzb/base/service/impl/ExcelServiceImpl.java @@ -1,14 +1,10 @@ package com.bonus.sgzb.base.service.impl;// ExcelServiceImpl.java - import com.bonus.sgzb.base.domain.MaPartType; import com.bonus.sgzb.base.domain.MapType; import com.bonus.sgzb.base.mapper.ExcelMapper; import com.bonus.sgzb.base.service.ExcelService; import com.bonus.sgzb.common.security.utils.SecurityUtils; -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.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -44,7 +40,37 @@ public class ExcelServiceImpl implements ExcelService { while (iterator.hasNext()) { Row currentRow = iterator.next(); if (currentRow.getRowNum() == 0) { - // 跳过标题行 + // 校验模板 + Cell zeroLevelCell = currentRow.getCell(0); + Cell firstLevelCell = currentRow.getCell(1); + Cell secondLevelCell = currentRow.getCell(2); + Cell thirdLevelCell = currentRow.getCell(3); + Cell fourthLevelCell = currentRow.getCell(4); + Cell fifthLevelCell = currentRow.getCell(5); + String zeroLevel = getCellValueAsString(zeroLevelCell); + String firstLevel = getCellValueAsString(firstLevelCell); + String secondLevel = getCellValueAsString(secondLevelCell); + String thirdLevel = getCellValueAsString(thirdLevelCell); + String fourthLevel = getCellValueAsString(fourthLevelCell); + String fifthLevel = getCellValueAsString(fifthLevelCell); + if (!(zeroLevel.equals("序号"))){ + throw new RuntimeException("导入模板不正确"); + } + if (!(firstLevel.equals("配件类型"))){ + throw new RuntimeException("导入模板不正确"); + } + if (!(secondLevel.equals("配件名称"))){ + throw new RuntimeException("导入模板不正确"); + } + if (!(thirdLevel.equals("规格型号"))){ + throw new RuntimeException("导入模板不正确"); + } + if (!(fourthLevel.equals("单位"))){ + throw new RuntimeException("导入模板不正确"); + } + if (!(fifthLevel.equals("价格"))){ + throw new RuntimeException("导入模板不正确"); + } continue; } @@ -163,7 +189,7 @@ public class ExcelServiceImpl implements ExcelService { for (Map.Entry> secondLevel : firstLevel.getValue().entrySet()) { System.out.println("\tlevel_2: " + secondLevel.getKey()); //TODO, sql查询:是否有此名字 + parentId=firstLevelId, select * from ma_part_type where name=? and parent_id = firstLevelId; - MaPartType maPartType_level2 = excelMapper.selectMa(firstLevel.getKey(),firstLevelId); + MaPartType maPartType_level2 = excelMapper.selectMa(secondLevel.getKey(),firstLevelId); //TODO, 以上返回 maPartType_level2 if (Objects.nonNull(maPartType_level2)) { looplevel3(secondLevel, outExist, maPartType_level2.getPaId());