package com.test.test.controller;

import com.test.common.annotation.Log;
import com.test.common.core.controller.BaseController;
import com.test.common.core.domain.AjaxResult;
import com.test.common.core.page.TableDataInfo;
import com.test.common.enums.BusinessType;
import com.test.common.utils.DateUtils;
import com.test.common.utils.SecurityUtils;
import com.test.test.domain.TestTask;
import com.test.test.domain.TestTaskLog;
import com.test.test.domain.TestTaskResult;
import com.test.test.domain.qo.GroupIdQO;
import com.test.test.domain.qo.IDQO;
import com.test.test.service.ITestTaskLogService;
import com.test.test.service.ITestTaskResultService;
import com.test.test.service.ITestTaskService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 定时任务Controller
 */
@RestController
@RequestMapping("/test/task")
public class TestTaskController extends BaseController {

    @Value("${test.jmeterHomePath:/opt/apache-jmeter}")
    private String jmeterHomePath;

    @Resource
    private ITestTaskService testTaskService;

    @Resource
    private ITestTaskResultService testTaskResultService;

    @Resource
    private ITestTaskLogService taskLogService;

    /**
     * 查询定时任务列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@Validated GroupIdQO qo) {
        startPage();
        List<TestTask> list = testTaskService.selectTestTaskList(qo);
        return getDataTable(list);
    }

    /**
     * 查询定时任务执行记录列表
     */
    @GetMapping("/taskResultListByTaskId")
    public TableDataInfo taskResultListByTaskId(@Validated IDQO qo) {
        startPage();
        TestTaskResult testTaskResult = new TestTaskResult();
        testTaskResult.setTaskId(qo.getId());
        List<TestTaskResult> list = testTaskResultService.selectTestTaskResultList(testTaskResult);
        return getDataTable(list);
    }

    /**
     * 查询定时任务操作记录列表
     */
    @GetMapping("/taskLogListByTaskId")
    public TableDataInfo taskLogListByTaskId(@Validated IDQO qo) {
        startPage();
        TestTaskLog testTaskLog = new TestTaskLog();
        testTaskLog.setTaskId(qo.getId());
        List<TestTaskLog> list = taskLogService.selectTestTaskLogList(testTaskLog);
        return getDataTable(list);
    }

    /**
     * 获取定时任务详细信息
     */
    @PostMapping(value = "/detail")
    public AjaxResult getInfo(@RequestBody IDQO qo) {
        return success(testTaskService.selectTestTaskById(qo.getId()));
    }

    /**
     * 新增定时任务
     */
    @Log(title = "定时任务", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TestTask testTask) {
        testTask.setCreateBy(getLoginUser().getUsername());
        testTask.setCreateTime(DateUtils.getNowDate());
        return toAjax(testTaskService.insertTestTask(testTask));
    }

    /**
     * 修改定时任务
     */
    @Log(title = "定时任务", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TestTask testTask) {
        return toAjax(testTaskService.updateTestTask(testTask));
    }

    /**
     * 删除定时任务
     */
    @Log(title = "定时任务", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult remove(@RequestBody IDQO qo) {
        return toAjax(testTaskService.deleteTestTaskById(qo.getId()));
    }

    /**
     * 执行任务
     */
    @Log(title = "任务", businessType = BusinessType.OTHER)
    @PostMapping("/run")
    public AjaxResult run(@RequestBody IDQO qo) {
        // 异步执行任务
        String username = SecurityUtils.getUsername();
        CompletableFuture.runAsync(() -> {
            testTaskService.executeTestTaskById(qo.getId(), 2, null, jmeterHomePath, username);
        });
        return toAjax(true);
    }


}
