package com.skr.mdm.task;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import com.skr.mdm.bean.bo.CompanyOnlineBO;
import com.skr.mdm.dao.AuCompanyStatisticsDao;
import com.skr.mdm.dao.DeviceEquipmentDao;
import com.skr.mdm.dao.SysCleanDao;
import com.skr.mdm.dao.SysSendMsgUserDao;
import com.skr.mdm.entity.SysCleanDO;
import com.skr.mdm.publish.MdmEventPublisher;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;

/**
 * @ProjectName: mdm_new
 * @Package: com.skr.mdm.task
 * @ClassName: ScheduledTimedTask
 * @Author: Tty
 * @Description: 定时任务
 * @Date: 2020/11/26 10:01
 * @Version: 1.0
 */
@Component
@EnableScheduling
@EnableAsync
@Slf4j
public class ScheduledTimedTask {

    @Autowired
    private DeviceEquipmentDao deviceEquipmentDao;
    @Autowired
    private AuCompanyStatisticsDao auCompanyStatisticsDao;

    @Autowired
    private SysCleanDao sysCleanDao;
    @Autowired
    private MdmEventPublisher mdmEventPublisher;
    @Autowired
    private SysSendMsgUserDao sendMsgUserDao;

    /**
     * 公司在线设备 获取当天数据
     * 59 58 23 * * ?  每晚 23:58:59
     */
    @Scheduled(cron = "59 58 23 * * ?")
    public void updateCompanyDeviceOnline() {
        log.info("====================定时统计当天在线设备 开始=================");
        TimeInterval timer = DateUtil.timer();
        LocalDate now = LocalDate.now();
        List<CompanyOnlineBO> companyOnlineBOList = deviceEquipmentDao.selectCompanyOnlineList(now.toString());
        if (CollectionUtils.isNotEmpty(companyOnlineBOList)) {
            auCompanyStatisticsDao.insertSelectiveByTime(now.toString(), companyOnlineBOList);
        }
        log.info("====================定时统计当天在线设备 完成 耗时：{}毫秒=================", timer.intervalMs());
    }

    /**
     * 定时清理指令用户表
     */
    @Scheduled(cron = "0 0 2 * * ?")
    public void cleanSysMsgUser() {
        TimeInterval timer = DateUtil.timer();
        // 默认保留90天的数据
        LocalDateTime expireDateTime = LocalDateTime.now().minus(90, ChronoUnit.DAYS);
        String expireDate = DateUtil.format(expireDateTime, DatePattern.NORM_DATE_PATTERN);
        List<Integer> listExpireDelId = sendMsgUserDao.listExpireDelId(expireDate);
        if (CollectionUtils.isNotEmpty(listExpireDelId)) {
            sendMsgUserDao.deleteBatchIds(listExpireDelId);
            log.info("===========================定时清理指令用户表完成 清理：{}条数据 耗时：{}毫秒==========================",
                    listExpireDelId.size(), timer.intervalMs());
        }
    }

    /**
     * 定时清理部分表超出阈值的数据
     */
    @Scheduled(cron = "0 0 3 * * ?")
    public void cleanData() {
        SysCleanDO sysCleanDO = sysCleanDao.selectById(1);
        if (sysCleanDO == null) {
            return;
        }
        if (sysCleanDO.getIsEnabled().equals(1)) {
            log.info("======================系统数据定时清理已启用 开始清理=======================");
            TimeInterval timer = DateUtil.timer();
            mdmEventPublisher.publishDataBaseCleanEvent(sysCleanDO.getMaxLimit());
            mdmEventPublisher.publishMinIoFileCleanEvent(sysCleanDO.getFileRetentionDays());
            log.info("=============================系统数据定时清理已启用 清理完成 耗时：{}毫秒============================", timer.intervalMs());
            return;
        }
        log.info("=====================系统数据定时清理未启用===================");
    }

}