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.enums.BusinessType;
import com.test.common.utils.DateUtils;
import com.test.test.domain.TestCaseStep;
import com.test.test.domain.qo.IDQO;
import com.test.test.domain.qo.TestCaseStepAddQO;
import com.test.test.service.ITestCaseStepService;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 步骤Controller
 */
@RestController
@RequestMapping("/test/caseStep")
public class TestCaseStepController extends BaseController {
    @Resource
    private ITestCaseStepService testCaseStepService;

    /**
     * 查询步骤列表
     */
    @GetMapping("/list")
    public AjaxResult list(@Validated TestCaseStep qo) {
        return success(testCaseStepService.selectTestCaseStepList(qo));
    }

    /**
     * 获取步骤详细信息
     */
    @PostMapping(value = "/detail")
    public AjaxResult getInfo(@RequestBody IDQO qo) {
        return success(testCaseStepService.selectTestCaseStepById(qo.getId()));
    }

    /**
     * 新增步骤
     */
    @Log(title = "步骤", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody TestCaseStepAddQO qo) {
        Date nowDate = DateUtils.getNowDate();
        qo.getList().forEach(testCaseStep -> {
            testCaseStep.setCreateBy(getLoginUser().getUsername());
            testCaseStep.setCreateTime(nowDate);
        });
        return toAjax(testCaseStepService.insertBatchTestCaseStep(qo));
    }

    /**
     * 修改步骤
     */
    @Log(title = "步骤", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    public AjaxResult edit(@RequestBody TestCaseStep testCaseStep) {
        if (testCaseStep.getId() == null) {
            return null;
        }
        return toAjax(testCaseStepService.updateTestCaseStep(testCaseStep));
    }

    /**
     * 删除步骤
     */
    @Log(title = "步骤", businessType = BusinessType.DELETE)
    @PostMapping("/del")
    public AjaxResult remove(@RequestBody IDQO qo) {
        return toAjax(testCaseStepService.deleteTestCaseStepById(qo.getId()));
    }
}
