package com.imdroid.beidou.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.imdroid.secapi.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/***
 * 定时任务：
 * 1、定时清理消息表（缺省保留3个月）
 * 2、定时清理原始数据表（缺省保留1个月）
 * 3、定时清理操作日志（缺省保留1年）
 * 4、定时检查设备是否在线
 * 5、定时检查FwdRecords(1 year)
 */

@Slf4j
@Configuration
@EnableScheduling
public class DatasetCleaner {
    final DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Autowired
    GnssMsgMapper gnssMsgMapper;
    @Autowired
    GnssStatusMsgMapper statusMsgMapper;
    @Autowired
    GnssTrxMsgMapper trxMsgMapper;
     @Autowired
    ResendRecordMapper resendRecordMapper;
    @Autowired
    FwdRecordMapper fwdRecordMapper;

    @Autowired
    GnssCalcDataMapper calcDataMapper;
    @Autowired
    OpLogMapper opLogMapper;

    //cron表达式格式：
    //{秒数} {分钟} {小时} {日期} {月份} {星期} {年份(可为空)}

    @Scheduled(cron = "0 10 0 * * *") // 每天凌晨执行一次(0:10:0)
    //@Scheduled(cron = "0 */2 * * * ?") // 每60分钟执行一次
    //@Scheduled(cron = "*/5 * * * * ?") // 每5秒执行一次
    public void dayTask() {
        checkMsgDataset();
        checkFwdDataset();
        checkStatusDataset();
        checkGnssDataDataset();
        checkLogDataset();
    }

    void checkLogDataset(){
        long before = 180;
        QueryWrapper<OperationLog> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("op_time", LocalDateTime.now().minusDays(before).format(dateFormatter));
        int count = opLogMapper.delete(queryWrapper);
        log.info("clean operation dataset num: "+count);
    }

    void checkMsgDataset(){
        long before = 30;
        QueryWrapper<GnssMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("createtime", LocalDateTime.now().minusDays(before).format(dateFormatter));
        int count = gnssMsgMapper.delete(queryWrapper);
        log.info("clean msg dataset num: "+count);
    }

    void checkGnssDataDataset(){
        long before = 180;
        QueryWrapper<GnssCalcData> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("createtime", LocalDateTime.now().minusDays(before).format(dateFormatter));
        int count = calcDataMapper.delete(queryWrapper);
        log.info("clean gnss dataset num: "+count);
    }

    void checkStatusDataset(){
        long before = 90;

        QueryWrapper<GnssStatusMsg> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("createtime", LocalDateTime.now().minusDays(before).format(dateFormatter));
        int count = statusMsgMapper.delete(queryWrapper);
        log.info("clean status msg dataset num: "+count);

        QueryWrapper<GnssTrxMsg> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.le("createtime", LocalDateTime.now().minusDays(before).format(dateFormatter));
        count = trxMsgMapper.delete(queryWrapper1);
        log.info("clean trx msg dataset num: "+count);
    }

    void checkFwdDataset(){
        long before = 180;

        QueryWrapper<FwdRecord> queryWrapper = new QueryWrapper<>();
        queryWrapper.le("createtime", LocalDateTime.now().minusDays(before).format(dateFormatter));
        int count = fwdRecordMapper.delete(queryWrapper);
        log.info("clean fwd dataset num: "+count);

        QueryWrapper<ResendRecord> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.le("createtime", LocalDateTime.now().minusDays(before).format(dateFormatter));
        count = resendRecordMapper.delete(queryWrapper1);
        log.info("clean resend dataset num: "+count);
    }
}