This commit is contained in:
parent
ef15359155
commit
7350061d49
|
|
@ -1,6 +1,7 @@
|
|||
package com.nflg.qms.admin.controller;
|
||||
|
||||
import cn.hutool.core.collection.CollectionUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.nflg.wms.common.constant.STATE;
|
||||
import com.nflg.wms.common.exception.NflgException;
|
||||
import com.nflg.wms.common.pojo.ApiResult;
|
||||
|
|
@ -18,7 +19,6 @@ import com.nflg.wms.common.util.EecExcelUtil;
|
|||
import com.nflg.wms.repository.service.IQmsInspectionItemService;
|
||||
import com.nflg.wms.starter.BaseController;
|
||||
import com.nflg.wms.starter.service.FileUploadService;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import jakarta.validation.Valid;
|
||||
|
|
@ -28,15 +28,11 @@ 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;
|
||||
|
||||
/**
|
||||
|
|
@ -165,7 +161,7 @@ public class QmsInspectionItemController extends BaseController {
|
|||
example.setLegend("示例图例URL(可不填)");
|
||||
example.setDeterminationType(0);
|
||||
|
||||
EecExcelUtil.export("检测项导入模板", "检测项导入模板", Collections.singletonList(example), response);
|
||||
EecExcelUtil.export("检测项导入模板", "检测项导入模板", List.of(example), response);
|
||||
}
|
||||
|
||||
// ==================== 导入 ====================
|
||||
|
|
@ -173,7 +169,7 @@ public class QmsInspectionItemController extends BaseController {
|
|||
/**
|
||||
* 导入检测项
|
||||
* 校验失败时返回带错误信息的文件URL
|
||||
* 图例字段支持:嵌入图片(自动上传)或图片URL
|
||||
* 图例字段支持:图片URL
|
||||
*
|
||||
* @param file 导入文件
|
||||
*/
|
||||
|
|
@ -185,19 +181,6 @@ 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<Drawings.Picture> pictures = null;
|
||||
try (InputStream in = file.getInputStream()) {
|
||||
pictures = new ExcelReader(in).listPictures();
|
||||
} catch (Exception e) {
|
||||
pictures = null;
|
||||
}
|
||||
|
||||
// 校验必填字段
|
||||
boolean hasError = false;
|
||||
for (QmsInspectionItemImportDTO dto : data) {
|
||||
|
|
@ -215,7 +198,7 @@ public class QmsInspectionItemController extends BaseController {
|
|||
}
|
||||
|
||||
if (!hasError) {
|
||||
inspectionItemService.importItems(data, pictures);
|
||||
inspectionItemService.importItems(data);
|
||||
return ApiResult.success("导入成功");
|
||||
} else {
|
||||
try (ByteArrayOutputStream osOut = new ByteArrayOutputStream()) {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@ import org.ttzero.excel.annotation.ExcelColumn;
|
|||
import org.ttzero.excel.annotation.MediaColumn;
|
||||
|
||||
/**
|
||||
* 检测项导出 DTO(不含错误信息字段)
|
||||
* 检测项导出 DTO
|
||||
*/
|
||||
@Data
|
||||
public class QmsInspectionItemExportDTO {
|
||||
|
|
@ -43,6 +43,7 @@ public class QmsInspectionItemExportDTO {
|
|||
/**
|
||||
* 图例(图片URL,导出时自动转为图片)
|
||||
*/
|
||||
@ExcelColumn("图例")
|
||||
@MediaColumn
|
||||
private String legend;
|
||||
|
||||
|
|
|
|||
|
|
@ -5,22 +5,10 @@ import org.ttzero.excel.annotation.ExcelColumn;
|
|||
|
||||
/**
|
||||
* 检测项导入 DTO
|
||||
* ExcelColumn 注解用于列头映射,带 * 的为必填字段
|
||||
*/
|
||||
@Data
|
||||
public class QmsInspectionItemImportDTO {
|
||||
|
||||
/**
|
||||
* 行号(从0开始,用于匹配图片位置,非Excel列)
|
||||
*/
|
||||
private int rowIndex;
|
||||
|
||||
/**
|
||||
* 错误信息(导入校验结果)
|
||||
*/
|
||||
@ExcelColumn("错误信息")
|
||||
private String error;
|
||||
|
||||
/**
|
||||
* 物料类别ID
|
||||
*/
|
||||
|
|
@ -52,7 +40,7 @@ public class QmsInspectionItemImportDTO {
|
|||
private String testStandard;
|
||||
|
||||
/**
|
||||
* 图例(图片URL)
|
||||
* 图例(图片URL,由Controller上传后设置)
|
||||
*/
|
||||
@ExcelColumn("图例")
|
||||
private String legend;
|
||||
|
|
@ -62,4 +50,10 @@ public class QmsInspectionItemImportDTO {
|
|||
*/
|
||||
@ExcelColumn("判定类型(0目视/1量具)*")
|
||||
private Integer determinationType;
|
||||
|
||||
/**
|
||||
* 错误信息(导入校验结果)
|
||||
*/
|
||||
@ExcelColumn("错误信息")
|
||||
private String error;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ 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,6 +62,7 @@ public interface IQmsInspectionItemService extends IService<QmsInspectionItem> {
|
|||
|
||||
/**
|
||||
* 批量导入
|
||||
* 按编号+名称分组,主表插一条,明细表插多条
|
||||
*/
|
||||
void importItems(List<QmsInspectionItemImportDTO> dtos, List<Drawings.Picture> pictures);
|
||||
void importItems(List<QmsInspectionItemImportDTO> dtos);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
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;
|
||||
|
|
@ -16,7 +15,6 @@ 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;
|
||||
|
|
@ -24,15 +22,9 @@ 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;
|
||||
|
|
@ -51,7 +43,6 @@ public class QmsInspectionItemServiceImpl extends ServiceImpl<QmsInspectionItemM
|
|||
@Resource
|
||||
private IQmsInspectionItemDetailsService detailsService;
|
||||
|
||||
|
||||
// ==================== 新增(主表 + 一条明细) ====================
|
||||
|
||||
@Transactional
|
||||
|
|
@ -264,7 +255,7 @@ public class QmsInspectionItemServiceImpl extends ServiceImpl<QmsInspectionItemM
|
|||
|
||||
@Transactional
|
||||
@Override
|
||||
public void importItems(List<QmsInspectionItemImportDTO> dtos, List<Drawings.Picture> pictures) {
|
||||
public void importItems(List<QmsInspectionItemImportDTO> dtos) {
|
||||
String operator = UserUtil.getUserName();
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
|
||||
|
|
@ -299,87 +290,16 @@ public class QmsInspectionItemServiceImpl extends ServiceImpl<QmsInspectionItemM
|
|||
|
||||
// 所有行作为明细插入
|
||||
for (QmsInspectionItemImportDTO dto : list) {
|
||||
String legend = dto.getLegend();
|
||||
|
||||
// 如果是 DISPIMG 公式,尝试从嵌入图片中获取
|
||||
if (StrUtil.isNotBlank(legend) && legend.startsWith("=DISPIMG(")) {
|
||||
if (pictures != null && !pictures.isEmpty()) {
|
||||
// 根据行号找到这一行对应的图片(Excel行号从1开始,需要+1跳过表头)
|
||||
Drawings.Picture pic = findPictureByRow(pictures, dto.getRowIndex() + 1);
|
||||
if (pic != null) {
|
||||
// 上传图片并获取URL
|
||||
legend = uploadPicture(pic);
|
||||
} else {
|
||||
legend = null;
|
||||
}
|
||||
} else {
|
||||
legend = null;
|
||||
}
|
||||
}
|
||||
|
||||
QmsInspectionItemDetails detail = new QmsInspectionItemDetails()
|
||||
.setInspectionItemId(item.getId())
|
||||
.setTestStandard(dto.getTestStandard())
|
||||
.setLegend(legend)
|
||||
.setLegend(dto.getLegend())
|
||||
.setDeterminationType(dto.getDeterminationType());
|
||||
detailsService.save(detail);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据行号查找图片
|
||||
*/
|
||||
private Drawings.Picture findPictureByRow(List<Drawings.Picture> 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) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue