upd sing method
parent
888ccac2ed
commit
b458abf461
|
@ -8,6 +8,8 @@ import org.springframework.beans.factory.annotation.Autowired;
|
|||
import org.springframework.data.annotation.CreatedDate;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.ZoneId;
|
||||
import java.util.Calendar;
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
@ -29,41 +31,54 @@ public class SysSignServiceImpl implements SysSignService {
|
|||
|
||||
@Override
|
||||
public Sign searchSignByUserId(Long userId) {
|
||||
//获取当天的日期 是第几日
|
||||
int today = new Date().getDate();
|
||||
System.out.println("当天是:"+today);
|
||||
//先从redis中获取连续签到次数
|
||||
|
||||
LocalDate today = LocalDate.now();
|
||||
System.out.println("当天是:" + today);
|
||||
|
||||
// 先从 Redis 中获取连续签到次数
|
||||
if (redisService.hasKey("sign:" + userId)) {
|
||||
Sign redisSign = redisService.getCacheObject("sign:" + userId);
|
||||
Date signTime = redisSign.getSignTime();
|
||||
LocalDate redisDate = signTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
|
||||
// 如果 Redis 缓存中的日期与当前日期相等,则认为客户当天已经签到过
|
||||
if (redisDate.isEqual(today)) {
|
||||
redisSign.setStatus(1);
|
||||
return redisSign;
|
||||
}
|
||||
//redis没有从数据库中获取
|
||||
//查询出当前登录人的最近一条的签到记录
|
||||
Sign sign = sysSignMapper.searchSignByUserId(userId);
|
||||
|
||||
//获取最近一次签到的日期 是第几日
|
||||
int recentDay = sign.getSignTime().getDate();
|
||||
System.out.println("最近签到是"+recentDay);
|
||||
//判断是否已经断签了
|
||||
if (today-recentDay!=1){
|
||||
System.out.println("断签了!!!!");
|
||||
sign.setSignAmount(0);
|
||||
//其次将签到次数signAmount改为0
|
||||
sysSignMapper.updateSingAmountByUserId(sign);
|
||||
System.out.println(sign);
|
||||
return sign;
|
||||
}
|
||||
|
||||
// 从数据库中获取最近一次签到记录
|
||||
Sign sign = sysSignMapper.searchSignByUserId(userId);
|
||||
Date recentTime = sign.getSignTime();
|
||||
LocalDate recentDate = recentTime.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
|
||||
System.out.println("最近签到是:" + recentDate);
|
||||
|
||||
// 判断是否已经断签
|
||||
if (recentDate.isEqual(today)) {
|
||||
// 今天已经签到过,只是 Redis 缓存故障导致数据丢失
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
// 将时间设置为第二天凌晨0点
|
||||
long expireTime = getExpireTime(calendar);
|
||||
sign.setStatus(1);
|
||||
redisService.setCacheObject("sign:" + userId, sign, expireTime, TimeUnit.SECONDS);
|
||||
} else if (today.minusDays(1).isEqual(recentDate)) {
|
||||
// 证明最近一次签到是昨天,今天目前为止不算断签,Redis 没有值也是因为数据丢失
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
long expireTime = getExpireTime(calendar);
|
||||
System.out.println("没有断签!!!");
|
||||
System.out.println(sign);
|
||||
//没有断签 直接返回当前查询出来的签到记录
|
||||
// 没有断签,直接返回当前查询出来的签到记录
|
||||
} else {
|
||||
// 断签了,需要将签到次数清零,并更新数据库中的签到次数
|
||||
System.out.println("断签了!!!!");
|
||||
sign.setSignAmount(0);
|
||||
sysSignMapper.updateSingAmountByUserId(sign);
|
||||
}
|
||||
|
||||
return sign;
|
||||
}
|
||||
|
||||
|
||||
private long getExpireTime(Calendar calendar) {
|
||||
calendar.set(Calendar.HOUR_OF_DAY, 0);
|
||||
calendar.set(Calendar.MINUTE, 0);
|
||||
|
@ -75,7 +90,7 @@ public class SysSignServiceImpl implements SysSignService {
|
|||
|
||||
@Override
|
||||
public void signByUserId(Long userId) {
|
||||
Sign sign = this.searchSignByUserId(userId);
|
||||
Sign sign = sysSignMapper.searchSignByUserId(userId);
|
||||
sign.setSignAmount(sign.getSignAmount()+1);
|
||||
sysSignMapper.signByUserId(sign);
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
|
|
Loading…
Reference in New Issue