code review:会员签到逻辑

This commit is contained in:
YunaiV 2023-09-18 22:18:42 +08:00
parent 477342d324
commit be1ecf96d4
4 changed files with 55 additions and 57 deletions

View File

@ -39,8 +39,7 @@ public interface ErrorCodeConstants {
ErrorCode SIGN_IN_CONFIG_EXISTS = new ErrorCode(1004009001, "签到天数规则已存在"); ErrorCode SIGN_IN_CONFIG_EXISTS = new ErrorCode(1004009001, "签到天数规则已存在");
//========== 签到配置 1004010000 ========== //========== 签到配置 1004010000 ==========
ErrorCode SIGN_IN_RECORD_EXISTS = new ErrorCode(1004010000,"用户重复签到"); ErrorCode SIGN_IN_RECORD_TODAY_EXISTS = new ErrorCode(1004010000,"今日已签到,请勿重复签到");
//========== 用户等级 1004011000 ========== //========== 用户等级 1004011000 ==========
ErrorCode LEVEL_NOT_EXISTS = new ErrorCode(1004011000, "用户等级不存在"); ErrorCode LEVEL_NOT_EXISTS = new ErrorCode(1004011000, "用户等级不存在");

View File

@ -1,7 +1,6 @@
package cn.iocoder.yudao.module.member.controller.app.signin; package cn.iocoder.yudao.module.member.controller.app.signin;
import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.convert.signin.MemberSignInRecordConvert; import cn.iocoder.yudao.module.member.convert.signin.MemberSignInRecordConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO; import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO;
import cn.iocoder.yudao.module.member.service.signin.MemberSignInRecordService; import cn.iocoder.yudao.module.member.service.signin.MemberSignInRecordService;
@ -14,42 +13,33 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
/** import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
* 描述 :用户签到相关信息接口 import static cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;
* Author :xiaqing
* Date :2023-09-15 09:02 // TODO @xiaqingsign-in
*/
@Tag(name = "签到APP - 签到") @Tag(name = "签到APP - 签到")
@RestController @RestController
@RequestMapping("/member/signin") @RequestMapping("/member/signin")
public class AppMemberSignInController { public class AppMemberSignInController {
@Resource @Resource
MemberSignInRecordService signInRecordService; private MemberSignInRecordService signInRecordService;
/**
* 描述 :获取个人签到信息
* Author :xiaqing
* Date :2023-09-15 12:56:47
*/
// TODO @xiaqing泛型
// TODO @xiaqing合并到 AppMemberSignInRecordController getSignInRecordSummary 里哈
@Operation(summary = "个人签到信息") @Operation(summary = "个人签到信息")
@GetMapping("/get-summary") @GetMapping("/get-summary")
public CommonResult getUserSummary(){ public CommonResult getUserSummary(){
return CommonResult.success(signInRecordService.getUserSummary(SecurityFrameworkUtils.getLoginUserId())); return success(signInRecordService.getSignInRecordSummary(getLoginUserId()));
} }
// TODO @xiaqing泛型
/** // TODO @xiaqing合并到 AppMemberSignInRecordController createSignInRecord 里哈
* 描述 :用户签到
* Author :xiaqing
* Date :2023-09-15 09:20:58
*/
@Operation(summary = "会员签到") @Operation(summary = "会员签到")
@PostMapping("/create") @PostMapping("/create")
public CommonResult create(){ public CommonResult create(){
MemberSignInRecordDO recordDO = signInRecordService.create(SecurityFrameworkUtils.getLoginUserId()); MemberSignInRecordDO recordDO = signInRecordService.createSignRecord(getLoginUserId());
return CommonResult.success(MemberSignInRecordConvert.INSTANCE.coverRecordToAppRecordVo(recordDO)); return success(MemberSignInRecordConvert.INSTANCE.coverRecordToAppRecordVo(recordDO));
} }
} }

View File

@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.member.service.signin;
import cn.iocoder.yudao.framework.common.pojo.PageParam; import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.record.MemberSignInRecordPageReqVO; import cn.iocoder.yudao.module.member.controller.admin.signin.vo.record.MemberSignInRecordPageReqVO;
import cn.iocoder.yudao.module.member.controller.app.signin.vo.AppMemberSignInRecordRespVO;
import cn.iocoder.yudao.module.member.controller.app.signin.vo.AppMemberSignInSummaryRespVO; import cn.iocoder.yudao.module.member.controller.app.signin.vo.AppMemberSignInSummaryRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO; import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInRecordDO;
@ -31,18 +30,21 @@ public interface MemberSignInRecordService {
*/ */
PageResult<MemberSignInRecordDO> getSignRecordPage(Long userId, PageParam pageParam); PageResult<MemberSignInRecordDO> getSignRecordPage(Long userId, PageParam pageParam);
/**
MemberSignInRecordDO create(Long userId); * 创建签到记录
*
* @param userId 用户编号
* @return 签到记录
*/
MemberSignInRecordDO createSignRecord(Long userId);
/** /**
* 根据用户编号获得个人签到统计信息
* *
*功能描述: 根据用户id获取个人签到信息 * @param userId 用户编号
* @param userId * @return 个人签到统计信息
* @return
* @author xiaqing
* @date 2023-09-15 14:21:01
*/ */
AppMemberSignInSummaryRespVO getUserSummary(Long userId); AppMemberSignInSummaryRespVO getSignInRecordSummary(Long userId);
} }

View File

@ -36,28 +36,28 @@ import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.
@Validated @Validated
public class MemberSignInRecordServiceImpl implements MemberSignInRecordService { public class MemberSignInRecordServiceImpl implements MemberSignInRecordService {
@Resource
private MemberSignInRecordMapper memberSignInRecordMapper;
@Resource @Resource
private MemberSignInRecordMapper signInRecordMapper; private MemberSignInRecordMapper signInRecordMapper;
@Resource @Resource
private MemberSignInConfigMapper signInConfigMapper; private MemberSignInConfigMapper signInConfigMapper;
@Resource @Resource
private MemberUserApi memberUserApi; private MemberUserApi memberUserApi;
@Override @Override
public AppMemberSignInSummaryRespVO getUserSummary(Long userId) { public AppMemberSignInSummaryRespVO getSignInRecordSummary(Long userId) {
AppMemberSignInSummaryRespVO vo = new AppMemberSignInSummaryRespVO(); AppMemberSignInSummaryRespVO vo = new AppMemberSignInSummaryRespVO();
vo.setTotalDay(0); vo.setTotalDay(0);
vo.setContinuousDay(0); vo.setContinuousDay(0);
vo.setTodaySignIn(false); vo.setTodaySignIn(false);
//获取用户签到的记录按照天数倒序获取 //获取用户签到的记录按照天数倒序获取
List <MemberSignInRecordDO> signInRecordDOList = signInRecordMapper.selectListByUserId(userId); List <MemberSignInRecordDO> signInRecordDOList = signInRecordMapper.selectListByUserId(userId);
// TODO @xiaqingif 空的时候直接 return这样括号少逻辑更简洁
if(!CollectionUtils.isEmpty(signInRecordDOList)){ if(!CollectionUtils.isEmpty(signInRecordDOList)){
//设置总签到天数 //设置总签到天数
vo.setTotalDay(signInRecordDOList.size()); vo.setTotalDay(signInRecordDOList.size()); // TODO @xiaqing是不是不用读取 signInRecordDOList 所有的而是 count下然后另外再读取一条最后一条
//判断当天是否有签到复用校验方法 //判断当天是否有签到复用校验方法
// TODO @xiaqing不要用异常实现逻辑还是判断哈
try { try {
validSignDay(signInRecordDOList.get(0)); validSignDay(signInRecordDOList.get(0));
vo.setTodaySignIn(false); vo.setTodaySignIn(false);
@ -69,6 +69,7 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
//下方计算连续签到从2天开始此处直接设置一天连续签到 //下方计算连续签到从2天开始此处直接设置一天连续签到
vo.setContinuousDay(1); vo.setContinuousDay(1);
//判断连续签到天数 //判断连续签到天数
// TODO @xiaqing这里逻辑想想怎么在简化下可读性可以在提升下哈
for (int i = 1; i < signInRecordDOList.size(); i++) { for (int i = 1; i < signInRecordDOList.size(); i++) {
//前一天减1等于当前天数则说明连续继续循环 //前一天减1等于当前天数则说明连续继续循环
LocalDate cur = signInRecordDOList.get(i).getCreateTime().toLocalDate(); LocalDate cur = signInRecordDOList.get(i).getCreateTime().toLocalDate();
@ -103,17 +104,19 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
userIds = convertSet(users, MemberUserRespDTO::getId); userIds = convertSet(users, MemberUserRespDTO::getId);
} }
// 分页查询 // 分页查询
return memberSignInRecordMapper.selectPage(pageReqVO, userIds); return signInRecordMapper.selectPage(pageReqVO, userIds);
} }
@Override @Override
public PageResult<MemberSignInRecordDO> getSignRecordPage(Long userId, PageParam pageParam) { public PageResult<MemberSignInRecordDO> getSignRecordPage(Long userId, PageParam pageParam) {
return memberSignInRecordMapper.selectPage(userId, pageParam); return signInRecordMapper.selectPage(userId, pageParam);
} }
@Override @Override
public MemberSignInRecordDO create(Long userId) { public MemberSignInRecordDO createSignRecord(Long userId) {
// 获取当前用户签到的最大天数 // 获取当前用户签到的最大天数
// TODO @xiaqingdb 操作dou封装到 mapper
// TODO @xiaqingmaxSignDay是不是变量叫 lastRecord 会更容易理解哈
MemberSignInRecordDO maxSignDay = signInRecordMapper.selectOne(new LambdaQueryWrapperX <MemberSignInRecordDO>() MemberSignInRecordDO maxSignDay = signInRecordMapper.selectOne(new LambdaQueryWrapperX <MemberSignInRecordDO>()
.eq(MemberSignInRecordDO::getUserId, userId) .eq(MemberSignInRecordDO::getUserId, userId)
.orderByDesc(MemberSignInRecordDO::getDay) .orderByDesc(MemberSignInRecordDO::getDay)
@ -121,29 +124,30 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
// 判断是否重复签到 // 判断是否重复签到
validSignDay(maxSignDay); validSignDay(maxSignDay);
// TODO @xiaqing可以使用 // 进行注释
/**1.查询出当前签到的天数**/ /**1.查询出当前签到的天数**/
MemberSignInRecordDO sign = new MemberSignInRecordDO(); MemberSignInRecordDO sign = new MemberSignInRecordDO().setUserId(userId); // TODO @xiaqing应该使用 record 变量会更合适
sign.setUserId(userId); sign.setDay(1); // 设置签到初始化天数
//设置签到初始化天数 sign.setPoint(0); // 设置签到分数默认为 0
sign.setDay(1);
//设置签到分数默认为0
sign.setPoint(0);
// 如果不为空则修改当前签到对应的天数 // 如果不为空则修改当前签到对应的天数
// TODO @xiaqing应该是要判断连续哈就是昨天
if (maxSignDay != null) { if (maxSignDay != null) {
sign.setDay(maxSignDay.getDay() + 1); sign.setDay(maxSignDay.getDay() + 1);
} }
/**2.获取签到对应的分数**/ /**2.获取签到对应的分数**/
//获取所有的签到规则按照天数排序只获取启用的 // 获取所有的签到规则按照天数排序只获取启用的 TODO @xiaqing不要使用 signInConfigMapper 直接查询而是要通过 SigninConfigService
List <MemberSignInConfigDO> configDOList = signInConfigMapper.selectList(new LambdaQueryWrapperX <MemberSignInConfigDO>() List <MemberSignInConfigDO> configDOList = signInConfigMapper.selectList(new LambdaQueryWrapperX <MemberSignInConfigDO>()
.eq(MemberSignInConfigDO::getStatus, CommonStatusEnum.ENABLE.getStatus()) .eq(MemberSignInConfigDO::getStatus, CommonStatusEnum.ENABLE.getStatus())
.orderByAsc(MemberSignInConfigDO::getDay)); .orderByAsc(MemberSignInConfigDO::getDay));
// 如果签到的天数大于最大启用的规则天数直接给最大签到的分数 // 如果签到的天数大于最大启用的规则天数直接给最大签到的分数
// TODO @xiaqing超过最大配置的天数应该直接重置到第一天哈
MemberSignInConfigDO lastConfig = configDOList.get(configDOList.size() - 1); MemberSignInConfigDO lastConfig = configDOList.get(configDOList.size() - 1);
if (sign.getDay() > lastConfig.getDay()) { if (sign.getDay() > lastConfig.getDay()) {
sign.setPoint(lastConfig.getPoint()); sign.setPoint(lastConfig.getPoint());
} else { } else {
configDOList.forEach(el -> { configDOList.forEach(el -> {
// 循环匹配对应天数设置对应分数 // 循环匹配对应天数设置对应分数
// TODO @xiaqing使用 equals另外这种不应该去遍历比较从可读性来说应该 CollUtil.findOne()
if (el.getDay() == sign.getDay()) { if (el.getDay() == sign.getDay()) {
sign.setPoint(el.getPoint()); sign.setPoint(el.getPoint());
} }
@ -153,16 +157,19 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
// 3. 插入当前签到获取的分数 // 3. 插入当前签到获取的分数
signInRecordMapper.insert(sign); signInRecordMapper.insert(sign);
//4.返回给用户
return sign; return sign;
} }
void validSignDay(MemberSignInRecordDO signInRecordDO) { // TODO @xiaqing校验使用 validate 动词哈可以改成 validateSigned
private void validSignDay(MemberSignInRecordDO signInRecordDO) {
// TODO @xiaqing代码格式if () {} 要有括号哈
if (signInRecordDO == null) if (signInRecordDO == null)
return; return;
// TODO @xiaqing可以直接使用 DateUtils.isToday()
LocalDate today = LocalDate.now(); LocalDate today = LocalDate.now();
if (today.equals(signInRecordDO.getCreateTime().toLocalDate())) { if (today.equals(signInRecordDO.getCreateTime().toLocalDate())) {
throw exception(ErrorCodeConstants.SIGN_IN_RECORD_EXISTS); throw exception(ErrorCodeConstants.SIGN_IN_RECORD_TODAY_EXISTS);
} }
} }
} }