release: 批量生成会员码

release-2024-08-08
huangyw 4 months ago
parent 3b570ce16c
commit b9f3b78d8f

@ -5,6 +5,7 @@ import api.Result;
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 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.*;
@ -25,30 +26,48 @@ 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")
}) })
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) {
} return Result.success(membershipCodeService.generateMembershipCode(authorization, duration));
}
@ApiOperation(value = "2.绑定会员码", notes = "仅限app用户调用") @ApiOperation(value = "1.1 批量生成会员码", notes = "仅限admin权限用户调用")
@PostMapping("/code/bind") @GetMapping("/code/generate/batch")
@GlobalInterceptor(checkAppUserLogin = true) @GlobalInterceptor(checkAdminLogin = true)
@ApiImplicitParams(value = { @ApiImplicitParams(value = {
@ApiImplicitParam(name = "code", value = "会员码", required = true, dataType = "String", paramType = "query") @ApiImplicitParam(name = "duration", value = "会员时长(天)", required = true, dataType = "Integer", paramType = "query"),
}) @ApiImplicitParam(name = "count", value = "生成数量", required = true, dataType = "Integer", paramType = "query")
public Result<Void> bindMembershipCode(@RequestHeader(value = "Authorization", required = true) String authorization, })
@NotNull String code) { public Result<List<String>> generateMembershipCodeBatch(
membershipCodeService.bindMembershipCode(authorization, code); @RequestHeader(value = "Authorization", required = true) String authorization,
return Result.success(); @NotNull Integer duration,
} @NotNull Integer count) {
List<String> list = membershipCodeService.generateMembershipCodeBatch(authorization,
duration, count);
return Result.success(list);
}
@ApiOperation(value = "2.绑定会员码", notes = "仅限app用户调用")
@PostMapping("/code/bind")
@GlobalInterceptor(checkAppUserLogin = true)
@ApiImplicitParams(value = {
@ApiImplicitParam(name = "code", value = "会员码", required = true, dataType = "String", paramType = "query")
})
public Result<Void> bindMembershipCode(
@RequestHeader(value = "Authorization", required = true) String authorization,
@NotNull String code) {
membershipCodeService.bindMembershipCode(authorization, code);
return Result.success();
}
} }

@ -8,6 +8,8 @@ import dto.UserLoginDto;
import enums.MembershipEnums; import enums.MembershipEnums;
import enums.UserVipStatusEnum; import enums.UserVipStatusEnum;
import exception.BizException; import exception.BizException;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -31,80 +33,99 @@ 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, MembershipCodeDao membershipCodeDao, UserInfoDao userInfoDao) { public MembershipCodeService(JwtUtil jwtUtil, IdWorker idWorker,
this.jwtUtil = jwtUtil; MembershipCodeDao membershipCodeDao, UserInfoDao userInfoDao) {
this.idWorker = idWorker; this.jwtUtil = jwtUtil;
this.membershipCodeDao = membershipCodeDao; this.idWorker = idWorker;
this.userInfoDao = userInfoDao; this.membershipCodeDao = membershipCodeDao;
} this.userInfoDao = userInfoDao;
}
/** /**
* * UUID
* UUID *
* * @param token token
* @param token token * @param duration
* @param duration * @return
* @return */
*/ @Transactional(rollbackFor = Exception.class)
@Transactional(rollbackFor = Exception.class) public String generateMembershipCode(String token, Integer duration) {
public String generateMembershipCode(String token, Integer duration) { UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
// UUID作为会员码 // UUID作为会员码
String code = UUID.randomUUID().toString(); String code = UUID.randomUUID().toString();
MembershipCode build = MembershipCode.builder() MembershipCode build = MembershipCode.builder()
.id(String.valueOf(idWorker.nextId())) .id(String.valueOf(idWorker.nextId()))
.code(code) .code(code)
.status(MembershipEnums.CREATED.getCode()) .status(MembershipEnums.CREATED.getCode())
.duration(duration) .duration(duration)
.createUser(userLoginDto.getUserId()) .createUser(userLoginDto.getUserId())
.build(); .build();
membershipCodeDao.save(build); membershipCodeDao.save(build);
return code; return code;
} }
/** /**
* *
* *
* @param token token * @param token token
* @param code * @param duration
*/ * @param count
@Transactional(rollbackFor = Exception.class) * @return list
public void bindMembershipCode(String token, String code) { */
UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token); @Transactional(rollbackFor = Exception.class)
MembershipCode membershipCode = membershipCodeDao.findMembershipCodeByCodeAndStatus(code, MembershipEnums.CREATED.getCode()); public List<String> generateMembershipCodeBatch(String token, Integer duration, Integer count) {
if (membershipCode == null) { List<String> result = new ArrayList<>();
throw new BizException("会员码不存在"); for (int i = 0; i < count; i++) {
} String code = generateMembershipCode(token, duration);
// 处理会员码表 result.add(code);
membershipCode.setStatus(MembershipEnums.BIND.getCode()); }
membershipCode.setUserId(userLoginDto.getUserId()); return result;
membershipCode.setUpdateUser(userLoginDto.getUserId()); }
membershipCodeDao.save(membershipCode);
// 处理用户表 /**
UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get(); *
// 计算会员到期时间 *
LocalDate vipExpireTime; * @param token token
if(Objects.equals(UserVipStatusEnum.INITIAL.getCode(), userInfo.getVipStatus())){ * @param code
vipExpireTime = userInfo.getVipExpireTime().plusDays(membershipCode.getDuration()); */
} else { @Transactional(rollbackFor = Exception.class)
// 从今天开始增加membershipCode的duration天 public void bindMembershipCode(String token, String code) {
userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(token);
vipExpireTime = LocalDate.now().plusDays(membershipCode.getDuration()); MembershipCode membershipCode = membershipCodeDao.findMembershipCodeByCodeAndStatus(code,
} MembershipEnums.CREATED.getCode());
userInfo.setVipExpireTime(vipExpireTime); if (membershipCode == null) {
// 无论如何当即设置为会员生效状态 throw new BizException("会员码不存在");
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());
} }
userInfo.setVipExpireTime(vipExpireTime);
// 无论如何当即设置为会员生效状态
userInfo.setVipStatus(UserVipStatusEnum.ACTIVE.getCode());
userInfoDao.save(userInfo);
}
} }

Loading…
Cancel
Save