mirror of
https://gitee.com/huangge1199_admin/vue-pro.git
synced 2025-01-18 19:20:05 +08:00
member: 完善连续签到 review 提到的问题
This commit is contained in:
parent
1ac68aca97
commit
63431082e5
@ -177,4 +177,14 @@ public class DateUtils {
|
||||
return LocalDateTimeUtil.isSameDay(date, LocalDateTime.now());
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否昨天
|
||||
*
|
||||
* @param date 日期
|
||||
* @return 是否
|
||||
*/
|
||||
public static boolean isYesterday(LocalDateTime date) {
|
||||
return LocalDateTimeUtil.isSameDay(date, LocalDateTime.now().minusDays(1));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.ObjUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
|
||||
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||
import cn.iocoder.yudao.module.member.controller.admin.signin.vo.record.MemberSignInRecordRespVO;
|
||||
import cn.iocoder.yudao.module.member.controller.app.signin.vo.record.AppMemberSignInRecordRespVO;
|
||||
import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInConfigDO;
|
||||
@ -47,10 +48,9 @@ public interface MemberSignInRecordConvert {
|
||||
// 1. 计算是第几天签到
|
||||
configs.sort(Comparator.comparing(MemberSignInConfigDO::getDay));
|
||||
MemberSignInConfigDO lastConfig = CollUtil.getLast(configs); // 最大签到天数配置
|
||||
// 1.2. 计算今天是第几天签到
|
||||
// 1.2. 计算今天是第几天签到 (只有连续签到才加否则重置为 1)
|
||||
int day = 1;
|
||||
// TODO @puhui999:要判断是不是昨天签到的;是否是昨天的判断,可以抽个方法到 util 里
|
||||
if (lastRecord != null) {
|
||||
if (lastRecord != null && DateUtils.isYesterday(lastRecord.getCreateTime())) {
|
||||
day = lastRecord.getDay() + 1;
|
||||
}
|
||||
// 1.3 判断是否超出了最大签到配置
|
||||
|
@ -19,14 +19,12 @@ import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum;
|
||||
import cn.iocoder.yudao.module.member.service.level.MemberLevelService;
|
||||
import cn.iocoder.yudao.module.member.service.point.MemberPointRecordService;
|
||||
import cn.iocoder.yudao.module.member.service.user.MemberUserService;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import jakarta.annotation.Resource;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
@ -77,50 +75,15 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService
|
||||
}
|
||||
summary.setTodaySignIn(DateUtils.isToday(lastRecord.getCreateTime()));
|
||||
|
||||
// 4. 校验今天是否签到,没有签到则直接返回
|
||||
// 4.1 校验今天是否签到,没有签到则直接返回
|
||||
if (!summary.getTodaySignIn()) {
|
||||
return summary;
|
||||
}
|
||||
// 4.1. 判断连续签到天数
|
||||
// TODO @puhui999:连续签到,可以基于 lastRecord 的 day 和当前时间判断呀?按 day 统计连续签到天数可能不准确
|
||||
// 1. day 只是记录第几天签到的有可能不连续,比如第一次签到是周一,第二次签到是周三这样 lastRecord 的 day 为 2 但是并不是连续的两天
|
||||
// 2. day 超出签到规则的最大天数会重置到从第一天开始签到(我理解为开始下一轮,类似一周签到七天七天结束下周又从周一开始签到)
|
||||
// 1. 回复:周三签到,day 要归 1 呀。连续签到哈;
|
||||
List<MemberSignInRecordDO> signInRecords = signInRecordMapper.selectListByUserId(userId);
|
||||
signInRecords.sort(Comparator.comparing(MemberSignInRecordDO::getCreateTime).reversed()); // 根据签到时间倒序
|
||||
summary.setContinuousDay(calculateConsecutiveDays(signInRecords));
|
||||
// 4.2 连续签到天数
|
||||
summary.setContinuousDay(lastRecord.getDay());
|
||||
return summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* 计算连续签到天数
|
||||
*
|
||||
* @param signInRecords 签到记录列表
|
||||
* @return int 连续签到天数
|
||||
*/
|
||||
public int calculateConsecutiveDays(List<MemberSignInRecordDO> signInRecords) {
|
||||
int consecutiveDays = 1; // 初始连续天数为1
|
||||
LocalDate previousDate = null;
|
||||
|
||||
for (MemberSignInRecordDO record : signInRecords) {
|
||||
LocalDate currentDate = record.getCreateTime().toLocalDate();
|
||||
|
||||
if (previousDate != null) {
|
||||
// 检查相邻两个日期是否连续
|
||||
if (currentDate.minusDays(1).isEqual(previousDate)) {
|
||||
consecutiveDays++;
|
||||
} else {
|
||||
// 如果日期不连续,停止遍历
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
previousDate = currentDate;
|
||||
}
|
||||
|
||||
return consecutiveDays;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<MemberSignInRecordDO> getSignInRecordPage(MemberSignInRecordPageReqVO pageReqVO) {
|
||||
// 根据用户昵称查询出用户ids
|
||||
|
Loading…
Reference in New Issue
Block a user