release: 修改会员,是否永久会员

release-2024-08-08
huangyw 4 months ago
parent 11b49e598f
commit a0d580aaf1

@ -31,6 +31,9 @@ public enum PointEnums {
LOTTERY_RESULT_WIN(1, "中奖"), LOTTERY_RESULT_WIN(1, "中奖"),
LOTTERY_RESULT_LOSE(2, "未中奖"), LOTTERY_RESULT_LOSE(2, "未中奖"),
MEMBERSHIP_LIFE(1, "永久会员"),
MEMBERSHIP_NOT_LIFE(2, "非永久会员"),
; ;
private final Integer code; private final Integer code;
private final String description; private final String description;

@ -8,7 +8,9 @@ import com.luoo.user.pojo.MembershipCode;
import com.luoo.user.service.MembershipCodeService; import com.luoo.user.service.MembershipCodeService;
import controller.BaseController; import controller.BaseController;
import io.swagger.annotations.*; import io.swagger.annotations.*;
import java.util.List; import java.util.List;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -29,60 +31,64 @@ import javax.validation.constraints.NotNull;
@Api(tags = "会员模块") @Api(tags = "会员模块")
public class MembershipController extends BaseController { public class MembershipController extends BaseController {
@Autowired @Autowired
private MembershipCodeService membershipCodeService; private MembershipCodeService membershipCodeService;
@ApiOperation(value = "1.生成会员码", notes = "仅限admin权限用户调用") @ApiOperation(value = "1.生成会员码", notes = "仅限admin权限用户调用")
@GetMapping("/code/generate") @GetMapping("/code/generate")
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams(value = { @ApiImplicitParams(value = {
@ApiImplicitParam(name = "duration", value = "会员时长(天)", required = true, dataType = "Integer", paramType = "query") @ApiImplicitParam(name = "duration", value = "会员时长(天)", required = true, dataType = "Integer", paramType = "query"),
}) @ApiImplicitParam(name = "life", value = "是否永久会员 1-是 2-否", required = true, dataType = "Integer", paramType = "query")
public Result<String> generateMembershipCode( })
@RequestHeader(value = "Authorization", required = true) String authorization, public Result<String> generateMembershipCode(
@NotNull Integer duration) { @RequestHeader(value = "Authorization", required = true) String authorization,
return Result.success(membershipCodeService.generateMembershipCode(authorization, duration)); @NotNull Integer duration,
} @NotNull Integer life) {
return Result.success(membershipCodeService.generateMembershipCode(authorization, duration, life));
}
@ApiOperation(value = "1.1 批量生成会员码", notes = "仅限admin权限用户调用") @ApiOperation(value = "1.1 批量生成会员码", notes = "仅限admin权限用户调用")
@GetMapping("/code/generate/batch") @GetMapping("/code/generate/batch")
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams(value = { @ApiImplicitParams(value = {
@ApiImplicitParam(name = "duration", value = "会员时长(天)", required = true, dataType = "Integer", paramType = "query"), @ApiImplicitParam(name = "duration", value = "会员时长(天)", required = true, dataType = "Integer", paramType = "query"),
@ApiImplicitParam(name = "count", value = "生成数量", required = true, dataType = "Integer", paramType = "query") @ApiImplicitParam(name = "count", value = "生成数量", required = true, dataType = "Integer", paramType = "query"),
}) @ApiImplicitParam(name = "life", value = "是否永久会员 1-是 2-否", required = true, dataType = "Integer", paramType = "query")
public Result<List<String>> generateMembershipCodeBatch( })
@RequestHeader(value = "Authorization", required = true) String authorization, public Result<List<String>> generateMembershipCodeBatch(
@NotNull Integer duration, @RequestHeader(value = "Authorization", required = true) String authorization,
@NotNull Integer count) { @NotNull Integer duration,
List<String> list = membershipCodeService.generateMembershipCodeBatch(authorization, @NotNull Integer count,
duration, count); @NotNull Integer life) {
return Result.success(list); List<String> list = membershipCodeService.generateMembershipCodeBatch(authorization,
} duration, count, life);
return Result.success(list);
}
@ApiOperation(value = "2.绑定会员码", notes = "仅限app用户调用") @ApiOperation(value = "2.绑定会员码", notes = "仅限app用户调用")
@PostMapping("/code/bind") @PostMapping("/code/bind")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAppUserLogin = true)
@ApiImplicitParams(value = { @ApiImplicitParams(value = {
@ApiImplicitParam(name = "code", value = "会员码", required = true, dataType = "String", paramType = "query") @ApiImplicitParam(name = "code", value = "会员码", required = true, dataType = "String", paramType = "query")
}) })
public Result<Void> bindMembershipCode( public Result<Void> bindMembershipCode(
@RequestHeader(value = "Authorization", required = true) String authorization, @RequestHeader(value = "Authorization", required = true) String authorization,
@NotNull String code) { @NotNull String code) {
membershipCodeService.bindMembershipCode(authorization, code); membershipCodeService.bindMembershipCode(authorization, code);
return Result.success(); return Result.success();
} }
@ApiOperation(value = "3.查询会员码", notes = "仅限admin权限用户调用") @ApiOperation(value = "3.查询会员码", notes = "仅限admin权限用户调用")
@GetMapping("/code/list") @GetMapping("/code/list{page}/{size}")
@GlobalInterceptor(checkAdminLogin = true) @GlobalInterceptor(checkAdminLogin = true)
public Result<PageResult<MembershipCode>> queryMembershipCode( public Result<PageResult<MembershipCode>> queryMembershipCode(
@RequestHeader(value = "Authorization", required = true) String authorization, @RequestHeader(value = "Authorization", required = true) String authorization,
@RequestBody @ApiParam(value = "查询参数", required = true) MembershipCodeSearchDto membershipCodeSearchDto, @RequestBody @ApiParam(value = "查询参数", required = true) MembershipCodeSearchDto membershipCodeSearchDto,
@ApiParam(value = "页码", required = true) @PathVariable Integer page, @ApiParam(value = "页码", required = true) @PathVariable Integer page,
@ApiParam(value = "每页条数", required = true) @PathVariable Integer size) { @ApiParam(value = "每页条数", required = true) @PathVariable Integer size) {
return Result.success(membershipCodeService.getMembershipCodeList(authorization, page, size, return Result.success(membershipCodeService.getMembershipCodeList(authorization, page, size,
membershipCodeSearchDto)); membershipCodeSearchDto));
} }
} }

@ -45,6 +45,10 @@ public class MembershipCode implements Serializable {
@ApiModelProperty(value = "时长") @ApiModelProperty(value = "时长")
private Integer duration; private Integer duration;
@Column(name = "life")
@ApiModelProperty(value = "是否永久会员 1-是 2-否")
private Integer life;
@Size(max = 20) @Size(max = 20)
@Column(name = "user_id", length = 20) @Column(name = "user_id", length = 20)
@ApiModelProperty(value = "用户id") @ApiModelProperty(value = "用户id")

@ -10,14 +10,17 @@ import com.luoo.user.pojo.UserInfo;
import com.luoo.user.pojo.UserPointLog; import com.luoo.user.pojo.UserPointLog;
import dto.UserLoginDto; import dto.UserLoginDto;
import enums.MembershipEnums; import enums.MembershipEnums;
import enums.PointEnums;
import enums.UserVipStatusEnum; import enums.UserVipStatusEnum;
import exception.BizException; import exception.BizException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate; import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root; import javax.persistence.criteria.Root;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -47,146 +50,157 @@ import java.util.UUID;
@Slf4j @Slf4j
public class MembershipCodeService { public class MembershipCodeService {
private final MembershipCodeDao membershipCodeDao; private final MembershipCodeDao membershipCodeDao;
private final IdWorker idWorker; private final IdWorker idWorker;
private final JwtUtil jwtUtil; private final JwtUtil jwtUtil;
private final UserInfoDao userInfoDao; private final UserInfoDao userInfoDao;
public MembershipCodeService(JwtUtil jwtUtil, IdWorker idWorker, public MembershipCodeService(JwtUtil jwtUtil, IdWorker idWorker,
MembershipCodeDao membershipCodeDao, UserInfoDao userInfoDao) { MembershipCodeDao membershipCodeDao, UserInfoDao userInfoDao) {
this.jwtUtil = jwtUtil; this.jwtUtil = jwtUtil;
this.idWorker = idWorker; this.idWorker = idWorker;
this.membershipCodeDao = membershipCodeDao; this.membershipCodeDao = membershipCodeDao;
this.userInfoDao = userInfoDao; this.userInfoDao = userInfoDao;
} }
/** /**
* UUID * UUID
* *
* @param token token * @param token token
* @param duration * @param duration
* @return * @param life
*/ * @return
@Transactional(rollbackFor = Exception.class) */
public String generateMembershipCode(String token, Integer duration) { @Transactional(rollbackFor = Exception.class)
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); public String generateMembershipCode(String token, Integer duration, Integer life) {
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
// UUID作为会员码
String code = UUID.randomUUID().toString(); // UUID作为会员码
MembershipCode build = MembershipCode.builder() String code = UUID.randomUUID().toString();
.id(String.valueOf(idWorker.nextId())) MembershipCode build = MembershipCode.builder()
.code(code) .id(String.valueOf(idWorker.nextId()))
.status(MembershipEnums.CREATED.getCode()) .code(code)
.duration(duration) .status(MembershipEnums.CREATED.getCode())
.createUser(userLoginDto.getUserId()) .duration(duration)
.build(); .life(life)
membershipCodeDao.save(build); .createUser(userLoginDto.getUserId())
return code; .build();
} membershipCodeDao.save(build);
return code;
/** }
*
* /**
* @param token token *
* @param duration *
* @param count * @param token token
* @return list * @param duration
*/ * @param count
@Transactional(rollbackFor = Exception.class) * @param life
public List<String> generateMembershipCodeBatch(String token, Integer duration, Integer count) { * @return list
List<String> result = new ArrayList<>(); */
for (int i = 0; i < count; i++) { @Transactional(rollbackFor = Exception.class)
String code = generateMembershipCode(token, duration); public List<String> generateMembershipCodeBatch(String token, Integer duration, Integer count, Integer life) {
result.add(code); 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
* @param token token */
* @param code @Transactional(rollbackFor = Exception.class)
*/ public void bindMembershipCode(String token, String code) {
@Transactional(rollbackFor = Exception.class) UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
public void bindMembershipCode(String token, String code) { MembershipCode membershipCode = membershipCodeDao.findMembershipCodeByCodeAndStatus(code,
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); MembershipEnums.CREATED.getCode());
MembershipCode membershipCode = membershipCodeDao.findMembershipCodeByCodeAndStatus(code, if (membershipCode == null) {
MembershipEnums.CREATED.getCode()); throw new BizException("会员码不存在");
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()); public PageResult<MembershipCode> getMembershipCodeList(String token, Integer page, Integer size,
membershipCode.setUserId(userLoginDto.getUserId()); MembershipCodeSearchDto membershipCodeSearchDto) {
membershipCode.setUpdateUser(userLoginDto.getUserId());
membershipCodeDao.save(membershipCode); Sort sort = new Sort(Direction.DESC, "createTime");
PageRequest pageRequest = PageRequest.of(page - 1, size, sort);
// 处理用户表
UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get(); Specification<MembershipCode> specification = buildSearchSpecification(membershipCodeSearchDto);
// 计算会员到期时间 Page<MembershipCode> membershipCodePage = membershipCodeDao.findAll(specification, pageRequest);
LocalDate vipExpireTime;
if (Objects.equals(UserVipStatusEnum.INITIAL.getCode(), userInfo.getVipStatus())) { long totalElements = membershipCodePage.getTotalElements();
vipExpireTime = userInfo.getVipExpireTime().plusDays(membershipCode.getDuration()); return new PageResult<>(totalElements, membershipCodePage.getContent());
} else { }
// 从今天开始增加membershipCode的duration天
userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); private Specification<MembershipCode> buildSearchSpecification(MembershipCodeSearchDto param) {
vipExpireTime = LocalDate.now().plusDays(membershipCode.getDuration()); 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()]));
};
}
} }

@ -3,14 +3,16 @@ create table tb_membership_code
id varchar(20) not null comment 'id' id varchar(20) not null comment 'id'
primary key, primary key,
code varchar(50) null comment '会员码', code varchar(50) null comment '会员码',
status tinyint null comment '状态 1-已生成 2-已绑定 3-已退款 4-已废弃', status tinyint null comment '状态 1-已生成 2-已绑定 3-已退款 4-已废弃',
duration int null comment '会员时长(天)', duration int null comment '会员时长(天)',
life tinyint null comment '是否永久会员 1-是 2-否',
user_id varchar(20) null comment '使用用户', user_id varchar(20) null comment '使用用户',
create_time datetime null comment '创建时间', create_time datetime null comment '创建时间',
update_time datetime null comment '修改时间', update_time datetime null comment '修改时间',
create_user varchar(20) null comment '创建人', create_user varchar(20) null comment '创建人',
update_user varchar(20) null comment '修改人' update_user varchar(20) null comment '修改人'
) comment '会员码'; )
comment '会员码';
create table tb_user_point_log create table tb_user_point_log
( (

Loading…
Cancel
Save