package com.jiayue.ipp.idp.job;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jiayue.ipp.common.data.entity.WindTurbineStatusData;
import com.jiayue.ipp.idp.service.WindTurbineStatusDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * 清理风机数据执行任务
 *
 * @author xsl
 * @version 3.0
 */
@Slf4j
@Service @EnableScheduling public class DeleteFjDataJob {
  @Autowired
  WindTurbineStatusDataService windTurbineStatusDataService;

  @Scheduled(cron = "0 11 3 * * ?") public void executeFjDataDelete() {
    log.info("-----------------开始执行清理风机数据任务----------------------");
    // 日志保留60天
    int keepDays = -31;
    // 删除到截止日期
    DateTime dateTime = DateUtil.offsetDay(new Date(), keepDays);
    String deleteEndTime = dateTime.toString(DatePattern.NORM_DATE_FORMAT);
    LambdaQueryWrapper<WindTurbineStatusData> lambdaQueryWrapper = new LambdaQueryWrapper<>();
    lambdaQueryWrapper.lt(WindTurbineStatusData::getTime, DateUtil.parse(deleteEndTime));
    windTurbineStatusDataService.remove(lambdaQueryWrapper);
    log.info("-----------------执行清理风机数据任务完成----------------------");
  }
}
