13 changed files with 383 additions and 1 deletions
-
64src/main/java/com/iflytop/colortitration/app/controller/PhotoController.java
-
18src/main/java/com/iflytop/colortitration/app/model/dto/PhotoSaveDTO.java
-
18src/main/java/com/iflytop/colortitration/common/enums/PhotoModeType.java
-
13src/main/java/com/iflytop/colortitration/common/mapper/PhotosMapper.java
-
31src/main/java/com/iflytop/colortitration/common/model/entity/Photos.java
-
31src/main/java/com/iflytop/colortitration/common/model/vo/PhotoListVO.java
-
40src/main/java/com/iflytop/colortitration/common/model/vo/PhotoVO.java
-
143src/main/java/com/iflytop/colortitration/common/service/PhotosService.java
-
4src/main/resources/application-dev.yml
-
4src/main/resources/application-prod.yml
-
4src/main/resources/application-test.yml
-
2src/main/resources/application.yml
-
12src/main/resources/sql/init.sql
@ -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<PhotoListVO> getList(BasePageQuery pageQuery) { |
|||
return PageResult.success(photosService.getList(pageQuery)); |
|||
} |
|||
|
|||
@Operation(summary = "根据id获取照片") |
|||
@GetMapping("/{id}") |
|||
public Result<PhotoVO> get(@PathVariable Long id) { |
|||
return Result.success(photosService.get(id)); |
|||
} |
|||
|
|||
|
|||
@Operation(summary = "拍摄一张照片") |
|||
@PostMapping("/take") |
|||
public Result<String> 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<String> delete(@Parameter(description = "ID,多个以英文逗号(,)分割") @PathVariable String ids) { |
|||
boolean isSuccess = photosService.deletePhoto(ids); |
|||
if (isSuccess) { |
|||
return Result.success(); |
|||
} |
|||
return Result.failed(); |
|||
} |
|||
} |
@ -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; |
|||
} |
@ -0,0 +1,18 @@ |
|||
package com.iflytop.colortitration.common.enums; |
|||
|
|||
import lombok.Getter; |
|||
|
|||
/** |
|||
* 照片数据类型 |
|||
*/ |
|||
@Getter |
|||
public enum PhotoModeType { |
|||
/** |
|||
* 工艺 |
|||
*/ |
|||
crafts, |
|||
/** |
|||
* 手动 |
|||
*/ |
|||
manual |
|||
} |
@ -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<Photos> { |
|||
|
|||
} |
@ -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; |
|||
} |
@ -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; |
|||
} |
@ -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; |
|||
} |
@ -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<PhotosMapper, Photos> { |
|||
|
|||
@Value("${photo.url}") |
|||
private String url; |
|||
@Value("${photo.path}") |
|||
private String path; |
|||
|
|||
public IPage<PhotoListVO> getList(BasePageQuery pageQuery) { |
|||
LambdaQueryWrapper<Photos> queryWrapper = new LambdaQueryWrapper<>(); |
|||
queryWrapper.orderByDesc(Photos::getCreateTime); |
|||
IPage<Photos> result = this.page(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()), queryWrapper); |
|||
long count = this.count(); |
|||
List<PhotoListVO> 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<PhotoListVO> 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<Path> 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<Long> ids = Arrays.stream(idsStr.split(",")) |
|||
.map(Long::parseLong) |
|||
.collect(Collectors.toList()); |
|||
return this.removeByIds(ids); |
|||
} |
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue