From 593c8547a51b84273662307ea1284b7c35752330 Mon Sep 17 00:00:00 2001 From: funny <834502597@qq.com> Date: Mon, 13 Apr 2026 16:15:13 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../QmsInspectionItemController.java | 20 ++++- .../pojo/dto/QmsInspectionItemImportDTO.java | 5 ++ .../service/IQmsInspectionItemService.java | 3 +- .../impl/QmsInspectionItemServiceImpl.java | 81 ++++++++++++++++++- .../resources/mapper/QmsCoaTaskMapper.xml | 5 ++ 5 files changed, 108 insertions(+), 6 deletions(-) create mode 100644 nflg-wms-repository/src/main/resources/mapper/QmsCoaTaskMapper.xml diff --git a/nflg-qms-admin/src/main/java/com/nflg/qms/admin/controller/QmsInspectionItemController.java b/nflg-qms-admin/src/main/java/com/nflg/qms/admin/controller/QmsInspectionItemController.java index 374833f5..ccced21a 100644 --- a/nflg-qms-admin/src/main/java/com/nflg/qms/admin/controller/QmsInspectionItemController.java +++ b/nflg-qms-admin/src/main/java/com/nflg/qms/admin/controller/QmsInspectionItemController.java @@ -28,10 +28,13 @@ import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import org.ttzero.excel.entity.ListSheet; import org.ttzero.excel.entity.Workbook; +import org.ttzero.excel.reader.Drawings; +import org.ttzero.excel.reader.ExcelReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.InputStream; import java.time.LocalDate; import java.util.Collections; import java.util.List; @@ -170,7 +173,7 @@ public class QmsInspectionItemController extends BaseController { /** * 导入检测项 * 校验失败时返回带错误信息的文件URL - * 图例字段填写图片URL(需先通过文件上传接口上传图片获取URL) + * 图例字段支持:嵌入图片(自动上传)或图片URL * * @param file 导入文件 */ @@ -182,6 +185,19 @@ public class QmsInspectionItemController extends BaseController { throw new NflgException(STATE.BusinessError, "导入文件内容为空"); } + // 设置行号(用于匹配图片) + for (int i = 0; i < data.size(); i++) { + data.get(i).setRowIndex(i); + } + + // 读取Excel中的嵌入图片 + List pictures = null; + try (InputStream in = file.getInputStream()) { + pictures = new ExcelReader(in).listPictures(); + } catch (Exception e) { + pictures = null; + } + // 校验必填字段 boolean hasError = false; for (QmsInspectionItemImportDTO dto : data) { @@ -199,7 +215,7 @@ public class QmsInspectionItemController extends BaseController { } if (!hasError) { - inspectionItemService.importItems(data); + inspectionItemService.importItems(data, pictures); return ApiResult.success("导入成功"); } else { try (ByteArrayOutputStream osOut = new ByteArrayOutputStream()) { diff --git a/nflg-wms-common/src/main/java/com/nflg/wms/common/pojo/dto/QmsInspectionItemImportDTO.java b/nflg-wms-common/src/main/java/com/nflg/wms/common/pojo/dto/QmsInspectionItemImportDTO.java index a19ea68e..4271a330 100644 --- a/nflg-wms-common/src/main/java/com/nflg/wms/common/pojo/dto/QmsInspectionItemImportDTO.java +++ b/nflg-wms-common/src/main/java/com/nflg/wms/common/pojo/dto/QmsInspectionItemImportDTO.java @@ -10,6 +10,11 @@ import org.ttzero.excel.annotation.ExcelColumn; @Data public class QmsInspectionItemImportDTO { + /** + * 行号(从0开始,用于匹配图片位置,非Excel列) + */ + private int rowIndex; + /** * 错误信息(导入校验结果) */ diff --git a/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/IQmsInspectionItemService.java b/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/IQmsInspectionItemService.java index fa4168cd..f7208e6e 100644 --- a/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/IQmsInspectionItemService.java +++ b/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/IQmsInspectionItemService.java @@ -11,6 +11,7 @@ import com.nflg.wms.common.pojo.qo.QmsInspectionItemUpdateQO; import com.nflg.wms.common.pojo.vo.QmsInspectionItemDetailsVO; import com.nflg.wms.common.pojo.vo.QmsInspectionItemVO; import com.nflg.wms.repository.entity.QmsInspectionItem; +import org.ttzero.excel.reader.Drawings; import java.util.List; @@ -63,5 +64,5 @@ public interface IQmsInspectionItemService extends IService { /** * 批量导入 */ - void importItems(List dtos); + void importItems(List dtos, List pictures); } diff --git a/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/impl/QmsInspectionItemServiceImpl.java b/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/impl/QmsInspectionItemServiceImpl.java index a9b7e9ed..d15b4c10 100644 --- a/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/impl/QmsInspectionItemServiceImpl.java +++ b/nflg-wms-repository/src/main/java/com/nflg/wms/repository/service/impl/QmsInspectionItemServiceImpl.java @@ -1,5 +1,6 @@ package com.nflg.wms.repository.service.impl; +import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; @@ -15,6 +16,7 @@ import com.nflg.wms.common.pojo.qo.QmsInspectionItemSearchQO; import com.nflg.wms.common.pojo.qo.QmsInspectionItemUpdateQO; import com.nflg.wms.common.pojo.vo.QmsInspectionItemDetailsVO; import com.nflg.wms.common.pojo.vo.QmsInspectionItemVO; +import com.nflg.wms.common.util.DateTimeUtil; import com.nflg.wms.common.util.UserUtil; import com.nflg.wms.repository.entity.QmsInspectionItem; import com.nflg.wms.repository.entity.QmsInspectionItemDetails; @@ -22,9 +24,15 @@ import com.nflg.wms.repository.mapper.QmsInspectionItemMapper; import com.nflg.wms.repository.service.IQmsInspectionItemDetailsService; import com.nflg.wms.repository.service.IQmsInspectionItemService; import jakarta.annotation.Resource; +import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import org.ttzero.excel.reader.Drawings; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.LocalTime; @@ -43,6 +51,7 @@ public class QmsInspectionItemServiceImpl extends ServiceImpl dtos) { + public void importItems(List dtos, List pictures) { String operator = UserUtil.getUserName(); LocalDateTime now = LocalDateTime.now(); @@ -290,11 +299,24 @@ public class QmsInspectionItemServiceImpl extends ServiceImpl pictures, int excelRowIndex) { + for (Drawings.Picture pic : pictures) { + // 使用dimension获取行号(dimension.firstRow是起始行号) + if (pic.dimension != null && pic.dimension.firstRow == excelRowIndex) { + return pic; + } + } + return null; + } + + /** + * 上传图片到文件服务器 + */ + private String uploadPicture(Drawings.Picture picture) { + try { + // 从临时路径读取图片数据 + Path localPath = picture.getLocalPath(); + if (localPath == null || !Files.exists(localPath)) { + return null; + } + + byte[] imageBytes = Files.readAllBytes(localPath); + + // 从文件名或路径推断扩展名 + String fileName = localPath.getFileName().toString(); + String ext = "png"; // 默认扩展名 + if (fileName.contains(".")) { + ext = fileName.substring(fileName.lastIndexOf(".") + 1); + } + + // 根据扩展名确定contentType + String contentType = "image/png"; + if ("jpg".equalsIgnoreCase(ext) || "jpeg".equalsIgnoreCase(ext)) { + contentType = "image/jpeg"; + } else if ("gif".equalsIgnoreCase(ext)) { + contentType = "image/gif"; + } else if ("bmp".equalsIgnoreCase(ext)) { + contentType = "image/bmp"; + } + + return fileUploadService.upload( + "inspection/legend/" + DateTimeUtil.format(LocalDate.now(), "yyyyMMdd") + + "/" + IdUtil.fastUUID() + "." + ext, + new ByteArrayInputStream(imageBytes), + contentType); + } catch (Exception e) { + return null; + } + } + // ==================== 私有工具 ==================== private QmsInspectionItemVO toVO(QmsInspectionItem item) { diff --git a/nflg-wms-repository/src/main/resources/mapper/QmsCoaTaskMapper.xml b/nflg-wms-repository/src/main/resources/mapper/QmsCoaTaskMapper.xml new file mode 100644 index 00000000..53dd5855 --- /dev/null +++ b/nflg-wms-repository/src/main/resources/mapper/QmsCoaTaskMapper.xml @@ -0,0 +1,5 @@ + + + + +