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.domain.GenericsAjaxResult;
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.product.domain.vo.PmsProductModuleDataVo;
import com.microservices.pms.product.domain.vo.PmsProductModuleUpdateVo;
import com.microservices.pms.product.domain.vo.TreeSelect;
import com.microservices.pms.product.service.IPmsProductModuleService;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseModuleInputVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseModuleSearchVo;
import com.microservices.pms.utils.PmsConstants;
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.*;

import java.util.List;

/**
 * 测试用例模块Controller
 *
 * @author otto
 * @date 2023-11-21
 */
@RestController
@Api(tags = "项目管理-测试用例模块相关接口")
@RequestMapping("/{enterpriseIdentifier}/pmsProjectTestcaseModule")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsProjectTestcaseModuleController extends BaseController {
    @Autowired
    private IPmsProductModuleService pmsProductModuleService;

    /**
     * 查询测试用例模块树状列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/treeList")
    @ApiOperation(value = "查测试用例模块树状列表")
    public GenericsAjaxResult<List<TreeSelect>> treeList(PmsProjectTestcaseModuleSearchVo pmsProjectTestcaseModuleSearchVo) {
        return genericsSuccess(pmsProductModuleService.selectPmsProductModuleTreeList(pmsProjectTestcaseModuleSearchVo.toPmsProductModule(), PmsConstants.TESTCASE_TAG_AND_MODULE_TYPE));
    }

    /**
     * 查询测试用例模块列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/list")
    @ApiOperation(value = "查询测试用例模块列表")
    public GenericsTableDataInfo<PmsProductModuleDataVo> list(PmsProjectTestcaseModuleSearchVo pmsProjectTestcaseModuleSearchVo) {
        startPage();
        return pmsProductModuleService.selectPmsProductModuleList(pmsProjectTestcaseModuleSearchVo.toPmsProductModule(), PmsConstants.TESTCASE_TAG_AND_MODULE_TYPE);
    }

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

    /**
     * 新增测试用例模块
     */
    @RequiresPermissions("pms:pmsProjectTestcaseModule:manage")
    @Log(title = "测试用例模块", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增测试用例模块")
    public AjaxResult add(@RequestBody @Validated PmsProjectTestcaseModuleInputVo pmsProjectTestcaseModuleInputVo) {
        return success(pmsProductModuleService.insertPmsProductModule(pmsProjectTestcaseModuleInputVo.toPmsProductModule(), PmsConstants.TESTCASE_TAG_AND_MODULE_TYPE));
    }

    /**
     * 同步产品模块到测试用例模块
     */
    @RequiresPermissions("pms:pmsProjectTestcaseModule:manage")
    @Log(title = "测试用例模块", businessType = BusinessType.INSERT)
    @PostMapping("/byProjectId/{projectId}/syncByProductModule")
    @ApiOperation(value = "同步产品模块到测试用例模块")
    public AjaxResult syncByProductModule(@PathVariable("projectId") Long projectId) {
        return toAjax(pmsProductModuleService.syncByProductModule(projectId));
    }

    /**
     * 修改测试用例模块
     */
    @RequiresPermissions("pms:pmsProjectTestcaseModule:manage")
    @Log(title = "测试用例模块", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改测试用例模块")
    public AjaxResult edit(@RequestBody @Validated PmsProductModuleUpdateVo pmsProductModuleUpdateVo) {
        return toAjax(pmsProductModuleService.updatePmsProductModule(pmsProductModuleUpdateVo.toPmsProductModule()));
    }

    /**
     * 删除测试用例模块
     */
    @RequiresPermissions("pms:pmsProjectTestcaseModule:manage")
    @Log(title = "测试用例模块", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除测试用例模块")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(pmsProductModuleService.deletePmsProductModuleById(id));
    }
}
