TASK: 试用用户添加测试;
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package com.ai.da.common.utils;
|
||||
|
||||
import com.ai.da.mapper.entity.TrialOrder;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ai.da.common.config.exception.BusinessException;
|
||||
import com.tencentcloudapi.common.Credential;
|
||||
@@ -11,6 +12,7 @@ import com.tencentcloudapi.ses.v20201002.models.SendEmailRequest;
|
||||
import com.tencentcloudapi.ses.v20201002.models.SendEmailResponse;
|
||||
import com.tencentcloudapi.ses.v20201002.models.Template;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@@ -114,4 +116,102 @@ public class SendEmailUtil {
|
||||
template.setTemplateData(jsonObject.toJSONString());
|
||||
return template;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送不同类型的邮件
|
||||
*
|
||||
* @param receiverAddress 收件人邮箱地址
|
||||
* @param senderAddress 发件人邮箱地址
|
||||
* @param emailType 邮件类型:1 - 提交试用请求,2 - 审批通过,3 - 试用请求通过通知
|
||||
* @return 发送结果
|
||||
*/
|
||||
private final static Long YOUR_TRIAL_TEMPLATE_ID = 117214L;
|
||||
private final static Long APPROVAL_TEMPLATE_ID = 117215L;
|
||||
private final static Long NOTIFICATION_TEMPLATE_ID = 117216L;
|
||||
public static Boolean sendCustomEmail(String receiverAddress, String senderAddress, TrialOrder trialOrder, int emailType) {
|
||||
try {
|
||||
// 实例化一个认证对象
|
||||
Credential cred = new Credential(SECRET_ID, SECRET_KEy);
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint("ses.tencentcloudapi.com");
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
SesClient client = new SesClient(cred, "ap-hongkong", clientProfile);
|
||||
SendEmailRequest req = new SendEmailRequest();
|
||||
if (StringUtils.isEmpty(senderAddress)) {
|
||||
senderAddress = SEND_ADDRESS;
|
||||
}
|
||||
req.setFromEmailAddress(senderAddress);
|
||||
req.setDestination(new String[]{receiverAddress});
|
||||
|
||||
// 根据邮件类型设置不同的主题和模板
|
||||
String subject = "";
|
||||
Template template = new Template();
|
||||
switch (emailType) {
|
||||
case 1:
|
||||
subject = "试用订单请求";
|
||||
template.setTemplateID(YOUR_TRIAL_TEMPLATE_ID);
|
||||
template.setTemplateData(buildTrialOrderData(trialOrder));
|
||||
break;
|
||||
case 2:
|
||||
subject = "试用订单审批通过";
|
||||
template.setTemplateID(APPROVAL_TEMPLATE_ID);
|
||||
template.setTemplateData(buildApprovalData(trialOrder));
|
||||
break;
|
||||
case 3:
|
||||
subject = "试用订单通过通知";
|
||||
template.setTemplateID(NOTIFICATION_TEMPLATE_ID);
|
||||
template.setTemplateData(buildNotificationData(trialOrder));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
req.setSubject(subject);
|
||||
req.setTemplate(template);
|
||||
|
||||
// 发送邮件
|
||||
SendEmailResponse resp = client.SendEmail(req);
|
||||
log.info("短信发送结果res###{}", SendEmailResponse.toJsonString(resp));
|
||||
return Boolean.TRUE;
|
||||
} catch (TencentCloudSDKException e) {
|
||||
log.info("邮件发送失败###{}", e.toString());
|
||||
throw new BusinessException("failed.to.send.mail");
|
||||
}
|
||||
}
|
||||
|
||||
// 构建试用订单数据
|
||||
private static String buildTrialOrderData(TrialOrder trialOrder) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 设置试用订单相关数据
|
||||
jsonObject.put("surname", trialOrder.getSurname());
|
||||
jsonObject.put("givenName", trialOrder.getGivenName());
|
||||
jsonObject.put("userName", trialOrder.getUserName());
|
||||
jsonObject.put("email", trialOrder.getEmail());
|
||||
jsonObject.put("country", trialOrder.getCountry());
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
|
||||
// 构建审批通过数据
|
||||
private static String buildApprovalData(TrialOrder trialOrder) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 设置审批通过相关数据
|
||||
jsonObject.put("surname", trialOrder.getSurname());
|
||||
jsonObject.put("givenName", trialOrder.getGivenName());
|
||||
jsonObject.put("userName", trialOrder.getUserName());
|
||||
jsonObject.put("email", trialOrder.getEmail());
|
||||
jsonObject.put("country", trialOrder.getCountry());
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
|
||||
// 构建试用订单通过通知数据
|
||||
private static String buildNotificationData(TrialOrder trialOrder) {
|
||||
JSONObject jsonObject = new JSONObject();
|
||||
// 设置试用订单通过通知相关数据
|
||||
jsonObject.put("surname", trialOrder.getSurname());
|
||||
jsonObject.put("givenName", trialOrder.getGivenName());
|
||||
jsonObject.put("userName", trialOrder.getUserName());
|
||||
jsonObject.put("email", trialOrder.getEmail());
|
||||
return jsonObject.toJSONString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,9 @@ public class ThirdPartyController {
|
||||
|
||||
@CrossOrigin
|
||||
@ApiOperation(value = "Add user information")
|
||||
@PutMapping("/addTrialUser")
|
||||
public Response<Boolean> addTrialUser(@RequestParam("userName") String userName, @RequestParam("email") String email) {
|
||||
return Response.success(accountService.addTrialUser(userName, email));
|
||||
@PostMapping("/addTrialUser")
|
||||
public Response<Boolean> addTrialUser(@RequestBody AccountTrialDTO accountTrialDTO) {
|
||||
return Response.success(accountService.addTrialUser(accountTrialDTO));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
19
src/main/java/com/ai/da/mapper/TrialOrderMapper.java
Normal file
19
src/main/java/com/ai/da/mapper/TrialOrderMapper.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package com.ai.da.mapper;
|
||||
|
||||
import com.ai.da.common.config.mybatis.plus.CommonMapper;
|
||||
import com.ai.da.mapper.entity.Account;
|
||||
import com.ai.da.mapper.entity.TrialOrder;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Mapper 接口
|
||||
*
|
||||
* @author easy-generator
|
||||
* @since 2022-06-13
|
||||
*/
|
||||
public interface TrialOrderMapper extends CommonMapper<TrialOrder> {
|
||||
|
||||
|
||||
|
||||
}
|
||||
64
src/main/java/com/ai/da/mapper/entity/TrialOrder.java
Normal file
64
src/main/java/com/ai/da/mapper/entity/TrialOrder.java
Normal file
@@ -0,0 +1,64 @@
|
||||
package com.ai.da.mapper.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Date;
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
@TableName("trial_order")
|
||||
public class TrialOrder implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* ID
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
@ApiModelProperty("surname")
|
||||
private String surname;
|
||||
|
||||
@ApiModelProperty("givenName")
|
||||
private String givenName;
|
||||
|
||||
@ApiModelProperty("userName")
|
||||
private String userName;
|
||||
|
||||
@ApiModelProperty("email")
|
||||
private String email;
|
||||
|
||||
@ApiModelProperty("country")
|
||||
private String country;
|
||||
|
||||
@ApiModelProperty("occupation")
|
||||
private String occupation;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private LocalDateTime updateTime;
|
||||
|
||||
|
||||
/**
|
||||
* 0待审评
|
||||
* 1通过
|
||||
* 2拒绝
|
||||
*/
|
||||
private Integer status;
|
||||
private Integer isDeleted;
|
||||
}
|
||||
14
src/main/java/com/ai/da/model/dto/AccountTrialDTO.java
Normal file
14
src/main/java/com/ai/da/model/dto/AccountTrialDTO.java
Normal file
@@ -0,0 +1,14 @@
|
||||
package com.ai.da.model.dto;
|
||||
|
||||
import com.ai.da.mapper.entity.TrialOrder;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
@EqualsAndHashCode(callSuper = true)
|
||||
@Data
|
||||
@ApiModel("AccountTrial")
|
||||
public class AccountTrialDTO extends TrialOrder {
|
||||
|
||||
}
|
||||
16
src/main/java/com/ai/da/model/enums/AutoApproved.java
Normal file
16
src/main/java/com/ai/da/model/enums/AutoApproved.java
Normal file
@@ -0,0 +1,16 @@
|
||||
package com.ai.da.model.enums;
|
||||
|
||||
public class AutoApproved {
|
||||
// 静态变量,整个应用程序共享
|
||||
private static boolean status = false;
|
||||
|
||||
// 获取当前状态
|
||||
public static boolean getStatus() {
|
||||
return status;
|
||||
}
|
||||
|
||||
// 设置新状态
|
||||
public static void setStatus(boolean newStatus) {
|
||||
status = newStatus;
|
||||
}
|
||||
}
|
||||
@@ -99,5 +99,5 @@ public interface AccountService extends IService<Account> {
|
||||
|
||||
Boolean completeGuidance();
|
||||
|
||||
Boolean addTrialUser(String userName, String email);
|
||||
Boolean addTrialUser(AccountTrialDTO accountTrialDTO);
|
||||
}
|
||||
|
||||
@@ -10,10 +10,13 @@ import com.ai.da.common.security.jwt.JWTTokenHelper;
|
||||
import com.ai.da.common.utils.*;
|
||||
import com.ai.da.mapper.AccountMapper;
|
||||
import com.ai.da.mapper.LibraryMapper;
|
||||
import com.ai.da.mapper.TrialOrderMapper;
|
||||
import com.ai.da.mapper.UserLikeGroupMapper;
|
||||
import com.ai.da.mapper.entity.Account;
|
||||
import com.ai.da.mapper.entity.AccountLoginLog;
|
||||
import com.ai.da.mapper.entity.TrialOrder;
|
||||
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;
|
||||
@@ -33,6 +36,9 @@ import org.springframework.util.Assert;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
@@ -62,6 +68,9 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
@Resource
|
||||
private UserLikeGroupService userLikeGroupService;
|
||||
|
||||
@Resource
|
||||
private TrialOrderMapper trialOrderMapper;
|
||||
|
||||
|
||||
@Override
|
||||
@Transactional
|
||||
@@ -453,9 +462,43 @@ public class AccountServiceImpl extends ServiceImpl<AccountMapper, Account> impl
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean addTrialUser(String userName, String email) {
|
||||
System.out.println("试用用户测试成功");
|
||||
log.info("试用用户测试成功");
|
||||
public Boolean addTrialUser(AccountTrialDTO accountTrialDTO) {
|
||||
// 先检测用户名和邮箱
|
||||
QueryWrapper<Account> qw = new QueryWrapper<>();
|
||||
qw.lambda().eq(Account::getUserEmail, accountTrialDTO.getEmail())
|
||||
.or()
|
||||
.eq(Account::getUserName, accountTrialDTO.getUserName());
|
||||
List<Account> accountList = accountMapper.selectList(qw);
|
||||
if (CollectionUtil.isNotEmpty(accountList)) {
|
||||
throw new BusinessException("The username or email has already been registered", ResultEnum.PROMPT.getCode());
|
||||
}
|
||||
// 接收到数据后要形成一条使用订单信息
|
||||
TrialOrder trialOrder = CopyUtil.copyObject(accountTrialDTO, TrialOrder.class);
|
||||
trialOrder.setCreateTime(LocalDateTime.now());
|
||||
trialOrder.setStatus(0);
|
||||
trialOrderMapper.insert(trialOrder);
|
||||
// 判断当前的试用订单是否自动批准
|
||||
if (AutoApproved.getStatus()) {
|
||||
// 改变试用订单状态,新增试用用户
|
||||
trialOrder.setStatus(1);
|
||||
trialOrder.setUpdateTime(LocalDateTime.now());
|
||||
trialOrderMapper.updateById(trialOrder);
|
||||
Account account = new Account();
|
||||
account.setUserName(trialOrder.getUserName());
|
||||
account.setUserPassword("Third-000000");
|
||||
account.setUserEmail(trialOrder.getEmail());
|
||||
account.setLanguage(Language.ENGLISH.getValue());
|
||||
account.setValidStartTime(System.currentTimeMillis());
|
||||
account.setValidEndTime(Instant.now().plus(3, ChronoUnit.DAYS).toEpochMilli());
|
||||
account.setCreateDate(new Date());
|
||||
account.setIsTrial(1);
|
||||
account.setIsBeginner(1);
|
||||
accountMapper.insert(account);
|
||||
// 发送邮件提醒用户试用用户已创建
|
||||
// if (SendEmailUtil.sendCustomEmail("1023316923@qq.com", null, trialOrder,1)) {
|
||||
// return Boolean.TRUE;
|
||||
// }
|
||||
}
|
||||
return Boolean.TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user