添加查询Code-Create数据库,监控新增付费记录并对付费用户进行账号新增或延长账号有效期的操作

This commit is contained in:
2024-06-21 15:11:31 +08:00
parent 8457a61ded
commit 30d5466805
5 changed files with 104 additions and 15 deletions

View File

@@ -0,0 +1,26 @@
package com.ai.da.common.task;
import com.ai.da.service.AccountService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@Component
public class AccountTask {
@Resource
private AccountService accountService;
/** 每个月1号凌晨刷新 年付用户的积分 */
// @Scheduled(cron = "0 0 0 1 * ?")
public void refreshCreditsMonthly(){
accountService.refreshCreditsMonthly();
}
// todo 多久执行一次?
public void getPaidUser(){
// 获取code-create 表中 指定日期之后 订单状态为wc-processing的订单
accountService.extendValidityForCC();
}
}

View File

@@ -87,5 +87,12 @@ public class Account implements Serializable {
*/
private BigDecimal credits;
private Integer SystemUser;
/**
* 用于区分游客与系统用户
* 0 游客
* 1 年付用户
* 2 月付用户
* 3 试用用户
*/
private Integer systemUser;
}

View File

@@ -2,23 +2,27 @@ package com.ai.da.model.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
@Data
@ApiModel("AccountAdd")
@AllArgsConstructor
public class AccountAddDTO {
@NotBlank(message = "email.cannot.be.empty")
@ApiModelProperty("email")
private String email;
@ApiModelProperty("userEmail")
private String userEmail;
@NotBlank(message = "userName.cannot.be.empty")
@ApiModelProperty("userName")
private String userName;
private String country;
@NotBlank(message = "validStartTime.cannot.be.empty")
@ApiModelProperty("Start time of account validity ")
private String validStartTime;
@@ -29,4 +33,8 @@ public class AccountAddDTO {
private Integer isTrial;
private BigDecimal credits;
private Integer systemUser;
}

View File

@@ -135,5 +135,9 @@ public interface AccountService extends IService<Account> {
AccountLoginVO designWorksRegisterCode(AccountDesignWorksRegisterDTO accountDesignWorksRegisterDTO);
Boolean extendValidityForCC();
Boolean collectQuestionnaires(String questionnaireInfo);
void refreshCreditsMonthly();
}

View File

@@ -4,6 +4,7 @@ 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.AuthenticationOperationTypeEnum;
import com.ai.da.common.enums.CreditsEventsEnum;
import com.ai.da.common.enums.LoginTypeEnum;
import com.ai.da.common.response.ResultEnum;
import com.ai.da.common.security.jwt.JWTTokenHelper;
@@ -22,6 +23,7 @@ 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.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
@@ -383,15 +385,18 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
@Override
public Boolean addUser(AccountAddDTO accountAddDTO) {
Account account = new Account();
account.setUserEmail(accountAddDTO.getEmail());
account.setUserPassword("Third-000000");
account.setUserName(accountAddDTO.getUserName());
Account account;
account = CopyUtil.copyObject(accountAddDTO,Account.class);
// account.setUserEmail(accountAddDTO.getUserEmail());
// account.setUserName(accountAddDTO.getUserName());
// account.setIsTrial(accountAddDTO.getIsTrial());
account.setValidStartTime(Long.valueOf(accountAddDTO.getValidStartTime()));
account.setValidEndTime(Long.valueOf(accountAddDTO.getValidEndTime()));
account.setUserPassword("Third-000000");
account.setLanguage(Language.ENGLISH.name());
account.setCreateDate(new Date());
account.setIsTrial(accountAddDTO.getIsTrial());
account.setSystemUser(1);
account.setIsTrial(0);
account.setIsBeginner(1);
return accountMapper.insert(account) > 0;
}
@@ -1010,10 +1015,10 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
}
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'" +
"WHERE status = 'wc-processing' AND date_paid > '2024-06-20 00:00:00'" +
" ORDER BY order_id DESC ";
private static final String QUERY_CUSTOMER_EMAIL = "SELECT username, email FROM pmr_wc_customer_lookup " +
private static final String QUERY_CUSTOMER_EMAIL = "SELECT username, email, country 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 = ? " +
@@ -1037,7 +1042,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
*
* @return null
*/
public String extendValidityForCC() {
@Override
@Transactional(rollbackFor = Exception.class)
public Boolean extendValidityForCC() {
try (Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement(QUERY_ORDER)) {
// preparedStatement.setString(1, "someCondition");
@@ -1049,6 +1056,8 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
double totalSales = queryOrderResultSet.getDouble("total_sales");
String email = "";
String userName = "";
// 为什么一般没有值
String country = "";
// 1、查pmr_wc_customer_lookup表确认当前订单对应的用户邮箱
PreparedStatement preparedQueryEmail = connection.prepareStatement(QUERY_CUSTOMER_EMAIL);
preparedQueryEmail.setInt(1, customerId);
@@ -1056,6 +1065,7 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
if (queryEmailResultSet.next()) {
email = queryEmailResultSet.getString("email");
userName = queryEmailResultSet.getString("username");
country = queryEmailResultSet.getString("country");
} else {
log.error("未知错误。code-create的用户表中没有付费用户的信息");
throw new BusinessException("user info missing");
@@ -1083,7 +1093,27 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
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));
String credits = "0";
int systemUserType = 0;
if (totalSales == 5000.0){
log.info("年付用户初始积分6000");
credits = CreditsEventsEnum.INIT_MONTHLY.getValue();
systemUserType = 1;
}else if (totalSales == 500.0){
log.info("月付用户初始积分5000");
credits = CreditsEventsEnum.INIT_MONTHLY.getValue();
systemUserType = 2;
}else if (totalSales == 0.0){
log.info("测试用户初始积分10");
credits = "10";
systemUserType = 3;
}
Boolean b = addUser(new AccountAddDTO(email,
StringUtil.isNullOrEmpty(userName) ? email.substring(0, email.indexOf("@")) : userName,
country,
account.getValidStartTime().toString(),
account.getValidEndTime().toString(), 0,new BigDecimal(credits),systemUserType));
if (b) log.info("付费新用户新增成功!");
} else {
userInfo.setValidEndTime(account.getValidEndTime());
@@ -1112,9 +1142,10 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
} catch (Exception e) {
// 记录异常并处理
e.printStackTrace();
return Boolean.FALSE;
}
return null;
return Boolean.TRUE;
}
private Account extendValidity(Long validEndTime, double totalSales) {
@@ -1199,4 +1230,17 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
return Boolean.TRUE;
}
/**
* 为年费用户每月更新积分
*/
public void refreshCreditsMonthly(){
UpdateWrapper<Account> accountUpdateWrapper = new UpdateWrapper<>();
// 刷新账号有效期截止之前的年付用户的积分
long epochMilli = Instant.now().toEpochMilli();
accountUpdateWrapper.lambda().set(Account::getCredits, CreditsEventsEnum.INIT_YEARLY.getValue())
.eq(Account::getSystemUser,1)
.gt(Account::getValidEndTime, epochMilli);
baseMapper.update(null,accountUpdateWrapper);
}
}