修正连续天数的判断问题

This commit is contained in:
LittleBoy 2022-11-21 15:54:20 +08:00
parent 28bb3fd5d5
commit 24cbd183de
3 changed files with 34 additions and 0 deletions

View File

@ -7,10 +7,13 @@ import me.xiaoyan.point.api.service.SignRecordService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("sign")
public class SignController {
@Resource
private SignRecordService signRecordService;
@ApiOperation("今日签到")

View File

@ -51,6 +51,14 @@ public class SignRecordServiceImpl extends ServiceImpl<SignRecordMapper, SignRec
List<Date> dates = new ArrayList<>();
if (isSign) {
dates.add(new Date());
}else{
// 没有记录直接返回0
if(signRecords.size() == 0) return 0;
Date firstDate = signRecords.get(0).getCreateTime();
// 第一个记录是否是今天
if(!DateUtils.isToday(firstDate) && !DateUtils.isYesterday(firstDate)){
return 0;
}
}
signRecords.forEach(it -> dates.add(it.getCreateTime()));
Date[] dateArr = new Date[dates.size()];

View File

@ -2,6 +2,7 @@ package me.xiaoyan.point.api.util;
import lombok.SneakyThrows;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
@ -31,6 +32,9 @@ public class DateUtils {
}
public static int continuousDays(Date[] dates) {
if(dates.length == 0){
return 0;
}
int totalDays = 1;
for (int i = 1;i < dates.length; i ++){
Date d1 = dates[i-1];
@ -43,6 +47,25 @@ public class DateUtils {
return totalDays;
}
public static boolean isToday(String d){
try {
return isToday(full.parse(d));
} catch (ParseException e) {
return false;
}
}
public static boolean isToday(Date d){
if(d == null) return false;
return today().equalsIgnoreCase(formatDate(d));
}
public static boolean isYesterday(Date d){
if(d == null) return false;
Date today = new Date(); // 今天
if(today.getTime() < d.getTime()) return false;
return isBetween(today,d);
}
public static boolean isBetween(Date d1,Date d2){
// 直接获取两个日期之间的毫秒,计算
Date date1 = parse(formatDate(d1) + " 00:00:00");