package cn.hanghui.outapi.platform.tenant.server.job;

import cn.hanghui.outapi.platform.tenant.entity.po.SysOperLog;
import cn.hanghui.outapi.platform.tenant.mapper.SysOperLogMapper;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.alipay.api.domain.UserInfoDetail;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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 javax.annotation.Resource;
import java.util.Date;


@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
@EnableAsync
@Slf4j
@RequiredArgsConstructor(onConstructor_ = @__(@Autowired))
public class ClearSysOperLogTask {

    private final SysOperLogMapper sysOperLogMapper;

    @Async("ThreadPoolExecutor")
    @Scheduled(cron = "0 0 0 * * ? ")
    public void userTask() {
        log.info("----------开始------清除一个月前的操作日志-------------");
        String monthAgo = DateUtil.formatDate(DateUtil.offsetMonth(new Date(), -1));//一个月前
        int delete = sysOperLogMapper.delete(
                Wrappers.<SysOperLog>lambdaQuery()
                        .lt(SysOperLog::getOperTime, monthAgo)
        );
        log.info("删除的数量：{}",delete);
        log.info("----------完成------清除一个月前的操作日志-------------");
    }
}