@@ -3,31 +3,31 @@ package com.ai.da.service.impl;
import cn.hutool.core.collection.CollectionUtil ;
import com.ai.da.common.config.exception.BusinessException ;
import com.ai.da.common.context.UserContext ;
import com.ai.da.common.enums.LoginTypeEnum ;
import com.ai.da.common.enums.AuthenticationOperationTypeEnum ;
import com.ai.da.common.enums.LoginTypeEnum ;
import com.ai.da.common.response.ResultEnum ;
import com.ai.da.common.security.jwt.JWTTokenHelper ;
import com.ai.da.common.utils.* ;
import com.ai.da.mapper.primary.AccountMapper ;
import com.ai.da.mapper.primary.QuestionnaireMapper ;
import com.ai.da.mapper.primary.TrialOrderMapper ;
import com.ai.da.mapper.primary.entity.Account ;
import com.ai.da.mapper.primary.entity.AccountLoginLog ;
import com.ai.da.mapper.primary.entity.TrialOrder ;
import com.ai.da.mapper.primary.entity.* ;
import com.ai.da.model.dto.* ;
import com.ai.da.model.enums.AutoApproved ;
import com.ai.da.model.enums.Language ;
import com.ai.da.model.vo.AccountLoginVO ;
import com.ai.da.model.vo.AccountPreLoginVO ;
import com.ai.da.model.vo.AuthPrincipalVo ;
import com.ai.da.service.AccountLoginLogService ;
import com.ai.da.service.AccountService ;
import com.ai.da.service.LibraryService ;
import com.ai.da.service.UserLikeGroupService ;
import com.ai.da.model.vo.QuestionnaireVO ;
import com.ai.da.service.* ;
import com.alibaba.fastjson.JSON ;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper ;
import com.baomidou.mybatisplus.core.metadata.IPage ;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page ;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl ;
import com.zaxxer.hikari.HikariConfig ;
import com.zaxxer.hikari.HikariDataSource ;
import io.netty.util.internal.StringUtil ;
import lombok.extern.slf4j.Slf4j ;
import org.apache.commons.lang3.StringUtils ;
import org.springframework.stereotype.Service ;
@@ -36,9 +36,16 @@ import org.springframework.util.Assert;
import javax.annotation.Resource ;
import javax.servlet.http.HttpServletRequest ;
import javax.sql.DataSource ;
import java.math.BigDecimal ;
import java.sql.Connection ;
import java.sql.PreparedStatement ;
import java.sql.ResultSet ;
import java.time.Instant ;
import java.time.LocalDateTime ;
import java.time.ZoneId ;
import java.time.ZonedDateTime ;
import java.time.format.DateTimeFormatter ;
import java.time.temporal.ChronoUnit ;
import java.util.Date ;
import java.util.List ;
@@ -72,6 +79,11 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
@Resource
private TrialOrderMapper trialOrderMapper ;
@Resource
private QuestionnaireMapper questionnaireMapper ;
@Resource
private CreditsService creditsService ;
@Override
@Transactional ( rollbackFor = Exception . class )
@@ -264,7 +276,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
if ( ! verifyCode . equals ( accountDTO . getEmailVerifyCode ( ) ) ) {
throw new BusinessException ( " verification.code.error " , ResultEnum . PROMPT . getCode ( ) ) ;
}
} else {
} else {
updatePwdByEmail ( accountDTO . getPassword ( ) , accountDTO . getEmail ( ) ) ;
}
return Boolean . TRUE ;
@@ -493,7 +505,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
if ( CollectionUtil . isNotEmpty ( accountList ) ) {
if ( accountList . get ( 0 ) . getIsTrial ( ) = = 1 ) {
throw new BusinessException ( " The email has already been registered " , ResultEnum . PROMPT . getCode ( ) ) ;
} else {
} else {
Account account = accountList . get ( 0 ) ;
if ( null = = account . getValidEndTime ( ) | | account . getValidEndTime ( ) > System . currentTimeMillis ( ) ) {
throw new BusinessException ( " The email has already been registered " , ResultEnum . PROMPT . getCode ( ) ) ;
@@ -523,11 +535,11 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
account . setValidStartTime ( System . currentTimeMillis ( ) ) ;
if ( link ) {
account . setValidEndTime ( Instant . now ( ) . plus ( 14 , ChronoUnit . DAYS ) . toEpochMilli ( ) ) ;
} else {
} else {
account . setValidEndTime ( Instant . now ( ) . plus ( 5 , ChronoUnit . DAYS ) . toEpochMilli ( ) ) ;
}
accountMapper . updateById ( account ) ;
} else {
} else {
account . setUserName ( trialOrder . getUserName ( ) ) ;
account . setUserPassword ( " Third-000000 " ) ;
account . setUserEmail ( trialOrder . getEmail ( ) ) ;
@@ -535,7 +547,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
account . setValidStartTime ( System . currentTimeMillis ( ) ) ;
if ( link ) {
account . setValidEndTime ( Instant . now ( ) . plus ( 14 , ChronoUnit . DAYS ) . toEpochMilli ( ) ) ;
} else {
} else {
account . setValidEndTime ( Instant . now ( ) . plus ( 5 , ChronoUnit . DAYS ) . toEpochMilli ( ) ) ;
}
account . setCreateDate ( new Date ( ) ) ;
@@ -550,7 +562,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
// SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2);
if ( trialOrder . getCountry ( ) . equals ( " China " ) ) {
SendEmailUtil . sendCustomEmail ( account . getUserEmail ( ) , null , trialOrder , 3 , trialOrder . getCountry ( ) , link ) ;
} else {
} else {
SendEmailUtil . sendCustomEmail ( account . getUserEmail ( ) , null , trialOrder , 3 , trialOrder . getCountry ( ) , link ) ;
}
}
@@ -584,7 +596,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
account . setValidStartTime ( System . currentTimeMillis ( ) ) ;
account . setValidEndTime ( Instant . now ( ) . plus ( 5 , ChronoUnit . DAYS ) . toEpochMilli ( ) ) ;
accountMapper . updateById ( account ) ;
} else {
} else {
account . setUserName ( trialOrder . getUserName ( ) ) ;
account . setUserPassword ( " Third-000000 " ) ;
account . setUserEmail ( trialOrder . getEmail ( ) ) ;
@@ -603,7 +615,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
// SendEmailUtil.sendCustomEmail("kaicpang.pang@connect.polyu.hk", null, trialOrder,2, trialOrder.getCountry());
if ( trialOrder . getCountry ( ) . equals ( " China " ) ) {
SendEmailUtil . sendCustomEmail ( account . getUserEmail ( ) , null , trialOrder , 3 , trialOrder . getCountry ( ) , false ) ;
} else {
} else {
SendEmailUtil . sendCustomEmail ( account . getUserEmail ( ) , null , trialOrder , 3 , trialOrder . getCountry ( ) , false ) ;
}
}
@@ -707,7 +719,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
accountMapper . deleteById ( accountDelete ) ;
userToBeUpdate . setUserName ( userName ) ;
accountMapper . updateById ( userToBeUpdate ) ;
} else {
} else {
accountMapper . deleteById ( accountDelete ) ;
}
}
@@ -845,7 +857,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
" <body>\ n " +
" </body>\ n " +
" <script>\ n " +
" window.location.href = 'https://polyu.aida.com.hk?" + id + " '; \ n " +
" window.location.href = 'https://polyu.aida.com.hk?" + id + " '; \ n " +
" </script>\ n " +
" </html>" ;
}
@@ -889,7 +901,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
throw new BusinessException ( " Illegal serial number. " ) ;
}
queryWrapperDelete . lambda ( ) . eq ( Account : : getUserName , " PolyU-SFT- " + noLoginRequiredDTO . getId ( ) ) ;
} else {
} else {
queryWrapperDelete . lambda ( ) . like ( Account : : getBrowserIdentifiers , ipAddress ) ;
}
List < Account > accountList = accountMapper . selectList ( queryWrapperDelete ) ;
@@ -923,7 +935,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
// SendEmailUtil.sendUpgradeNotification(account, null, 1);
if ( account . getLanguage ( ) . equals ( Language . CHINESE_SIMPLIFIED . name ( ) ) ) {
SendEmailUtil . sendUpgradeNotification ( account , null , 0 ) ;
} else {
} else {
// 英文
SendEmailUtil . sendUpgradeNotification ( account , null , 1 ) ;
}
@@ -937,7 +949,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
// 未迁移过的进行迁移,注意模特数据迁移打点信息以及转换模特格式
}
public void updateCredits ( Long accountId , String value ) {
public void updateCredits ( Long accountId , String value ) {
Account account = new Account ( ) ;
account . setId ( accountId ) ;
account . setCredits ( new BigDecimal ( value ) ) ;
@@ -954,7 +966,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
String randomVerifyCode = RandomsUtil . generateVerifyCode ( 100000L , 999999L ) ;
LocalCacheUtils . setVerifyCodeCache ( " DesignWorksRegister " + " _ " + accountDesignWorksRegisterDTO . getUserEmail ( ) , randomVerifyCode ) ;
LocalCacheUtils . setVerifyCodeCache ( " DesignWorksRegister " + " _ " + accountDesignWorksRegisterDTO . getUserEmail ( ) , randomVerifyCode ) ;
Boolean b = SendEmailUtil . designWorksRegister ( accountDesignWorksRegisterDTO . getUserEmail ( ) , randomVerifyCode ) ;
if ( ! b ) {
@@ -966,7 +978,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
@Override
public AccountLoginVO designWorksRegisterCode ( AccountDesignWorksRegisterDTO accountDesignWorksRegisterDTO ) {
String verifyCode = LocalCacheUtils . getVerifyCodeCache ( " DesignWorksRegister " + " _ " + accountDesignWorksRegisterDTO . getUserEmail ( ) ) ;
String verifyCode = LocalCacheUtils . getVerifyCodeCache ( " DesignWorksRegister " + " _ " + accountDesignWorksRegisterDTO . getUserEmail ( ) ) ;
if ( StringUtils . isBlank ( verifyCode ) ) {
throw new BusinessException ( " the.verification.code.has.expired " , ResultEnum . PROMPT . getCode ( ) ) ;
}
@@ -996,4 +1008,195 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
response . setSystemUser ( account . getSystemUser ( ) ) ;
return response ;
}
private static final String QUERY_ORDER = " SELECT * FROM pmr_wc_order_stats " +
" WHERE status = 'wc-processing' AND date_paid > '2024-06-10 00:00:00' " +
" ORDER BY order_id DESC " ;
private static final String QUERY_CUSTOMER_EMAIL = " SELECT username, email FROM pmr_wc_customer_lookup " +
" WHERE customer_id = ? " ;
private static final String UPDATE_ORDER_STATUS = " UPDATE pmr_wc_order_stats " +
" SET status = 'wc-complete' , date_completed = ? " +
" WHERE order_id = ? " ;
private static final DataSource dataSource ;
static {
HikariConfig config = new HikariConfig ( ) ;
config . setJdbcUrl ( " jdbc:mysql://code-create.com.hk:3306/db1nfvsgmjp3b8 " ) ;
config . setUsername ( " uafqtz4gsvfrw " ) ;
config . setPassword ( " aida123456. " ) ;
config . addDataSourceProperty ( " cachePrepStmts " , " true " ) ;
config . addDataSourceProperty ( " prepStmtCacheSize " , " 250 " ) ;
config . addDataSourceProperty ( " prepStmtCacheSqlLimit " , " 2048 " ) ;
dataSource = new HikariDataSource ( config ) ;
}
/**
* 为Code-Create的用户延长有效期
*
* @return null
*/
public String extendValidityForCC ( ) {
try ( Connection connection = dataSource . getConnection ( ) ;
PreparedStatement preparedStatement = connection . prepareStatement ( QUERY_ORDER ) ) {
// preparedStatement.setString(1, "someCondition");
try ( ResultSet queryOrderResultSet = preparedStatement . executeQuery ( ) ) {
while ( queryOrderResultSet . next ( ) ) {
/* 处理结果集 */
int orderId = queryOrderResultSet . getInt ( " order_id " ) ;
int customerId = queryOrderResultSet . getInt ( " customer_id " ) ;
double totalSales = queryOrderResultSet . getDouble ( " total_sales " ) ;
String email = " " ;
String userName = " " ;
// 1、查pmr_wc_customer_lookup表; 确认当前订单对应的用户邮箱
PreparedStatement preparedQueryEmail = connection . prepareStatement ( QUERY_CUSTOMER_EMAIL ) ;
preparedQueryEmail . setInt ( 1 , customerId ) ;
try ( ResultSet queryEmailResultSet = preparedQueryEmail . executeQuery ( ) ) {
if ( queryEmailResultSet . next ( ) ) {
email = queryEmailResultSet . getString ( " email " ) ;
userName = queryEmailResultSet . getString ( " username " ) ;
} else {
log . error ( " 未知错误。code-create的用户表中没有付费用户的信息 " ) ;
throw new BusinessException ( " user info missing " ) ;
}
}
// 2、查t_account表中是否有该用户
// 2.1 没有 新建用户
Account userInfo = null ;
Long validEndTime = null ;
// 标志当前用户是不是新用户
Boolean flag = Boolean . FALSE ;
try {
// 不是新用户 直接延长使用期限
userInfo = getOneByEmail ( email ) ;
} catch ( BusinessException e ) {
// 通过邮箱找不到用户 说明是新用户 => 创建用户
flag = Boolean . TRUE ;
}
if ( ! Objects . isNull ( userInfo ) & & ! Objects . isNull ( userInfo . getValidEndTime ( ) ) )
validEndTime = userInfo . getValidEndTime ( ) ;
// 2、获取当前续费费用能延长多长时间
Account account = extendValidity ( validEndTime , totalSales ) ;
if ( flag ) {
// 是新用户 => 新增一条数据
Boolean b = addUser ( new AccountAddDTO ( email , StringUtil . isNullOrEmpty ( userName ) ? email . substring ( 0 , email . indexOf ( " @ " ) - 1 ) : userName , account . getValidStartTime ( ) . toString ( ) , account . getValidEndTime ( ) . toString ( ) , 0 ) ) ;
if ( b ) log . info ( " 付费新用户新增成功! " ) ;
} else {
userInfo . setValidEndTime ( account . getValidEndTime ( ) ) ;
baseMapper . updateById ( account ) ;
log . info ( " 付费用户续订成功 " ) ;
}
// 3、todo 邮件通知
// 3.1 判断用户语言
// 3.2 将用户信息填到邮件中
// 4、更新订单状态和日期
PreparedStatement preparedUpdateOrder = connection . prepareStatement ( UPDATE_ORDER_STATUS ) ;
LocalDateTime localDateTime = LocalDateTime . now ( ) ;
String currentTime = localDateTime . format ( DateTimeFormatter . ofPattern ( " yyyy-MM-dd HH:mm:ss " ) ) ;
preparedUpdateOrder . setString ( 1 , currentTime ) ;
preparedUpdateOrder . setInt ( 2 , orderId ) ;
int row = preparedUpdateOrder . executeUpdate ( ) ;
if ( row = = 1 ) log . info ( " 表 pmr_wc_order_stats 订单状态更新成功 " ) ;
}
}
} catch ( Exception e ) {
// 记录异常并处理
e . printStackTrace ( ) ;
}
return null ;
}
private Account extendValidity ( Long validEndTime , double totalSales ) {
Instant specifiedInstant ;
Account account = new Account ( ) ;
long epochMilli = Instant . now ( ) . toEpochMilli ( ) ;
if ( ! Objects . isNull ( validEndTime ) & & validEndTime > epochMilli ) {
// 将 Unix 毫秒级时间戳转换为 Instant
specifiedInstant = Instant . ofEpochMilli ( validEndTime ) ;
} else {
specifiedInstant = Instant . now ( ) ;
account . setValidStartTime ( specifiedInstant . toEpochMilli ( ) ) ;
}
// 指定时区
ZoneId zoneId = ZoneId . of ( " Asia/Shanghai " ) ;
// 将 Instant 转换为 ZonedDateTime, 使用指定时区
ZonedDateTime specifiedDateTime = ZonedDateTime . ofInstant ( specifiedInstant , zoneId ) ;
ZonedDateTime validityExtension ;
if ( totalSales = = 500 ) {
// 一个月
validityExtension = specifiedDateTime . plusMonths ( 1 ) ;
} else if ( totalSales = = 5000 ) {
// 一年
validityExtension = specifiedDateTime . plusYears ( 1 ) ;
} else {
// 测试 一天
validityExtension = specifiedDateTime . plusDays ( 1 ) ;
}
// 获取一个月之后的时间的 Unix 毫秒级时间戳
account . setValidEndTime ( validityExtension . toInstant ( ) . toEpochMilli ( ) ) ;
return account ;
}
// 收集调查问卷的信息
@Override
@Transactional ( rollbackFor = Exception . class )
public Boolean collectQuestionnaires ( String questionnaireInfo ) {
log . info ( " 调查问卷详细信息:{} " , questionnaireInfo ) ;
QuestionnaireVO questionnaireVO = JSON . parseObject ( questionnaireInfo , QuestionnaireVO . class ) ;
String email = questionnaireVO . getEmail ( ) ;
// 1、通过邮箱判断当前用户有无系统账号
Account account ;
try {
account = getOneByEmail ( email ) ;
} catch ( BusinessException e ) {
log . info ( e . getMessage ( ) ) ;
log . warn ( " 当前用户 {} 在AiDA中没有账号 " , email ) ;
throw new BusinessException ( " user.has.no.account " , ResultEnum . PROMPT . getCode ( ) ) ;
}
// 2、先判断当前用户是否已经填写过问卷
CreditsDetail record = creditsService . getByAccountIdAndChangeEvent ( account . getId ( ) , " Fill out the questionnaire " , " +100 " ) ;
if ( ! Objects . isNull ( record ) ) {
log . info ( " 当前用户 {} 已经填写过问卷 " , email ) ;
throw new BusinessException ( " questionnaire.filled.out " , ResultEnum . PROMPT . getCode ( ) ) ;
}
// 3.1、将问卷信息存储到数据库
Questionnaire questionnaire = new Questionnaire ( ) ;
questionnaire . setQuestionnaireInfo ( questionnaireInfo ) ;
questionnaire . setTitle ( " AiDA_3.0 Feedback Survey--06/2024 " ) ;
questionnaire . setCreateTime ( LocalDateTime . now ( ) ) ;
int insert = questionnaireMapper . insert ( questionnaire ) ;
if ( insert = = 1 ) {
// 3.2、更新 t_credits_detail表
CreditsDetail creditsDetail = new CreditsDetail ( ) ;
creditsDetail . setAccountId ( account . getId ( ) ) ;
creditsDetail . setChangeEvent ( " Fill out the questionnaire " ) ;
creditsDetail . setChangedCredits ( " +100 " ) ;
BigDecimal added = account . getCredits ( ) . add ( new BigDecimal ( " 100 " ) ) ;
creditsDetail . setCredits ( added ) ;
creditsDetail . setCreateTime ( LocalDateTime . now ( ) ) ;
creditsService . save ( creditsDetail ) ;
// 3.3、更新 t_account 表
updateCredits ( account . getId ( ) , added . toString ( ) ) ;
}
// 4、发邮件 区分中英文
SendEmailUtil . questionnaireRelatedNotify ( questionnaireVO . getUserName ( ) , email , questionnaireVO . getLanguage ( ) ) ;
return Boolean . TRUE ;
}
}