Commit 63431082e51c7d0d0d86ce92573ab1ae1f700553
1 parent
1ac68aca
member: 完善连续签到 review 提到的问题
Showing
3 changed files
with
17 additions
and
44 deletions
yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/util/date/DateUtils.java
| ... | ... | @@ -177,4 +177,14 @@ public class DateUtils { |
| 177 | 177 | return LocalDateTimeUtil.isSameDay(date, LocalDateTime.now()); |
| 178 | 178 | } |
| 179 | 179 | |
| 180 | + /** | |
| 181 | + * 是否昨天 | |
| 182 | + * | |
| 183 | + * @param date 日期 | |
| 184 | + * @return 是否 | |
| 185 | + */ | |
| 186 | + public static boolean isYesterday(LocalDateTime date) { | |
| 187 | + return LocalDateTimeUtil.isSameDay(date, LocalDateTime.now().minusDays(1)); | |
| 188 | + } | |
| 189 | + | |
| 180 | 190 | } | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/convert/signin/MemberSignInRecordConvert.java
| ... | ... | @@ -4,6 +4,7 @@ import cn.hutool.core.collection.CollUtil; |
| 4 | 4 | import cn.hutool.core.util.ObjUtil; |
| 5 | 5 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 6 | import cn.iocoder.yudao.framework.common.util.collection.MapUtils; |
| 7 | +import cn.iocoder.yudao.framework.common.util.date.DateUtils; | |
| 7 | 8 | import cn.iocoder.yudao.module.member.controller.admin.signin.vo.record.MemberSignInRecordRespVO; |
| 8 | 9 | import cn.iocoder.yudao.module.member.controller.app.signin.vo.record.AppMemberSignInRecordRespVO; |
| 9 | 10 | import cn.iocoder.yudao.module.member.dal.dataobject.signin.MemberSignInConfigDO; |
| ... | ... | @@ -47,10 +48,9 @@ public interface MemberSignInRecordConvert { |
| 47 | 48 | // 1. 计算是第几天签到 |
| 48 | 49 | configs.sort(Comparator.comparing(MemberSignInConfigDO::getDay)); |
| 49 | 50 | MemberSignInConfigDO lastConfig = CollUtil.getLast(configs); // 最大签到天数配置 |
| 50 | - // 1.2. 计算今天是第几天签到 | |
| 51 | + // 1.2. 计算今天是第几天签到 (只有连续签到才加否则重置为 1) | |
| 51 | 52 | int day = 1; |
| 52 | - // TODO @puhui999:要判断是不是昨天签到的;是否是昨天的判断,可以抽个方法到 util 里 | |
| 53 | - if (lastRecord != null) { | |
| 53 | + if (lastRecord != null && DateUtils.isYesterday(lastRecord.getCreateTime())) { | |
| 54 | 54 | day = lastRecord.getDay() + 1; |
| 55 | 55 | } |
| 56 | 56 | // 1.3 判断是否超出了最大签到配置 | ... | ... |
yudao-module-member/yudao-module-member-biz/src/main/java/cn/iocoder/yudao/module/member/service/signin/MemberSignInRecordServiceImpl.java
| ... | ... | @@ -19,14 +19,12 @@ import cn.iocoder.yudao.module.member.enums.point.MemberPointBizTypeEnum; |
| 19 | 19 | import cn.iocoder.yudao.module.member.service.level.MemberLevelService; |
| 20 | 20 | import cn.iocoder.yudao.module.member.service.point.MemberPointRecordService; |
| 21 | 21 | import cn.iocoder.yudao.module.member.service.user.MemberUserService; |
| 22 | +import jakarta.annotation.Resource; | |
| 22 | 23 | import org.apache.commons.lang3.StringUtils; |
| 23 | 24 | import org.springframework.stereotype.Service; |
| 24 | 25 | import org.springframework.transaction.annotation.Transactional; |
| 25 | 26 | import org.springframework.validation.annotation.Validated; |
| 26 | 27 | |
| 27 | -import jakarta.annotation.Resource; | |
| 28 | -import java.time.LocalDate; | |
| 29 | -import java.util.Comparator; | |
| 30 | 28 | import java.util.List; |
| 31 | 29 | import java.util.Set; |
| 32 | 30 | |
| ... | ... | @@ -77,50 +75,15 @@ public class MemberSignInRecordServiceImpl implements MemberSignInRecordService |
| 77 | 75 | } |
| 78 | 76 | summary.setTodaySignIn(DateUtils.isToday(lastRecord.getCreateTime())); |
| 79 | 77 | |
| 80 | - // 4. 校验今天是否签到,没有签到则直接返回 | |
| 78 | + // 4.1 校验今天是否签到,没有签到则直接返回 | |
| 81 | 79 | if (!summary.getTodaySignIn()) { |
| 82 | 80 | return summary; |
| 83 | 81 | } |
| 84 | - // 4.1. 判断连续签到天数 | |
| 85 | - // TODO @puhui999:连续签到,可以基于 lastRecord 的 day 和当前时间判断呀?按 day 统计连续签到天数可能不准确 | |
| 86 | - // 1. day 只是记录第几天签到的有可能不连续,比如第一次签到是周一,第二次签到是周三这样 lastRecord 的 day 为 2 但是并不是连续的两天 | |
| 87 | - // 2. day 超出签到规则的最大天数会重置到从第一天开始签到(我理解为开始下一轮,类似一周签到七天七天结束下周又从周一开始签到) | |
| 88 | - // 1. 回复:周三签到,day 要归 1 呀。连续签到哈; | |
| 89 | - List<MemberSignInRecordDO> signInRecords = signInRecordMapper.selectListByUserId(userId); | |
| 90 | - signInRecords.sort(Comparator.comparing(MemberSignInRecordDO::getCreateTime).reversed()); // 根据签到时间倒序 | |
| 91 | - summary.setContinuousDay(calculateConsecutiveDays(signInRecords)); | |
| 82 | + // 4.2 连续签到天数 | |
| 83 | + summary.setContinuousDay(lastRecord.getDay()); | |
| 92 | 84 | return summary; |
| 93 | 85 | } |
| 94 | 86 | |
| 95 | - /** | |
| 96 | - * 计算连续签到天数 | |
| 97 | - * | |
| 98 | - * @param signInRecords 签到记录列表 | |
| 99 | - * @return int 连续签到天数 | |
| 100 | - */ | |
| 101 | - public int calculateConsecutiveDays(List<MemberSignInRecordDO> signInRecords) { | |
| 102 | - int consecutiveDays = 1; // 初始连续天数为1 | |
| 103 | - LocalDate previousDate = null; | |
| 104 | - | |
| 105 | - for (MemberSignInRecordDO record : signInRecords) { | |
| 106 | - LocalDate currentDate = record.getCreateTime().toLocalDate(); | |
| 107 | - | |
| 108 | - if (previousDate != null) { | |
| 109 | - // 检查相邻两个日期是否连续 | |
| 110 | - if (currentDate.minusDays(1).isEqual(previousDate)) { | |
| 111 | - consecutiveDays++; | |
| 112 | - } else { | |
| 113 | - // 如果日期不连续,停止遍历 | |
| 114 | - break; | |
| 115 | - } | |
| 116 | - } | |
| 117 | - | |
| 118 | - previousDate = currentDate; | |
| 119 | - } | |
| 120 | - | |
| 121 | - return consecutiveDays; | |
| 122 | - } | |
| 123 | - | |
| 124 | 87 | @Override |
| 125 | 88 | public PageResult<MemberSignInRecordDO> getSignInRecordPage(MemberSignInRecordPageReqVO pageReqVO) { |
| 126 | 89 | // 根据用户昵称查询出用户ids | ... | ... |