SchedulerJob.java 5.51 KB
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>
 *  定时器
 * </p>
 *
 * @author DengMin
 * @since 2021/12/21
 */

@Component
public class SchedulerJob {

    @Autowired
    private OprAdmDictMapper oprAdmDictMapper;

    @Autowired
    private OprMemDictMapper oprMemDictMapper;

    @Autowired
    private VodPlayHistoryMapper vodPlayHistoryMapper;

    @Autowired
    private SignInRecordMapper signInRecordMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private MongoTemplate mongoTemplate;
    /**
        删除十天前登录记录(管理端)
     */
    //@Scheduled(cron = "0 42 18 * * ?")
    public void delOprAdmRecord() {
        List<OprAdmDictDO> list = oprAdmDictMapper.getRecordByDate();
        if(list != null) {
            for (OprAdmDictDO oprAdmDictDO : list) {
                //oprAdmDictMapper.deleteById(oprAdmDictDO.getId());
            }
        }
    }

    ///**
    // * 删除十天前登录记录(学员)
    // */
    ////@Scheduled(cron = "0 42 18 * * ?")
    //public void delOprMemRecord() {
    //    List<OprMemDictDO> list = oprMemDictMapper.getRecordByDate();
    //    if(list != null) {
    //        for (OprMemDictDO oprMemDictDO : list) {
    //            oprMemDictMapper.deleteById(oprMemDictDO.getId());
    //        }
    //    }
    //}

    ///**
    // * 用户数据审计记录添加(学员)
    // */
    ////@Scheduled(cron = "0 39 18 * * ?")
    //public void getOprMemRecord() {
    //    Set<String> set = redisUtil.scan("*memberLogin*");
    //    if(set != null) {
    //        for (String str : set) {
    //            int result = (Integer) redisUtil.get(str);
    //            OprMemDictDO oprMemDictDO = new OprMemDictDO();
    //            String [] opr = str.split("_");
    //            LocalDateTime createDate = DateFormatUtil.secondToLocalDateTime(Long.valueOf(opr[3]));
    //            oprMemDictDO.setResult(result);
    //            oprMemDictDO.setOprType("登录");
    //            oprMemDictDO.setUserId(Long.valueOf(opr[2]));
    //            oprMemDictDO.setCreateDate(createDate);
    //            oprMemDictMapper.insert(oprMemDictDO);
    //            redisUtil.del(str);
    //        }
    //    }
    //}

    /**
     * 视频播放记录缩量
     */
//    @Scheduled(cron = "0 48 14 * * ?")
//    @Transactional(rollbackFor = Exception.class)
//    public void condenseVodPlayHistory() {
//        List<VodPlayHistoryDO> list = vodPlayHistoryMapper.getVodPlayHistory();
//        vodPlayHistoryMapper.delete(null);
//        //List<VodPlayHistoryDO> vodPlayHistoryList = vodPlayHistoryMapper.selectList(null);
//        //for (VodPlayHistoryDO vodPlayHistoryDO : vodPlayHistoryList) {
//            vodPlayHistoryMapper.deleteById(null);
//        //}
//
//        for (VodPlayHistoryDO vodPlayHistoryDO : list) {
//            vodPlayHistoryMapper.insert(vodPlayHistoryDO);
//        }
//    }

    /**
     * 定时同步Redis签到数据
     * 1天前的
     * 增量版
     */
    @Transactional(rollbackFor = Exception.class)
    public void synchronizeSignInRecord() {
        List<SignInRecordDO> list = signInRecordMapper.getSignInRecord();
        if(list != null && list.size() > 0) {
            for (SignInRecordDO signInRecordDO : list) {
                Set<String> set = redisUtil.scan(RedisPrefixConstant.SUBSIDY_SIGN_INFO_PREFIX + signInRecordDO.getMemberId() + ":classId:" + signInRecordDO.getClassId() + ":" + DateFormatUtil.format(signInRecordDO.getSignInDate(), "yyyy-MM-dd")+":*");
                if(set != null && set.size() > 0) {
                    redisUtil.del(set);
                }
                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);
        }

    }
}