|
|
@@ -1,8 +1,10 @@
|
|
|
package com.cyksj.service.chatgpt.impl;
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUnit;
|
|
|
import cn.hutool.core.date.DateUtil;
|
|
|
import cn.hutool.core.lang.UUID;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpResponse;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
@@ -11,10 +13,13 @@ import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.cyksj.common.constant.Constant;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
+import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
import com.cyksj.mapper.*;
|
|
|
+import com.cyksj.mapper.station.CollegeStudentStationUseTicketRecordMapper;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.model.request.gpt.ConversationRequest;
|
|
|
import com.cyksj.model.response.ConversationLimitResponse;
|
|
|
@@ -23,6 +28,7 @@ import com.cyksj.model.views.ChatGptUserView;
|
|
|
import com.cyksj.model.views.ChatgptCarInfoView;
|
|
|
import com.cyksj.redis.RedisService;
|
|
|
import com.cyksj.service.chatgpt.ChatGptAccountService;
|
|
|
+import com.cyksj.service.sys.SysConfigService;
|
|
|
import com.cyksj.service.user.UserBindRelationService;
|
|
|
import com.ejlchina.searcher.BeanSearcher;
|
|
|
import com.ejlchina.searcher.SearchResult;
|
|
|
@@ -95,6 +101,14 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
|
|
|
private final ChatgptUserCarUsedRecordMapper chatgptUserCarUsedRecordMapper;
|
|
|
|
|
|
+ private final CollegeStudentStationUseTicketRecordMapper collegeStudentStationUseTicketRecordMapper;
|
|
|
+
|
|
|
+ private final UpgradePackageMapper upgradePackageMapper;
|
|
|
+
|
|
|
+ private final OrderDonMapper orderDonMapper;
|
|
|
+
|
|
|
+ private final SysConfigService sysConfigService;
|
|
|
+
|
|
|
@Override
|
|
|
public String addAccount(Account account) {
|
|
|
if (isAccountExists(account)) {
|
|
|
@@ -241,7 +255,25 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
GoodsDonSku goodsDonSku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
if (goodsDonSku != null && goodsDonSku.getIsMirror() && goodsDonSku.getIsCar()) {
|
|
|
ChatgptUser chatgptUser = getChatgptCarUser(userId, relationId, groupsRelation, goodsDonSku);
|
|
|
+
|
|
|
String DOMAIN = "https://chat.galaxydvd.com";
|
|
|
+ if(goodsDonSku.getId() == 444){
|
|
|
+ DOMAIN = "https://car.galaxydvd.com";
|
|
|
+ }else {
|
|
|
+ try {
|
|
|
+ SysConfig sysConfig = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class).eq(SysConfig::getSysKey, "mirror_gpt_domain"));
|
|
|
+ if (sysConfig != null) {
|
|
|
+ String sysValue = sysConfig.getSysValue();
|
|
|
+ if (JSONUtil.isJsonArray(sysValue)) {
|
|
|
+ List<String> domainList = JSONUtil.parseArray(sysValue).toList(String.class);
|
|
|
+ DOMAIN = domainList.get(RandomUtil.randomInt(domainList.size()));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }catch (Exception e){
|
|
|
+ log.error("随机抽取域名错误. msg:{}", StringUtil.getErrorText(e));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
log.info("domain:{},用户:{},所在车次:{},座位id:{},在{}获取跳转GPT镜像的登录url", DOMAIN, userId, groupsTrips.getId(), relationId, DateTime.now());
|
|
|
return DOMAIN + "/auth/logintoken?carid=" + carId + "&usertoken=" + chatgptUser.getUserToken();
|
|
|
} else {
|
|
|
@@ -355,6 +387,17 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
chatgptUser.setRelationId(groupsRelation.getId());
|
|
|
chatgptUser.setUserToken(UUID.randomUUID().toString());
|
|
|
chatgptUserMapper.insert(chatgptUser);
|
|
|
+
|
|
|
+ //是否是大学生限时车队
|
|
|
+ Date expiryTime = groupsRelation.getExpiryTime();
|
|
|
+ Date startTime = groupsRelation.getStartTime();
|
|
|
+ //大学生站 1天免费试用
|
|
|
+ if (DateUtil.between(startTime, expiryTime, DateUnit.DAY) <= 1) {
|
|
|
+ CollegeStudentStationUseTicketRecord useTicketRecord = new CollegeStudentStationUseTicketRecord();
|
|
|
+ useTicketRecord.setUserId(user.getId());
|
|
|
+ useTicketRecord.setUserToken(chatgptUser.getUserToken());
|
|
|
+ collegeStudentStationUseTicketRecordMapper.insert(useTicketRecord);
|
|
|
+ }
|
|
|
} catch (DuplicateKeyException e) {
|
|
|
log.error("重复插入镜像用户:{}token,errmsg:{}", user.getNickname(), StringUtil.getErrorText(e));
|
|
|
}
|
|
|
@@ -431,11 +474,10 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
@Override
|
|
|
public void saveConversationRecord(String userToken, ConversationRequest conversationRequest) {
|
|
|
LambdaQueryWrapper<ChatgptSession> wrapper = Wrappers.lambdaQuery(ChatgptSession.class);
|
|
|
-
|
|
|
+ ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getUserToken, userToken));
|
|
|
if (StringUtils.isNotBlank(conversationRequest.getCarId())) {
|
|
|
wrapper.eq(ChatgptSession::getCarId, conversationRequest.getCarId());
|
|
|
} else {
|
|
|
- ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getUserToken, userToken));
|
|
|
if (chatgptUser.getSessionId() != null) {
|
|
|
wrapper.eq(ChatgptSession::getId, chatgptUser.getSessionId());
|
|
|
}
|
|
|
@@ -445,15 +487,32 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
chatgptUserConversationRecord.setUserToken(userToken);
|
|
|
chatgptUserConversationRecord.setCarId(chatgptSession.getCarId());
|
|
|
chatgptUserConversationRecord.setCarName(chatgptSession.getCarName());
|
|
|
- if (StringUtils.isBlank(conversationRequest.getConversation_id())) {
|
|
|
- chatgptUserConversationRecord.setMessageId(conversationRequest.getMessages().get(0).getId());
|
|
|
- } else {
|
|
|
+
|
|
|
+ if (StringUtils.isNotBlank(conversationRequest.getConversation_id())) {
|
|
|
chatgptUserConversationRecord.setConversationId(conversationRequest.getConversation_id());
|
|
|
}
|
|
|
|
|
|
chatgptUserConversationRecord.setModel(conversationRequest.getModel());
|
|
|
chatgptUserConversationRecordMapper.insert(chatgptUserConversationRecord);
|
|
|
|
|
|
+ if(!"text-davinci-002-render-sha".equals(conversationRequest.getModel())){
|
|
|
+ String key = RedisService.key.CHATGPT_CONVERSATION_LIMIT.getName() + ":" + userToken;
|
|
|
+ long currentTimeMillis = System.currentTimeMillis();
|
|
|
+
|
|
|
+ redisService.del();
|
|
|
+ if(redisService.hasKey("chatgpt:team:clears_in:" + chatgptSession.getCarId())){
|
|
|
+ redisService.del("chatgpt:team:clears_in:" + chatgptSession.getCarId());
|
|
|
+ }
|
|
|
+ if (redisService.hasKey("chatgpt:clears_in:" + chatgptSession.getCarId())) {
|
|
|
+ redisService.del("chatgpt:clears_in:" + chatgptSession.getCarId());
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果未超过限制,记录当前请求的时间戳
|
|
|
+ redisService.zAdd(key, currentTimeMillis, currentTimeMillis);
|
|
|
+ // 设置ZSet的过期时间,窗口大小加上一段冗余时间
|
|
|
+ redisService.expire(key, (chatgptUser.getLimitTime() * 60 * 60) + 20);
|
|
|
+ }
|
|
|
+
|
|
|
if (StringUtils.isNotBlank(chatgptSession.getCarId())) {
|
|
|
updateExperienceAndScore(chatgptSession.getCarId(), !"text-davinci-002-render-sha".equals(conversationRequest.getModel()), System.currentTimeMillis());
|
|
|
}
|
|
|
@@ -508,10 +567,6 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
chatgptUserCarUsedRecordMapper.insert(chatgptUserCarUsedRecord);
|
|
|
return conversationLimitResponse;
|
|
|
} else {
|
|
|
- // 如果未超过限制,记录当前请求的时间戳
|
|
|
- redisService.zAdd(key, currentTimeMillis, currentTimeMillis);
|
|
|
- // 设置ZSet的过期时间,窗口大小加上一段冗余时间
|
|
|
- redisService.expire(key, (limitTime * 60 * 60) + 20);
|
|
|
conversationLimitResponse.setLimited(false);
|
|
|
return conversationLimitResponse;
|
|
|
}
|
|
|
@@ -586,7 +641,7 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
GroupsTrips groupsTrips = getGroupsTrips(groupsRelation);
|
|
|
GoodsDonSku goodsDonSku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
|
|
|
- if (goodsDonSku != null && goodsDonSku.getIsMirror() && goodsDonSku.getIsCar()) {
|
|
|
+ if (goodsDonSku != null && goodsDonSku.getGoodsId() == 18 && goodsDonSku.getIsMirror() && goodsDonSku.getIsCar()) {
|
|
|
ChatgptUser chatgptUser = getChatgptCarUser(userId, relationId, groupsRelation, goodsDonSku);
|
|
|
return true;
|
|
|
} else {
|
|
|
@@ -786,12 +841,41 @@ public class ChatGptAccountServiceImpl implements ChatGptAccountService {
|
|
|
GroupsTrips groupsTrips = getGroupsTrips(groupsRelation);
|
|
|
GoodsDonSku goodsDonSku = skuMapper.selectById(groupsTrips.getSkuId());
|
|
|
ChatgptUser chatgptUser = chatgptUserMapper.selectOne(Wrappers.lambdaQuery(ChatgptUser.class).eq(ChatgptUser::getRelationId, relationId));
|
|
|
+
|
|
|
+ Integer count = upgradePackageMapper.selectCount(Wrappers.lambdaQuery(UpgradePackage.class).apply("JSON_CONTAINS(sku_ids,'\"" + groupsTrips.getSkuId() + "\"')")
|
|
|
+ .gt(UpgradePackage::getNum, chatgptUser.getLimitNum())
|
|
|
+ .eq(UpgradePackage::getStatus, Boolean.TRUE)
|
|
|
+ .eq(UpgradePackage::getGptType, 0)
|
|
|
+ .eq(UpgradePackage::getDeleted, Boolean.TRUE));
|
|
|
+ Boolean upgrade = count > 0 ? true : false;
|
|
|
+ OrderDon orderDon = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class)
|
|
|
+ .eq(OrderDon::getRelationId, relationId)
|
|
|
+ .eq(OrderDon::getUserId, groupsRelation.getUserId())
|
|
|
+ .notIn(OrderDon::getStatus, Constant.noOrderAllStatus)
|
|
|
+ .orderByDesc(OrderDon::getId)
|
|
|
+ .last("limit 1"));
|
|
|
+ Long orderId = null;
|
|
|
+ if (orderDon != null) {
|
|
|
+ orderId = orderDon.getId();
|
|
|
+ }
|
|
|
+ BigDecimal packagePrice = null;
|
|
|
+ if (chatgptUser.getPackageId() != 0) {
|
|
|
+ UpgradePackage upgradePackage = upgradePackageMapper.selectById(chatgptUser.getPackageId());
|
|
|
+ packagePrice = upgradePackage.getPrice();
|
|
|
+ }
|
|
|
return ChatGptUserView.builder()
|
|
|
.skuName(goodsDonSku.getSubTitle())
|
|
|
.expireTime(chatgptUser.getExpireTime())
|
|
|
.limitNum(chatgptUser.getLimitNum())
|
|
|
.limitTime(chatgptUser.getLimitTime())
|
|
|
- .use(getConversationCount(chatgptUser.getUserToken(), chatgptUser.getLimitTime()))
|
|
|
+ .upgrade(upgrade)
|
|
|
+ .orderId(orderId)
|
|
|
+ .goodsId(goodsDonSku.getGoodsId())
|
|
|
+ .skuId(goodsDonSku.getId())
|
|
|
+ .months(goodsDonSku.getMonths())
|
|
|
+ .use(Math.min(chatgptUser.getLimitNum(), getConversationCount(chatgptUser.getUserToken(), chatgptUser.getLimitTime())))
|
|
|
+ .packageId(chatgptUser.getPackageId())
|
|
|
+ .packagePrice(packagePrice)
|
|
|
.build();
|
|
|
} else {
|
|
|
throw BusinessRuntimeException.getInstance("您还未购买该车票");
|