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.GptTeamAccountService; import com.ejlchina.searcher.BeanSearcher; import com.ejlchina.searcher.SearchResult; import com.ejlchina.searcher.util.MapUtils; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import javax.servlet.http.HttpServletRequest; 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 implements GptTeamAccountService { private final BeanSearcher beanSearcher; private final HttpServletRequest request; @Override public SearchResult getPage() { return beanSearcher.search(GptTeamAccount.class, MapUtils.flatBuilder(request.getParameterMap()) .orderBy(GptTeamAccount::getId).desc() .build()); } @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 tokens) { if (CollUtil.isEmpty(tokens)) { throw BusinessRuntimeException.getInstance("导入数据不能为空"); } // 去重 Set tokenSet = new HashSet<>(tokens); // 过滤空白token tokenSet.removeIf(StrUtil::isBlank); if (tokenSet.isEmpty()) { throw BusinessRuntimeException.getInstance("有效token为空"); } // 查询已存在的token List existingAccounts = baseMapper.selectList( Wrappers.lambdaQuery(GptTeamAccount.class) .in(GptTeamAccount::getToken, tokenSet) ); Set existingTokens = new HashSet<>(); for (GptTeamAccount account : existingAccounts) { existingTokens.add(account.getToken()); } // 准备新增的账号列表 List 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); } } }