package com.microservices.pms.product.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.PmsProductDataVo;
import com.microservices.pms.product.domain.vo.PmsProductInputVo;
import com.microservices.pms.product.domain.vo.PmsProductSearchVo;
import com.microservices.pms.product.domain.vo.PmsProductUpdateVo;
import com.microservices.pms.product.service.IPmsProductService;
import com.microservices.system.api.domain.SimpleSysUser;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

    /**
     * 查询产品列表
     */
    @RequiresPermissions(value = {"pms:pmsProduct:show", "pms:pmsProject:add", "pms:pmsProject:edit"}, logical = Logical.OR)
    @GetMapping("/list")
    @ApiOperation(value = "查询产品列表")
    public GenericsTableDataInfo<PmsProductDataVo> list(
            @PathVariable String enterpriseIdentifier,
            @Validated PmsProductSearchVo pmsProductSearchVo) {
        startPage();
        pmsProductSearchVo.setPmsEnterpriseIdentifier(enterpriseIdentifier);
        return pmsProductService.selectPmsProductPageList(pmsProductSearchVo);
    }

    /**
     * 获取产品负责人可选用户列表
     */
    @GetMapping("/userList")
    @ApiOperation("获取产品负责人可选用户列表")
    public GenericsTableDataInfo<SimpleSysUser> getUserList(
            @ApiParam(name = "enterpriseIdentifier", value = "企业标识") @PathVariable String enterpriseIdentifier,
            @ApiParam(name = "search", value = "模糊查询") String search) {
        return pmsProductService.getUserList(enterpriseIdentifier, search);
    }

    /**
     * 通过产品标识获取产品详细信息
     */
    @RequiresPermissions("pms:pmsProduct:show")
    @GetMapping(value = "/{identifier}")
    @ApiOperation(value = "通过产品标识获取产品详细信息（路由更新删除/ByIdentifier）")
    public GenericsAjaxResult<PmsProductDataVo> getInfo(@PathVariable("identifier") String identifier) {
        return genericsSuccess(pmsProductService.selectPmsProductDataByIdentifier(identifier));
    }

    /**
     * 新增产品
     */
    @RequiresPermissions("pms:pmsProduct:add")
    @Log(title = "产品", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增产品（返回产品标识）")
    public AjaxResult add(
            @ApiParam(name = "enterpriseIdentifier", value = "企业标识") @PathVariable String enterpriseIdentifier,
            @RequestBody @Validated PmsProductInputVo pmsProductInputVo) {
        return success((Object) pmsProductService.insertPmsProduct(pmsProductInputVo.toPmsProduct(enterpriseIdentifier)));
    }

    /**
     * 修改产品
     */
    @RequiresPermissions("pms:pmsProduct:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改产品")
    public AjaxResult edit(@RequestBody @Validated PmsProductUpdateVo pmsProductUpdateVo) {
        return toAjax(pmsProductService.updatePmsProduct(pmsProductUpdateVo.toPmsProduct()));
    }

    /**
     * 置顶/取消置顶产品
     */
    @RequiresPermissions("pms:pmsProduct:edit")
    @Log(title = "产品", businessType = BusinessType.UPDATE)
    @PutMapping("/operateTop/{identifier}")
    @ApiOperation(value = "置顶/取消置顶产品")
    public AjaxResult edit(@PathVariable("identifier") String identifier) {
        return toAjax(pmsProductService.updatePmsProductTop(identifier));
    }

    /**
     * 通过产品标识删除产品
     */
    @RequiresPermissions("pms:pmsProduct:remove")
    @Log(title = "产品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{identifier}")
    @ApiOperation(value = "通过产品标识删除产品（路由更新删除/ByIdentifier）")
    public AjaxResult removeByIdentifier(@PathVariable String identifier) {
        return toAjax(pmsProductService.deletePmsProductByIdentifier(identifier));
    }
}
