package com.bonus.system.warning;

import com.bonus.system.api.domain.SysUser;
import com.bonus.system.service.ISysLogService;
import com.bonus.system.service.ISysUserService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Component
public class ScheduledTasks {

    private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    @Resource(name = "ISysLogService")
    private ISysLogService service;

    @Resource
    private ISysUserService userService;

    // 每5分钟执行一次
    @Scheduled(fixedRate = 300000)
    public void taskWithFixedRate() {
        service.logWarn();
        System.out.println("数据库容量固定速率任务执行时间：" + LocalDateTime.now().format(formatter));
    }

    // 每6个小时执行
    @Scheduled(fixedRate = 10*60*1000)
    public void taskToChangeUserStatus() {
        System.out.println("用户状态修正任务开始执行，执行时间：" + LocalDateTime.now().format(formatter));
        List<SysUser> sysUsers = userService.selectUserList4Job(new SysUser());
        for (SysUser sysUser : sysUsers) {
            userService.setUserStatus(sysUser);
            userService.updateUserStatus4Job(sysUser);
        }
    }

//    // 每天凌晨1点执行
//    @Scheduled(cron = "0 */5 * * * ?")
//    public void taskWithCron() {
//        System.out.println("定时任务执行时间：" + LocalDateTime.now().format(formatter));
//    }
}