RenSheJuJob.java 7.09 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.ActivityDetectionMapper;
import com.subsidy.mapper.ImageCheckRecordMapper;
import com.subsidy.mapper.OprMemDictMapper;
import com.subsidy.model.ActivityDetectionDO;
import com.subsidy.model.ImageCheckRecordDO;
import com.subsidy.model.OprMemDictDO;
import com.subsidy.service.RenSheJuService;
import com.subsidy.util.IpAddressUtil;
import com.subsidy.util.websocket.WebSocketUtil;
import com.subsidy.vo.opr.InsertLastStudyRecordVO;
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.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;

    @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();
        }
    }

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


    /**
     * 强制用户用户下线(websocket强制下线)
     */
    @Scheduled(cron = "0 30 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 01 16 * * ?")
    @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());
            }
        }
    }

}