package com.ruoyi.web.controller.tool;

import com.ruoyi.framework.annotation.RyLog;
import com.ruoyi.schedule.domain.SysJobLog;
import com.ruoyi.schedule.service.ISysJobLogService;
import org.noear.solon.annotation.*;
import org.noear.solon.auth.annotation.AuthPermissions;
import org.noear.solon.core.handle.Result;
import com.mybatisflex.core.query.QueryWrapper;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ExcelUtil;
import com.ruoyi.common.web.controller.BaseController;
import com.mybatisflex.core.paginate.Page;
import com.ruoyi.common.web.domain.PageData;

import java.util.Arrays;
import java.util.List;

/**
 * 定时任务调度日志 操作处理
 * @author ruoyi
 */
@Controller
@Mapping("tool/jobLog")
public class SysJobLogController extends BaseController {

    @Inject
    ISysJobLogService sysJobLogService;

    /**
     * 查询定时任务调度日志列表
     */
    @AuthPermissions("tool:job:list")
    @Get
    @Mapping("list")
    public PageData<SysJobLog> list(Page<SysJobLog> page, SysJobLog sysJobLog) {
        QueryWrapper qw = getQW(sysJobLog);
        Page<SysJobLog> result = sysJobLogService.page(page, qw);
        return getPageData(result);
    }

    /**
     * 导出定时任务调度日志列表
     */
    @RyLog(title = "定时任务调度日志", businessType = BusinessType.EXPORT)
    @AuthPermissions("tool:job:export")
    @Post
    @Mapping("export")
    public void export(SysJobLog sysJobLog) {
        QueryWrapper qw = getQW(sysJobLog);
        List<SysJobLog> list = sysJobLogService.list(qw);
        ExcelUtil.export(list);
    }

    /**
     * 删除定时任务调度日志
     */
    @RyLog(title = "定时任务调度日志", businessType = BusinessType.DELETE)
    @AuthPermissions("tool:job:remove")
    @Delete
    @Mapping("{jobLogIds}")
    public Result<Void> delete(@Path Long[] jobLogIds) {
        return toResult(sysJobLogService.removeByIds(Arrays.asList(jobLogIds)));
    }

    /**
     * 清空定时任务调度日志
     */
    @RyLog(title = "定时任务调度日志", businessType = BusinessType.CLEAN)
    @AuthPermissions("tool:job:remove")
    @Delete
    @Mapping("clean")
    public Result<Void> clean() {
        sysJobLogService.clean();
        return Result.succeed();
    }

    private QueryWrapper getQW(SysJobLog sysJobLog) {
        QueryWrapper qw = QueryWrapper.create();
        qw.and(SysJobLog::getJobName).like(sysJobLog.getJobName());
        qw.and(SysJobLog::getJobSimpleName).like(sysJobLog.getJobSimpleName());
        qw.and(SysJobLog::getJobGroup).eq(sysJobLog.getJobGroup());
        qw.and(SysJobLog::getStatus).eq(sysJobLog.getStatus());
        qw.and(SysJobLog::getCreateTime).ge(sysJobLog.getStartTime());
        qw.and(SysJobLog::getCreateTime).le(sysJobLog.getEndTime());
        qw.orderBy(SysJobLog::getCreateTime).desc();
        return qw;
    }

}
