package com.microservices.pms.project.controller;

import com.microservices.common.core.web.controller.BaseController;
import com.microservices.common.core.web.domain.AjaxResult;
import com.microservices.common.core.web.page.GenericsTableDataInfo;
import com.microservices.common.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.common.security.annotation.Logical;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.pms.project.domain.PmsProjectTestsheetCases;
import com.microservices.pms.project.domain.vo.*;
import com.microservices.pms.project.service.IPmsProjectTestsheetCasesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 测试单执行用例Controller
 *
 * @author xxq
 * @date 2023-11-24
 */
@Api(tags = "项目管理-测试单执行用例")
@RestController
@RequestMapping("/{enterpriseIdentifier}/pmsProjectTestsheetCases")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsProjectTestsheetCasesController extends BaseController
{
    @Autowired
    private IPmsProjectTestsheetCasesService pmsProjectTestsheetCasesService;


    /**
     * 查询测试单执行用例列表
     */
    @ApiOperation(value = "查询测试单执行用例列表")
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/list")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "当前记录起始索引", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页显示记录数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "orderByColumn", value = "排序列（更新时间：updateTime，创建时间：createTime）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<PmsProjectTestsheetCaseVo> list(@Validated PmsProjectTestsheetCaseSearchVo searchVo)
    {
        return  pmsProjectTestsheetCasesService.selectPmsProjectTestsheetCasesList(searchVo);
    }

    /**
     * 获取测试单执行用例详细信息
     */
    @ApiOperation(value = "获取测试单执行用例详细信息")
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(pmsProjectTestsheetCasesService.selectPmsProjectTestsheetCasesVoById(id));
    }

    /**
     * 新增测试单执行用例
     */
    @ApiOperation(value = "增加测试单执行用例")
    @RequiresPermissions("pms:pmsProjectTestsheet:edit")
    @Log(title = "测试单执行用例", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody PmsProjectTestsheetCaseInputVo addCaseVo)
    {
        return toAjax(pmsProjectTestsheetCasesService.insertPmsProjectTestsheetCases(addCaseVo));
    }

    /**
     * 执行测试单执行用例
     */
    @ApiOperation(value = "执行测试单执行用例")
    @RequiresPermissions("pms:pmsProjectTestsheet:edit")
    @Log(title = "测试单执行用例", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody PmsProjectTestsheetCaseExecVo testsheetCaseExecVo)
    {
        return toAjax(pmsProjectTestsheetCasesService.updatePmsProjectTestsheetCases(testsheetCaseExecVo));
    }

    /**
     * 批量修改测试单执行用例
     */
    @RequiresPermissions("pms:pmsProjectTestsheet:edit")
    @PutMapping("/batchUpdate")
    @ApiOperation(value = "批量修改测试单执行用例")
    public AjaxResult batchUpdate(@Validated @RequestBody PmsProjectTestsheetCasesBatchUpdateVo batchUpdateVo) {
        batchUpdateVo.getIds().forEach(id->{
            PmsProjectTestsheetCases testsheetCasesDb = pmsProjectTestsheetCasesService.selectPmsProjectTestsheetCasesById(id);
            if (null != testsheetCasesDb) {
                PmsProjectTestsheetCaseExecVo testsheetCaseExecVo = new PmsProjectTestsheetCaseExecVo();
                testsheetCaseExecVo.setId(id);
                testsheetCaseExecVo.setTesterId(batchUpdateVo.getTesterId() == null ? testsheetCasesDb.getTesterId() : batchUpdateVo.getTesterId());
                testsheetCaseExecVo.setTestStatus(batchUpdateVo.getTestStatus() == null ? testsheetCasesDb.getTestStatus() : batchUpdateVo.getTestStatus());
                pmsProjectTestsheetCasesService.updatePmsProjectTestsheetCases(testsheetCaseExecVo);
            }
        });
        return success();
    }

    /**
     * 删除测试单执行用例
     */
    @RequiresPermissions("pms:pmsProjectTestsheet:edit")
    @Log(title = "测试单执行用例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation(value = "批量删除测试单执行用例")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(pmsProjectTestsheetCasesService.deletePmsProjectTestsheetCasesByIds(ids));
    }
}
