package com.microservices.pms.project.controller;

import com.alibaba.fastjson2.JSONObject;
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.service.IPmsProductModuleService;
import com.microservices.pms.project.domain.PmsProjectTestcase;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseDataVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseInputVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseSearchVo;
import com.microservices.pms.project.domain.vo.PmsProjectTestcaseUpdateVo;
import com.microservices.pms.project.service.IPmsProjectTestcaseService;
import io.swagger.annotations.*;
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-22
 */
@RestController
@Api(tags = "项目管理-测试用例管理")
@RequestMapping("/{enterpriseIdentifier}/pmsProjectTestcase")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsProjectTestcaseController extends BaseController {
    @Autowired
    private IPmsProjectTestcaseService pmsProjectTestcaseService;
    @Autowired
    private IPmsProductModuleService pmsProductModuleService;

    /**
     * 查询测试用例管理列表
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping("/list")
    @ApiOperation(value = "查询测试用例列表")
    @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<PmsProjectTestcaseDataVo> list(@Validated PmsProjectTestcaseSearchVo pmsProjectTestcaseSearchVo) {
        PmsProjectTestcase pmsProjectTestcase = pmsProjectTestcaseSearchVo.toPmsProjectTestcase();
        if (pmsProjectTestcase.getPmsModuleId() != null && pmsProjectTestcase.getPmsModuleId() != 0) {
            List<Long> moduleIdList = pmsProductModuleService.selectChildrenPmsProductModuleIdList(pmsProjectTestcase.getPmsModuleId());
            pmsProjectTestcase.setChildrenModuleIdList(moduleIdList);
        }
        return pmsProjectTestcaseService.selectPmsProjectTestcasePageList(pmsProjectTestcase);
    }

    /**
     * 获取测试用例管理详细信息
     */
    @RequiresPermissions(value = {"pms:pmsProject:show", "pms:pmsProject:showMe"}, logical = Logical.OR)
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取测试用例详细信息")
    public GenericsAjaxResult<PmsProjectTestcaseDataVo> getInfo(@PathVariable("id") Long id) {
        return genericsSuccess(pmsProjectTestcaseService.selectPmsProjectTestcaseDataVoById(id));
    }

    /**
     * 新增测试用例管理
     */
    @RequiresPermissions("pms:pmsProjectTestcase:add")
    @Log(title = "测试用例管理", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增测试用例")
    public AjaxResult add(@RequestBody @Validated PmsProjectTestcaseInputVo pmsProjectTestcaseInputVo) {
        return success(pmsProjectTestcaseService.insertPmsProjectTestcase(pmsProjectTestcaseInputVo.toPmsProjectTestcase()));
    }

    /**
     * 修改测试用例管理
     */
    @RequiresPermissions("pms:pmsProjectTestcase:edit")
    @Log(title = "测试用例管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改测试用例")
    public AjaxResult edit(@RequestBody @Validated PmsProjectTestcaseUpdateVo pmsProjectTestcaseUpdateVo) {
        return toAjax(pmsProjectTestcaseService.updatePmsProjectTestcase(pmsProjectTestcaseUpdateVo.toPmsProjectTestcase()));
    }

    /**
     * 删除测试用例管理
     */
    @RequiresPermissions("pms:pmsProjectTestcase:remove")
    @Log(title = "测试用例管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除测试用例")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsProjectTestcaseService.deletePmsProjectTestcaseByIds(ids));
    }

    /**
     * 批量修改测试用例
     */
    @RequiresPermissions("pms:pmsProjectTestcase:edit")
    @Log(title = "测试用例管理", businessType = BusinessType.UPDATE)
    @PutMapping("/batch")
    @ApiOperation(value = "批量修改测试用例")
    public AjaxResult batchEdit(
            @ApiParam(name = "batchUpdateVo",
                    value = "【字段说明】(非必填字段不传递则代表不更新，传null则代表置空):\n" +
                            "idList:需求Id列表（必填）,示例：[1,2,3];\n" +
                            "assigneeGitlinkId:用例维护人Gitlink ID;\n" +
                            "typeId:用例类型ID;\n" +
                            "tags:标记Id列表，使用逗号分割的字符串;\n" +
                            "productReqSpecsId:关联产品需求规格id;\n" +
                            "pmsModuleId:关联模块ID;\n")
            @RequestBody JSONObject batchUpdateVo) {
        return toAjax(pmsProjectTestcaseService.batchUpdatePmsProjectTestcase(batchUpdateVo));
    }
}
