| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148 |
- package com.cyksj.service.chatgpt.impl;
- import cn.hutool.core.collection.CollUtil;
- import cn.hutool.core.util.StrUtil;
- import com.baomidou.mybatisplus.core.toolkit.Wrappers;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.cyksj.common.exception.BusinessRuntimeException;
- import com.cyksj.mapper.GptTeamAccountMapper;
- import com.cyksj.model.entity.GptTeamAccount;
- import com.cyksj.service.chatgpt.ChatGptWorkspaceService;
- import com.cyksj.service.chatgpt.GptTeamAccountService;
- import lombok.RequiredArgsConstructor;
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import java.util.ArrayList;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- /**
- * GPT Team账号 Service实现
- */
- @Slf4j
- @Service
- @RequiredArgsConstructor
- public class GptTeamAccountServiceImpl extends ServiceImpl<GptTeamAccountMapper, GptTeamAccount> implements GptTeamAccountService {
- private final ChatGptWorkspaceService gptWorkspaceService;
- @Override
- @Transactional(rollbackFor = Exception.class)
- public void addAccount(GptTeamAccount account) {
- // 验证token唯一性
- if (StrUtil.isNotBlank(account.getToken())) {
- Integer count = baseMapper.selectCount(Wrappers.lambdaQuery(GptTeamAccount.class)
- .eq(GptTeamAccount::getToken, account.getToken()));
- if (count > 0) {
- throw BusinessRuntimeException.getInstance("Token已存在");
- }
- }
- // 验证账号唯一性
- if (StrUtil.isNotBlank(account.getAccount())) {
- Integer count = baseMapper.selectCount(Wrappers.lambdaQuery(GptTeamAccount.class)
- .eq(GptTeamAccount::getAccount, account.getAccount()));
- if (count > 0) {
- throw BusinessRuntimeException.getInstance("账号已存在");
- }
- }
- this.save(account);
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public void updateAccount(GptTeamAccount account) {
- if (account.getId() == null) {
- throw BusinessRuntimeException.getInstance("ID不能为空");
- }
- // 验证token唯一性
- if (StrUtil.isNotBlank(account.getToken())) {
- Integer count = baseMapper.selectCount(Wrappers.lambdaQuery(GptTeamAccount.class)
- .eq(GptTeamAccount::getToken, account.getToken())
- .ne(GptTeamAccount::getId, account.getId()));
- if (count > 0) {
- throw BusinessRuntimeException.getInstance("Token已存在");
- }
- }
- // 验证账号唯一性
- if (StrUtil.isNotBlank(account.getAccount())) {
- Integer count = baseMapper.selectCount(Wrappers.lambdaQuery(GptTeamAccount.class)
- .eq(GptTeamAccount::getAccount, account.getAccount())
- .ne(GptTeamAccount::getId, account.getId()));
- if (count > 0) {
- throw BusinessRuntimeException.getInstance("账号已存在");
- }
- }
- this.updateById(account);
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public void deleteAccount(Long id) {
- if (id == null) {
- throw BusinessRuntimeException.getInstance("ID不能为空");
- }
- this.removeById(id);
- }
- @Override
- @Transactional(rollbackFor = Exception.class)
- public void batchImport(List<String> tokens) {
- if (CollUtil.isEmpty(tokens)) {
- throw BusinessRuntimeException.getInstance("导入数据不能为空");
- }
- // 去重
- Set<String> tokenSet = new HashSet<>(tokens);
-
- // 过滤空白token
- tokenSet.removeIf(StrUtil::isBlank);
-
- if (tokenSet.isEmpty()) {
- throw BusinessRuntimeException.getInstance("有效token为空");
- }
- // 查询已存在的token
- List<GptTeamAccount> existingAccounts = baseMapper.selectList(
- Wrappers.lambdaQuery(GptTeamAccount.class)
- .in(GptTeamAccount::getToken, tokenSet)
- );
-
- Set<String> existingTokens = new HashSet<>();
- for (GptTeamAccount account : existingAccounts) {
- existingTokens.add(account.getToken());
- }
- // 准备新增的账号列表
- List<GptTeamAccount> newAccounts = new ArrayList<>();
- int skipCount = 0;
-
- for (String token : tokenSet) {
- if (existingTokens.contains(token)) {
- skipCount++;
- continue;
- }
-
- GptTeamAccount account = new GptTeamAccount();
- account.setToken(token);
- account.setStatus(true); // 默认启用
- newAccounts.add(account);
- }
- // 批量保存
- if (CollUtil.isNotEmpty(newAccounts)) {
- this.saveBatch(newAccounts);
- }
- }
- @Override
- public void removeTeamUser(String accountId, String userKey) {
- gptWorkspaceService.deleteWorkspaceUser(accountId, userKey);
- }
- }
|