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.uuid.IdUtils;
import com.test.test.domain.TestCase;
import com.test.test.domain.TestPlanCase;
import com.test.test.domain.qo.IDQO;
import com.test.test.domain.qo.TestCaseListQO;
import com.test.test.service.ITestCaseService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
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
 */
@Slf4j
@RestController
@RequestMapping("/test/case")
public class TestCaseController extends BaseController {

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

    @Resource
    private ITestCaseService testCaseService;

    /**
     * 查询用例列表
     */
    @GetMapping("/list")
    public TableDataInfo list(@Validated TestCaseListQO qo) {
        startPage();
        List<TestCase> list = testCaseService.selectTestCaseList(qo);
        return getDataTable(list);
    }

    /**
     * 查询测试计划可关联的用例列表
     */
    @GetMapping("/planRelateList")
    public TableDataInfo planRelateList(TestCaseListQO qo) {
        startPage();
        List<TestCase> list = testCaseService.selectValidTestPlanCaseList(qo);
        return getDataTable(list);
    }

    /**
     * 获取用例详细信息
     */
    @PostMapping(value = "/detail")
    public AjaxResult getInfo(@RequestBody IDQO qo) {
        return success(testCaseService.selectTestCaseById(qo.getId()));
    }

    /**
     * 新增用例
     */
    @Log(title = "用例", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TestCase testCase) {
        testCase.setCreateBy(getLoginUser().getUsername());
        testCase.setCreateTime(DateUtils.getNowDate());
        testCase.setStatus(1L);
        return toAjax(testCaseService.insertTestCase(testCase));
    }

    /**
     * 修改用例
     */
    @Log(title = "用例", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TestCase testCase) {
        return toAjax(testCaseService.updateTestCase(testCase));
    }

    /**
     * 删除用例
     */
    @Log(title = "用例", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult remove(@RequestBody IDQO qo) {
        return toAjax(testCaseService.deleteTestCaseById(qo.getId()));
    }

    /**
     * 异步执行用例
     */
    @Log(title = "用例", businessType = BusinessType.OTHER)
    @PostMapping("/run")
    public AjaxResult run(@RequestBody IDQO qo) {
        log.info("执行用例 id:{}, jmeterHomePath:{}", qo.getId(), jmeterHomePath);
        String caseSid = IdUtils.simpleUUID();
        // 异步执行任务
        CompletableFuture.runAsync(() -> {
            testCaseService.executeTestCaseById(qo.getId(), jmeterHomePath, caseSid);
        });
        return toAjax(true);
    }

    /**
     * 同步执行用例
     */
    @Log(title = "用例", businessType = BusinessType.OTHER)
    @PostMapping("/runSync")
    public AjaxResult runSync(@RequestBody IDQO qo) {
        log.info("同步执行用例 id:{}, jmeterHomePath:{}", qo.getId(), jmeterHomePath);
        String caseSid = IdUtils.simpleUUID();
        testCaseService.executeTestCaseById(qo.getId(), jmeterHomePath, caseSid);
        return AjaxResult.success("执行成功", caseSid);
    }

    /**
     * 异步执行测试计划用例
     */
    @Log(title = "用例", businessType = BusinessType.OTHER)
    @PostMapping("/runTestPlanCase")
    public AjaxResult runTestPlanCase(@RequestBody TestPlanCase testPlanCase) {
        String caseSid = IdUtils.simpleUUID();
        // 异步执行任务
        CompletableFuture.runAsync(() -> {
            testCaseService.executeTestCaseByPlanCase(testPlanCase, jmeterHomePath, caseSid);
        });
        return toAjax(true);
    }
}
