| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- package com.cyksj.service.gpt.impl;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.date.DateTime;
- import cn.hutool.core.date.DateUtil;
- import cn.hutool.core.util.RandomUtil;
- 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.util.StringUtil;
- import com.cyksj.mapper.GptQuickLinkRecordMapper;
- import com.cyksj.mapper.GroupsRelationMapper;
- import com.cyksj.model.entity.GptQuickLinkRecord;
- import com.cyksj.model.entity.SysConfig;
- 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.sys.SysConfigService;
- 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;
- private final SysConfigService sysConfigService;
- @Override
- public GptQuickLinkRecord createQuickLink(Long userId) {
- // 检查用户是否有GPT镜像车票
- List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
- if (getValidGptTicket(userIdList).isEmpty()) {
- throw new BusinessRuntimeException("您没有有效的GPT镜像车票");
- }
- //生成标识
- String code = StringUtil.randomString(6);;
- GptQuickLinkRecord quickLink = new GptQuickLinkRecord();
- quickLink.setUserId(userId);
- quickLink.setCode(code);
- // 创建新的快捷链接,有效期30天
- quickLink.setExpiryTime(DateUtil.offsetDay(DateTime.now(), 30));
- try {
- gptQuickLinkRecordMapper.insert(quickLink);
- //将之前的链接变成失效
- gptQuickLinkRecordMapper.update(null, Wrappers.lambdaUpdate(GptQuickLinkRecord.class)
- .set(GptQuickLinkRecord::getExpiryTime, DateTime.now())
- .in(GptQuickLinkRecord::getUserId, userIdList)
- .ne(GptQuickLinkRecord::getCode, code)
- .gt(GptQuickLinkRecord::getExpiryTime, DateTime.now()));
- } 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);
- if (gptQuickLinkRecord == null) {
- throw BusinessRuntimeException.getInstance("快捷链接不存在");
- }
- GptQuickLinkResp resp = new GptQuickLinkResp();
- resp.setUserId(gptQuickLinkRecord.getUserId());
- //成功
- 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(getGptDomain(), 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;
- }
- public String getGptDomain() {
- SysConfig gptNewDomain = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
- .eq(SysConfig::getSysKey, "mirror_gpt_new_domain")
- .last("limit 1"));
- if (gptNewDomain != null) {
- String sysValue = gptNewDomain.getSysValue();
- if (JSONUtil.isJsonArray(sysValue)) {
- List<String> domainList = JSONUtil.parseArray(sysValue).toList(String.class);
- if (CollUtil.isNotEmpty(domainList)) {
- int randomInt = RandomUtil.randomInt(domainList.size());
- return domainList.get(randomInt);
- }
- }
- }
- return null;
- }
- }
|