Commit a74b24ef by 邓敏

签到数据同步到数据库和Redis中

1 parent d60823da
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;
/**
* <p>
......@@ -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<SignInRecordDO> 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<String> set = redisUtil.scan(RedisPrefixConstant.SUBSIDY_SIGN_INFO_PREFIX+"*");
if(set != null && set.size() > 0) {
redisUtil.del(set);
}
List<SignInRecordDO> 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);
}
}
}
......@@ -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<SignInRecordDO> {
......@@ -16,4 +18,5 @@ public interface SignInRecordMapper extends BaseMapper<SignInRecordDO> {
IPage<ClassSignInfoVO> classSignInfo(IPage iPage, String className, String courseName, Integer id);
List<SignInRecordDO> getSignInRecord();
}
......@@ -85,6 +85,21 @@ public class SignInRecordServiceImpl extends ServiceImpl<SignInRecordMapper, Sig
for (ClassDictDO classDictDO : classDictDOS) {
redisUtil.set(RedisPrefixConstant.SUBSIDY_SIGN_INFO_PREFIX + signInRecordDO.getMemberId() + ":classId:" + classDictDO.getId() + ":" + DateFormatUtil.format(new Date(), "yyyy-MM-dd") + ":" + System.currentTimeMillis(), null);
// 存储到缓存到同时保存到数据库
SignInRecordDO sign = this.baseMapper.selectOne(new QueryWrapper<SignInRecordDO>()
.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;
......
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();
}
}
......@@ -47,4 +47,16 @@
</if>
</select>
<select id="getSignInRecord" resultType="com.subsidy.model.SignInRecordDO">
SELECT
*
FROM
sign_in_record
WHERE
delete_date IS NULL
AND date(sign_in_date) = date_sub(
curdate(),
INTERVAL 1 DAY)
</select>
</mapper>
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!