|
|
|
@ -10,14 +10,17 @@ import com.luoo.user.pojo.UserInfo;
|
|
|
|
|
import com.luoo.user.pojo.UserPointLog;
|
|
|
|
|
import dto.UserLoginDto;
|
|
|
|
|
import enums.MembershipEnums;
|
|
|
|
|
import enums.PointEnums;
|
|
|
|
|
import enums.UserVipStatusEnum;
|
|
|
|
|
import exception.BizException;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
|
|
|
import javax.persistence.criteria.Predicate;
|
|
|
|
|
import javax.persistence.criteria.Root;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang.StringUtils;
|
|
|
|
|
import org.springframework.data.domain.Page;
|
|
|
|
@ -47,146 +50,157 @@ import java.util.UUID;
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class MembershipCodeService {
|
|
|
|
|
|
|
|
|
|
private final MembershipCodeDao membershipCodeDao;
|
|
|
|
|
|
|
|
|
|
private final IdWorker idWorker;
|
|
|
|
|
|
|
|
|
|
private final JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
private final UserInfoDao userInfoDao;
|
|
|
|
|
|
|
|
|
|
public MembershipCodeService(JwtUtil jwtUtil, IdWorker idWorker,
|
|
|
|
|
MembershipCodeDao membershipCodeDao, UserInfoDao userInfoDao) {
|
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
|
this.idWorker = idWorker;
|
|
|
|
|
this.membershipCodeDao = membershipCodeDao;
|
|
|
|
|
this.userInfoDao = userInfoDao;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建会员码并保存 会员码取UUID
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param duration 会员时长(天)
|
|
|
|
|
* @return 会员码
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public String generateMembershipCode(String token, Integer duration) {
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
|
|
|
|
|
// UUID作为会员码
|
|
|
|
|
String code = UUID.randomUUID().toString();
|
|
|
|
|
MembershipCode build = MembershipCode.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.code(code)
|
|
|
|
|
.status(MembershipEnums.CREATED.getCode())
|
|
|
|
|
.duration(duration)
|
|
|
|
|
.createUser(userLoginDto.getUserId())
|
|
|
|
|
.build();
|
|
|
|
|
membershipCodeDao.save(build);
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量生成会员码
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param duration 会员时长(天)
|
|
|
|
|
* @param count 生成会员码数量
|
|
|
|
|
* @return 会员码list
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public List<String> generateMembershipCodeBatch(String token, Integer duration, Integer count) {
|
|
|
|
|
List<String> result = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
String code = generateMembershipCode(token, duration);
|
|
|
|
|
result.add(code);
|
|
|
|
|
private final MembershipCodeDao membershipCodeDao;
|
|
|
|
|
|
|
|
|
|
private final IdWorker idWorker;
|
|
|
|
|
|
|
|
|
|
private final JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
private final UserInfoDao userInfoDao;
|
|
|
|
|
|
|
|
|
|
public MembershipCodeService(JwtUtil jwtUtil, IdWorker idWorker,
|
|
|
|
|
MembershipCodeDao membershipCodeDao, UserInfoDao userInfoDao) {
|
|
|
|
|
this.jwtUtil = jwtUtil;
|
|
|
|
|
this.idWorker = idWorker;
|
|
|
|
|
this.membershipCodeDao = membershipCodeDao;
|
|
|
|
|
this.userInfoDao = userInfoDao;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 创建会员码并保存 会员码取UUID
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param duration 会员时长(天)
|
|
|
|
|
* @param life 是否永久会员
|
|
|
|
|
* @return 会员码
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public String generateMembershipCode(String token, Integer duration, Integer life) {
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
|
|
|
|
|
// UUID作为会员码
|
|
|
|
|
String code = UUID.randomUUID().toString();
|
|
|
|
|
MembershipCode build = MembershipCode.builder()
|
|
|
|
|
.id(String.valueOf(idWorker.nextId()))
|
|
|
|
|
.code(code)
|
|
|
|
|
.status(MembershipEnums.CREATED.getCode())
|
|
|
|
|
.duration(duration)
|
|
|
|
|
.life(life)
|
|
|
|
|
.createUser(userLoginDto.getUserId())
|
|
|
|
|
.build();
|
|
|
|
|
membershipCodeDao.save(build);
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 批量生成会员码
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param duration 会员时长(天)
|
|
|
|
|
* @param count 生成会员码数量
|
|
|
|
|
* @param life 是否永久会员
|
|
|
|
|
* @return 会员码list
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public List<String> generateMembershipCodeBatch(String token, Integer duration, Integer count, Integer life) {
|
|
|
|
|
List<String> result = new ArrayList<>();
|
|
|
|
|
for (int i = 0; i < count; i++) {
|
|
|
|
|
String code = generateMembershipCode(token, duration, life);
|
|
|
|
|
result.add(code);
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 绑定会员码
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param code 会员码
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void bindMembershipCode(String token, String code) {
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
MembershipCode membershipCode = membershipCodeDao.findMembershipCodeByCodeAndStatus(code,
|
|
|
|
|
MembershipEnums.CREATED.getCode());
|
|
|
|
|
if (membershipCode == null) {
|
|
|
|
|
throw new BizException("会员码不存在");
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 绑定会员码
|
|
|
|
|
*
|
|
|
|
|
* @param token token
|
|
|
|
|
* @param code 会员码
|
|
|
|
|
*/
|
|
|
|
|
@Transactional(rollbackFor = Exception.class)
|
|
|
|
|
public void bindMembershipCode(String token, String code) {
|
|
|
|
|
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
|
|
|
|
|
MembershipCode membershipCode = membershipCodeDao.findMembershipCodeByCodeAndStatus(code,
|
|
|
|
|
MembershipEnums.CREATED.getCode());
|
|
|
|
|
if (membershipCode == null) {
|
|
|
|
|
throw new BizException("会员码不存在");
|
|
|
|
|
}
|
|
|
|
|
// 处理会员码表
|
|
|
|
|
membershipCode.setStatus(MembershipEnums.BIND.getCode());
|
|
|
|
|
membershipCode.setUserId(userLoginDto.getUserId());
|
|
|
|
|
membershipCode.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
membershipCodeDao.save(membershipCode);
|
|
|
|
|
|
|
|
|
|
// 处理用户表
|
|
|
|
|
UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get();
|
|
|
|
|
|
|
|
|
|
// 永久会员码
|
|
|
|
|
if(Objects.equals(membershipCode.getLife(), PointEnums.MEMBERSHIP_LIFE.getCode())) {
|
|
|
|
|
userInfo.setVipStatus(UserVipStatusEnum.LIFE.getCode());
|
|
|
|
|
} else {
|
|
|
|
|
// 非永久会员码
|
|
|
|
|
// 计算会员到期时间
|
|
|
|
|
LocalDate vipExpireTime;
|
|
|
|
|
if (Objects.equals(UserVipStatusEnum.INITIAL.getCode(), userInfo.getVipStatus())) {
|
|
|
|
|
vipExpireTime = userInfo.getVipExpireTime().plusDays(membershipCode.getDuration());
|
|
|
|
|
} else {
|
|
|
|
|
// 从今天开始,增加membershipCode的duration天
|
|
|
|
|
userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode());
|
|
|
|
|
vipExpireTime = LocalDate.now().plusDays(membershipCode.getDuration());
|
|
|
|
|
}
|
|
|
|
|
userInfo.setVipExpireTime(vipExpireTime);
|
|
|
|
|
// 无论如何当即设置为会员生效状态
|
|
|
|
|
userInfo.setVipStatus(UserVipStatusEnum.ACTIVE.getCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
userInfoDao.save(userInfo);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
// 处理会员码表
|
|
|
|
|
membershipCode.setStatus(MembershipEnums.BIND.getCode());
|
|
|
|
|
membershipCode.setUserId(userLoginDto.getUserId());
|
|
|
|
|
membershipCode.setUpdateUser(userLoginDto.getUserId());
|
|
|
|
|
membershipCodeDao.save(membershipCode);
|
|
|
|
|
|
|
|
|
|
// 处理用户表
|
|
|
|
|
UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get();
|
|
|
|
|
// 计算会员到期时间
|
|
|
|
|
LocalDate vipExpireTime;
|
|
|
|
|
if (Objects.equals(UserVipStatusEnum.INITIAL.getCode(), userInfo.getVipStatus())) {
|
|
|
|
|
vipExpireTime = userInfo.getVipExpireTime().plusDays(membershipCode.getDuration());
|
|
|
|
|
} else {
|
|
|
|
|
// 从今天开始,增加membershipCode的duration天
|
|
|
|
|
userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode());
|
|
|
|
|
vipExpireTime = LocalDate.now().plusDays(membershipCode.getDuration());
|
|
|
|
|
|
|
|
|
|
public PageResult<MembershipCode> getMembershipCodeList(String token, Integer page, Integer size,
|
|
|
|
|
MembershipCodeSearchDto membershipCodeSearchDto) {
|
|
|
|
|
|
|
|
|
|
Sort sort = new Sort(Direction.DESC, "createTime");
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
|
|
|
|
|
|
|
|
|
|
Specification<MembershipCode> specification = buildSearchSpecification(membershipCodeSearchDto);
|
|
|
|
|
Page<MembershipCode> membershipCodePage = membershipCodeDao.findAll(specification, pageRequest);
|
|
|
|
|
|
|
|
|
|
long totalElements = membershipCodePage.getTotalElements();
|
|
|
|
|
return new PageResult<>(totalElements, membershipCodePage.getContent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Specification<MembershipCode> buildSearchSpecification(MembershipCodeSearchDto param) {
|
|
|
|
|
return (Root<MembershipCode> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) -> {
|
|
|
|
|
List<Predicate> predicateList = new ArrayList<Predicate>();
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(param.getCode())) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("code"), param.getCode())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(param.getCreateUser())) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(
|
|
|
|
|
criteriaBuilder.equal(root.get("createUser"), param.getCreateUser())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(param.getUserId())) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("userId"), param.getUserId())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (param.getStatus() != null) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), param.getStatus())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (param.getDuration() != null) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("duration"), param.getDuration())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
userInfo.setVipExpireTime(vipExpireTime);
|
|
|
|
|
// 无论如何当即设置为会员生效状态
|
|
|
|
|
userInfo.setVipStatus(UserVipStatusEnum.ACTIVE.getCode());
|
|
|
|
|
userInfoDao.save(userInfo);
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public PageResult<MembershipCode> getMembershipCodeList(String token, Integer page, Integer size,
|
|
|
|
|
MembershipCodeSearchDto membershipCodeSearchDto) {
|
|
|
|
|
|
|
|
|
|
Sort sort = new Sort(Direction.DESC, "createTime");
|
|
|
|
|
PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
|
|
|
|
|
|
|
|
|
|
Specification<MembershipCode> specification = buildSearchSpecification(membershipCodeSearchDto);
|
|
|
|
|
Page<MembershipCode> membershipCodePage = membershipCodeDao.findAll(specification, pageRequest);
|
|
|
|
|
|
|
|
|
|
long totalElements = membershipCodePage.getTotalElements();
|
|
|
|
|
return new PageResult<>(totalElements, membershipCodePage.getContent());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private Specification<MembershipCode> buildSearchSpecification(MembershipCodeSearchDto param) {
|
|
|
|
|
return (Root<MembershipCode> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) -> {
|
|
|
|
|
List<Predicate> predicateList = new ArrayList<Predicate>();
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(param.getCode())) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("code"), param.getCode())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(param.getCreateUser())) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(
|
|
|
|
|
criteriaBuilder.equal(root.get("createUser"), param.getCreateUser())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (StringUtils.isNotBlank(param.getUserId())) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("userId"), param.getUserId())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (param.getStatus() != null) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("status"), param.getStatus())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (param.getDuration() != null) {
|
|
|
|
|
predicateList.add(
|
|
|
|
|
criteriaBuilder.and(criteriaBuilder.equal(root.get("duration"), param.getDuration())));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return criteriaBuilder.and(predicateList.toArray(new Predicate[predicateList.size()]));
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|