package org.matrix.autotest.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.matrix.database.entity.TestCase;
import org.matrix.database.service.ITestCaseService;
import org.matrix.database.vo.CommonResult;
import org.matrix.database.vo.CommonResultObj;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author mry
 * @since 2022-01-07
 */
@CrossOrigin
@RestController
@RequestMapping("/testCases")
@Api(tags = "对用例test_case的基本操作")
public class TestCaseController {

    private final ITestCaseService testCaseService;

    public TestCaseController(ITestCaseService testCaseService) {
        this.testCaseService = testCaseService;
    }

    /**
     * 分页查询所有用例
     *
     * @param pageSize 每页多少条数据
     * @param pageNum  当前第几页
     * @param name     用例名称
     * @param projectId 项目id
     * @return 分页查询的结果, 用例
     */
    @ApiOperation(value = "分页查询用例")
    @GetMapping("/{projectId}")
    public ResponseEntity<CommonResultObj<Page<TestCase>>> findPageConnects(
            int pageSize,
            int pageNum,
            String name, @PathVariable Long projectId) {
        Page<TestCase> results = Optional.ofNullable(testCaseService.page(Page.of(pageNum, pageSize)
                , Wrappers.lambdaQuery(TestCase.class).eq(TestCase::getProjectId, projectId)
                        .like(StringUtils.hasLength(name)
                                , TestCase::getName, name))).orElse(new Page<>());
        return results.getRecords().size() != 0
                ? CommonResult.success(results, "查询成功")
                : CommonResult.failed(results, "查询失败或无数据");
    }

    /**
     * 添加用例
     *
     * @param testCase 用例对象
     * @return 添加的用例, 以及是否添加成功的提示信息
     */
    @ApiOperation(value = "添加用例")
    @PostMapping
    public ResponseEntity<CommonResultObj<TestCase>> insertTestCase(@RequestBody TestCase testCase) {
        return CommonResult.pred(testCaseService::save, testCase
                , "添加成功", "添加失败");
    }

    /**
     * 修改用例
     *
     * @param testCase 用例对象
     * @return 修改后的用例, 以及是否修改成功的提示信息
     */
    @ApiOperation(value = "修改用例")
    @PutMapping
    public ResponseEntity<CommonResultObj<TestCase>> updateTestCase(@RequestBody TestCase testCase) {
        return CommonResult.pred(testCaseService::updateById, testCase
                , "修改成功", "修改失败");
    }

    /**
     * 删除用例
     *
     * @param id 用例id
     * @return 删除的用例id, 以及是否删除成功的提示信息
     */
    @ApiOperation(value = "根据主键id删除用例")
    @DeleteMapping("/{id}")
    public ResponseEntity<CommonResultObj<Long>> deleteTestCase(@PathVariable Long id) {
        return CommonResult.pred(testCaseService::removeById, id
                , "删除成功", "删除失败或id不存在");
    }

}
