package com.ozs.web.core.util;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ozs.entity.VehiclePosition;
import com.ozs.mapper.VehiclePositionMapper;
import com.ozs.system.domain.SysLoginInfo;
import com.ozs.system.domain.SysOperLog;
import com.ozs.system.mapper.SysLoginInfoMapper;
import com.ozs.system.mapper.SysOperLogMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableScheduling
@EnableAsync
public class DeleteDataUtil {

    private static final Logger log = LoggerFactory.getLogger(DeleteDataUtil.class);
    @Autowired
    ThreadPoolExecutor executor;
    @Autowired
    private VehiclePositionMapper vehiclePositionMapper;


    @Async
    @Scheduled(cron = "0 0 0 * * ?")
    public void tableCreationAndDelete() {
        log.info("开始执行 tableCreationAndDelete 任务");
        // 计算当前时间往前推 60 天的日期
        LocalDateTime ninetyDaysAgo = LocalDateTime.now().minus(1, ChronoUnit.DAYS);

         CompletableFuture.supplyAsync(() -> {
            try {
                deleteVehiclePosition(ninetyDaysAgo);
                return 1;
            } catch (Exception e) {
                log.error("执行 createTable 方法时出现异常", e);
                throw new RuntimeException(e);
            }
        }, executor);
    }


    public String deleteVehiclePosition(LocalDateTime ninetyDaysAgo) {
        log.info("开始执行---deleteSysOperLogData");
        int deletedCount = vehiclePositionMapper.delete(new LambdaQueryWrapper<VehiclePosition>().le(VehiclePosition::getCreateTime, ninetyDaysAgo));

        // 可以根据需要添加日志记录
        System.out.println("deleteSysOperLogData方法成功删除 " + deletedCount + " 条登录记录。");
        return "数据删除完成";
    }

}
