RenSheJuJob.java 12.2 KB
package com.subsidy.jobs;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.subsidy.common.ResponseData;
import com.subsidy.mapper.*;
import com.subsidy.model.*;
import com.subsidy.service.RenSheJuService;
import com.subsidy.util.IpAddressUtil;
import com.subsidy.util.websocket.WebSocketUtil;
import com.subsidy.vo.opr.InsertLastStudyRecordVO;
import com.subsidy.vo.renshe.DailyAnswerVO;
import com.subsidy.vo.vod.DailyClassMembersVO;
import com.subsidy.vo.vod.VodPlayStateVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 企业职工线上培训
 */
@Component
public class RenSheJuJob {


    @Autowired
    private RenSheJuService renSheJuService;

    @Autowired
    private OprMemDictMapper oprMemDictMapper;

    @Autowired
    private ImageCheckRecordMapper imageCheckRecordMapper;

    @Autowired
    private ActivityDetectionMapper activityDetectionMapper;

    @Autowired
    private VodPlayHistoryMapper vodPlayHistoryMapper;

    @Autowired
    private SignInRecordMapper signInRecordMapper;

    @Autowired
    private ClassMemberMappingMapper classMemberMappingMapper;

    @Autowired
    private VodDictMapper vodDictMapper;

    @Value("${spring.profiles.active}")
    private String env;


    /**
//     * POST-2:班级基本信息信息采集接口
//     */
    @Scheduled(cron = "0 0 1 * * ?")
    public void classBaseInfo() throws IOException {
        if ("prod".equals(env)) {
            renSheJuService.classBaseInfo();
        }
    }

    /**
     * POST-3:学时信息采集接口
     */
    @Scheduled(cron = "0 5 1 * * ?")
    public void classHourBehavior() throws IOException {
        if ("prod".equals(env)) {
            renSheJuService.classHourBehavior();
        }
    }

    /**
     * POST-4:考试信息采集接口
     */
    @Scheduled(cron = "0 10 1 * * ?")
    public void uploadChapterBehavior() throws IOException {
        if ("prod".equals(env)) {
            renSheJuService.uploadChapterBehavior();
        }
    }

    /**
     * POST-5:答疑辅导采集接口
     */
    @Scheduled(cron = "0 15 1 * * ?")
    public void uploadClassAnswerQuestionBehavior() throws IOException {
        if ("prod".equals(env)) {
            renSheJuService.uploadClassAnswerQuestionBehavior();
        }
    }

    /**
     * POST-6 班级活跃度/实名认证照片信息采集接口
     */
    @Scheduled(cron = "0 20 1 * * ?")
    public void uploadImage() throws IOException {
        if ("prod".equals(env)) {
            renSheJuService.uploadImage();
        }
    }

    /**
     * POST-7 获取培训待绑定的(班级编号,项目编号)列表
     */
    @Scheduled(cron = "0 25 1 * * ?")
    public void getClassCodeByPrivateKey() throws IOException {
        if ("prod".equals(env)) {
            renSheJuService.getClassCodeByPrivateKey();
        }
    }

    /**
     * POST-8 上下游班级数据绑定接口
     */
    @Scheduled(cron = "0 30 1 * * ?")
    public void uploadClassCode() throws IOException {
        if ("prod".equals(env)) {
            renSheJuService.uploadClassCode();
        }
        System.gc();
    }

//    /**
//     * POST-9 获取推送失败班级列表
//     */
//    @Scheduled(cron = "0 35 1 * * ?")
//    public void getErrorClass()throws IOException{
//        renSheJuService.getErrorClass();
//    }

//    /**
//     * 定时gc
//     */
//    @Scheduled(cron = "0 10 16 * * ?")
//    public void gc(){
//        Runtime.getRuntime().gc();
//    }


    /**
     * 强制用户用户下线(websocket强制下线)
     */
    @Scheduled(cron = "0 00 23 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void logout() {
        //让当前用户下线
        ConcurrentHashMap<Long, WebSocketSession> webSocketMap = WebSocketUtil.webSocketMap;

        //查看当天登录过的人最后一次活跃数据
        List<InsertLastStudyRecordVO> insertLastStudyRecordVOS = oprMemDictMapper.insertLastStudyRecord();

        for (InsertLastStudyRecordVO ilsr : insertLastStudyRecordVOS) {

            try {
                if (null != webSocketMap.get(ilsr.getMemberId())) {
                    webSocketMap.get(ilsr.getMemberId()).sendMessage(new TextMessage(JSONObject.toJSONString(ResponseData.generateCreatedResponse(17001))));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 补当天有学习记录但没有签到的签到数据
     */
    @Scheduled(cron = "0 10 23 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void  fillSignInRecord(){

        //查找有视频的学生
        List<DailyClassMembersVO> dailyClassMembersVOS = vodPlayHistoryMapper.dailyClassMembers();
        for (DailyClassMembersVO dailyClassMembersVO : dailyClassMembersVOS){
            try {
                //去看签到表当天有没有数据,没有的话就要补
                SignInRecordDO dailySignInRecord = signInRecordMapper.getDailySignInRecord(dailyClassMembersVO.getMemberId(), dailyClassMembersVO.getClassId());
                if (null == dailySignInRecord){
                    //没有签到数据,补
                    List<VodPlayHistoryDO> vodPlayHistoryDOS = vodPlayHistoryMapper.exPlayInfoAsc(dailyClassMembersVO.getMemberId(), dailyClassMembersVO.getClassId());
                    List<SignInRecordDO> signInRecordDOS = signInRecordMapper.selectList(new QueryWrapper<SignInRecordDO>()
                            .lambda()
                            .eq(SignInRecordDO::getMemberId,dailyClassMembersVO.getMemberId())
                            .eq(SignInRecordDO::getClassId,dailyClassMembersVO.getClassId())
                            .orderByDesc(SignInRecordDO::getSignInDate));
                    SignInRecordDO signInRecordDO = signInRecordDOS.get(0);
                    LocalDateTime localDateTime = vodPlayHistoryDOS.get(0).getCreateDate()
                            .minusSeconds(vodPlayHistoryDOS.get(0).getPlayLength())
                            .minusSeconds(vodPlayHistoryDOS.get(0).getSuspendLength())
                            .minusSeconds(random0to9());
                    signInRecordMapper.insertRecord(dailyClassMembersVO.getClassId(),dailyClassMembersVO.getMemberId(),localDateTime,signInRecordDO.getIpAddress(),signInRecordDO.getDeviceNo(),localDateTime);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }



    @Scheduled(cron = "0 15 23 * * ?")
    public void clearErrorData(){
        //查看当天产生视频学习的班级
        List<Long> classIds = vodPlayHistoryMapper.dailyVodRecord();

        HashMap<Long, HashSet<Long>> hashMap = new HashMap();

        for (Long classId : classIds) {
            List<ClassMemberMappingDO> classMemberMappingDOS = classMemberMappingMapper.selectList(new QueryWrapper<ClassMemberMappingDO>()
                    .lambda()
                    .eq(ClassMemberMappingDO::getClassId, classId));
            HashSet<Long> memberIds = new HashSet<>();
            for (ClassMemberMappingDO classMemberMappingDO : classMemberMappingDOS) {
                List<VodPlayStateVO> vodPlayStateVOS = vodDictMapper.testPlays(classMemberMappingDO.getMemberId(), Long.valueOf(classId));
                for (int i = 0; i < vodPlayStateVOS.size() - 1; i++) {
                    VodPlayStateVO vodPlayStateVO = vodPlayStateVOS.get(i + 1); //后面
                    VodPlayStateVO vodPlayStateVO1 = vodPlayStateVOS.get(i);    //前面
                    if (vodPlayStateVO1.getEndDate().getTime() > vodPlayStateVO.getStartDate().getTime()) {
                        memberIds.add(vodPlayStateVO.getMemberId());
                    }
                }
            }
            hashMap.put(classId, memberIds);
        }

        for (Long clsId : hashMap.keySet()) {
            HashSet<Long> memberIds = hashMap.get(clsId);
            for (Long memberId : memberIds) {
                //查看这个人这个班当天的签到时间
                SignInRecordDO signInRecordDO = signInRecordMapper.getDailySignInRecord(memberId, clsId);
                if (null != signInRecordDO) {
                    LocalDateTime signInDate = signInRecordDO.getSignInDate();
                    //查看这个人当天的学习数据
                    List<VodPlayHistoryDO> vodPlayHistoryDOS = vodPlayHistoryMapper.exPlayInfoAsc(memberId, clsId);
                    //从签到时间开始依次“展开”数据
                    int currentLength = 0;
                    for (VodPlayHistoryDO vodPlayHistoryDO : vodPlayHistoryDOS) {
                        currentLength += random0to9() + vodPlayHistoryDO.getPlayLength() + vodPlayHistoryDO.getSuspendLength();
                        LocalDateTime newCreateDate = signInDate.plusSeconds(currentLength);
                        vodPlayHistoryMapper.updatePlayRecord(vodPlayHistoryDO.getId(), newCreateDate);
                    }
                }
            }
        }
    }

    /**
     * 随机0-9秒
     */
    public int random0to9() {
        return (int) (Math.random() * 10+3);
    }

    /**
     * 删掉有登录数据 做过人脸 没有学习数据(视频+考试+答疑)
     */
    @Scheduled(cron = "0 35 23 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void deleteNoStudyMembers() {

        //查看登陆过做过人脸,但是没视频记录的人
        List<ImageCheckRecordDO> vodHistoryMembers = imageCheckRecordMapper.noVodHistoryMembers();
        vodHistoryMembers.stream().forEach(x -> {
            imageCheckRecordMapper.deleteById(x.getId());

            ActivityDetectionDO activityDetectionDO = activityDetectionMapper.selectOne(new QueryWrapper<ActivityDetectionDO>()
                    .lambda()
                    .eq(ActivityDetectionDO::getFaceCheckId, x.getId()));
            if (null != activityDetectionDO) {
                activityDetectionMapper.deleteById(activityDetectionDO.getId());
            }
        });

        //查看登陆过做过人脸,但是没考试记录的人
        List<ImageCheckRecordDO> examHistoryMembers = imageCheckRecordMapper.noExamHistoryMembers();
        examHistoryMembers.stream().forEach(x -> {
            imageCheckRecordMapper.deleteById(x.getId());

            ActivityDetectionDO activityDetectionDO = activityDetectionMapper.selectOne(new QueryWrapper<ActivityDetectionDO>()
                    .lambda()
                    .eq(ActivityDetectionDO::getFaceCheckId, x.getId()));
            if (null != activityDetectionDO) {
                activityDetectionMapper.deleteById(activityDetectionDO.getId());
            }
        });
    }


    /**
     * 补登录登出数据
     */
    @Scheduled(cron = "0 40 23 * * ?")
    @Transactional(rollbackFor = Exception.class)
    public void cancelLogin() {
        //让当前用户下线
//        ConcurrentHashMap<Long, WebSocketSession> webSocketMap = WebSocketUtil.webSocketMap;

        //查看当天登录过的人最后一次活跃数据
        List<InsertLastStudyRecordVO> insertLastStudyRecordVOS = oprMemDictMapper.insertLastStudyRecord();
        for (InsertLastStudyRecordVO ilsr : insertLastStudyRecordVOS) {
            oprMemDictMapper.insertOrUpdate(ilsr.getMemberId(), "登录", 1, ilsr.getIpAddress(), ilsr.getSignInDate());

            if (null == ilsr.getMt()) {
                //补23:58的数据
                oprMemDictMapper.insertOrUpdate(ilsr.getMemberId(), "登出", 1, null, new Date());
            } else {
                //最后一次学习的数据
                oprMemDictMapper.insertOrUpdate(ilsr.getMemberId(), "登出", 1, null, ilsr.getMt());
            }
        }
    }

}