package com.microservices.pms.product.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.RequiresPermissions;
import com.microservices.pms.product.domain.vo.*;
import com.microservices.pms.product.service.IPmsProductRequirementService;
import com.microservices.pms.project.domain.PmsProject;
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;

/**
 * 产品需求相关接口
 *
 * @author otto
 * @date 2023-10-10
 */
@RestController
@Api(tags = "项目管理-需求相关接口")
@RequestMapping("/{enterpriseIdentifier}/productRequirementSpecs")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class ProductReqSpecsController extends BaseController {
    @Autowired
    private IPmsProductRequirementService pmsProductRequirementService;

    /**
     * 查询产品需求列表
     */
    @ApiOperation(value = "查询需求列表")
    @RequiresPermissions("pms:pmsProduct:show")
    @GetMapping("/list")
    public GenericsTableDataInfo<ProductReqSpecsDataVo> list(@PathVariable String enterpriseIdentifier, @Validated ProductReqSpecsSearchVo productReqSpecsSearchVo) {
        return pmsProductRequirementService.selectReqSpecsList(productReqSpecsSearchVo);
    }

    /**
     * 获取需求详细信息
     */
    @RequiresPermissions("pms:pmsProduct:show")
    @GetMapping(value = "/{id}")
    @ApiOperation(value = "获取需求详细信息")
    public GenericsAjaxResult<ProductReqSpecsDetailVo> getInfo(@PathVariable("id") Long id) {
        return genericsSuccess(pmsProductRequirementService.selectReqSpecsDetailById(id));
    }

    /**
     * 新增需求
     */
    @RequiresPermissions("pms:pmsProductRequirement:add")
    @Log(title = "需求", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增需求")
    public AjaxResult add(@PathVariable String enterpriseIdentifier,
                          @RequestBody @Validated ProductReqSpecsInputVo productReqSpecsInputVo) {
        productReqSpecsInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return success(pmsProductRequirementService.insertReqSpecs(productReqSpecsInputVo));
    }

    /**
     * 修改需求
     */
    @RequiresPermissions("pms:pmsProductRequirement:edit")
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改需求")
    public AjaxResult edit(@RequestBody @Validated ProductReqSpecsUpdateVo productReqSpecsUpdateVo) {
        return toAjax(pmsProductRequirementService.updateReqSpecs(productReqSpecsUpdateVo));
    }

    /**
     * 变更需求
     */
    @RequiresPermissions("pms:pmsProductRequirement:edit")
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PutMapping("/change")
    @ApiOperation(value = "变更需求")
    public AjaxResult change(@RequestBody @Validated ProductReqSpecsChangeVo productReqSpecsChangeVo) {
        return toAjax(pmsProductRequirementService.changeReqSpecs(productReqSpecsChangeVo));
    }

    /**
     * 删除需求
     */
    @RequiresPermissions("pms:pmsProductRequirement:remove")
    @Log(title = "需求", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    @ApiOperation(value = "删除需求")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pmsProductRequirementService.deleteReqSpecsByIds(ids));
    }

    /**
     * 评审需求
     */
    @RequiresPermissions("pms:pmsProductRequirement:edit")
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PutMapping("/review")
    @ApiOperation(value = "评审需求")
    public AjaxResult review(@RequestBody @Validated ProductReqReviewVo productReqReviewVo) {
        return toAjax(pmsProductRequirementService.reviewReqSpecs(productReqReviewVo));
    }

    /**
     * 需求纳入计划
     */
    @RequiresPermissions("pms:pmsProductRequirement:edit")
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PutMapping("/activePlan")
    @ApiOperation(value = "需求纳入计划")
    public AjaxResult activePlan(@PathVariable String enterpriseIdentifier, @RequestBody @Validated ProductReqSpecsActivePlanVo productReqSpecsActivePlanVo) {
        productReqSpecsActivePlanVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return toAjax(pmsProductRequirementService.activePlan(productReqSpecsActivePlanVo));
    }

    /**
     * 纳入计划时可选择项目列表
     */
    @ApiOperation(value = "纳入计划时可选择项目列表")
    @RequiresPermissions("pms:pmsProduct:show")
    @GetMapping("/{productIdentifier}/projectList")
    public List<PmsProject> projectList(@PathVariable("productIdentifier") String productIdentifier) {
        return pmsProductRequirementService.selectReqSpecsProjectList(productIdentifier);
    }

    /**
     * 查询需求状态列表
     */
    @ApiOperation(value = "查询需求状态列表")
    @RequiresPermissions("pms:pmsProduct:show")
    @GetMapping("/statusList")
    public List<ReqStatusCommonVo> statusList() {
        return pmsProductRequirementService.selectReqSpecsStatusList();
    }

    /**
     * 查询需求类型列表
     */
    @ApiOperation(value = "查询需求类型列表")
    @RequiresPermissions("pms:pmsProduct:show")
    @GetMapping("/typeList")
    public List<EnumCommonVo> typeList() {
        return pmsProductRequirementService.selectReqSpecsTypeList();
    }

    /**
     * 批量修改产品需求
     */
    @RequiresPermissions("pms:pmsProductRequirement:edit")
    @Log(title = "需求", businessType = BusinessType.UPDATE)
    @PutMapping("/batch")
    @ApiOperation(value = "批量修改产品需求")
    public AjaxResult batchEdit(
            @ApiParam(name = "pmsProductRequirementBatchUpdateVo",
                    value = "【字段说明】(非必填字段不传递则代表不更新，传null则代表置空):\n" +
                            "idList:需求Id列表（必填）,示例：[1,2,3];\n" +
                            "pmsProductModuleId:关联产品模块ID;\n" +
                            "pmsProductPlanId:关联产品里程碑ID;\n")
            @RequestBody JSONObject pmsProductRequirementBatchUpdateVo) {
        return toAjax(pmsProductRequirementService.batchUpdatePmsProductRequirement(pmsProductRequirementBatchUpdateVo));
    }
}
