Commit 2b3593dfe019080602402014cdfd32ae03cdc1fa

Authored by cherishsince
1 parent 000bcf11

【优化】抽离 midjourneyNotify 需要更新的 image 对象,方便在job中公用

yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/image/AiImageService.java
@@ -65,6 +65,15 @@ public interface AiImageService { @@ -65,6 +65,15 @@ public interface AiImageService {
65 Boolean midjourneyNotify(MidjourneyNotifyReqVO notifyReqVO); 65 Boolean midjourneyNotify(MidjourneyNotifyReqVO notifyReqVO);
66 66
67 /** 67 /**
  68 + * 构建 midjourney - 更新对象
  69 + *
  70 + * @param imageId
  71 + * @param notifyReqVO
  72 + * @return
  73 + */
  74 + AiImageDO buildUpdateImage(Long imageId, MidjourneyNotifyReqVO notifyReqVO);
  75 +
  76 + /**
68 * midjourney - action(放大、缩小、U1、U2...) 77 * midjourney - action(放大、缩小、U1、U2...)
69 * 78 *
70 * @param loginUserId 79 * @param loginUserId
yudao-module-ai/yudao-module-ai-biz/src/main/java/cn/iocoder/yudao/module/ai/service/image/AiImageServiceImpl.java
@@ -194,6 +194,13 @@ public class AiImageServiceImpl implements AiImageService { @@ -194,6 +194,13 @@ public class AiImageServiceImpl implements AiImageService {
194 return false; 194 return false;
195 } 195 }
196 // 2、转换状态 196 // 2、转换状态
  197 + AiImageDO updateImage = buildUpdateImage(image.getId(), notifyReqVO);
  198 + // 3、更新 image 状态
  199 + return imageMapper.updateById(updateImage) > 0;
  200 + }
  201 +
  202 + public AiImageDO buildUpdateImage(Long imageId, MidjourneyNotifyReqVO notifyReqVO) {
  203 + // 2、转换状态
197 String imageStatus = null; 204 String imageStatus = null;
198 MidjourneyTaskStatusEnum taskStatusEnum = MidjourneyTaskStatusEnum.valueOf(notifyReqVO.getStatus()); 205 MidjourneyTaskStatusEnum taskStatusEnum = MidjourneyTaskStatusEnum.valueOf(notifyReqVO.getStatus());
199 if (MidjourneyTaskStatusEnum.SUCCESS == taskStatusEnum) { 206 if (MidjourneyTaskStatusEnum.SUCCESS == taskStatusEnum) {
@@ -211,17 +218,14 @@ public class AiImageServiceImpl implements AiImageService { @@ -211,17 +218,14 @@ public class AiImageServiceImpl implements AiImageService {
211 } 218 }
212 } 219 }
213 // 4、更新 image 状态 220 // 4、更新 image 状态
214 - imageMapper.updateById(  
215 - new AiImageDO()  
216 - .setId(image.getId())  
217 - .setStatus(imageStatus)  
218 - .setPicUrl(filePath)  
219 - .setProgress(notifyReqVO.getProgress())  
220 - .setResponse(notifyReqVO)  
221 - .setButtons(notifyReqVO.getButtons())  
222 - .setErrorMessage(notifyReqVO.getFailReason())  
223 - );  
224 - return true; 221 + return new AiImageDO()
  222 + .setId(imageId)
  223 + .setStatus(imageStatus)
  224 + .setPicUrl(filePath)
  225 + .setProgress(notifyReqVO.getProgress())
  226 + .setResponse(notifyReqVO)
  227 + .setButtons(notifyReqVO.getButtons())
  228 + .setErrorMessage(notifyReqVO.getFailReason());
225 } 229 }
226 230
227 @Override 231 @Override
@@ -234,12 +238,28 @@ public class AiImageServiceImpl implements AiImageService { @@ -234,12 +238,28 @@ public class AiImageServiceImpl implements AiImageService {
234 throw exception(AI_IMAGE_CUSTOM_ID_NOT_EXISTS); 238 throw exception(AI_IMAGE_CUSTOM_ID_NOT_EXISTS);
235 } 239 }
236 // 3、调用 midjourney proxy 240 // 3、调用 midjourney proxy
237 - midjourneyProxyClient.action( 241 + MidjourneySubmitRespVO submitRespVO = midjourneyProxyClient.action(
238 new MidjourneyActionReqVO() 242 new MidjourneyActionReqVO()
239 .setCustomId(customId) 243 .setCustomId(customId)
240 .setTaskId(aiImageDO.getTaskId()) 244 .setTaskId(aiImageDO.getTaskId())
241 .setNotifyHook(midjourneyNotifyUrl) 245 .setNotifyHook(midjourneyNotifyUrl)
242 ); 246 );
  247 + // 6、保存任务 id (状态码: 1(提交成功), 21(已存在), 22(排队中), other(错误))
  248 + if (!MidjourneySubmitCodeEnum.SUCCESS_CODES.contains(submitRespVO.getCode())) {
  249 + throw exception(AI_IMAGE_MIDJOURNEY_SUBMIT_FAIL, submitRespVO.getDescription());
  250 + }
  251 + // 4、新增 image 记录
  252 + AiImageDO newImage = BeanUtils.toBean(aiImageDO, AiImageDO.class);
  253 + // 4.1、重置参数
  254 + newImage.setId(null);
  255 + newImage.setStatus(AiImageStatusEnum.IN_PROGRESS.getStatus());
  256 + newImage.setPicUrl(null);
  257 + newImage.setResponse(null);
  258 + newImage.setProgress(null);
  259 + newImage.setTaskId(submitRespVO.getResult());
  260 + newImage.setErrorMessage(null);
  261 + // 4.2、保存数据库
  262 + imageMapper.insert(newImage);
243 return Boolean.TRUE; 263 return Boolean.TRUE;
244 } 264 }
245 265