|
|
@@ -16,9 +16,11 @@ import com.cyksj.common.util.StringUtil;
|
|
|
import com.cyksj.mapper.*;
|
|
|
import com.cyksj.mapper.claudecode.ClaudeCodeUserResetRecordMapper;
|
|
|
import com.cyksj.mapper.claudecode.ClaudeCodeUserCreditCardRecordMapper;
|
|
|
+import com.cyksj.mapper.claudecode.ClaudeCodeCreditCardMapper;
|
|
|
import com.cyksj.model.entity.*;
|
|
|
import com.cyksj.model.manage.views.GroupsRelationView;
|
|
|
import com.cyksj.model.request.ClaudeCodeApiKeysReq;
|
|
|
+import com.cyksj.model.request.ClaudeCodeCreditCardUseReq;
|
|
|
import com.cyksj.model.request.ClaudeCodeDelReq;
|
|
|
import com.cyksj.model.request.CreditResetReq;
|
|
|
import com.cyksj.model.request.TimeRateConfigReq;
|
|
|
@@ -74,6 +76,8 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
|
|
|
|
|
|
private final ClaudeCodeUserCreditCardRecordMapper claudeCodeUserCreditCardRecordMapper;
|
|
|
|
|
|
+ private final ClaudeCodeCreditCardMapper claudeCodeCreditCardMapper;
|
|
|
+
|
|
|
private final static String CLAUDE_CODE_API_PREFIX = "https://relay01.yhlxj.com/";
|
|
|
|
|
|
public static final String CLAUDE_CODE_API_ADMIN_KEY = "claudecodeyhlxjclaude";
|
|
|
@@ -687,7 +691,7 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void useCreditCard(Long userId, Integer addCodePoints, Integer cardType, String cardNumber, Long operatorId, String operatorName, String useReason) {
|
|
|
+ public void useCreditCard(Long userId, Integer addCodePoints, String cardNumber, Long operatorId, String operatorName, String useReason) {
|
|
|
ClaudeCodeUser claudeCodeUser = claudeCodeUserMapper.selectOne(
|
|
|
Wrappers.<ClaudeCodeUser>lambdaQuery().eq(ClaudeCodeUser::getUserId, userId)
|
|
|
);
|
|
|
@@ -707,7 +711,6 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
|
|
|
creditCardRecord.setBeforeCodePoints(beforeCodePoints);
|
|
|
creditCardRecord.setAddCodePoints(addCodePoints);
|
|
|
creditCardRecord.setAfterCodePoints(afterCodePoints);
|
|
|
- creditCardRecord.setCardType(cardType);
|
|
|
creditCardRecord.setCardNumber(cardNumber);
|
|
|
creditCardRecord.setOperatorId(operatorId);
|
|
|
creditCardRecord.setOperatorName(operatorName);
|
|
|
@@ -758,4 +761,86 @@ public class ClaudeCodeServiceImpl implements ClaudeCodeService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public ClaudeCodeResp useCreditCard(ClaudeCodeCreditCardUseReq req) throws Exception {
|
|
|
+ Long userId = req.getUserId();
|
|
|
+ List<Long> userIdList = userBindRelationService.getRelationUserIdList(userId, null);
|
|
|
+
|
|
|
+ ClaudeCodeUser claudeCodeUser = claudeCodeUserMapper.selectOne(
|
|
|
+ Wrappers.lambdaQuery(ClaudeCodeUser.class)
|
|
|
+ .in(ClaudeCodeUser::getUserId, userIdList)
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+
|
|
|
+ if (claudeCodeUser == null) {
|
|
|
+ throw new BusinessRuntimeException("用户未开通Claude Code服务");
|
|
|
+ }
|
|
|
+
|
|
|
+ ClaudeCodeCreditCard creditCard = claudeCodeCreditCardMapper.selectOne(
|
|
|
+ Wrappers.lambdaQuery(ClaudeCodeCreditCard.class)
|
|
|
+ .eq(ClaudeCodeCreditCard::getStatus, 0)
|
|
|
+ .last("limit 1")
|
|
|
+ );
|
|
|
+
|
|
|
+ if (creditCard == null) {
|
|
|
+ throw new BusinessRuntimeException("积分卡不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ if (creditCard.getStatus() != 0) {
|
|
|
+ if (creditCard.getStatus() == 1) {
|
|
|
+ throw new BusinessRuntimeException("积分卡已被使用");
|
|
|
+ } else if (creditCard.getStatus() == 2) {
|
|
|
+ throw new BusinessRuntimeException("积分卡已过期");
|
|
|
+ } else {
|
|
|
+ throw new BusinessRuntimeException("积分卡状态异常");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ String cardNumber = creditCard.getCardNumber();
|
|
|
+
|
|
|
+ if (creditCard.getExpiryTime() != null && creditCard.getExpiryTime().before(new Date())) {
|
|
|
+ creditCard.setStatus(2);
|
|
|
+ claudeCodeCreditCardMapper.updateById(creditCard);
|
|
|
+ throw new BusinessRuntimeException("积分卡已过期");
|
|
|
+ }
|
|
|
+
|
|
|
+ Integer beforeCodePoints = claudeCodeUser.getCodePoints();
|
|
|
+ Integer addCodePoints = creditCard.getCreditAmount();
|
|
|
+ Integer afterCodePoints = beforeCodePoints + addCodePoints;
|
|
|
+
|
|
|
+ try {
|
|
|
+ claudeCodeUser.setCodePoints(afterCodePoints);
|
|
|
+ int userUpdateResult = claudeCodeUserMapper.updateById(claudeCodeUser);
|
|
|
+
|
|
|
+ creditCard.setStatus(1);
|
|
|
+ creditCard.setUsedUserId(userId);
|
|
|
+ creditCard.setUsedTime(new Date());
|
|
|
+ int cardUpdateResult = claudeCodeCreditCardMapper.update(creditCard,
|
|
|
+ Wrappers.lambdaUpdate(ClaudeCodeCreditCard.class)
|
|
|
+ .eq(ClaudeCodeCreditCard::getId, creditCard.getId())
|
|
|
+ .eq(ClaudeCodeCreditCard::getStatus, 0)
|
|
|
+ );
|
|
|
+
|
|
|
+ if (userUpdateResult > 0 && cardUpdateResult > 0) {
|
|
|
+ useCreditCard(userId,addCodePoints,cardNumber,null,"用户自助","使用积分卡");
|
|
|
+
|
|
|
+ log.info("用户{}成功使用积分卡{},增加{}积分", userId, cardNumber, addCodePoints);
|
|
|
+
|
|
|
+ ClaudeCodeResp resp = new ClaudeCodeResp();
|
|
|
+ resp.setSuccess(true);
|
|
|
+ resp.setMessage("积分卡使用成功");
|
|
|
+ resp.setData(Map.of(
|
|
|
+ "beforeCodePoints", beforeCodePoints,
|
|
|
+ "addCodePoints", addCodePoints,
|
|
|
+ "afterCodePoints", afterCodePoints
|
|
|
+ ));
|
|
|
+ return resp;
|
|
|
+ } else {
|
|
|
+ throw new BusinessRuntimeException("积分卡使用失败,请重试");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("用户{}使用积分卡{}异常", userId, cardNumber, e);
|
|
|
+ throw new BusinessRuntimeException("积分卡使用失败:" + e.getMessage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|