import request from '@/utils/request';
import { JobLogQuery, JobLogVO, JobLogDetail } from './types';
import { JobInfoVO } from '@/api/xxl-job/job/types';
import { AxiosPromise } from 'axios';

// 查询调度日志列表
export function listJobLog(query: JobLogQuery): AxiosPromise<JobLogVO[]> {
  return request({
    url: '/xxl-job/joblog/pageList',
    method: 'get',
    params: {
      pageNum: query.pageNum,
      pageSize: query.pageSize,
      jobGroup: query.jobGroup,
      jobId: query.jobId,
      logStatus: query.logStatus,
      startTime: query.startTime,
      endTime: query.endTime
    }
  });
}

// 根据执行器组获取任务列表
export function getJobsByGroup(jobGroup: number): AxiosPromise<JobInfoVO[]> {
  return request({
    url: '/xxl-job/joblog/getJobsByGroup',
    method: 'get',
    params: { jobGroup }
  });
}

// 查询调度日志详情
export function getJobLogDetail(id: string | number): AxiosPromise<JobLogDetail> {
  return request({
    url: `/xxl-job/joblog/${id}`,
    method: 'get'
  });
}

// 获取执行日志内容
export function getJobLogContent(id: string | number, fromLineNum: number = 1): AxiosPromise<any> {
  return request({
    url: '/xxl-job/joblog/logDetailCat',
    method: 'get',
    params: {
      id,
      fromLineNum
    }
  });
}

// 删除调度日志
export function delJobLog(ids: (string | number)[]) {
  return request({
    url: '/xxl-job/joblog/remove',
    method: 'post',
    data: { ids }
  });
}

// 清空日志（根据条件）
export function clearJobLog(jobGroup: number, jobId: number, type: number) {
  return request({
    url: '/xxl-job/joblog/clearLog',
    method: 'post',
    data: {
      jobGroup,
      jobId,
      type // 1-清空一个月前 2-清空三个月前 3-清空六个月前 4-清空一年前 5-清空一千条以前 6-清空一万条以前 7-清空三万条以前 8-清空十万条以前 9-清空全部
    }
  });
}

// 终止运行中的任务
export function killJob(id: string | number) {
  return request({
    url: '/xxl-job/joblog/logKill',
    method: 'post',
    data: { id }
  });
}
