From a74b24ef59690d360981cb9083fd270ff1c7fbfe Mon Sep 17 00:00:00 2001
From: DengMin <16679070171@qq.com>
Date: Tue, 8 Feb 2022 17:23:14 +0800
Subject: [PATCH] 签到数据同步到数据库和Redis中
---
src/main/java/com/subsidy/jobs/Scheduler.java | 44 ++++++++++++++++++++++++++++++++++++++++++++
src/main/java/com/subsidy/mapper/SignInRecordMapper.java | 3 +++
src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java | 15 +++++++++++++++
src/main/java/com/subsidy/util/DateFormatUtil.java | 24 ++++++++++++++++++++++++
src/main/resources/mapper/SignInRecordMapper.xml | 12 ++++++++++++
5 files changed, 98 insertions(+)
diff --git a/src/main/java/com/subsidy/jobs/Scheduler.java b/src/main/java/com/subsidy/jobs/Scheduler.java
index 856370a..80f78f0 100644
--- a/src/main/java/com/subsidy/jobs/Scheduler.java
+++ b/src/main/java/com/subsidy/jobs/Scheduler.java
@@ -1,15 +1,25 @@
package com.subsidy.jobs;
+import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
+import com.subsidy.common.RedisPrefixConstant;
import com.subsidy.mapper.OprAdmDictMapper;
import com.subsidy.mapper.OprMemDictMapper;
+import com.subsidy.mapper.SignInRecordMapper;
import com.subsidy.mapper.VodPlayHistoryMapper;
import com.subsidy.model.OprAdmDictDO;
+import com.subsidy.model.SignInRecordDO;
+import com.subsidy.util.DateFormatUtil;
import com.subsidy.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
+import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
+import org.springframework.transaction.annotation.Transactional;
+import java.util.ArrayList;
+import java.util.Date;
import java.util.List;
+import java.util.Set;
/**
*
@@ -33,6 +43,9 @@ public class Scheduler {
private VodPlayHistoryMapper vodPlayHistoryMapper;
@Autowired
+ private SignInRecordMapper signInRecordMapper;
+
+ @Autowired
private RedisUtil redisUtil;
@Autowired
@@ -103,4 +116,35 @@ public class Scheduler {
// }
// }
+ /**
+ * 定时同步Redis签到数据
+ * 1天前的
+ * 增量版
+ */
+ @Transactional(rollbackFor = Exception.class)
+ public void synchronizeSignInRecord() {
+ List list = signInRecordMapper.getSignInRecord();
+ if(list != null && list.size() > 0) {
+ for (SignInRecordDO signInRecordDO : list) {
+ redisUtil.set(RedisPrefixConstant.SUBSIDY_SIGN_INFO_PREFIX + signInRecordDO.getMemberId() + ":classId:" + signInRecordDO.getClassId() + ":" + DateFormatUtil.format(signInRecordDO.getSignInDate(), "yyyy-MM-dd") + ":" + DateFormatUtil.LocalDateTimeToSecond(signInRecordDO.getSignInDate()), null);
+ }
+ }
+ }
+
+ /**
+ * 全部同步Redis签到数据
+ */
+ @Transactional(rollbackFor = Exception.class)
+ public void synchronizeSignInRecordAll() {
+ Set set = redisUtil.scan(RedisPrefixConstant.SUBSIDY_SIGN_INFO_PREFIX+"*");
+ if(set != null && set.size() > 0) {
+ redisUtil.del(set);
+ }
+
+ List list = signInRecordMapper.selectList(new QueryWrapper<>());
+ for (SignInRecordDO signInRecordDO : list) {
+ redisUtil.set(RedisPrefixConstant.SUBSIDY_SIGN_INFO_PREFIX + signInRecordDO.getMemberId() + ":classId:" + signInRecordDO.getClassId() + ":" + DateFormatUtil.format(signInRecordDO.getSignInDate(), "yyyy-MM-dd") + ":" + DateFormatUtil.LocalDateTimeToSecond(signInRecordDO.getSignInDate()), null);
+ }
+
+ }
}
diff --git a/src/main/java/com/subsidy/mapper/SignInRecordMapper.java b/src/main/java/com/subsidy/mapper/SignInRecordMapper.java
index 11b1261..76960b1 100644
--- a/src/main/java/com/subsidy/mapper/SignInRecordMapper.java
+++ b/src/main/java/com/subsidy/mapper/SignInRecordMapper.java
@@ -6,6 +6,8 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.subsidy.vo.sign.ClassSignInfoVO;
import org.springframework.stereotype.Repository;
+import java.util.List;
+
@Repository
public interface SignInRecordMapper extends BaseMapper {
@@ -16,4 +18,5 @@ public interface SignInRecordMapper extends BaseMapper {
IPage classSignInfo(IPage iPage, String className, String courseName, Integer id);
+ List getSignInRecord();
}
diff --git a/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java b/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java
index 8026b68..141e014 100644
--- a/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java
+++ b/src/main/java/com/subsidy/service/impl/SignInRecordServiceImpl.java
@@ -85,6 +85,21 @@ public class SignInRecordServiceImpl extends ServiceImpl()
+ .lambda()
+ .eq(SignInRecordDO::getMemberId, signInRecordDO.getMemberId())
+ .eq(SignInRecordDO::getClassId, classDictDO.getId())
+ .like(SignInRecordDO::getSignInDate, DateFormatUtil.format(new Date(), "yyyy-MM-dd")));
+ if(sign != null) {
+ this.baseMapper.deleteById(sign.getId());
+ }
+
+ signInRecordDO.setClassId(classDictDO.getId());
+ signInRecordDO.setSignInDate(LocalDateTime.now());
+ signInRecordDO.setCreateDate(LocalDateTime.now());
+ this.baseMapper.insert(signInRecordDO);
}
return ConstantUtils.ADD_SUCCESS;
diff --git a/src/main/java/com/subsidy/util/DateFormatUtil.java b/src/main/java/com/subsidy/util/DateFormatUtil.java
index 65b9687..f39120e 100644
--- a/src/main/java/com/subsidy/util/DateFormatUtil.java
+++ b/src/main/java/com/subsidy/util/DateFormatUtil.java
@@ -1,5 +1,6 @@
package com.subsidy.util;
+import java.awt.SystemTray;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneId;
@@ -41,6 +42,22 @@ public class DateFormatUtil {
}
/**
+ * LocalDateTime转String,自定义格式
+ * @param localDateTime
+ * @param pattern
+ * @return
+ */
+ public static String format(LocalDateTime localDateTime, String pattern) {
+ try {
+ SimpleDateFormat sdf = new SimpleDateFormat(pattern);
+ return sdf.format(localDateTime);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return null;
+ }
+ }
+
+ /**
* String转Date,自定义格式
* @param date
* @param pattern
@@ -93,4 +110,11 @@ public class DateFormatUtil {
}
return LocalDateTime.now();
}
+
+ public static Long LocalDateTimeToSecond(LocalDateTime localDateTime) {
+ if(localDateTime != null) {
+ return localDateTime.atZone(ZoneOffset.ofHours(8)).toInstant().toEpochMilli();
+ }
+ return System.currentTimeMillis();
+ }
}
diff --git a/src/main/resources/mapper/SignInRecordMapper.xml b/src/main/resources/mapper/SignInRecordMapper.xml
index 2dc5322..17f66be 100644
--- a/src/main/resources/mapper/SignInRecordMapper.xml
+++ b/src/main/resources/mapper/SignInRecordMapper.xml
@@ -47,4 +47,16 @@
+
+
--
libgit2 0.25.0