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.Logical;
import com.microservices.common.security.annotation.RequiresPermissions;
import com.microservices.pms.product.domain.PmsProductRequirement;
import com.microservices.pms.product.domain.vo.*;
import com.microservices.pms.product.service.IPmsProductModuleService;
import com.microservices.pms.product.service.IPmsProductRequirementService;
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-10-10
 */
@RestController
@Api(tags = "项目管理-产品需求相关接口")
@RequestMapping("/{enterpriseIdentifier}/pmsProductRequirement")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "企业标识", paramType = "path", dataType = "String")
})
public class PmsProductRequirementController extends BaseController
{
    @Autowired
    private IPmsProductRequirementService pmsProductRequirementService;
    @Autowired
    private IPmsProductModuleService pmsProductModuleService;

    /**
     * 查询产品需求列表
     */
    @RequiresPermissions("pms:pmsProduct:show")
    @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,优先级：priorityId，开始时间：startTime，结束时间：endTime）", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "isAsc", value = "排序的方向desc或者asc,默认asc", paramType = "query", dataType = "String")
    })
    public GenericsTableDataInfo<PmsProductRequirementDataVo> list(@Validated PmsProductRequirementSearchVo pmsProductRequirementSearchVo)
    {
        PmsProductRequirement pmsProductRequirementSearch=pmsProductRequirementSearchVo.toPmsProductRequirement();

        if (pmsProductRequirementSearch.getPmsProductModuleId() != null && pmsProductRequirementSearch.getPmsProductModuleId() != 0) {
            List<Long> moduleIdList=pmsProductModuleService.selectChildrenPmsProductModuleIdList(pmsProductRequirementSearch.getPmsProductModuleId());
            pmsProductRequirementSearch.setChildrenModuleIdList(moduleIdList);
        }
        return pmsProductRequirementService.selectPmsProductRequirementPageList(pmsProductRequirementSearch);
    }

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

    /**
     * 根据产品标识和需求索引获取产品需求详细信息
     */
    @RequiresPermissions("pms:pmsProduct:show")
    @GetMapping(value = "/ProductIdentifier/{productIdentifier}/index/{index}")
    @ApiOperation(value = "根据产品标识和需求索引获取产品需求详细信息")
    public GenericsAjaxResult<PmsProductRequirementDetailVo> getInfo(@PathVariable("productIdentifier") String productIdentifier, @PathVariable("index") Long index)
    {
        return genericsSuccess(pmsProductRequirementService.selectPmsProductRequirementDetailByProductIdentifierAndIndex(productIdentifier, index));
    }

    /**
     * 新增产品需求
     */
    @RequiresPermissions("pms:pmsProductRequirement:add")
    @Log(title = "产品需求", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增产品需求")
    public AjaxResult add(@RequestBody @Validated  PmsProductRequirementInputVo pmsProductRequirementInputVo)
    {
        return success(pmsProductRequirementService.insertPmsProductRequirement(pmsProductRequirementInputVo.toPmsProductRequirement()));
    }

    /**
     * 修改产品需求
     */
    @RequiresPermissions(value = {"pms:pmsProductRequirement:edit", "pms:pmsProductPlan:edit"}, logical = Logical.OR)
    @Log(title = "产品需求", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改产品需求")
    public AjaxResult edit(@RequestBody @Validated PmsProductRequirementUpdateVo pmsProductRequirementUpdateVo)
    {
        return toAjax(pmsProductRequirementService.updatePmsProductRequirement(pmsProductRequirementUpdateVo.toPmsProductRequirement()));
    }

    /**
     * 批量修改产品需求
     */
    @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" +
                    "priorityId:关联优先级ID;\n" +
                    "statusId:关联状态ID;\n" +
                    "pmsProductModuleId:关联产品模块ID;\n" +
                    "pmsProductPlanId:关联产品计划ID;\n" +
                    "tags:标记Id列表，使用逗号分割的字符串;\n" +
                    "requirementAssigneeGitlinkIds:需求负责人Gitlink ID列表（逗号分隔）;\n" +
                    "pmsProjectId:关联项目ID\n")
            @RequestBody JSONObject pmsProductRequirementBatchUpdateVo)
    {
        return toAjax(pmsProductRequirementService.batchUpdatePmsProductRequirement(pmsProductRequirementBatchUpdateVo));
    }

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