From d23ac82b1b1f619695784a0cb5e65c53c97ce14f Mon Sep 17 00:00:00 2001 From: xupei Date: Tue, 27 Aug 2024 11:27:05 +0800 Subject: [PATCH] =?UTF-8?q?TASK:code-create=20=E6=B3=A8=E5=86=8C=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E6=B7=BB=E5=8A=A0=E4=B8=BAAiDA=E7=9A=84=E6=B8=B8?= =?UTF-8?q?=E5=AE=A2=20BUGFIX=EF=BC=9A0=E5=85=B3=E6=B3=A8=E7=8A=B6?= =?UTF-8?q?=E6=80=81=E4=B8=8B=E8=8E=B7=E5=8F=96=E5=8A=A8=E6=80=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ai/da/common/constant/CommonConstant.java | 4 + .../com/ai/da/common/task/AccountTask.java | 8 ++ .../com/ai/da/common/utils/SendEmailUtil.java | 36 ++++++++ .../com/ai/da/service/AccountService.java | 2 + .../da/service/impl/AccountServiceImpl.java | 83 +++++++++++++++++++ .../impl/ConvenientInquiryServiceImpl.java | 10 +++ .../service/impl/DesignItemServiceImpl.java | 1 + .../impl/MessageCenterServiceImpl.java | 18 +++- 8 files changed, 160 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/ai/da/common/constant/CommonConstant.java b/src/main/java/com/ai/da/common/constant/CommonConstant.java index 3f29a938..b167f4b6 100644 --- a/src/main/java/com/ai/da/common/constant/CommonConstant.java +++ b/src/main/java/com/ai/da/common/constant/CommonConstant.java @@ -61,4 +61,8 @@ public class CommonConstant { public static final String DEFAULT_AVATAR = "aida-users/87/avatar/default.jpg"; + /* 截止至2024/08/26,在Code-Create DB中pmr_users表中最大的用户id */ + public static final Long MAXIMUM_USER_ID = 704L; +// public static final Long MAXIMUM_USER_ID = 225L; + } diff --git a/src/main/java/com/ai/da/common/task/AccountTask.java b/src/main/java/com/ai/da/common/task/AccountTask.java index 0151665d..b2e0df65 100644 --- a/src/main/java/com/ai/da/common/task/AccountTask.java +++ b/src/main/java/com/ai/da/common/task/AccountTask.java @@ -57,4 +57,12 @@ public class AccountTask { accountService.toVisitor(account); } } + + /** + * 将Code-Create上注册的用户添加为AiDA的游客 + */ + // @Scheduled(cron = "0 */5 * * * *") // Run every 5 minutes + public void registerUserToVisitor(){ + accountService.registerUserToVisitor(); + } } diff --git a/src/main/java/com/ai/da/common/utils/SendEmailUtil.java b/src/main/java/com/ai/da/common/utils/SendEmailUtil.java index 7f6706cc..16fcfe21 100644 --- a/src/main/java/com/ai/da/common/utils/SendEmailUtil.java +++ b/src/main/java/com/ai/da/common/utils/SendEmailUtil.java @@ -590,4 +590,40 @@ public class SendEmailUtil { throw new BusinessException("failed.to.send.mail"); } } + + private final static Long NEW_USER_REGISTER_NOTIFICATION_EN = 126919L; + + public static void notificationForRegisterUser(String receiverAddress){ + try{ + // 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密 + // 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305 + // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取 + Credential cred = new Credential(SECRET_ID, SECRET_KEy); + // 实例化一个http选项,可选的,没有特殊需求可以跳过 + HttpProfile httpProfile = new HttpProfile(); + httpProfile.setEndpoint("ses.tencentcloudapi.com"); + // 实例化一个client选项,可选的,没有特殊需求可以跳过 + ClientProfile clientProfile = new ClientProfile(); + clientProfile.setHttpProfile(httpProfile); + // 实例化要请求产品的client对象,clientProfile是可选的 + SesClient client = new SesClient(cred, "ap-hongkong", clientProfile); + // 实例化一个请求对象,每个接口都会对应一个request对象 + SendEmailRequest req = new SendEmailRequest(); + req.setFromEmailAddress(SEND_ADDRESS); + req.setDestination(new String[]{receiverAddress}); + + req.setSubject("Welcome to AiDa"); + Template template = new Template(); + template.setTemplateID(NEW_USER_REGISTER_NOTIFICATION_EN); + template.setTemplateData("{\"email\":\" " + receiverAddress + "\"}"); + req.setTemplate(template); + + // 返回的resp是一个SendEmailResponse的实例,与请求对象对应 + SendEmailResponse resp = client.SendEmail(req); + log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp)); + } catch (TencentCloudSDKException e) { + log.info("邮件发送失败###{}", e.toString()); + throw new BusinessException("failed.to.send.mail"); + } + } } diff --git a/src/main/java/com/ai/da/service/AccountService.java b/src/main/java/com/ai/da/service/AccountService.java index 8d97d549..01b7d03e 100644 --- a/src/main/java/com/ai/da/service/AccountService.java +++ b/src/main/java/com/ai/da/service/AccountService.java @@ -165,4 +165,6 @@ public interface AccountService extends IService { PersonalHomepageVO getPersonalHomepage(Long accountId); Boolean viewsIncrease(Long id); + + void registerUserToVisitor(); } diff --git a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java index 7b96b3a2..d135060b 100644 --- a/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/AccountServiceImpl.java @@ -1079,6 +1079,9 @@ public class AccountServiceImpl extends ServiceImpl impl config.setJdbcUrl("jdbc:mysql://code-create.com.hk:3306/db1nfvsgmjp3b8"); config.setUsername("uafqtz4gsvfrw"); config.setPassword("aida123456."); +// config.setJdbcUrl("jdbc:mysql://localhost:3306/code-create-local?serverTimezone=UTC"); +// config.setUsername("root"); +// config.setPassword("root"); config.addDataSourceProperty("cachePrepStmts", "true"); config.addDataSourceProperty("prepStmtCacheSize", "250"); config.addDataSourceProperty("prepStmtCacheSqlLimit", "2048"); @@ -1235,6 +1238,86 @@ public class AccountServiceImpl extends ServiceImpl impl return account; } + 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 " + + "WHERE ID > ? "; + + @Value("${redis.key.maximumUserId}") + private String maximumUserIdKey; + + /** + * 将Code-Create上注册的用户添加为AiDA的游客 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public void registerUserToVisitor(){ + ArrayList newUserEmails = new ArrayList<>(); + long maxUserId = CommonConstant.MAXIMUM_USER_ID; + try (Connection connection = dataSource.getConnection(); + PreparedStatement preparedStatement = connection.prepareStatement(QUERY_MAXIMUM_USERID)) { + try (ResultSet queryOrderResultSet = preparedStatement.executeQuery()) { + while (queryOrderResultSet.next()) { + // 获取最新的最大用户id + maxUserId = queryOrderResultSet.getLong("max_id"); + // 获取历史最大用户id + long maxUserIdHistory = StringUtil.isNullOrEmpty(redisUtil.getFromString(maximumUserIdKey)) ? CommonConstant.MAXIMUM_USER_ID : Long.parseLong(redisUtil.getFromString(maximumUserIdKey)); + if (maxUserId > maxUserIdHistory){ + // 查出新增用户的邮箱 + PreparedStatement newUserEmail = connection.prepareStatement(QUERY_NEW_USER_EMAIL); + // 填充参数 - 历史最大用户ID + newUserEmail.setLong(1, maxUserIdHistory); + try (ResultSet queryEmailResultSet = newUserEmail.executeQuery()) { + if (queryEmailResultSet.next()) { + String email = queryEmailResultSet.getString("user_email"); + newUserEmails.add(email); + } /*else { + log.error("未知错误。code-create的用户表中没有付费用户的信息"); + throw new BusinessException("user info missing"); + }*/ + } + } + } + } + } catch (Exception e) { + // 记录异常并处理 + e.printStackTrace(); +// return null; + } + + if (!newUserEmails.isEmpty()){ + // 查询这些邮箱在aida上是否有账号 + QueryWrapper queryWrapper = new QueryWrapper<>(); + queryWrapper.in("user_email", newUserEmails).select("user_email"); + + List collect = baseMapper.selectList(queryWrapper).stream().map(Account::getUserEmail).collect(Collectors.toList()); + if (!collect.isEmpty()){ + // 移除Code-Create新增用户中在AiDA已有账号的邮箱 + newUserEmails.removeAll(collect); + } + // 将新增用户添加到AiDA,身份为游客 + if (!newUserEmails.isEmpty()){ + newUserEmails.forEach(email -> { + Account account = new Account(); + account.setUserEmail(email); + account.setUserName(email); + account.setUserPassword("Third-000000"); + account.setLanguage(Language.ENGLISH.name()); + account.setCreateDate(new Date()); + account.setIsTrial(0); + account.setIsBeginner(1); + account.setCredits(new BigDecimal(0)); + account.setSystemUser(0); + baseMapper.insert(account); + // 邮件通知用户 + SendEmailUtil.notificationForRegisterUser(email); + }); + } + // 记录当前最大的用户id + redisUtil.addToString(maximumUserIdKey, String.valueOf(maxUserId)); + } + } + // 收集调查问卷的信息 @Override @Transactional(rollbackFor = Exception.class) diff --git a/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java b/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java index b664ed21..2bad5969 100644 --- a/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/ConvenientInquiryServiceImpl.java @@ -27,6 +27,7 @@ import com.mysql.cj.util.StringUtils; import io.netty.util.internal.StringUtil; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; import java.math.BigDecimal; @@ -434,11 +435,20 @@ public class ConvenientInquiryServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); + queryWrapper.eq("user_email", account.getUserEmail()); + + Account existsAccount = accountMapper.selectOne(queryWrapper); + if (!Objects.isNull(existsAccount)){ + throw new BusinessException("The email address already exists. One email address can only register one AiDA account"); + } // 添加正式用户 + assert accountAddDTO != null; if (Objects.isNull(accountAddDTO.getSystemUser())) { throw new BusinessException("you have to choose user type"); } else { diff --git a/src/main/java/com/ai/da/service/impl/DesignItemServiceImpl.java b/src/main/java/com/ai/da/service/impl/DesignItemServiceImpl.java index 1a12f6e4..52d589d9 100644 --- a/src/main/java/com/ai/da/service/impl/DesignItemServiceImpl.java +++ b/src/main/java/com/ai/da/service/impl/DesignItemServiceImpl.java @@ -401,6 +401,7 @@ public class DesignItemServiceImpl extends ServiceImpl getHistoryNotification(GetNotificationDTO getNotificationDTO) { + log.info("获取历史消息:parameter => {}", getNotificationDTO); + if (StringUtil.isNullOrEmpty(getNotificationDTO.getType())){ + throw new BusinessException("type cannot be empty"); + } Long accountId = UserContext.getUserHolder().getId(); // 查动态 if (!StringUtils.isNullOrEmpty(getNotificationDTO.getType()) && getNotificationDTO.getType().equals("newPosted")){ @@ -291,7 +296,11 @@ public class MessageCenterServiceImpl extends ServiceImpl queryWrapper = new QueryWrapper<>(); - queryWrapper.in("account_id", followeeList); + if (!followeeList.isEmpty()){ + queryWrapper.in("account_id", followeeList); + }else { + return 0L; + } if (!StringUtil.isNullOrEmpty(lastViewTime)){ queryWrapper.gt("create_date", lastViewTime); @@ -311,7 +320,12 @@ public class MessageCenterServiceImpl extends ServiceImpl followeeList = portfolioService.getFolloweeList(accountId); // 1.2 分页查询我关注的用户发布的作品 QueryWrapper queryWrapper = new QueryWrapper<>(); - queryWrapper.in("account_id", followeeList).orderByDesc("create_date"); + if (!followeeList.isEmpty()){ + queryWrapper.in("account_id", followeeList); + }else { + return new PageBaseResponse<>(new ArrayList<>(), page, size, 0, 0); + } + queryWrapper.orderByDesc("create_date"); Page portfolioPage = portfolioService.getBaseMapper().selectPage(new Page<>(page, size), queryWrapper); // 2、组装返回的数据