From 4a30da0f67a7bf5a22e8f88b09835da848835238 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Fri, 13 Jun 2025 14:58:50 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E8=BF=9B=E8=A1=8C=E6=8B=8D=E7=85=A7?= =?UTF-8?q?=E7=9A=84=E6=97=B6=E5=80=99=EF=BC=8C=E6=B8=85=E7=A9=BAtemp?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../iflytop/gd/app/controller/PhotoController.java | 2 +- .../iflytop/gd/app/service/api/PhotosService.java | 30 +++++++++++++++++++--- .../com/iflytop/gd/common/result/ResultCode.java | 5 ++-- 3 files changed, 31 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/iflytop/gd/app/controller/PhotoController.java b/src/main/java/com/iflytop/gd/app/controller/PhotoController.java index 79c2ca8..4fe9333 100644 --- a/src/main/java/com/iflytop/gd/app/controller/PhotoController.java +++ b/src/main/java/com/iflytop/gd/app/controller/PhotoController.java @@ -41,7 +41,7 @@ public class PhotoController { @Operation(summary = "拍摄一张照片") - @GetMapping("/take") + @PostMapping("/take") public Result take(@RequestBody PhotoTakeDTO photoTakeDTO) throws Exception { return Result.success(photosService.take(photoTakeDTO)); } diff --git a/src/main/java/com/iflytop/gd/app/service/api/PhotosService.java b/src/main/java/com/iflytop/gd/app/service/api/PhotosService.java index ce65d46..dea0a2e 100644 --- a/src/main/java/com/iflytop/gd/app/service/api/PhotosService.java +++ b/src/main/java/com/iflytop/gd/app/service/api/PhotosService.java @@ -7,6 +7,7 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.iflytop.gd.app.mapper.PhotosMapper; import com.iflytop.gd.app.model.dto.PhotoSaveDTO; import com.iflytop.gd.app.model.dto.PhotoTakeDTO; +import com.iflytop.gd.app.model.entity.Ores; import com.iflytop.gd.app.model.entity.Photos; import com.iflytop.gd.app.model.vo.PhotoListVO; import com.iflytop.gd.app.model.vo.PhotoVO; @@ -21,6 +22,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; +import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -31,6 +33,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * 照片接口服务 @@ -41,6 +44,7 @@ import java.util.stream.Collectors; public class PhotosService extends ServiceImpl { private final CameraBaslerDriver driver; private final SolutionModuleService solutionModuleService; + private final OresService oresService; @Value("${photo.url}") private String url; @@ -84,14 +88,28 @@ public class PhotosService extends ServiceImpl { } public String take(PhotoTakeDTO photoTakeDTO) throws Exception { + Path directoryPath = Paths.get(path + "/temp"); + try (Stream paths = Files.list(directoryPath)) { + paths.forEach(file -> { + try { + Files.delete(file); + } catch (IOException e) { + log.error("删除文件时发生错误", e); + } + }); + } catch (IOException e) { + log.error("读取目录时发生错误", e); + } + if (photoTakeDTO.getLightIntensity() != null) { solutionModuleService.fillLightOpen(photoTakeDTO.getLightIntensity()); } try { - String filePath = path + "/temp/" + System.currentTimeMillis() + ".png"; + String tempFilePath = "/temp/" + System.currentTimeMillis() + ".png"; + String filePath = path + tempFilePath; driver.enable(); driver.saveColorImg(filePath); - return url + filePath; + return url + tempFilePath; } finally { driver.disable(); if (photoTakeDTO.getLightIntensity() != null) { @@ -104,9 +122,10 @@ public class PhotosService extends ServiceImpl { Photos photos = this.getById(photoSaveDTO.getId()); if (photos == null) {//新增 String todayDate = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); + String tempPath = "/temp/" + photoSaveDTO.getFileName(); String imagePath = "/data/" + todayDate + "/" + photoSaveDTO.getFileName(); try { - Path sourcePath = Paths.get(path + "/temp/" + photoSaveDTO.getFileName()); + Path sourcePath = Paths.get(path + tempPath); Path targetPath = Paths.get(path + imagePath); Path targetDirectory = targetPath.getParent(); if (!Files.exists(targetDirectory)) { @@ -122,7 +141,12 @@ public class PhotosService extends ServiceImpl { photos.setImagePath(imagePath); photos.setMode(PhotoModeType.manual); } + Ores ores = oresService.getById(photoSaveDTO.getOreId()); + if (ores == null) { + throw new AppException(ResultCode.DATA_ALREADY_NOT_EXISTS); + } photos.setOreId(photoSaveDTO.getOreId()); + photos.setOreName(ores.getName()); photos.setProblem(photoSaveDTO.getProblem()); photos.setRemarks(photoSaveDTO.getRemarks()); this.saveOrUpdate(photos); diff --git a/src/main/java/com/iflytop/gd/common/result/ResultCode.java b/src/main/java/com/iflytop/gd/common/result/ResultCode.java index ea1fa13..a414d09 100644 --- a/src/main/java/com/iflytop/gd/common/result/ResultCode.java +++ b/src/main/java/com/iflytop/gd/common/result/ResultCode.java @@ -36,12 +36,13 @@ public enum ResultCode implements IResultCode, Serializable { INVALID_CREDENTIALS("4002", "用户名或密码错误"), OPERATION_NOT_ALLOWED("4003", "业务操作不允许"), DATA_ALREADY_EXISTS("4004", "数据已存在"), - - CONTAINER_NOT_FOUND("4005", "未找到对应溶液容器"), + DATA_ALREADY_NOT_EXISTS("4005", "数据不存在"), CRAFT_RUNNING("4101", "工艺正在执行"), CRAFT_CONTEXT_NULL("4102", "请先配置该加热区工艺"), CRAFT_NO_TRAY("4005", "工艺未找到托盘"), + + CONTAINER_NOT_FOUND("4201", "未找到对应溶液容器"), //============================ 5xxx:系统 & 第三方 ============================ SYSTEM_ERROR("5000", "系统内部错误"), SERVICE_UNAVAILABLE("5001", "服务暂不可用"),