|
|
@@ -1,21 +1,36 @@
|
|
|
package com.cyksj.web.controller.mirror;
|
|
|
|
|
|
+import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.dto.Result;
|
|
|
import com.cyksj.enums.GatewayResponse;
|
|
|
+import com.cyksj.mapper.UpgradePackageMapper;
|
|
|
+import com.cyksj.model.entity.ChatgptUser;
|
|
|
import com.cyksj.model.entity.MidjourneyUser;
|
|
|
-import com.cyksj.model.views.ChatgptCarInfoView;
|
|
|
+import com.cyksj.model.entity.UpgradePackage;
|
|
|
+import com.cyksj.model.views.ChatGptUserConversationRecordHistoryView;
|
|
|
+import com.cyksj.model.views.MidjouneyCarInfoView;
|
|
|
+import com.cyksj.model.views.MidjourneyUserConversationRecordHistoryView;
|
|
|
import com.cyksj.model.views.MidjourneyUserView;
|
|
|
+import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.service.midjourney.MidjourneyService;
|
|
|
+import com.cyksj.service.user.UserBindRelationService;
|
|
|
import com.cyksj.web.util.StpUserUtil;
|
|
|
import com.ejlchina.searcher.BeanSearcher;
|
|
|
import com.ejlchina.searcher.SearchResult;
|
|
|
+import com.ejlchina.searcher.param.Operator;
|
|
|
+import com.ejlchina.searcher.util.MapBuilder;
|
|
|
import com.ejlchina.searcher.util.MapUtils;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.math.RoundingMode;
|
|
|
+import java.util.Comparator;
|
|
|
+import java.util.List;
|
|
|
|
|
|
/** server/镜像服务/mj绘画
|
|
|
* @author zwhui
|
|
|
@@ -34,6 +49,12 @@ public class MidjourneyController {
|
|
|
|
|
|
private final BeanSearcher beanSearcher;
|
|
|
|
|
|
+ private final UpgradePackageMapper upgradePackageMapper;
|
|
|
+
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
+
|
|
|
+ private final RedisService redisService;
|
|
|
+
|
|
|
|
|
|
//-------------------------alpha -----------------------
|
|
|
|
|
|
@@ -43,31 +64,63 @@ public class MidjourneyController {
|
|
|
* @param relationId
|
|
|
* @return
|
|
|
*/
|
|
|
- @GetMapping("/gpt/cars")
|
|
|
- public Result<SearchResult<ChatgptCarInfoView>> getCarInfoList(Long relationId, @RequestParam(defaultValue = "true") Boolean isPlus) {
|
|
|
+ @GetMapping("/mj/cars")
|
|
|
+ public Result<SearchResult<MidjouneyCarInfoView>> getCarInfoList(Long relationId,Boolean taskNumAsc) {
|
|
|
long userId = StpUserUtil.getLoginIdAsLong();
|
|
|
//查看用户是否存在车票
|
|
|
MidjourneyUser midjourneyUser = midjourneyService.getUser(userId, relationId);
|
|
|
if (midjourneyUser == null) {
|
|
|
throw BusinessRuntimeException.getInstance("您还未购买车票");
|
|
|
}
|
|
|
- SearchResult<ChatgptCarInfoView> carInfoList = beanSearcher.search(ChatgptCarInfoView.class,MapUtils.flatBuilder(request.getParameterMap()).build());
|
|
|
-
|
|
|
+ SearchResult<MidjouneyCarInfoView> carInfoList = beanSearcher.search(MidjouneyCarInfoView.class,MapUtils.flatBuilder(request.getParameterMap()).build());
|
|
|
+ carInfoList.getDataList().forEach(midjouneyCarInfoView -> {
|
|
|
+ midjouneyCarInfoView.setTaskNum(getCarConversationCount(midjouneyCarInfoView.getCarId(), 5L));
|
|
|
+ });
|
|
|
+ if(taskNumAsc){
|
|
|
+ carInfoList.getDataList().sort(Comparator.comparing(MidjouneyCarInfoView::getTaskNum));
|
|
|
+ }else {
|
|
|
+ carInfoList.getDataList().sort(Comparator.comparing(MidjouneyCarInfoView::getTaskNum).reversed());
|
|
|
+ }
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(carInfoList);
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 获取指定车队ID在滑动窗口内的提问次数
|
|
|
+ *
|
|
|
+ * @param carId 车队ID
|
|
|
+ * @return 滑动窗口内的请求次数
|
|
|
+ */
|
|
|
+ private Long getCarConversationCount(String carId, Long limitTime) {
|
|
|
+ // 定义键名
|
|
|
+ String key = RedisService.key.MIDJOURNEY_CAR_TASK.getName() + carId;
|
|
|
+ long timestamp = System.currentTimeMillis();
|
|
|
+
|
|
|
+ long windowStartMillis = timestamp - (limitTime * 60 * 1000);
|
|
|
|
|
|
+ // 清除时间窗口之前的请求记录(可选,根据需要决定是否在此处清理过期记录)
|
|
|
+ redisService.zRemoveRangeByScore(key, 0, windowStartMillis);
|
|
|
+
|
|
|
+ // 获取当前窗口内的请求次数
|
|
|
+ Long currentSize = redisService.zCard(key);
|
|
|
+ return currentSize != null ? currentSize : 0L;
|
|
|
+ }
|
|
|
/**
|
|
|
* 获取mj车票信息
|
|
|
*/
|
|
|
- @GetMapping("/gpt/userInfo")
|
|
|
+ @GetMapping("/mj/userInfo")
|
|
|
public Result<MidjourneyUserView> getUserInfo(Long relationId) {
|
|
|
long userId = StpUserUtil.getLoginIdAsLong();
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
MidjourneyUserView search = beanSearcher.searchFirst(MidjourneyUserView.class, MapUtils.flatBuilder(request.getParameterMap())
|
|
|
- .field(MidjourneyUserView::getUserId, userId).field(MidjourneyUserView::getRelationId, relationId)
|
|
|
+ .field(MidjourneyUserView::getUserId, userIdList).op(Operator.InList)
|
|
|
+ .field(MidjourneyUserView::getRelationId, relationId)
|
|
|
.build());
|
|
|
if (search == null) {
|
|
|
throw BusinessRuntimeException.getInstance("您还未购买车票");
|
|
|
}
|
|
|
+ List<UpgradePackage> upgradePackages = upgradePackageMapper.selectList(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + search.getSkuId() + "\"')").eq(UpgradePackage::getStatus, Boolean.TRUE).eq(UpgradePackage::getDeleted, Boolean.TRUE));
|
|
|
+ if (CollectionUtil.isNotEmpty(upgradePackages)){
|
|
|
+ upgradePackages.stream().map(e -> e.getPrice().divide(BigDecimal.valueOf(e.getNum()), 2, RoundingMode.HALF_UP)).min(BigDecimal::compareTo).ifPresent(search::setMjPackagePrice);
|
|
|
+ }
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(search);
|
|
|
}
|
|
|
|
|
|
@@ -75,7 +128,7 @@ public class MidjourneyController {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 根据userToken GPT车队跳转登录
|
|
|
+ * 根据userToken mj车队跳转登录
|
|
|
*
|
|
|
* @param carId 车队id
|
|
|
*/
|
|
|
@@ -87,4 +140,19 @@ public class MidjourneyController {
|
|
|
return GatewayResponse.SUCCESS.newBuilder().toResult(url);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取车队历史记录
|
|
|
+ */
|
|
|
+ @GetMapping("/get/car/history")
|
|
|
+ public Result<SearchResult<MidjourneyUserConversationRecordHistoryView>> getCarHistory(Long relationId) {
|
|
|
+ long userId = StpUserUtil.getLoginIdAsLong();
|
|
|
+ MidjourneyUser midjourneyUser = midjourneyService.getUser(userId, relationId);
|
|
|
+ if (midjourneyUser == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("车票出现了异常.");
|
|
|
+ }
|
|
|
+ MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap()).field("userToken", midjourneyUser.getUserToken());
|
|
|
+ SearchResult<MidjourneyUserConversationRecordHistoryView> search = beanSearcher.search(MidjourneyUserConversationRecordHistoryView.class, builder.build());
|
|
|
+ return GatewayResponse.SUCCESS.newBuilder().toResult(search);
|
|
|
+ }
|
|
|
+
|
|
|
}
|