GptQuickLinkServiceImpl.java 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package com.cyksj.service.gpt.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.date.DateTime;
  4. import cn.hutool.core.date.DateUtil;
  5. import cn.hutool.core.util.RandomUtil;
  6. import cn.hutool.json.JSONUtil;
  7. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  8. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  9. import com.cyksj.common.constant.Constant;
  10. import com.cyksj.common.exception.BusinessRuntimeException;
  11. import com.cyksj.common.util.StringUtil;
  12. import com.cyksj.mapper.GptQuickLinkRecordMapper;
  13. import com.cyksj.mapper.GroupsRelationMapper;
  14. import com.cyksj.model.entity.GptQuickLinkRecord;
  15. import com.cyksj.model.entity.SysConfig;
  16. import com.cyksj.model.manage.views.GroupsRelationView;
  17. import com.cyksj.model.response.GptQuickLinkResp;
  18. import com.cyksj.service.chatgpt.ChatGptAccountService;
  19. import com.cyksj.service.gpt.GptQuickLinkService;
  20. import com.cyksj.service.sys.SysConfigService;
  21. import com.cyksj.service.user.UserBindRelationService;
  22. import com.ejlchina.searcher.BeanSearcher;
  23. import com.ejlchina.searcher.param.Operator;
  24. import com.ejlchina.searcher.util.MapUtils;
  25. import lombok.RequiredArgsConstructor;
  26. import lombok.extern.slf4j.Slf4j;
  27. import org.springframework.dao.DuplicateKeyException;
  28. import org.springframework.stereotype.Service;
  29. import java.util.Date;
  30. import java.util.List;
  31. /**
  32. * GPT快捷链接服务实现类
  33. */
  34. @Slf4j
  35. @Service
  36. @RequiredArgsConstructor
  37. public class GptQuickLinkServiceImpl implements GptQuickLinkService {
  38. private final GptQuickLinkRecordMapper gptQuickLinkRecordMapper;
  39. private final GroupsRelationMapper groupsRelationMapper;
  40. private final UserBindRelationService userBindRelationService;
  41. private final BeanSearcher beanSearcher;
  42. private final ChatGptAccountService chatGptAccountService;
  43. private final SysConfigService sysConfigService;
  44. @Override
  45. public GptQuickLinkRecord createQuickLink(Long userId) {
  46. // 检查用户是否有GPT镜像车票
  47. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  48. if (getValidGptTicket(userIdList).isEmpty()) {
  49. throw new BusinessRuntimeException("您没有有效的GPT镜像车票");
  50. }
  51. //生成标识
  52. String code = StringUtil.randomString(6);;
  53. GptQuickLinkRecord quickLink = new GptQuickLinkRecord();
  54. quickLink.setUserId(userId);
  55. quickLink.setCode(code);
  56. // 创建新的快捷链接,有效期30天
  57. quickLink.setExpiryTime(DateUtil.offsetDay(DateTime.now(), 30));
  58. try {
  59. gptQuickLinkRecordMapper.insert(quickLink);
  60. //将之前的链接变成失效
  61. gptQuickLinkRecordMapper.update(null, Wrappers.lambdaUpdate(GptQuickLinkRecord.class)
  62. .set(GptQuickLinkRecord::getExpiryTime, DateTime.now())
  63. .in(GptQuickLinkRecord::getUserId, userIdList)
  64. .ne(GptQuickLinkRecord::getCode, code)
  65. .gt(GptQuickLinkRecord::getExpiryTime, DateTime.now()));
  66. } catch (DuplicateKeyException e) {
  67. throw BusinessRuntimeException.getInstance("系统繁忙,请重新生成!");
  68. }
  69. return quickLink;
  70. }
  71. @Override
  72. public GptQuickLinkRecord getLatestQuickLink(Long userId) {
  73. Date now = DateTime.now();
  74. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  75. LambdaQueryWrapper<GptQuickLinkRecord> wrapper = Wrappers.lambdaQuery(GptQuickLinkRecord.class)
  76. .in(GptQuickLinkRecord::getUserId, userIdList)
  77. .gt(GptQuickLinkRecord::getExpiryTime, now)
  78. .orderByDesc(GptQuickLinkRecord::getId)
  79. .last("limit 1");
  80. return gptQuickLinkRecordMapper.selectOne(wrapper);
  81. }
  82. @Override
  83. public GptQuickLinkResp getQuickUrlByCode(String code) {
  84. Date now = DateTime.now();
  85. LambdaQueryWrapper<GptQuickLinkRecord> wrapper = Wrappers.lambdaQuery(GptQuickLinkRecord.class)
  86. .eq(GptQuickLinkRecord::getCode, code)
  87. .last("limit 1");
  88. GptQuickLinkRecord gptQuickLinkRecord = gptQuickLinkRecordMapper.selectOne(wrapper);
  89. if (gptQuickLinkRecord == null) {
  90. throw BusinessRuntimeException.getInstance("快捷链接不存在");
  91. }
  92. GptQuickLinkResp resp = new GptQuickLinkResp();
  93. resp.setUserId(gptQuickLinkRecord.getUserId());
  94. //成功
  95. Integer state = 1;
  96. if (gptQuickLinkRecord != null) {
  97. if (gptQuickLinkRecord.getExpiryTime().before(now)) {
  98. //链接失效
  99. state = 2;
  100. } else {
  101. Long userId = gptQuickLinkRecord.getUserId();
  102. List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
  103. List<GroupsRelationView> validGptTicket = getValidGptTicket(userIdList);
  104. if (validGptTicket.isEmpty()) {
  105. //车票过期
  106. state = 3;
  107. } else {
  108. if (validGptTicket.size() > 1) {
  109. //多张车票
  110. state = 4;
  111. } else {
  112. GroupsRelationView groupsRelationView = validGptTicket.get(0);
  113. Long relationId = groupsRelationView.getId();
  114. //获取gpt镜像访问url
  115. String url = chatGptAccountService.getCarLoginUrl(getGptDomain(), userId, relationId, Constant.GPT_CARD_ID);
  116. resp.setUrl(url);
  117. }
  118. }
  119. }
  120. }
  121. resp.setState(state);
  122. return resp;
  123. }
  124. public List<GroupsRelationView> getValidGptTicket(List<Long> userIdList) {
  125. List<GroupsRelationView> relationViews = beanSearcher.searchAll(GroupsRelationView.class, MapUtils.builder()
  126. .field(GroupsRelationView::getGoodsId, Constant.GPT_PLUS_GID)
  127. .field(GroupsRelationView::getUserId, userIdList).op(Operator.InList)
  128. .field(GroupsRelationView::getIsMirror, Boolean.TRUE)
  129. .field(GroupsRelationView::getExpiryTime, DateTime.now()).op(Operator.GreaterThan)
  130. .onlySelect(GroupsRelationView::getId)
  131. .build());
  132. return relationViews;
  133. }
  134. public String getGptDomain() {
  135. SysConfig gptNewDomain = sysConfigService.getOne(Wrappers.lambdaQuery(SysConfig.class)
  136. .eq(SysConfig::getSysKey, "mirror_gpt_new_domain")
  137. .last("limit 1"));
  138. if (gptNewDomain != null) {
  139. String sysValue = gptNewDomain.getSysValue();
  140. if (JSONUtil.isJsonArray(sysValue)) {
  141. List<String> domainList = JSONUtil.parseArray(sysValue).toList(String.class);
  142. if (CollUtil.isNotEmpty(domainList)) {
  143. int randomInt = RandomUtil.randomInt(domainList.size());
  144. return domainList.get(randomInt);
  145. }
  146. }
  147. }
  148. return null;
  149. }
  150. }