|
|
@@ -0,0 +1,143 @@
|
|
|
+package com.cyksj.service.gpt.impl;
|
|
|
+
|
|
|
+import cn.hutool.core.date.DateTime;
|
|
|
+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.GoodsDonSkuMapper;
|
|
|
+import com.cyksj.mapper.GptTeamAccountMapper;
|
|
|
+import com.cyksj.mapper.GptTeamUserBindRecordMapper;
|
|
|
+import com.cyksj.mapper.OrderDonMapper;
|
|
|
+import com.cyksj.model.entity.GoodsDonSku;
|
|
|
+import com.cyksj.model.entity.GptTeamAccount;
|
|
|
+import com.cyksj.model.entity.GptTeamUserBindRecord;
|
|
|
+import com.cyksj.model.entity.OrderDon;
|
|
|
+import com.cyksj.model.request.GptTeamBindRequest;
|
|
|
+import com.cyksj.service.gpt.GptTeamUserBindRecordService;
|
|
|
+import com.cyksj.service.user.UserBindRelationService;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.dao.DuplicateKeyException;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * GPT Team用户绑定记录服务实现
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class GptTeamUserBindRecordServiceImpl implements GptTeamUserBindRecordService {
|
|
|
+
|
|
|
+ private final GptTeamUserBindRecordMapper bindRecordMapper;
|
|
|
+
|
|
|
+ private final GptTeamAccountMapper gptTeamAccountMapper;
|
|
|
+
|
|
|
+ private final OrderDonMapper orderDonMapper;
|
|
|
+
|
|
|
+ private final UserBindRelationService userBindRelationService;
|
|
|
+
|
|
|
+ private final GoodsDonSkuMapper goodsDonSkuMapper;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Throwable.class)
|
|
|
+ public void bindByOrderId(GptTeamBindRequest request) {
|
|
|
+ Long orderId = request.getOrderId();
|
|
|
+ Long userId = request.getUserId();
|
|
|
+ String email = request.getEmail();
|
|
|
+
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ // 1. 验证订单
|
|
|
+ OrderDon order = orderDonMapper.selectOne(Wrappers.lambdaQuery(OrderDon.class).eq(OrderDon::getId, orderId).in(OrderDon::getUserId, userIdList).notIn(OrderDon::getStatus, Constant.noOrderAllStatus).last("limit 1"));
|
|
|
+ if (order == null) {
|
|
|
+ throw new BusinessRuntimeException("订单不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ GoodsDonSku sku = goodsDonSkuMapper.selectById(order.getSkuId());
|
|
|
+ //校验是否是gptTeam规格
|
|
|
+ if (sku == null || !sku.getIsGptTeam()) {
|
|
|
+ throw new BusinessRuntimeException("错误订单规格");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 2. 检查是否已经绑定
|
|
|
+ Integer selectCount = bindRecordMapper.selectCount(Wrappers.lambdaQuery(GptTeamUserBindRecord.class).eq(GptTeamUserBindRecord::getOrderId, orderId));
|
|
|
+ if (selectCount > 0) {
|
|
|
+ throw BusinessRuntimeException.getInstance("已有绑定的Team");
|
|
|
+ }
|
|
|
+ // 4. 创建绑定记录
|
|
|
+ GptTeamUserBindRecord bindRecord = new GptTeamUserBindRecord();
|
|
|
+ //订单用户id
|
|
|
+ bindRecord.setUserId(order.getUserId());
|
|
|
+ bindRecord.setBindTime(DateTime.now());
|
|
|
+ bindRecord.setBindEmail(email);
|
|
|
+ try {
|
|
|
+ bindRecordMapper.insert(bindRecord);
|
|
|
+ } catch (DuplicateKeyException e) {
|
|
|
+ throw BusinessRuntimeException.getInstance("已有绑定的Team");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 3. 查找可用的GPT Team账号
|
|
|
+ LambdaQueryWrapper<GptTeamAccount> accountWrapper = new LambdaQueryWrapper<>();
|
|
|
+ accountWrapper.eq(GptTeamAccount::getStatus, true)
|
|
|
+ .notExists("SELECT 1 FROM gpt_team_user_bind_record WHERE gpt_team_account_id = gpt_team_account.id AND bind_status = 1");
|
|
|
+
|
|
|
+ List<GptTeamAccount> availableAccounts = gptTeamAccountMapper.selectList(accountWrapper);
|
|
|
+ if (availableAccounts.isEmpty()) {
|
|
|
+ throw new BusinessRuntimeException("暂无可用的GPT Team账号,请稍后再试");
|
|
|
+ }
|
|
|
+
|
|
|
+ GptTeamAccount selectedAccount = availableAccounts.get(0);
|
|
|
+
|
|
|
+ //绑定 Team API
|
|
|
+ bindRecord.setToken("");
|
|
|
+ bindRecordMapper.updateById(bindRecord);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public GptTeamUserBindRecord getBindRecord(Long orderId, Long userId) {
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ GptTeamUserBindRecord bindRecord = bindRecordMapper.selectOne(Wrappers.lambdaQuery(GptTeamUserBindRecord.class).eq(GptTeamUserBindRecord::getOrderId, orderId).in(GptTeamUserBindRecord::getUserId, userIdList).last("limit 1"));
|
|
|
+ return bindRecord;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
+ public void changeBind(GptTeamBindRequest request) {
|
|
|
+ Long orderId = request.getOrderId();
|
|
|
+ Long userId = request.getUserId();
|
|
|
+
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+ GptTeamUserBindRecord bindRecord = bindRecordMapper.selectOne(Wrappers.lambdaQuery(GptTeamUserBindRecord.class).eq(GptTeamUserBindRecord::getOrderId, orderId).in(GptTeamUserBindRecord::getUserId, userIdList).last("limit 1"));
|
|
|
+ if (bindRecord == null) {
|
|
|
+ throw BusinessRuntimeException.getInstance("您还未假如Team");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 1. 验证换绑次数
|
|
|
+ Integer changeNum = bindRecord.getChangeNum();
|
|
|
+ if (changeNum == 0) {
|
|
|
+ throw new BusinessRuntimeException("仅可换绑一次");
|
|
|
+ }
|
|
|
+
|
|
|
+ int update = bindRecordMapper.update(null, Wrappers.lambdaUpdate(GptTeamUserBindRecord.class)
|
|
|
+ .set(GptTeamUserBindRecord::getChangeNum, 0)
|
|
|
+ .eq(GptTeamUserBindRecord::getId, bindRecord.getId())
|
|
|
+ .eq(GptTeamUserBindRecord::getChangeNum, changeNum));
|
|
|
+ if (update == 0) {
|
|
|
+ throw new BusinessRuntimeException("仅可换绑一次");
|
|
|
+ }
|
|
|
+ //换绑前的token
|
|
|
+ String bfToken = bindRecord.getToken();
|
|
|
+ // 4. 查找新的可用账号
|
|
|
+ LambdaQueryWrapper<GptTeamAccount> accountWrapper = new LambdaQueryWrapper<>();
|
|
|
+ accountWrapper.eq(GptTeamAccount::getStatus, true)
|
|
|
+ .ne(GptTeamAccount::getToken, bfToken)
|
|
|
+ .notExists("SELECT 1 FROM gpt_team_user_bind_record WHERE gpt_team_account_id = gpt_team_account.id AND bind_status = 1");
|
|
|
+ //换绑其他Team API
|
|
|
+ bindRecord.setToken("");
|
|
|
+ bindRecord.setBfToken(bfToken);
|
|
|
+ bindRecordMapper.updateById(bindRecord);
|
|
|
+ }
|
|
|
+}
|