diff --git a/src/main/java/com/iflytop/colortitration/app/controller/PhotoController.java b/src/main/java/com/iflytop/colortitration/app/controller/PhotoController.java new file mode 100644 index 0000000..4b042cc --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/controller/PhotoController.java @@ -0,0 +1,64 @@ +package com.iflytop.colortitration.app.controller; + +import com.iflytop.colortitration.app.model.dto.PhotoSaveDTO; +import com.iflytop.colortitration.common.base.BasePageQuery; +import com.iflytop.colortitration.common.model.vo.PhotoListVO; +import com.iflytop.colortitration.common.model.vo.PhotoVO; +import com.iflytop.colortitration.common.result.PageResult; +import com.iflytop.colortitration.common.result.Result; +import com.iflytop.colortitration.common.service.PhotosService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +/** + * 照片接口 + */ +@Tag(name = "\uD83D\uDCF7照片") +@RestController +@RequestMapping("/api/photo") +@RequiredArgsConstructor +@Slf4j +public class PhotoController { + private final PhotosService photosService; + + @Operation(summary = "照片列表") + @GetMapping("/list") + public PageResult getList(BasePageQuery pageQuery) { + return PageResult.success(photosService.getList(pageQuery)); + } + + @Operation(summary = "根据id获取照片") + @GetMapping("/{id}") + public Result get(@PathVariable Long id) { + return Result.success(photosService.get(id)); + } + + + @Operation(summary = "拍摄一张照片") + @PostMapping("/take") + public Result take() throws Exception { + return Result.success(photosService.take()); + } + + + @Operation(summary = "保存照片") + @PostMapping("/save") + public Result save(@RequestBody PhotoSaveDTO photoSaveDTO) { + photosService.save(photoSaveDTO); + return Result.success(); + } + + @Operation(summary = "删除照片") + @DeleteMapping("/{ids}") + public Result delete(@Parameter(description = "ID,多个以英文逗号(,)分割") @PathVariable String ids) { + boolean isSuccess = photosService.deletePhoto(ids); + if (isSuccess) { + return Result.success(); + } + return Result.failed(); + } +} diff --git a/src/main/java/com/iflytop/colortitration/app/model/dto/PhotoSaveDTO.java b/src/main/java/com/iflytop/colortitration/app/model/dto/PhotoSaveDTO.java new file mode 100644 index 0000000..981e86c --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/app/model/dto/PhotoSaveDTO.java @@ -0,0 +1,18 @@ +package com.iflytop.colortitration.app.model.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +public class PhotoSaveDTO { + private Long id; + + @Schema(description = "颜色16进制代码") + private String code; + + @Schema(description = "文件名称", example = "123.png") + private String fileName; + + @Schema(description = "备注") + private String remarks; +} diff --git a/src/main/java/com/iflytop/colortitration/common/enums/PhotoModeType.java b/src/main/java/com/iflytop/colortitration/common/enums/PhotoModeType.java new file mode 100644 index 0000000..73c1a6a --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/enums/PhotoModeType.java @@ -0,0 +1,18 @@ +package com.iflytop.colortitration.common.enums; + +import lombok.Getter; + +/** + * 照片数据类型 + */ +@Getter +public enum PhotoModeType { + /** + * 工艺 + */ + crafts, + /** + * 手动 + */ + manual +} diff --git a/src/main/java/com/iflytop/colortitration/common/mapper/PhotosMapper.java b/src/main/java/com/iflytop/colortitration/common/mapper/PhotosMapper.java new file mode 100644 index 0000000..8eb8d66 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/mapper/PhotosMapper.java @@ -0,0 +1,13 @@ +package com.iflytop.colortitration.common.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.iflytop.colortitration.common.model.entity.Photos; +import org.apache.ibatis.annotations.Mapper; + +/** + * 照片持久层接口 + */ +@Mapper +public interface PhotosMapper extends BaseMapper { + +} diff --git a/src/main/java/com/iflytop/colortitration/common/model/entity/Photos.java b/src/main/java/com/iflytop/colortitration/common/model/entity/Photos.java new file mode 100644 index 0000000..66a6429 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/model/entity/Photos.java @@ -0,0 +1,31 @@ +package com.iflytop.colortitration.common.model.entity; + + +import com.baomidou.mybatisplus.annotation.TableName; +import com.iflytop.colortitration.common.base.BaseEntity; +import com.iflytop.colortitration.common.enums.PhotoModeType; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +@Data +@EqualsAndHashCode(callSuper = true) +@TableName("photos") // 表名 +@Schema(description = "照片实体") +public class Photos extends BaseEntity { + + @Schema(description = "模式(手动、自动)") + private PhotoModeType mode; + + @Schema(description = "工艺ID") + private Long craftsId; + + @Schema(description = "工艺名称(自动模式)") + private String craftsName; + + @Schema(description = "图片相对路径") + private String imagePath; + + @Schema(description = "备注") + private String remarks; +} diff --git a/src/main/java/com/iflytop/colortitration/common/model/vo/PhotoListVO.java b/src/main/java/com/iflytop/colortitration/common/model/vo/PhotoListVO.java new file mode 100644 index 0000000..72fbf97 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/model/vo/PhotoListVO.java @@ -0,0 +1,31 @@ +package com.iflytop.colortitration.common.model.vo; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.TableField; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.iflytop.colortitration.common.enums.PhotoModeType; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +@EqualsAndHashCode(callSuper = false) +@Data +public class PhotoListVO{ + private Long id; + + @Schema(description = "模式(手动、自动)") + private PhotoModeType mode; + + @Schema(description = "工艺名称(自动模式)") + private String craftsName; + + @TableField(fill = FieldFill.INSERT) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime createTime; + + @TableField(fill = FieldFill.INSERT_UPDATE) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime updateTime; +} diff --git a/src/main/java/com/iflytop/colortitration/common/model/vo/PhotoVO.java b/src/main/java/com/iflytop/colortitration/common/model/vo/PhotoVO.java new file mode 100644 index 0000000..a858189 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/model/vo/PhotoVO.java @@ -0,0 +1,40 @@ +package com.iflytop.colortitration.common.model.vo; + +import com.baomidou.mybatisplus.annotation.FieldFill; +import com.baomidou.mybatisplus.annotation.TableField; +import com.fasterxml.jackson.annotation.JsonFormat; +import com.iflytop.colortitration.common.enums.PhotoModeType; +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +@EqualsAndHashCode(callSuper = false) +@Data +public class PhotoVO { + private Long id; + + @Schema(description = "模式(手动、自动)") + private PhotoModeType mode; + + @Schema(description = "工艺ID") + private Long craftsId; + + @Schema(description = "工艺名称(自动模式)") + private String craftsName; + + @Schema(description = "图片地址") + private String url; + + @Schema(description = "备注") + private String remarks; + + @TableField(fill = FieldFill.INSERT) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime createTime; + + @TableField(fill = FieldFill.INSERT_UPDATE) + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") + private LocalDateTime updateTime; +} diff --git a/src/main/java/com/iflytop/colortitration/common/service/PhotosService.java b/src/main/java/com/iflytop/colortitration/common/service/PhotosService.java new file mode 100644 index 0000000..3091606 --- /dev/null +++ b/src/main/java/com/iflytop/colortitration/common/service/PhotosService.java @@ -0,0 +1,143 @@ +package com.iflytop.colortitration.common.service; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.iflytop.colortitration.app.model.dto.PhotoSaveDTO; +import com.iflytop.colortitration.common.base.BasePageQuery; +import com.iflytop.colortitration.common.enums.PhotoModeType; +import com.iflytop.colortitration.common.exception.AppException; +import com.iflytop.colortitration.common.mapper.PhotosMapper; +import com.iflytop.colortitration.common.model.entity.Photos; +import com.iflytop.colortitration.common.model.vo.PhotoListVO; +import com.iflytop.colortitration.common.model.vo.PhotoVO; +import com.iflytop.colortitration.common.result.ResultCode; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Service; + +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.stream.Collectors; + +/** + * 照片接口服务 + */ +@Slf4j +@Service +@RequiredArgsConstructor +public class PhotosService extends ServiceImpl { + + @Value("${photo.url}") + private String url; + @Value("${photo.path}") + private String path; + + public IPage getList(BasePageQuery pageQuery) { + LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>(); + queryWrapper.orderByDesc(Photos::getCreateTime); + IPage result = this.page(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), queryWrapper); + long count = this.count(); + List dataList = new ArrayList<>(); + for (Photos photos : result.getRecords()) { + PhotoListVO photoListVO = new PhotoListVO(); + photoListVO.setId(photos.getId()); + photoListVO.setMode(photos.getMode()); + photoListVO.setCraftsName(photos.getCraftsName()); + photoListVO.setCreateTime(photos.getCreateTime()); + photoListVO.setUpdateTime(photos.getUpdateTime()); + dataList.add(photoListVO); + } + Page resultPage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()); + resultPage.setTotal(count); + resultPage.setRecords(dataList); + return resultPage; + } + + public PhotoVO get(Long id) { + Photos photos = this.getById(id); + if (photos != null) { + PhotoVO photoVO = new PhotoVO(); + photoVO.setId(photos.getId()); + photoVO.setMode(photos.getMode()); + photoVO.setCraftsName(photos.getCraftsName()); + photoVO.setRemarks(photos.getRemarks()); + photoVO.setCreateTime(photos.getCreateTime()); + photoVO.setUpdateTime(photos.getUpdateTime()); + photoVO.setUrl(url + photos.getImagePath()); + return photoVO; + } + return null; + } + + public String take() 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); +// } +// +// try { +// String tempFilePath = "/temp/" + System.currentTimeMillis() + ".png"; +// String filePath = path + tempFilePath; +// driver.enable(); +// driver.saveColorImg(filePath); +// return url + tempFilePath; +// } catch (Exception e) { +// throw new AppException(ResultCode.SYSTEM_ERROR); +// } finally { +// driver.disable(); +// } + return null; + } + + public void save(PhotoSaveDTO photoSaveDTO) { + 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 + tempPath); + Path targetPath = Paths.get(path + imagePath); + Path targetDirectory = targetPath.getParent(); + if (!Files.exists(targetDirectory)) { + Files.createDirectories(targetDirectory); + } + Files.move(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING); + log.info("照片文件已成功移动!"); + } catch (Exception e) { + log.error("照片文件已成功移动失败 ", e); + throw new AppException(ResultCode.SYSTEM_ERROR); + } + photos = new Photos(); + photos.setImagePath(imagePath); + photos.setMode(PhotoModeType.manual); + } + photos.setRemarks(photoSaveDTO.getRemarks()); + this.saveOrUpdate(photos); + } + + public boolean deletePhoto(String idsStr) { + List ids = Arrays.stream(idsStr.split(",")) + .map(Long::parseLong) + .collect(Collectors.toList()); + return this.removeByIds(ids); + } +} diff --git a/src/main/resources/application-dev.yml b/src/main/resources/application-dev.yml index be6bc21..75f28ab 100644 --- a/src/main/resources/application-dev.yml +++ b/src/main/resources/application-dev.yml @@ -25,3 +25,7 @@ iflytophald: modbus: port: ttyS1 baudrate: 9600 + +photo: + url: http://192.168.8.168/static/photo + path: /home/firefly/package/photo \ No newline at end of file diff --git a/src/main/resources/application-prod.yml b/src/main/resources/application-prod.yml index 3e5c70f..c04b527 100644 --- a/src/main/resources/application-prod.yml +++ b/src/main/resources/application-prod.yml @@ -19,3 +19,7 @@ iflytophald: modbus: port: ttyS1 baudrate: 9600 + +photo: + url: http://192.168.8.168/static/photo + path: /home/firefly/package/photo \ No newline at end of file diff --git a/src/main/resources/application-test.yml b/src/main/resources/application-test.yml index be6bc21..75f28ab 100644 --- a/src/main/resources/application-test.yml +++ b/src/main/resources/application-test.yml @@ -25,3 +25,7 @@ iflytophald: modbus: port: ttyS1 baudrate: 9600 + +photo: + url: http://192.168.8.168/static/photo + path: /home/firefly/package/photo \ No newline at end of file diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 5d71761..45dd4c2 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -18,4 +18,4 @@ mybatis-plus: mapper-locations: classpath*:mapper/*.xml springdoc: - default-flat-param-object: true \ No newline at end of file + default-flat-param-object: true diff --git a/src/main/resources/sql/init.sql b/src/main/resources/sql/init.sql index 8db2f9f..df6da83 100644 --- a/src/main/resources/sql/init.sql +++ b/src/main/resources/sql/init.sql @@ -105,4 +105,16 @@ CREATE TABLE IF NOT EXISTS crafts create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); +-- 照片表 +CREATE TABLE IF NOT EXISTS photos +( + id INTEGER PRIMARY KEY AUTOINCREMENT, -- 主键ID + mode TEXT NOT NULL, -- 模式(手动、自动) + crafts_id INTEGER, -- 工艺ID + crafts_name TEXT, -- 工艺名称(自动模式) + image_path TEXT, -- 图片相对路径 + remarks TEXT, -- 备注 + create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- 创建时间 + update_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP -- 修改时间 +);