package com.ruoyi.cron.task;

import cn.hutool.core.util.RandomUtil;
import com.ruoyi.cron.TaskLog;
import com.ruoyi.cron.annotation.Cron;
import com.ruoyi.cron.annotation.CronComponent;
import com.ruoyi.cron.annotation.CronParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@CronComponent
@RequiredArgsConstructor
public class CronTaskTestTask {

  private final TaskLog log;

  @Cron("测试任务一")
  public void test() {
    log.log("测试任务一执行中...");
    if (RandomUtil.randomBoolean()) {
      throw new RuntimeException("测试异常");
    }
  }

  public String name1() {
    return "自己方法执行的值";
  }

  public String name2() {
    return "Springbean执行的值";
  }

  @Cron(value = "测试任务二", order = 2, group = 0)
  public void test2(@CronParam("测试日期参数") Date date, @CronParam("测试bean参数") String s1, String s2, int args) {
    log.log("测试任务二执行中...{},{},{},{}", date, s1, s2, args);
    if (RandomUtil.randomBoolean()) {
      throw new RuntimeException("测试异常");
    }
  }

  @Cron("测试任务三：耗时很长")
  public void test3()throws Exception {
    log.log("开始执行");
    for(int i=0;i<100;i++){
      if(RandomUtil.randomInt(100)>95){
        log.log("完成： {}%",new Exception("测试执行过程异常"),i+1);
      }else{
        log.log("完成： {}%",i+1);
      }
      TimeUnit.SECONDS.sleep(RandomUtil.randomInt(1,3));
    }
    log.log("完成");
  }
}
