|
|
@@ -13,14 +13,13 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.EnvCommonService;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
-import com.cyksj.common.util.Codec;
|
|
|
-import com.cyksj.common.util.J11HttpC;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
import com.cyksj.mapper.MidjourneyUserConversationMapper;
|
|
|
import com.cyksj.mapper.MidjourneyUserMapper;
|
|
|
import com.cyksj.model.dto.BlendDimensions;
|
|
|
import com.cyksj.model.dto.MessageButton;
|
|
|
+import com.cyksj.model.dto.SubmitModalDTO;
|
|
|
import com.cyksj.model.entity.MidjourneyAccount;
|
|
|
import com.cyksj.model.entity.MidjourneyUser;
|
|
|
import com.cyksj.model.entity.MidjourneyUserConversation;
|
|
|
@@ -34,18 +33,10 @@ import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
import javax.imageio.stream.FileImageOutputStream;
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
import java.io.*;
|
|
|
-import java.net.URI;
|
|
|
-import java.net.URLEncoder;
|
|
|
-import java.net.http.HttpResponse;
|
|
|
-import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
-import java.security.MessageDigest;
|
|
|
-import java.time.ZoneOffset;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -92,6 +83,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
imagineParam.put("base64Array", base64Array);
|
|
|
}
|
|
|
SubmitResult result = submit(user.getMode(),"imagine", null,imagineParam);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"IMAGINE", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@@ -122,6 +116,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("base64", base64)
|
|
|
.build();
|
|
|
SubmitResult result = submit(user.getMode(),"describe", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"DESCRIBE", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@@ -135,6 +132,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
param.put("dimensions", dimensions);
|
|
|
}
|
|
|
SubmitResult result = submit(user.getMode(),"blend", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"BLEND", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@@ -151,6 +151,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
param.put("maskBase64", maskBase64);
|
|
|
}
|
|
|
SubmitResult result = submit(user.getMode(),"modal", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(),result,"MODAL", StringUtils.EMPTY, StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
@@ -161,41 +164,45 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("state", user.getId())
|
|
|
.build();
|
|
|
SubmitResult result = submit(user.getMode(),"shorten", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
return saveConversation(user.getId(), user.getMode(), result,"SHORTEN", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 恢复次数
|
|
|
*/
|
|
|
- public Long recoverUserLimit(Long id,Integer mode,Long num){
|
|
|
+ public void recoverUserLimit(Long id,Integer mode,Long num){
|
|
|
if (mode == 1){
|
|
|
- num = redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + id, 1L);
|
|
|
+ redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + id, 1L);
|
|
|
+ midjourneyUserMapper.incrFastNum(id, 1);
|
|
|
}
|
|
|
if (mode == 2){
|
|
|
- if (num != null) {
|
|
|
- num = redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + id, 1L);
|
|
|
+ if (num != null){
|
|
|
+ redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + id, 1L);
|
|
|
+ midjourneyUserMapper.incrRelaxNum(id, 1);
|
|
|
}
|
|
|
}
|
|
|
- return num;
|
|
|
- }
|
|
|
- /**
|
|
|
- * 同步数据库
|
|
|
- */
|
|
|
- public void syncUser(Long id,Integer mode,Long num){
|
|
|
- log.info("同步次数 id:{},mode:{},num:{}",id,mode,num);
|
|
|
- if (num == null){
|
|
|
- return;
|
|
|
- }
|
|
|
- LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
|
|
|
- .eq(MidjourneyUser::getId, id);
|
|
|
- if (mode == 1){
|
|
|
- wrapper.set(MidjourneyUser::getMjFastNum, num);
|
|
|
- }
|
|
|
- if (mode == 2){
|
|
|
- wrapper.set(MidjourneyUser::getMjRelaxNum, num);
|
|
|
- }
|
|
|
- midjourneyUserMapper.update(null, wrapper);
|
|
|
}
|
|
|
+// /**
|
|
|
+// * 同步数据库
|
|
|
+// */
|
|
|
+// public void syncUser(Long id,Integer mode,Long num){
|
|
|
+// log.info("同步次数 id:{},mode:{},num:{}",id,mode,num);
|
|
|
+// if (num == null){
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
|
|
|
+// .eq(MidjourneyUser::getId, id);
|
|
|
+// if (mode == 1){
|
|
|
+// wrapper.set(MidjourneyUser::getMjFastNum, num);
|
|
|
+// }
|
|
|
+// if (mode == 2){
|
|
|
+// wrapper.set(MidjourneyUser::getMjRelaxNum, num);
|
|
|
+// }
|
|
|
+// midjourneyUserMapper.update(null, wrapper);
|
|
|
+// }
|
|
|
|
|
|
@Override
|
|
|
public SubmitResult submitAction(MidjourneyUser user, Long taskId, String customId, Long num, String botType) throws Exception {
|
|
|
@@ -206,12 +213,16 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (!user.getMode().equals(conversation.getMode())) {
|
|
|
throw BusinessRuntimeException.getInstance("当前出图模式与关联任务出图模式不符");
|
|
|
}
|
|
|
+ AtomicBoolean modalFlag = new AtomicBoolean(false);
|
|
|
if (StringUtils.isNotBlank(conversation.getButtons())){
|
|
|
List<MessageButton> messageButtons = Jsons.parseList(conversation.getButtons(), MessageButton.class);
|
|
|
messageButtons.forEach(button -> {
|
|
|
if (button.getCustomId().equals(customId)) {
|
|
|
log.info("action customId:{}", customId);
|
|
|
button.setStyle(3);
|
|
|
+ if (!"Vary (Region)".equals(button.getLabel()) && "Custom Zoom".equals(button.getLabel())) {
|
|
|
+ modalFlag.set(true);
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
conversation.setButtons(Jsons.toJson(messageButtons));
|
|
|
@@ -230,11 +241,19 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (result.getCode() == 21) {
|
|
|
// 以上操作有弹窗确认,恢复次数
|
|
|
recoverUserLimit(user.getId(), user.getMode(),num);
|
|
|
- if (user.getMode() == 2){
|
|
|
- redisService.hdecr(RedisService.key.MIDJOURNEY_ACCOUNT.getName() , String.valueOf(conversation.getInstanceId()), 1.0);
|
|
|
+ if (user.getMode() == 2 && modalFlag.get()){
|
|
|
+ SubmitModalDTO modalParam = new SubmitModalDTO();
|
|
|
+ modalParam.setPrompt(conversation.getPrompt() + " --v 6");
|
|
|
+ modalParam.setTaskId(Long.valueOf(result.getResult()));
|
|
|
+ String modal = HttpUtil.post("http://localhost:" + (EnvCommonService.active.equals(envCommonService.getEnv()) ? "/8081":"/8082") + "/applets/midjourney/submit/modal", Jsons.toJson(modalParam));
|
|
|
+ log.info("modal:{}", modal);
|
|
|
+ return result;
|
|
|
}
|
|
|
}else {
|
|
|
- syncUser(user.getId(), user.getMode(),num);
|
|
|
+ //syncUser(user.getId(), user.getMode(),num);
|
|
|
+ if (!customId.contains("upsample") && user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
}
|
|
|
saveConversation(user.getId(), user.getMode(), result,"ACTION", StringUtils.EMPTY, botType);
|
|
|
return result;
|
|
|
@@ -281,10 +300,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
log.error("action:" + action + " error message:" + submitResult.getDescription());
|
|
|
throw BusinessRuntimeException.getInstance("队列已满,请稍后尝试");
|
|
|
}
|
|
|
- // 增加使用次数
|
|
|
if (StringUtils.isNotBlank(accountWithMinUsage) && mode == 2) {
|
|
|
submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
- redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), accountWithMinUsage, 1.0);
|
|
|
}
|
|
|
return submitResult;
|
|
|
}
|
|
|
@@ -296,9 +313,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
Map<Object, Object> accountsUsage = redisService.hmget(key);
|
|
|
|
|
|
if (accountsUsage == null || accountsUsage.isEmpty()) {
|
|
|
- Map<Object, Object> map = midjourneyAccountService.list(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getStatus,Boolean.TRUE)).stream().collect(Collectors.toMap(k -> k.getInstanceId().toString(), v -> 0));
|
|
|
+ accountsUsage = midjourneyAccountService.list(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getStatus,Boolean.TRUE)).stream().collect(Collectors.toMap(k -> k.getInstanceId().toString(), v -> 0));
|
|
|
// 保存所有账号ID和使用次数
|
|
|
- redisService.hmset(key, map);
|
|
|
+ redisService.hmset(key, accountsUsage);
|
|
|
}
|
|
|
|
|
|
// 找到使用次数最少的账号ID
|
|
|
@@ -380,14 +397,11 @@ 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(dbConversation.getStatus())) {
|
|
|
+ if ("SUCCESS".equals(dbConversation.getStatus()) || "FAILURE".equals(dbConversation.getStatus()) || "MODAL".equals(dbConversation.getStatus())) {
|
|
|
return;
|
|
|
}
|
|
|
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();
|