优化-自动将Code-Create注册的新用户添加到AiDA

This commit is contained in:
2024-11-28 17:14:11 +08:00
parent e5e514b522
commit b70f909a32

View File

@@ -1392,7 +1392,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
private static final String QUERY_MAXIMUM_USERID = "SELECT MAX(ID) AS max_id FROM pmr_users;"; private static final String QUERY_MAXIMUM_USERID = "SELECT MAX(ID) AS max_id FROM pmr_users;";
private static final String QUERY_NEW_USER_EMAIL = "SELECT user_email FROM pmr_users " + private static final String QUERY_NEW_USER_EMAIL = "SELECT user_email, user_nicename FROM pmr_users " +
"WHERE ID > ? "; "WHERE ID > ? ";
@Value("${redis.key.maximumUserId}") @Value("${redis.key.maximumUserId}")
@@ -1404,7 +1404,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void registerUserToVisitor(){ public void registerUserToVisitor(){
ArrayList<String> newUserEmails = new ArrayList<>(); ArrayList<Map<String, String>> newUsersInfo = new ArrayList<>();
ArrayList<String> allEmail = new ArrayList<>();
long maxUserId = CommonConstant.MAXIMUM_USER_ID; long maxUserId = CommonConstant.MAXIMUM_USER_ID;
try (Connection connection = dataSource.getConnection(); try (Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(QUERY_MAXIMUM_USERID)) { PreparedStatement preparedStatement = connection.prepareStatement(QUERY_MAXIMUM_USERID)) {
@@ -1420,9 +1421,14 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
// 填充参数 - 历史最大用户ID // 填充参数 - 历史最大用户ID
newUserEmail.setLong(1, maxUserIdHistory); newUserEmail.setLong(1, maxUserIdHistory);
try (ResultSet queryEmailResultSet = newUserEmail.executeQuery()) { try (ResultSet queryEmailResultSet = newUserEmail.executeQuery()) {
if (queryEmailResultSet.next()) { while (queryEmailResultSet.next()) {
String email = queryEmailResultSet.getString("user_email"); String email = queryEmailResultSet.getString("user_email");
newUserEmails.add(email); String username = queryEmailResultSet.getString("user_nicename");
HashMap<String, String> info = new HashMap<>();
info.put("email", email);
info.put("username", username);
newUsersInfo.add(info);
allEmail.add(email);
} /*else { } /*else {
log.error("未知错误。code-create的用户表中没有付费用户的信息"); log.error("未知错误。code-create的用户表中没有付费用户的信息");
throw new BusinessException("user info missing"); throw new BusinessException("user info missing");
@@ -1437,22 +1443,32 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
// return null; // return null;
} }
if (!newUserEmails.isEmpty()){ if (!newUsersInfo.isEmpty()){
// 查询这些邮箱在aida上是否有账号 // 查询这些邮箱在aida上是否有账号
QueryWrapper<Account> queryWrapper = new QueryWrapper<>(); QueryWrapper<Account> queryWrapper = new QueryWrapper<>();
queryWrapper.in("user_email", newUserEmails).select("user_email"); queryWrapper.in("user_email", allEmail).select("user_email");
// 重复的邮箱
List<String> collect = baseMapper.selectList(queryWrapper).stream().map(Account::getUserEmail).collect(Collectors.toList()); List<String> collect = baseMapper.selectList(queryWrapper).stream().map(Account::getUserEmail).collect(Collectors.toList());
if (!collect.isEmpty()){ if (!collect.isEmpty()){
// 移除Code-Create新增用户中在AiDA已有账号的邮箱 // 移除Code-Create新增用户中在AiDA已有账号的邮箱allEmail中剩余邮箱均为新用户邮箱
newUserEmails.removeAll(collect); allEmail.removeAll(collect);
if (!allEmail.isEmpty()){
for (Map<String,String> userInfo : newUsersInfo){
String email = userInfo.get("email");
if (!allEmail.contains(email)) {
newUsersInfo.remove(userInfo); // 移除不在 allEmail 中的用户
}
}
}
} }
// 将新增用户添加到AiDA身份为游客 // 将新增用户添加到AiDA身份为游客
if (!newUserEmails.isEmpty()){ if (!newUsersInfo.isEmpty()){
newUserEmails.forEach(email -> { newUsersInfo.forEach(userInfo -> {
Account account = new Account(); Account account = new Account();
account.setUserEmail(email); account.setUserEmail(userInfo.get("email"));
account.setUserName(email); account.setUserName(userInfo.get("username"));
account.setUserPassword("Third-000000"); account.setUserPassword("Third-000000");
account.setLanguage(Language.ENGLISH.name()); account.setLanguage(Language.ENGLISH.name());
account.setValidStartTime(Instant.now().toEpochMilli()); account.setValidStartTime(Instant.now().toEpochMilli());
@@ -1463,7 +1479,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
account.setSystemUser(0); account.setSystemUser(0);
baseMapper.insert(account); baseMapper.insert(account);
// 邮件通知用户 // 邮件通知用户
SendEmailUtil.notificationForRegisterUser(email); SendEmailUtil.notificationForRegisterUser(userInfo.get("email"));
}); });
} }
// 记录当前最大的用户id // 记录当前最大的用户id