mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2024-11-23 07:41:53 +08:00
code review:会员签到逻辑
This commit is contained in:
parent
477342d324
commit
be1ecf96d4
@ -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, "用户等级不存在");
|
||||||
|
@ -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 @xiaqing:sign-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));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -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 @xiaqing:if 空的时候,直接 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,47 +104,50 @@ 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 @xiaqing:db 操作,dou封装到 mapper 中;
|
||||||
|
// TODO @xiaqing:maxSignDay,是不是变量叫 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)
|
||||||
.last("limit 1"));
|
.last("limit 1"));
|
||||||
//判断是否重复签到
|
// 判断是否重复签到
|
||||||
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
|
// TODO @xiaqing:应该是要判断连续哈,就是昨天;
|
||||||
sign.setPoint(0);
|
|
||||||
//如果不为空则修改当前签到对应的天数
|
|
||||||
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());
|
||||||
}
|
}
|
||||||
@ -151,18 +155,21 @@ 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user