|
|
@@ -0,0 +1,143 @@
|
|
|
+package com.cyksj.service.gpt.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+import cn.hutool.core.date.DateUtil;
|
|
|
+import cn.hutool.core.util.RandomUtil;
|
|
|
+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.mapper.GptQuickLinkRecordMapper;
|
|
|
+import com.cyksj.mapper.GroupsRelationMapper;
|
|
|
+import com.cyksj.model.entity.GptQuickLinkRecord;
|
|
|
+import com.cyksj.model.manage.views.GroupsRelationView;
|
|
|
+import com.cyksj.model.response.GptQuickLinkResp;
|
|
|
+import com.cyksj.service.chatgpt.ChatGptAccountService;
|
|
|
+import com.cyksj.service.gpt.GptQuickLinkService;
|
|
|
+import com.cyksj.service.user.UserBindRelationService;
|
|
|
+import com.ejlchina.searcher.BeanSearcher;
|
|
|
+import com.ejlchina.searcher.param.Operator;
|
|
|
+import com.ejlchina.searcher.util.MapUtils;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Date;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * GPT快捷链接服务实现类
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class GptQuickLinkServiceImpl implements GptQuickLinkService {
|
|
|
+
|
|
|
+ private final GptQuickLinkRecordMapper gptQuickLinkRecordMapper;
|
|
|
+
|
|
|
+ private final GroupsRelationMapper groupsRelationMapper;
|
|
|
+
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
+
|
|
|
+ private final BeanSearcher beanSearcher;
|
|
|
+
|
|
|
+ private final ChatGptAccountService chatGptAccountService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public GptQuickLinkRecord createQuickLink(Long userId) {
|
|
|
+ // 检查用户是否有GPT镜像车票
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ if (getValidGptTicket(userIdList).isEmpty()) {
|
|
|
+ throw new BusinessRuntimeException("您没有有效的GPT镜像车票");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否存在未过期的快捷链接
|
|
|
+ Date now = DateTime.now();
|
|
|
+ LambdaQueryWrapper<GptQuickLinkRecord> wrapper = Wrappers.lambdaQuery(GptQuickLinkRecord.class)
|
|
|
+ .in(GptQuickLinkRecord::getUserId, userIdList)
|
|
|
+ .gt(GptQuickLinkRecord::getExpiryTime, now)
|
|
|
+ .last("limit 1")
|
|
|
+ .orderByDesc(GptQuickLinkRecord::getId);
|
|
|
+
|
|
|
+ GptQuickLinkRecord existingLink = gptQuickLinkRecordMapper.selectOne(wrapper);
|
|
|
+ if (existingLink != null) {
|
|
|
+ log.info("用户 {} 已存在未过期的快捷链接: {}", userId, existingLink.getCode());
|
|
|
+ return existingLink;
|
|
|
+ }
|
|
|
+ //生成标识
|
|
|
+ String code = RandomUtil.randomString(6).toLowerCase();;
|
|
|
+ GptQuickLinkRecord quickLink = new GptQuickLinkRecord();
|
|
|
+ quickLink.setUserId(userId);
|
|
|
+ quickLink.setCode(code);
|
|
|
+ // 创建新的快捷链接,有效期30天
|
|
|
+ quickLink.setExpiryTime(DateUtil.offsetDay(now, 30));
|
|
|
+ try {
|
|
|
+ gptQuickLinkRecordMapper.insert(quickLink);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ throw BusinessRuntimeException.getInstance("系统繁忙,请重新生成!");
|
|
|
+ }
|
|
|
+ return quickLink;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public GptQuickLinkRecord getLatestQuickLink(Long userId) {
|
|
|
+ Date now = DateTime.now();
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ LambdaQueryWrapper<GptQuickLinkRecord> wrapper = Wrappers.lambdaQuery(GptQuickLinkRecord.class)
|
|
|
+ .in(GptQuickLinkRecord::getUserId, userIdList)
|
|
|
+ .gt(GptQuickLinkRecord::getExpiryTime, now)
|
|
|
+ .orderByDesc(GptQuickLinkRecord::getId)
|
|
|
+ .last("limit 1");
|
|
|
+ return gptQuickLinkRecordMapper.selectOne(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public GptQuickLinkResp getQuickUrlByCode(String code) {
|
|
|
+ Date now = DateTime.now();
|
|
|
+ LambdaQueryWrapper<GptQuickLinkRecord> wrapper = Wrappers.lambdaQuery(GptQuickLinkRecord.class)
|
|
|
+ .eq(GptQuickLinkRecord::getCode, code)
|
|
|
+ .last("limit 1");
|
|
|
+ GptQuickLinkRecord gptQuickLinkRecord = gptQuickLinkRecordMapper.selectOne(wrapper);
|
|
|
+ GptQuickLinkResp resp = new GptQuickLinkResp();
|
|
|
+ //成功
|
|
|
+ Integer state = 1;
|
|
|
+ if (gptQuickLinkRecord != null) {
|
|
|
+ if (gptQuickLinkRecord.getExpiryTime().before(now)) {
|
|
|
+ //链接失效
|
|
|
+ state = 2;
|
|
|
+ } else {
|
|
|
+ Long userId = gptQuickLinkRecord.getUserId();
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ List<GroupsRelationView> validGptTicket = getValidGptTicket(userIdList);
|
|
|
+ if (validGptTicket.isEmpty()) {
|
|
|
+ //车票过期
|
|
|
+ state = 3;
|
|
|
+ } else {
|
|
|
+ if (validGptTicket.size() > 1) {
|
|
|
+ //多张车票
|
|
|
+ state = 4;
|
|
|
+ } else {
|
|
|
+ GroupsRelationView groupsRelationView = validGptTicket.get(0);
|
|
|
+ Long relationId = groupsRelationView.getId();
|
|
|
+ //获取gpt镜像访问url
|
|
|
+ String url = chatGptAccountService.getCarLoginUrl(null, userId, relationId, Constant.GPT_CARD_ID);
|
|
|
+ resp.setUrl(url);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ resp.setState(state);
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+ public List<GroupsRelationView> getValidGptTicket(List<Long> userIdList) {
|
|
|
+ List<GroupsRelationView> relationViews = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
|
|
|
+ .field(GroupsRelationView::getGoodsId, Constant.GPT_PLUS_GID)
|
|
|
+ .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
|
|
|
+ .field(GroupsRelationView::getIsMirror, Boolean.TRUE)
|
|
|
+ .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
|
|
|
+ .onlySelect(GroupsRelationView::getId)
|
|
|
+ .build());
|
|
|
+ return relationViews;
|
|
|
+ }
|
|
|
+}
|