Commit 342f603964aae28373f2cc830d4c164238e2fc39
1 parent
e4a0512b
【代码评审】AI:AI 绘图的逻辑
Showing
8 changed files
with
46 additions
and
30 deletions
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/ErrorCodeConstants.java
| @@ -42,5 +42,7 @@ public interface ErrorCodeConstants { | @@ -42,5 +42,7 @@ public interface ErrorCodeConstants { | ||
| 42 | 42 | ||
| 43 | // ========== API 绘画 1-040-005-000 ========== | 43 | // ========== API 绘画 1-040-005-000 ========== |
| 44 | 44 | ||
| 45 | + // TODO @fan:这个直接返回找不到图片就好了 | ||
| 45 | ErrorCode AI_IMAGE_NOT_CREATE_USER = new ErrorCode(1_022_005_000, "不是创建用户,不能删除 image!"); | 46 | ErrorCode AI_IMAGE_NOT_CREATE_USER = new ErrorCode(1_022_005_000, "不是创建用户,不能删除 image!"); |
| 47 | + | ||
| 46 | } | 48 | } |
yudao-module-ai/yudao-module-ai-api/src/main/java/cn/iocoder/yudao/module/ai/enums/AiImageStatusEnum.java
| @@ -4,11 +4,9 @@ import lombok.AllArgsConstructor; | @@ -4,11 +4,9 @@ import lombok.AllArgsConstructor; | ||
| 4 | import lombok.Getter; | 4 | import lombok.Getter; |
| 5 | 5 | ||
| 6 | /** | 6 | /** |
| 7 | - * ai绘画状态 | 7 | + * AI 绘画状态的枚举 |
| 8 | * | 8 | * |
| 9 | * @author fansili | 9 | * @author fansili |
| 10 | - * @time 2024/4/28 17:05 | ||
| 11 | - * @since 1.0 | ||
| 12 | */ | 10 | */ |
| 13 | @AllArgsConstructor | 11 | @AllArgsConstructor |
| 14 | @Getter | 12 | @Getter |
| @@ -16,22 +14,24 @@ public enum AiImageStatusEnum { | @@ -16,22 +14,24 @@ public enum AiImageStatusEnum { | ||
| 16 | 14 | ||
| 17 | IN_PROGRESS("10", "进行中"), | 15 | IN_PROGRESS("10", "进行中"), |
| 18 | COMPLETE("20", "完成"), | 16 | COMPLETE("20", "完成"), |
| 19 | - FAIL("30", "失败"), | 17 | + FAIL("30", "失败"); |
| 20 | 18 | ||
| 21 | - ; | ||
| 22 | - | ||
| 23 | - // TODO @fan:final 一下 | 19 | + /** |
| 20 | + * 状态 | ||
| 21 | + */ | ||
| 24 | private final String status; | 22 | private final String status; |
| 25 | - | 23 | + /** |
| 24 | + * 状态名 | ||
| 25 | + */ | ||
| 26 | private final String name; | 26 | private final String name; |
| 27 | 27 | ||
| 28 | - | ||
| 29 | public static AiImageStatusEnum valueOfStatus(String status) { | 28 | public static AiImageStatusEnum valueOfStatus(String status) { |
| 30 | - for (AiImageStatusEnum itemEnum : AiImageStatusEnum.values()) { | ||
| 31 | - if (itemEnum.getStatus().equals(status)) { | ||
| 32 | - return itemEnum; | 29 | + for (AiImageStatusEnum statusEnum : AiImageStatusEnum.values()) { |
| 30 | + if (statusEnum.getStatus().equals(status)) { | ||
| 31 | + return statusEnum; | ||
| 33 | } | 32 | } |
| 34 | } | 33 | } |
| 35 | - throw new IllegalArgumentException("Invalid MessageType value: " + status); | 34 | + throw new IllegalArgumentException("未知会话状态: " + status); |
| 36 | } | 35 | } |
| 36 | + | ||
| 37 | } | 37 | } |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/AiImageController.java
| @@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | @@ -5,6 +5,7 @@ import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 5 | import cn.iocoder.yudao.module.ai.controller.admin.image.vo.*; | 5 | import cn.iocoder.yudao.module.ai.controller.admin.image.vo.*; |
| 6 | import cn.iocoder.yudao.module.ai.service.image.AiImageService; | 6 | import cn.iocoder.yudao.module.ai.service.image.AiImageService; |
| 7 | import io.swagger.v3.oas.annotations.Operation; | 7 | import io.swagger.v3.oas.annotations.Operation; |
| 8 | +import io.swagger.v3.oas.annotations.Parameter; | ||
| 8 | import io.swagger.v3.oas.annotations.tags.Tag; | 9 | import io.swagger.v3.oas.annotations.tags.Tag; |
| 9 | import jakarta.annotation.Resource; | 10 | import jakarta.annotation.Resource; |
| 10 | import lombok.extern.slf4j.Slf4j; | 11 | import lombok.extern.slf4j.Slf4j; |
| @@ -23,13 +24,15 @@ public class AiImageController { | @@ -23,13 +24,15 @@ public class AiImageController { | ||
| 23 | @Resource | 24 | @Resource |
| 24 | private AiImageService aiImageService; | 25 | private AiImageService aiImageService; |
| 25 | 26 | ||
| 26 | - @Operation(summary = "获取 - 我的分页列表", description = "dall3、midjourney") | 27 | + // TODO @fan:方法名叫做,getImagePageMy ;我们的命名,还是以动名词哈。不考虑省略名词的原因,是担心一个 Service 扩多个模块,纯粹动词无法表达 |
| 28 | + @Operation(summary = "获取【我的】绘图分页") | ||
| 27 | @GetMapping("/my-page") | 29 | @GetMapping("/my-page") |
| 28 | public CommonResult<PageResult<AiImageListRespVO>> myPage(@Validated AiImageListReqVO req) { | 30 | public CommonResult<PageResult<AiImageListRespVO>> myPage(@Validated AiImageListReqVO req) { |
| 29 | return success(aiImageService.list(req)); | 31 | return success(aiImageService.list(req)); |
| 30 | } | 32 | } |
| 31 | 33 | ||
| 32 | - @Operation(summary = "获取 - 我的 image 信息", description = "...") | 34 | + // TODO @fan:类似 /my-page 的建议 |
| 35 | + @Operation(summary = "获取【我的】绘图记录", description = "...") | ||
| 33 | @GetMapping("/get-my") | 36 | @GetMapping("/get-my") |
| 34 | public CommonResult<AiImageListRespVO> getMy(@RequestParam("id") Long id) { | 37 | public CommonResult<AiImageListRespVO> getMy(@RequestParam("id") Long id) { |
| 35 | return CommonResult.success(aiImageService.getMy(id)); | 38 | return CommonResult.success(aiImageService.getMy(id)); |
| @@ -64,9 +67,13 @@ public class AiImageController { | @@ -64,9 +67,13 @@ public class AiImageController { | ||
| 64 | return success(null); | 67 | return success(null); |
| 65 | } | 68 | } |
| 66 | 69 | ||
| 67 | - @Operation(summary = "删除绘画记录", description = "") | 70 | + // TODO @fan:类似 /my-page 的建议 |
| 71 | + // TODO @fan:目前如果没结果,返回 Boolean 哈 | ||
| 72 | + @Operation(summary = "删除【我的】绘画记录") | ||
| 68 | @DeleteMapping("/delete-my") | 73 | @DeleteMapping("/delete-my") |
| 74 | + @Parameter(name = "id", required = true, description = "绘画编号", example = "1024") | ||
| 69 | public CommonResult<Void> deleteMy(@RequestParam("id") Long id) { | 75 | public CommonResult<Void> deleteMy(@RequestParam("id") Long id) { |
| 76 | + // TODO @fan:这种一次性的 loginUserId,可以不用定义变量,直接当参数传递 | ||
| 70 | Long loginUserId = getLoginUserId(); | 77 | Long loginUserId = getLoginUserId(); |
| 71 | aiImageService.deleteMy(id, loginUserId); | 78 | aiImageService.deleteMy(id, loginUserId); |
| 72 | return success(null); | 79 | return success(null); |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageDallRespVO.java
| @@ -44,17 +44,18 @@ public class AiImageDallRespVO { | @@ -44,17 +44,18 @@ public class AiImageDallRespVO { | ||
| 44 | @Schema(description = "是否发布", example = "public") | 44 | @Schema(description = "是否发布", example = "public") |
| 45 | private String publicStatus; | 45 | private String publicStatus; |
| 46 | 46 | ||
| 47 | - @Schema(description = "图片地址(自己服务器)", example = "http://") | 47 | + @Schema(description = "图片地址(自己服务器)", example = "https://") |
| 48 | private String picUrl; | 48 | private String picUrl; |
| 49 | 49 | ||
| 50 | - @Schema(description = "绘画图片地址(绘画好的服务器)", example = "http://") | 50 | + @Schema(description = "绘画图片地址(绘画好的服务器)", example = "https://") |
| 51 | private String originalPicUrl; | 51 | private String originalPicUrl; |
| 52 | 52 | ||
| 53 | @Schema(description = "绘画错误信息", example = "图片错误信息") | 53 | @Schema(description = "绘画错误信息", example = "图片错误信息") |
| 54 | private String errorMessage; | 54 | private String errorMessage; |
| 55 | 55 | ||
| 56 | - // ============ 绘画请求参数 | 56 | + // ============ 绘画请求参数 ============ |
| 57 | 57 | ||
| 58 | + // todo @fan:下面的 style、mjNonceId 直接就不用注释啦,直接去看 DO 完事哈 | ||
| 58 | /** | 59 | /** |
| 59 | * - style | 60 | * - style |
| 60 | */ | 61 | */ |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/controller/admin/image/vo/AiImageListRespVO.java
| @@ -12,6 +12,7 @@ import lombok.experimental.Accessors; | @@ -12,6 +12,7 @@ import lombok.experimental.Accessors; | ||
| 12 | import java.time.LocalDateTime; | 12 | import java.time.LocalDateTime; |
| 13 | import java.util.Map; | 13 | import java.util.Map; |
| 14 | 14 | ||
| 15 | +// TODO @fan:可以考虑,复用 AiImageDallRespVO,统一成 AIImageRespVO | ||
| 15 | /** | 16 | /** |
| 16 | * midjourney req | 17 | * midjourney req |
| 17 | * | 18 | * |
| @@ -61,6 +62,8 @@ public class AiImageListRespVO extends PageParam { | @@ -61,6 +62,8 @@ public class AiImageListRespVO extends PageParam { | ||
| 61 | 62 | ||
| 62 | // ============ 绘画请求参数 | 63 | // ============ 绘画请求参数 |
| 63 | 64 | ||
| 65 | + // todo @fan:下面的 style、mjNonceId 直接就不用注释啦,直接去看 DO 完事哈 | ||
| 66 | + | ||
| 64 | /** | 67 | /** |
| 65 | * - style | 68 | * - style |
| 66 | */ | 69 | */ |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/dataobject/image/AiImageDO.java
| @@ -20,10 +20,12 @@ import java.util.Map; | @@ -20,10 +20,12 @@ import java.util.Map; | ||
| 20 | @Data | 20 | @Data |
| 21 | public class AiImageDO extends BaseDO { | 21 | public class AiImageDO extends BaseDO { |
| 22 | 22 | ||
| 23 | + // TODO @fan:1)使用 java 注释哈,不要注解。2)关联、枚举字段,要关联到对应类,参考 AiChatMessageDO 的注释 | ||
| 24 | + | ||
| 23 | @TableId(type = IdType.AUTO) | 25 | @TableId(type = IdType.AUTO) |
| 24 | private Long id; | 26 | private Long id; |
| 25 | 27 | ||
| 26 | - @Schema(description = "用户id") | 28 | + @Schema(description = "用户编号") |
| 27 | private Long userId; | 29 | private Long userId; |
| 28 | 30 | ||
| 29 | @Schema(description = "提示词") | 31 | @Schema(description = "提示词") |
| @@ -41,6 +43,7 @@ public class AiImageDO extends BaseDO { | @@ -41,6 +43,7 @@ public class AiImageDO extends BaseDO { | ||
| 41 | @Schema(description = "图片高度") | 43 | @Schema(description = "图片高度") |
| 42 | private String height; | 44 | private String height; |
| 43 | 45 | ||
| 46 | + // TODO @fan:这种就注释绘画状态,然后枚举类关联下就好啦 | ||
| 44 | @Schema(description = "绘画状态:提交、排队、绘画中、绘画完成、绘画失败") | 47 | @Schema(description = "绘画状态:提交、排队、绘画中、绘画完成、绘画失败") |
| 45 | private String status; | 48 | private String status; |
| 46 | 49 | ||
| @@ -53,10 +56,7 @@ public class AiImageDO extends BaseDO { | @@ -53,10 +56,7 @@ public class AiImageDO extends BaseDO { | ||
| 53 | @Schema(description = "绘画图片地址(绘画好的服务器)") | 56 | @Schema(description = "绘画图片地址(绘画好的服务器)") |
| 54 | private String originalPicUrl; | 57 | private String originalPicUrl; |
| 55 | 58 | ||
| 56 | - @Schema(description = "绘画错误信息") | ||
| 57 | - private String errorMessage; | ||
| 58 | - | ||
| 59 | - // ============ 绘画请求参数 | 59 | + // ============ 绘画请求参数 ============ |
| 60 | 60 | ||
| 61 | /** | 61 | /** |
| 62 | * - style | 62 | * - style |
| @@ -75,5 +75,8 @@ public class AiImageDO extends BaseDO { | @@ -75,5 +75,8 @@ public class AiImageDO extends BaseDO { | ||
| 75 | @TableField(typeHandler = JacksonTypeHandler.class) | 75 | @TableField(typeHandler = JacksonTypeHandler.class) |
| 76 | private Map<String, Object> drawResponse; | 76 | private Map<String, Object> drawResponse; |
| 77 | 77 | ||
| 78 | + @Schema(description = "绘画错误信息") | ||
| 79 | + private String errorMessage; | ||
| 80 | + | ||
| 78 | } | 81 | } |
| 79 | 82 |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/dal/mysql/image/AiImageMapper.java
| @@ -7,16 +7,14 @@ import org.apache.ibatis.annotations.Mapper; | @@ -7,16 +7,14 @@ import org.apache.ibatis.annotations.Mapper; | ||
| 7 | import org.springframework.stereotype.Repository; | 7 | import org.springframework.stereotype.Repository; |
| 8 | 8 | ||
| 9 | /** | 9 | /** |
| 10 | - * ai image | 10 | + * AI 绘图 Mapper |
| 11 | * | 11 | * |
| 12 | * @author fansili | 12 | * @author fansili |
| 13 | - * @time 2024/4/28 14:01 | ||
| 14 | - * @since 1.0 | ||
| 15 | */ | 13 | */ |
| 16 | @Mapper | 14 | @Mapper |
| 17 | public interface AiImageMapper extends BaseMapperX<AiImageDO> { | 15 | public interface AiImageMapper extends BaseMapperX<AiImageDO> { |
| 18 | 16 | ||
| 19 | - | 17 | + // TODO @fan:这个建议,直接使用 update,service 拼接要改的状态哈 |
| 20 | /** | 18 | /** |
| 21 | * 更新 - 根据 messageId | 19 | * 更新 - 根据 messageId |
| 22 | * | 20 | * |
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/image/AiImageServiceImpl.java
| @@ -200,10 +200,12 @@ public class AiImageServiceImpl implements AiImageService { | @@ -200,10 +200,12 @@ public class AiImageServiceImpl implements AiImageService { | ||
| 200 | 200 | ||
| 201 | // TODO @fan:1)需要校验存在;2)需要校验属于我; | 201 | // TODO @fan:1)需要校验存在;2)需要校验属于我; |
| 202 | @Override | 202 | @Override |
| 203 | - public void deleteMy(Long id, Long loginUserId) { | 203 | + public void deleteMy(Long id, Long userId) { |
| 204 | // 校验记录是否存在 | 204 | // 校验记录是否存在 |
| 205 | + // TODO @fan:aiImageDO 这种命名 image 就 ok 拉,更简洁 | ||
| 206 | + // TODO @fan:下面这个,可以返回图片不存在 | ||
| 205 | AiImageDO aiImageDO = validateExists(id); | 207 | AiImageDO aiImageDO = validateExists(id); |
| 206 | - if (!aiImageDO.getUserId().equals(loginUserId)) { | 208 | + if (!aiImageDO.getUserId().equals(userId)) { |
| 207 | throw exception(ErrorCodeConstants.AI_IMAGE_NOT_CREATE_USER); | 209 | throw exception(ErrorCodeConstants.AI_IMAGE_NOT_CREATE_USER); |
| 208 | } | 210 | } |
| 209 | // 删除记录 | 211 | // 删除记录 |