【代码评审】AI:AI 绘图的逻辑

This commit is contained in:
YunaiV 2024-05-30 09:35:23 +08:00
parent e4a0512b51
commit 342f603964
8 changed files with 46 additions and 30 deletions

View File

@ -42,5 +42,7 @@ public interface ErrorCodeConstants {
// ========== API 绘画 1-040-005-000 ========== // ========== API 绘画 1-040-005-000 ==========
// TODO @fan这个直接返回找不到图片就好了
ErrorCode AI_IMAGE_NOT_CREATE_USER = new ErrorCode(1_022_005_000, "不是创建用户,不能删除 image!"); ErrorCode AI_IMAGE_NOT_CREATE_USER = new ErrorCode(1_022_005_000, "不是创建用户,不能删除 image!");
} }

View File

@ -4,11 +4,9 @@ import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
/** /**
* ai绘画状态 * AI 绘画状态的枚举
* *
* @author fansili * @author fansili
* @time 2024/4/28 17:05
* @since 1.0
*/ */
@AllArgsConstructor @AllArgsConstructor
@Getter @Getter
@ -16,22 +14,24 @@ public enum AiImageStatusEnum {
IN_PROGRESS("10", "进行中"), IN_PROGRESS("10", "进行中"),
COMPLETE("20", "完成"), COMPLETE("20", "完成"),
FAIL("30", "失败"), FAIL("30", "失败");
; /**
* 状态
// TODO @fanfinal 一下 */
private final String status; private final String status;
/**
* 状态名
*/
private final String name; private final String name;
public static AiImageStatusEnum valueOfStatus(String status) { public static AiImageStatusEnum valueOfStatus(String status) {
for (AiImageStatusEnum itemEnum : AiImageStatusEnum.values()) { for (AiImageStatusEnum statusEnum : AiImageStatusEnum.values()) {
if (itemEnum.getStatus().equals(status)) { if (statusEnum.getStatus().equals(status)) {
return itemEnum; return statusEnum;
} }
} }
throw new IllegalArgumentException("Invalid MessageType value: " + status); throw new IllegalArgumentException("未知会话状态: " + status);
} }
} }

View File

@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.ai.controller.admin.image.vo.*; import cn.iocoder.yudao.module.ai.controller.admin.image.vo.*;
import cn.iocoder.yudao.module.ai.service.image.AiImageService; import cn.iocoder.yudao.module.ai.service.image.AiImageService;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -23,13 +24,15 @@ public class AiImageController {
@Resource @Resource
private AiImageService aiImageService; private AiImageService aiImageService;
@Operation(summary = "获取 - 我的分页列表", description = "dall3、midjourney") // TODO @fan方法名叫做getImagePageMy 我们的命名还是以动名词哈不考虑省略名词的原因是担心一个 Service 扩多个模块纯粹动词无法表达
@Operation(summary = "获取【我的】绘图分页")
@GetMapping("/my-page") @GetMapping("/my-page")
public CommonResult<PageResult<AiImageListRespVO>> myPage(@Validated AiImageListReqVO req) { public CommonResult<PageResult<AiImageListRespVO>> myPage(@Validated AiImageListReqVO req) {
return success(aiImageService.list(req)); return success(aiImageService.list(req));
} }
@Operation(summary = "获取 - 我的 image 信息", description = "...") // TODO @fan类似 /my-page 的建议
@Operation(summary = "获取【我的】绘图记录", description = "...")
@GetMapping("/get-my") @GetMapping("/get-my")
public CommonResult<AiImageListRespVO> getMy(@RequestParam("id") Long id) { public CommonResult<AiImageListRespVO> getMy(@RequestParam("id") Long id) {
return CommonResult.success(aiImageService.getMy(id)); return CommonResult.success(aiImageService.getMy(id));
@ -64,9 +67,13 @@ public class AiImageController {
return success(null); return success(null);
} }
@Operation(summary = "删除绘画记录", description = "") // TODO @fan类似 /my-page 的建议
// TODO @fan目前如果没结果返回 Boolean
@Operation(summary = "删除【我的】绘画记录")
@DeleteMapping("/delete-my") @DeleteMapping("/delete-my")
@Parameter(name = "id", required = true, description = "绘画编号", example = "1024")
public CommonResult<Void> deleteMy(@RequestParam("id") Long id) { public CommonResult<Void> deleteMy(@RequestParam("id") Long id) {
// TODO @fan这种一次性的 loginUserId可以不用定义变量直接当参数传递
Long loginUserId = getLoginUserId(); Long loginUserId = getLoginUserId();
aiImageService.deleteMy(id, loginUserId); aiImageService.deleteMy(id, loginUserId);
return success(null); return success(null);

View File

@ -44,17 +44,18 @@ public class AiImageDallRespVO {
@Schema(description = "是否发布", example = "public") @Schema(description = "是否发布", example = "public")
private String publicStatus; private String publicStatus;
@Schema(description = "图片地址(自己服务器)", example = "http://") @Schema(description = "图片地址(自己服务器)", example = "https://")
private String picUrl; private String picUrl;
@Schema(description = "绘画图片地址(绘画好的服务器)", example = "http://") @Schema(description = "绘画图片地址(绘画好的服务器)", example = "https://")
private String originalPicUrl; private String originalPicUrl;
@Schema(description = "绘画错误信息", example = "图片错误信息") @Schema(description = "绘画错误信息", example = "图片错误信息")
private String errorMessage; private String errorMessage;
// ============ 绘画请求参数 // ============ 绘画请求参数 ============
// todo @fan下面的 stylemjNonceId 直接就不用注释啦直接去看 DO 完事哈
/** /**
* - style * - style
*/ */

View File

@ -12,6 +12,7 @@ import lombok.experimental.Accessors;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Map; import java.util.Map;
// TODO @fan可以考虑复用 AiImageDallRespVO统一成 AIImageRespVO
/** /**
* midjourney req * midjourney req
* *
@ -61,6 +62,8 @@ public class AiImageListRespVO extends PageParam {
// ============ 绘画请求参数 // ============ 绘画请求参数
// todo @fan下面的 stylemjNonceId 直接就不用注释啦直接去看 DO 完事哈
/** /**
* - style * - style
*/ */

View File

@ -20,10 +20,12 @@ import java.util.Map;
@Data @Data
public class AiImageDO extends BaseDO { public class AiImageDO extends BaseDO {
// TODO @fan1使用 java 注释哈不要注解2关联枚举字段要关联到对应类参考 AiChatMessageDO 的注释
@TableId(type = IdType.AUTO) @TableId(type = IdType.AUTO)
private Long id; private Long id;
@Schema(description = "用户id") @Schema(description = "用户编号")
private Long userId; private Long userId;
@Schema(description = "提示词") @Schema(description = "提示词")
@ -41,6 +43,7 @@ public class AiImageDO extends BaseDO {
@Schema(description = "图片高度") @Schema(description = "图片高度")
private String height; private String height;
// TODO @fan这种就注释绘画状态然后枚举类关联下就好啦
@Schema(description = "绘画状态:提交、排队、绘画中、绘画完成、绘画失败") @Schema(description = "绘画状态:提交、排队、绘画中、绘画完成、绘画失败")
private String status; private String status;
@ -53,10 +56,7 @@ public class AiImageDO extends BaseDO {
@Schema(description = "绘画图片地址(绘画好的服务器)") @Schema(description = "绘画图片地址(绘画好的服务器)")
private String originalPicUrl; private String originalPicUrl;
@Schema(description = "绘画错误信息") // ============ 绘画请求参数 ============
private String errorMessage;
// ============ 绘画请求参数
/** /**
* - style * - style
@ -75,5 +75,8 @@ public class AiImageDO extends BaseDO {
@TableField(typeHandler = JacksonTypeHandler.class) @TableField(typeHandler = JacksonTypeHandler.class)
private Map<String, Object> drawResponse; private Map<String, Object> drawResponse;
@Schema(description = "绘画错误信息")
private String errorMessage;
} }

View File

@ -7,16 +7,14 @@ import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
/** /**
* ai image * AI 绘图 Mapper
* *
* @author fansili * @author fansili
* @time 2024/4/28 14:01
* @since 1.0
*/ */
@Mapper @Mapper
public interface AiImageMapper extends BaseMapperX<AiImageDO> { public interface AiImageMapper extends BaseMapperX<AiImageDO> {
// TODO @fan这个建议直接使用 updateservice 拼接要改的状态哈
/** /**
* 更新 - 根据 messageId * 更新 - 根据 messageId
* *

View File

@ -200,10 +200,12 @@ public class AiImageServiceImpl implements AiImageService {
// TODO @fan1需要校验存在2需要校验属于我 // TODO @fan1需要校验存在2需要校验属于我
@Override @Override
public void deleteMy(Long id, Long loginUserId) { public void deleteMy(Long id, Long userId) {
// 校验记录是否存在 // 校验记录是否存在
// TODO @fanaiImageDO 这种命名 image ok 更简洁
// TODO @fan下面这个可以返回图片不存在
AiImageDO aiImageDO = validateExists(id); AiImageDO aiImageDO = validateExists(id);
if (!aiImageDO.getUserId().equals(loginUserId)) { if (!aiImageDO.getUserId().equals(userId)) {
throw exception(ErrorCodeConstants.AI_IMAGE_NOT_CREATE_USER); throw exception(ErrorCodeConstants.AI_IMAGE_NOT_CREATE_USER);
} }
// 删除记录 // 删除记录