Просмотр исходного кода

Merge branch 'mj_modal' into pre

# Conflicts:
#	netflix-service/src/main/java/com/cyksj/redis/RedisService.java
#	netflix-web/src/main/java/com/cyksj/web/controller/mirror/MidjourneyController.java
zwhui 2 лет назад
Родитель
Сommit
d3721af2ed

+ 2 - 1
netflix-service/src/main/java/com/cyksj/redis/RedisService.java

@@ -844,7 +844,8 @@ public class RedisService {
         MIDJOURNEY_ACCOUNT("midjourney:account:", "midjourney account", 60 * 60 * 48L),
         MIDJOURNEY_QUERY("midjourney:query:", "midjourney query", 60 * 60 * 2L),
         APPLY_MONEY_DAY_LIMIT_KEY("apply_money_day_limit_key:%s:%s:%s", "每日提现次数限制", 60 * 60 * 24L),
-
+        MIDJOURNEY_RELAX_CONVERSATION("midjourney:relax:conversation:", "midjourney relax conversation", 60 * 60 * 2L),
+        MIDJOURNEY_ACCOUNT_USED("midjourney:account:used:", "midjourney account used", 60 * 60 * 48L),
         ;
 
         private String name;

+ 11 - 4
netflix-service/src/main/java/com/cyksj/service/midjourney/impl/MidJourneyAccountServiceImpl.java

@@ -63,7 +63,11 @@ public class MidJourneyAccountServiceImpl extends ServiceImpl<MidjourneyAccountM
         Long instanceId = getAccount(midjourneyAccount.getId());
         midjourneyAccount.setInstanceId(instanceId);
         baseMapper.updateById(midjourneyAccount);
-        redisService.hset(RedisService.key.MIDJOURNEY_ACCOUNT.getName(),midjourneyAccount.getInstanceId().toString(),0);
+        String key = RedisService.key.MIDJOURNEY_ACCOUNT.getName();
+        Map<Object, Object> accountsUsage = redisService.hmget(key);
+        if (MapUtil.isNotEmpty(accountsUsage)) {
+            redisService.hset(key,midjourneyAccount.getInstanceId().toString(),accountsUsage.values().toArray()[0]);
+        }
     }
 
     @Override
@@ -100,11 +104,14 @@ public class MidJourneyAccountServiceImpl extends ServiceImpl<MidjourneyAccountM
         midjourneyAccount.setStatus(!midjourneyAccount.getStatus());
         baseMapper.updateById(midjourneyAccount);
         updAccount(midjourneyAccount);
-
+        String key = RedisService.key.MIDJOURNEY_ACCOUNT.getName();
         if (!midjourneyAccount.getStatus()) {
-            redisService.hdel(RedisService.key.MIDJOURNEY_ACCOUNT.getName(),midjourneyAccount.getInstanceId());
+            redisService.hdel(key,midjourneyAccount.getInstanceId());
         }else {
-            redisService.hset(RedisService.key.MIDJOURNEY_ACCOUNT.getName(),midjourneyAccount.getInstanceId().toString(),0);
+            Map<Object, Object> accountsUsage = redisService.hmget(key);
+            if (MapUtil.isNotEmpty(accountsUsage)) {
+                redisService.hset(key,midjourneyAccount.getInstanceId().toString(),accountsUsage.values().toArray()[0]);
+            }
         }
     }
 

+ 58 - 44
netflix-service/src/main/java/com/cyksj/service/midjourney/impl/MidjourneyServiceImpl.java

@@ -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();

+ 11 - 0
netflix-service/src/main/java/com/cyksj/service/order/impl/OrderDonServiceImpl.java

@@ -3599,4 +3599,15 @@ public class OrderDonServiceImpl extends ServiceImpl<OrderDonMapper, OrderDon> i
 			userTrialTicketJumpPayRecordMapper.insert(userTrialTicketJumpPayRecord);
 		}
 	}
+
+	/**
+	 * 试用车票跳转购买正式车票订单记录
+	 */
+	public void recordOrderTrialTicketJumpPay(Boolean isTrial, Long orderId) {
+		if (isTrial != null && isTrial) {
+			UserTrialTicketJumpPayRecord userTrialTicketJumpPayRecord = new UserTrialTicketJumpPayRecord();
+			userTrialTicketJumpPayRecord.setOrderId(orderId);
+			userTrialTicketJumpPayRecordMapper.insert(userTrialTicketJumpPayRecord);
+		}
+	}
 }

+ 26 - 40
netflix-web/src/main/java/com/cyksj/web/controller/mirror/MidjourneyController.java

@@ -1,6 +1,7 @@
 package com.cyksj.web.controller.mirror;
 
 import cn.hutool.core.util.StrUtil;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.toolkit.Wrappers;
 import com.cyksj.common.annotation.NoSubmit;
 import com.cyksj.common.exception.BusinessRuntimeException;
@@ -13,6 +14,8 @@ import com.cyksj.mapper.MidjourneyUserMapper;
 import com.cyksj.model.dto.*;
 import com.cyksj.model.entity.GroupsRelation;
 import com.cyksj.model.entity.MidjourneyPaintingPlaza;
+import com.cyksj.model.entity.GroupsRelation;
+import com.cyksj.model.entity.MidjourneyPaintingPlaza;
 import com.cyksj.model.entity.MidjourneyUser;
 import com.cyksj.model.entity.MidjourneyUserConversation;
 import com.cyksj.model.response.SubmitResult;
@@ -35,7 +38,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.Date;
 import java.util.List;
 
-/**
+/** server/镜像服务/mj绘画
  * @author zwhui
  * @date 2024/4/23 10:31
  */
@@ -61,44 +64,23 @@ public class MidjourneyController {
 
     public MidjourneyUser getUser(){
         String userToken = request.getHeader("user-token");
-        MidjourneyUser midjourneyUser = (MidjourneyUser) redisService.get(RedisService.key.MIDJOURNEY_USER.getName() + userToken);
+
+        MidjourneyUser midjourneyUser = midjourneyUserMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUser.class)
+                .eq(MidjourneyUser::getUserToken, userToken).last("limit 1"));
         if (midjourneyUser == null){
-            midjourneyUser = midjourneyUserMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUser.class)
-                    .eq(MidjourneyUser::getUserToken, userToken).last("limit 1"));
-            if (midjourneyUser == null){
-                throw new BusinessRuntimeException("账号不存在,请重新登录");
-            }
-            Long relationId = midjourneyUser.getRelationId();
-            GroupsRelation relation = groupsRelationMapper.selectById(relationId);
-            midjourneyUser.setAqType(relation.getAqType());
-            redisService.set(RedisService.key.MIDJOURNEY_USER.getName() + userToken, midjourneyUser, RedisService.key.MIDJOURNEY_USER.getTimeout());
-            Object fastNum = redisService.get(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId());
-            if (fastNum == null){
-                redisService.set(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId(), midjourneyUser.getMjFastNum(), RedisService.key.MIDJOURNEY_FAST_LIMIT.getTimeout());
-            }
-            Object relaxNum = redisService.get(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId());
-            if (relaxNum == null){
-                if (midjourneyUser.getMjRelaxNum() != null){
-                    redisService.set(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId(), midjourneyUser.getMjRelaxNum(), RedisService.key.MIDJOURNEY_RELAX_LIMIT.getTimeout());
-                }
-            }
-        }else {
-            Object fastNum = redisService.get(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId());
-            if (fastNum == null){
-                redisService.set(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId(), midjourneyUser.getMjFastNum(), RedisService.key.MIDJOURNEY_FAST_LIMIT.getTimeout());
-            }else {
-                midjourneyUser.setMjFastNum(Integer.parseInt(fastNum.toString()));
-            }
-            Object relaxNum = redisService.get(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId());
-            if (relaxNum == null){
-                if (midjourneyUser.getMjRelaxNum() != null){
-                    redisService.set(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId(), midjourneyUser.getMjRelaxNum(), RedisService.key.MIDJOURNEY_RELAX_LIMIT.getTimeout());
-                }
-            }else {
-                midjourneyUser.setMjRelaxNum(Integer.parseInt(relaxNum.toString()));
-            }
+            throw new BusinessRuntimeException("账号不存在,请重新登录");
+        }
+        if(!redisService.hasKey(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId())){
+            redisService.set(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId(),midjourneyUser.getMjFastNum());
         }
 
+        if(!redisService.hasKey(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId())){
+            redisService.set(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId(),midjourneyUser.getMjRelaxNum());
+        }
+        Long relationId = midjourneyUser.getRelationId();
+        GroupsRelation relation = groupsRelationMapper.selectById(relationId);
+        midjourneyUser.setAqType(relation.getAqType());
+
         return midjourneyUser;
 
     }
@@ -319,9 +301,12 @@ public class MidjourneyController {
     public Result<SubmitResult> action(@RequestBody SubmitActionDTO actionDTO) {
         log.info("任务id:{},执行动作:{}",actionDTO.getTaskId(),actionDTO.getCustomId());
         MidjourneyUser user = getUser();
-        Long num = checkUserLimit(user);
-        if (num != null && num < 0){
-            throw BusinessRuntimeException.getInstance("次数已用完");
+        Long num = 0L;
+        if (!actionDTO.getCustomId().contains("BOOKMARK")) {
+            num = checkUserLimit(user);
+            if (num != null && num < 0) {
+                throw BusinessRuntimeException.getInstance("次数已用完");
+            }
         }
         SubmitResult conversation;
         try {
@@ -397,6 +382,7 @@ public class MidjourneyController {
     }
 
 
+
     /**
      * mj绘画广场
      */
@@ -427,7 +413,7 @@ public class MidjourneyController {
         MidjourneyUser user = getUser();
         String condition = String.format(" and mc.user_id = %s", user.getId());
         if (StrUtil.isNotBlank(prompt)) {
-            condition += String.format(" and mc.prompt = '%s'", prompt);
+            condition += String.format(" and mc.prompt like '%%%s%%'", prompt);
         }
         if (bookmark != null) {
             condition += String.format(" and mc.bookmark is %s", bookmark);