Browse Source

fix 增加未执行状态

zwhui 2 năm trước cách đây
mục cha
commit
cb8764e924

+ 27 - 31
netflix-service/src/main/java/com/cyksj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -89,14 +89,16 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             imagineParam.put("base64Array", base64Array);
         }
         SubmitResult result = submit(user.getMode(),"imagine", null,imagineParam);
-        return saveConversation(user.getId(), user.getMode(),Long.parseLong(result.getResult()),result.getProperties(),"IMAGINE", StringUtils.EMPTY);
+        return saveConversation(user.getId(), user.getMode(),result,"IMAGINE", StringUtils.EMPTY);
     }
 
-    public MidjourneyUserConversation saveConversation(Long userId,Integer mode,Long taskId,Map<String,Object> properties, String action,String prompt) throws Exception {
+    public MidjourneyUserConversation saveConversation(Long userId,Integer mode,SubmitResult result, String action,String prompt) throws Exception {
+        Long taskId = Long.parseLong(result.getResult());
+        Map<String, Object> properties = result.getProperties();
         MidjourneyUserConversation conversation = new MidjourneyUserConversation()
                 .setUserId(userId).setTaskId(taskId).setAction(action).setPrompt(prompt)
                 .setMode(mode).setStartTime(System.currentTimeMillis()).setProgress("0%")
-                .setStatus("IN_PROGRESS").setTaskId(taskId);
+                .setStatus(result.getCode() == 21 ? "NOT_START" : "IN_PROGRESS").setTaskId(taskId);
         if (MapUtil.isNotEmpty(properties)) {
             conversation.setProperties(Jsons.toJson(properties));
             conversation.setChannelId(properties.get("discordChannelId") == null ? null : Long.parseLong(properties.get("discordChannelId").toString()))
@@ -111,7 +113,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
     public MidjourneyUserConversation submitDescribe(MidjourneyUser user, String base64) throws Exception {
         Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>()).put("state", user.getId()).put("base64", base64).build();
         SubmitResult result = submit(user.getMode(),"describe", null, param);
-        return saveConversation(user.getId(), user.getMode(),Long.parseLong(result.getResult()),result.getProperties(),"DESCRIBE", StringUtils.EMPTY);
+        return saveConversation(user.getId(), user.getMode(),result,"DESCRIBE", StringUtils.EMPTY);
     }
 
     @Override
@@ -122,14 +124,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             param.put("dimensions", dimensions);
         }
         SubmitResult result = submit(user.getMode(),"blend", null, param);
-        Map<String, Object> properties = result.getProperties();
-//        if (user.getMode() == 1) {
-//            String finalPrompt = "%s --ar %s --style raw --s 250";
-//            List<String> picList = uploadBase64Pic(base64Array);
-//            String pics = picList.stream().map(pic -> "<" + pic + ">").collect(Collectors.joining(" "));
-//            properties.put("finalPrompt", String.format(finalPrompt, pics,dimensions.getValue()));
-//        }
-        return saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),properties,"BLEND", StringUtils.EMPTY);
+        return saveConversation(user.getId(), user.getMode(),result,"BLEND", StringUtils.EMPTY);
     }
 
     @Override
@@ -143,7 +138,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             param.put("maskBase64", maskBase64);
         }
         SubmitResult result = submit(user.getMode(),"modal", null, param);
-        return saveConversation(user.getId(),  user.getMode(),Long.parseLong(result.getResult()),result.getProperties(),"MODAL", StringUtils.EMPTY);
+        return saveConversation(user.getId(),  user.getMode(),result,"MODAL", StringUtils.EMPTY);
     }
 
     @Override
@@ -151,7 +146,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
         Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
                 .put("prompt", prompt).put("state", user.getId()).build();
         SubmitResult result = submit(user.getMode(),"shorten", null, param);
-        return saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),result.getProperties(),"SHORTEN", StringUtils.EMPTY);
+        return saveConversation(user.getId(), user.getMode(), result,"SHORTEN", StringUtils.EMPTY);
     }
 
     /**
@@ -220,7 +215,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
         }else {
             syncUser(user.getId(), user.getMode(),num);
         }
-        saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),result.getProperties(),"ACTION", StringUtils.EMPTY);
+        saveConversation(user.getId(), user.getMode(), result,"ACTION", StringUtils.EMPTY);
         return result;
     }
 
@@ -355,25 +350,26 @@ public class MidjourneyServiceImpl implements MidjourneyService {
             MidjourneyUserConversation dbConversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, conversation.getTaskId())
                     .eq(MidjourneyUserConversation::getUserId, userId).orderByDesc(MidjourneyUserConversation::getId).last("limit 1"));
             if (dbConversation != null) {
-                if ("SUCCESS".equals(conversation.getStatus())) {
+                if ("SUCCESS".equals(dbConversation.getStatus())) {
                     return;
                 }
-                try {
-                    String imageUrl = conversation.getImageUrl();
-                    if (StringUtil.isNotBlank(imageUrl)) {
-                        conversation.setImageUrl(uploadPic(imageUrl, "conversation"+dbConversation.getId()));
-                    }
-                } catch (IOException e) {
-                    log.error("上传图片失败",e);
-                }finally {
-                    conversation.setId(dbConversation.getId());
-                    conversationMapper.updateById(conversation);
-                    if (dbConversation.getMode() == 2){
-                        redisService.hdecr(RedisService.key.MIDJOURNEY_ACCOUNT.getName() , dbConversation.getInstanceId().toString(), 1.0);
-                    }
+                conversation.setId(dbConversation.getId());
+                conversationMapper.updateById(conversation);
+                if (dbConversation.getMode() == 2){
+                    redisService.hdecr(RedisService.key.MIDJOURNEY_ACCOUNT.getName() , dbConversation.getInstanceId().toString(), 1.0);
                 }
+                TASK_EXECUTOR.execute(() -> {
+                    try {
+                        String imageUrl = conversation.getImageUrl();
+                        if (StringUtil.isNotBlank(imageUrl)) {
+                            conversation.setImageUrl(uploadPic(imageUrl, "conversation"+dbConversation.getId()));
+                            conversationMapper.updateById(conversation);
+                        }
+                    } catch (IOException e) {
+                        log.error("上传图片失败",e);
+                    }
+                });
                 //失败返还次数
-
                 if ("FAILURE".equals(conversation.getStatus())){
                     Object num = redisService.get(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + userId);
                     LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
@@ -386,8 +382,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
                     if (mode == 2){
                         if (num != null) {
                             num = redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + userId, 1L);
+                            wrapper.set(MidjourneyUser::getMjRelaxNum, num);
                         }
-                        wrapper.set(MidjourneyUser::getMjRelaxNum, num);
                     }
                     midjourneyUserMapper.update(null, wrapper);
                 }