|
|
@@ -2,6 +2,7 @@ package com.cyksj.service.sora.impl;
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil;
|
|
|
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.mapper.SoraCodeMapper;
|
|
|
import com.cyksj.model.entity.SoraCode;
|
|
|
@@ -12,6 +13,7 @@ import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.dao.DataAccessException;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
|
|
|
/**
|
|
|
* Sora邀请码Service实现类
|
|
|
@@ -87,4 +89,47 @@ public class SoraCodeServiceImpl implements SoraCodeService {
|
|
|
}
|
|
|
soraCodeMapper.deleteById(id);
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ @Transactional
|
|
|
+ public void grantSoraCodeForGptMember(Long userId, Long orderId) {
|
|
|
+ // 检查该用户是否已经通过此订单获得过sora邀请码
|
|
|
+ Integer existCount = soraCodeMapper.selectCount(
|
|
|
+ new LambdaQueryWrapper<SoraCode>()
|
|
|
+ .eq(SoraCode::getUserId, userId)
|
|
|
+ .eq(SoraCode::getOrderId, orderId)
|
|
|
+ );
|
|
|
+
|
|
|
+ if (existCount > 0) {
|
|
|
+ log.warn("用户{}订单{}已赠送过sora邀请码,跳过重复赠送", userId, orderId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查找一个未使用的sora邀请码
|
|
|
+ SoraCode availableCode = soraCodeMapper.selectOne(
|
|
|
+ new LambdaQueryWrapper<SoraCode>()
|
|
|
+ .eq(SoraCode::getStatus, false)
|
|
|
+ .isNull(SoraCode::getUserId)
|
|
|
+ .isNull(SoraCode::getOrderId)
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+
|
|
|
+ if (availableCode == null) {
|
|
|
+ log.error("没有可用的sora邀请码赠送给用户{},订单{}", userId, orderId);
|
|
|
+ throw new BusinessRuntimeException("暂无可用的sora邀请码");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 将邀请码绑定给用户和订单
|
|
|
+ availableCode.setUserId(userId);
|
|
|
+ availableCode.setOrderId(orderId);
|
|
|
+ availableCode.setStatus(Boolean.TRUE);
|
|
|
+
|
|
|
+ int updateResult = soraCodeMapper.update(availableCode, Wrappers.lambdaUpdate(SoraCode.class).eq(SoraCode::getId, availableCode.getId()).eq(SoraCode::getStatus, Boolean.FALSE));
|
|
|
+ if (updateResult > 0) {
|
|
|
+ log.info("成功为用户{}订单{}赠送sora邀请码:{}", userId, orderId, availableCode.getCode());
|
|
|
+ } else {
|
|
|
+ log.error("为用户{}订单{}赠送sora邀请码失败", userId, orderId);
|
|
|
+ throw new BusinessRuntimeException("赠送sora邀请码失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|