package com.microservices.pms.productLibrary.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.pms.productLibrary.domain.vo.*;
import com.microservices.pms.productLibrary.service.IPmsProductLibraryComponentService;
import com.microservices.pms.productLibrary.service.IPmsProductLibraryService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 制品库-仓库Controller
 *
 * @author otto
 * @date 2024-06-20
 */
@RestController
@Api(tags = "产品库-相关接口")
@RequestMapping("/{enterpriseIdentifier}/pmsProductLibrary")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "组织标识", paramType = "path", dataType = "String")
})
public class PmsProductLibraryController extends BaseController {
    @Autowired
    private IPmsProductLibraryService pmsProductLibraryService;
    @Autowired
    private IPmsProductLibraryComponentService pmsProductLibraryComponentService;

    /**
     * 获取产品库制品列表
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:list")
    @GetMapping("/componentList")
    @ApiOperation(value = "获取产品库制品列表")
    public GenericsTableDataInfo<PmsProductLibraryComponentDataVo> selectComponentList(
            @PathVariable String enterpriseIdentifier,
            @ApiParam(value = "产品库制品查询条件") PmsProductLibraryComponentSearchVo search) {
        search.setPmsEnterpriseIdentifier(enterpriseIdentifier);
        startPage();
        return pmsProductLibraryService.selectComponentList(search);
    }

    /**
     * 查询制品库-仓库列表
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:list")
    @GetMapping("/byRepoName/{repoName}")
    @ApiOperation(value = "获取产品库详情")
    public GenericsAjaxResult<PmsProductLibraryRepositoriesDataVo> getByRepoName(@PathVariable String enterpriseIdentifier,
                                                                                 @ApiParam(value = "产品库标识", required = true) @PathVariable String repoName) {
        return GenericsAjaxResult.success(pmsProductLibraryService.selectPmsProductLibraryDataByName(enterpriseIdentifier, repoName));
    }

    /**
     * 查询产品库列表
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:list")
    @GetMapping("/list")
    @ApiOperation(value = "查询产品库列表")
    public GenericsTableDataInfo<PmsProductLibrarySimpleVo> list(
            @PathVariable String enterpriseIdentifier,
            @ApiParam("模糊搜索产品库标识") String name) {
        startPage();
        return pmsProductLibraryService.selectPmsProductLibraryPageList(enterpriseIdentifier, name);
    }

    /**
     * 新增产品库
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:add")
    @Log(title = "产品库", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增产品库")
    public AjaxResult addProduct(@PathVariable String enterpriseIdentifier,
                                 @Valid @RequestBody ProductRepositoriesInputVo productRepositoriesInputVo) {
        productRepositoriesInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return toAjax(pmsProductLibraryService.insertPmsProductLibrary(productRepositoriesInputVo));
    }

    /**
     * 更新产品库
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:edit")
    @Log(title = "产品库", businessType = BusinessType.UPDATE)
    @PutMapping("/{name}")
    @ApiOperation(value = "更新产品库")
    public AjaxResult editProduct(@PathVariable String enterpriseIdentifier,
                                  @ApiParam(value = "产品库标识", required = true) @PathVariable String name,
                                  @Valid @RequestBody ProductRepositoriesOperateVo productRepositoriesOperateVo) {
        productRepositoriesOperateVo.setEnterpriseIdentifier(enterpriseIdentifier);
        productRepositoriesOperateVo.setName(name);
        return toAjax(pmsProductLibraryService.updatePmsProductLibrary(productRepositoriesOperateVo));
    }

    /**
     * 删除产品库
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:remove")
    @Log(title = "产品库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{name}")
    @ApiOperation(value = "删除产品库")
    public AjaxResult remove(@PathVariable String enterpriseIdentifier,
                             @ApiParam(value = "产品库标识", required = true) @PathVariable String name) {
        return toAjax(pmsProductLibraryService.deletePmsProductLibraryByName(enterpriseIdentifier, name));
    }


    /**
     * 上传产品库制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "产品库制品", businessType = BusinessType.INSERT)
    @PostMapping("/{name}")
    @ApiOperation(value = "上传知识库文档")
    public AjaxResult uploadProductAsset(@PathVariable String enterpriseIdentifier,
                                         @ApiParam(value = "产品库标识", required = true) @PathVariable String name,
                                         @Valid @RequestBody ProductComponentInputVo productComponentInputVo) {
        productComponentInputVo.setProductRepoName(name);
        productComponentInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return toAjax(pmsProductLibraryService.uploadProductAsset(productComponentInputVo));
    }

    /**
     * 产品库查询制品列表
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:list")
    @GetMapping("/{repoName}/component/list")
    @ApiOperation(value = "产品库查询制品列表")
    public AjaxResult list(@PathVariable String enterpriseIdentifier,
                           @ApiParam(value = "产品库标识", required = true) @PathVariable String repoName,
                           @Valid ComponentSearchVo componentSearchVo) {
        componentSearchVo.setRepositoryName(repoName);
        NexusComponentResultVo<List<NexusResultDataVo>> result =
                pmsProductLibraryComponentService.selectPmsProductLibraryComponentListByNexus(enterpriseIdentifier, componentSearchVo, true);
        return success(result);
    }

    /**
     * 产品库获取制品文件详细信息
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:query")
    @GetMapping(value = "/{repoName}/component/readAsset/{assetId}")
    @ApiOperation(value = "产品库获取制品文件详细信息")
    public AjaxResult getAssetDetail(@PathVariable String enterpriseIdentifier,
                                     @ApiParam(value = "产品库标识", required = true) @PathVariable("repoName") String repoName,
                                     @PathVariable("assetId") String assetId) {
        NexusComponentResultVo<NexusAssetResultDataVo> result =
                pmsProductLibraryComponentService.getAssetDetailByAssetId(enterpriseIdentifier, repoName, null, assetId, true);
        return success(result);
    }

    /**
     * 产品库获取制品详细信息
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:query")
    @GetMapping(value = "/{repoName}/component/readComponent/{componentId}")
    @ApiOperation(value = "产品库获取制品详细信息")
    public AjaxResult getComponentDetail(@PathVariable String enterpriseIdentifier,
                                         @ApiParam(value = "产品库标识", required = true) @PathVariable("repoName") String repoName,
                                         @PathVariable("componentId") String componentId) {
        NexusComponentResultVo<NexusComponentResultDataVo> result =
                pmsProductLibraryComponentService.getComponentDetailByComponentId(enterpriseIdentifier, repoName, null, componentId, true);
        return success(result);
    }

    /**
     * 产品库获取制品文件夹是否能删除状态
     */
    @GetMapping("/{repoName}/component/canDeleteFolder")
    @ApiOperation(value = "产品库获取制品文件夹是否能删除的状态")
    public AjaxResult canDeleteFolder(@PathVariable String enterpriseIdentifier,
                                      @ApiParam(value = "产品库标识", required = true) @PathVariable String repoName,
                                      @Valid ComponentSearchVo componentSearchVo) {
        componentSearchVo.setRepositoryName(repoName);
        NexusComponentResultVo<Boolean> result =
                pmsProductLibraryComponentService.getCanDeleteFolderByNode(enterpriseIdentifier, componentSearchVo, true);
        return success(result);
    }

    /**
     * 产品库获取制品文件夹是否能删除状态
     */
    @GetMapping("/{repoName}/component/canDeleteComponent/{componentId}")
    @ApiOperation(value = "产品库获取制品是否能删除的状态")
    public AjaxResult canDeleteComponent(@PathVariable String enterpriseIdentifier,
                                         @ApiParam(value = "产品库标识", required = true) @PathVariable("repoName") String repoName,
                                         @PathVariable("componentId") String componentId) {
        NexusComponentResultVo<Boolean> result =
                pmsProductLibraryComponentService.getCanDeleteComponentByComponentId(enterpriseIdentifier, repoName, null, componentId, true);
        return success(result);
    }

    /**
     * 产品库获取制品文件夹是否能删除状态
     */
    @GetMapping("/{repoName}/component/canDeleteAsset/{assetId}")
    @ApiOperation(value = "产品库获取制品文件是否能删除的状态")
    public AjaxResult canDeleteAsset(@PathVariable String enterpriseIdentifier,
                                     @ApiParam(value = "产品库标识", required = true) @PathVariable("repoName") String repoName,
                                     @PathVariable("assetId") String assetId) {
        NexusComponentResultVo<Boolean> result =
                pmsProductLibraryComponentService.getCanDeleteAssetByAssetId(enterpriseIdentifier, repoName, null, assetId, true);
        return success(result);
    }

    /**
     * 产品库删除制品文件
     */
    @DeleteMapping("/{repoName}/component/deleteAsset/{assetId}")
    @ApiOperation(value = "产品库删除制品文件")
    public AjaxResult deleteAsset(@PathVariable String enterpriseIdentifier,
                                  @ApiParam(value = "产品库标识", required = true) @PathVariable("repoName") String repoName,
                                  @PathVariable("assetId") String assetId) {
        NexusComponentResultVo<List<String>> result =
                pmsProductLibraryComponentService.deleteAssetByAssetId(enterpriseIdentifier, repoName, null, assetId, true);
        return success(result);
    }

    /**
     * 产品库删除制品
     */
    @DeleteMapping("/{repoName}/component/deleteComponent/{componentId}")
    @ApiOperation(value = "产品库删除制品")
    public AjaxResult deleteComponent(@PathVariable String enterpriseIdentifier,
                                      @ApiParam(value = "产品库标识", required = true) @PathVariable("repoName") String repoName,
                                      @PathVariable("componentId") String componentId) {
        NexusComponentResultVo<List<String>> result =
                pmsProductLibraryComponentService.deleteComponentByComponentId(enterpriseIdentifier, repoName, null, componentId, true);
        return success(result);
    }

    /**
     * 产品库异步删除制品文件夹
     */
    @DeleteMapping("/{repoName}/component/deleteFolder")
    @ApiOperation(value = "产品库异步删除制品文件夹")
    public AjaxResult deleteFolder(@PathVariable String enterpriseIdentifier,
                                   @ApiParam(value = "产品库标识", required = true) @PathVariable("repoName") String repoName,
                                   @Valid ComponentSearchVo componentSearchVo) {
        componentSearchVo.setRepositoryName(repoName);
        NexusComponentResultVo<Object> result =
                pmsProductLibraryComponentService.deleteFolderByNode(enterpriseIdentifier, componentSearchVo, true);
        return success(result);
    }

    /**
     * 获取制品库下关键制品（排除自动生成的无关制品）
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:list")
    @GetMapping("/{productRepoName}/{repoName}/keyComponent/list")
    @ApiOperation(value = "制品库查询关键制品列表")
    public GenericsAjaxResult<NexusComponentResultVo<List<NexusResultDataVo>>> keyComponentList(@PathVariable String enterpriseIdentifier,
                                       @ApiParam(value = "产品库标识", required = true) @PathVariable("productRepoName") String productRepoName,
                                       @ApiParam(value = "制品库标识", required = true) @PathVariable("repoName") String repoName,
                                       @Valid ComponentSearchVo componentSearchVo) {
        componentSearchVo.setRepositoryName(repoName);
        componentSearchVo.setProductRepoName(productRepoName);
        NexusComponentResultVo<List<NexusResultDataVo>> result =
                pmsProductLibraryService.selectPmsProductLibraryKeyComponentList(enterpriseIdentifier, componentSearchVo);
        return genericsSuccess(result);
    }

    /**
     * 获取制品库下关键制品（排除自动生成的无关制品）
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:list")
    @GetMapping("/{productRepoName}/docList")
    @ApiOperation(value = "查询知识库文档列表")
    public GenericsTableDataInfo<DocResultVo> docList(@PathVariable String enterpriseIdentifier,
                                                      @ApiParam(value = "产品库标识", required = true) @PathVariable("productRepoName") String productRepoName,
                                                      @Valid DocSearchVo docSearchVo) {
        docSearchVo.setEnterpriseIdentifier(enterpriseIdentifier);
        docSearchVo.setProductRepoName(productRepoName);
        return pmsProductLibraryService.selectDocList(docSearchVo);
    }

    /**
     * 选择普通制品移入产品库
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:add")
    @Log(title = "普通制品移入产品库", businessType = BusinessType.INSERT)
    @PostMapping("/{productRepoName}/commonAssetToProductRepo/{repoName}")
    @ApiOperation(value = "选择普通制品移入产品库")
    public AjaxResult addCommonAssetToProductRepo(@PathVariable String enterpriseIdentifier,
                                                  @ApiParam("产品库标识") @PathVariable String productRepoName,
                                                  @ApiParam("制品库标识") @PathVariable String repoName,
                                                  @Valid @RequestBody CommonAssetAddToProductRepoVo commonAssetAddToProductRepoVo) {
        commonAssetAddToProductRepoVo.setEnterpriseIdentifier(enterpriseIdentifier);
        commonAssetAddToProductRepoVo.setProductRepoName(productRepoName);
        commonAssetAddToProductRepoVo.setRepoName(repoName);
        return toAjax(pmsProductLibraryService.addCommonAssetToProductRepo(commonAssetAddToProductRepoVo));
    }

    /**
     * 选择Docker制品移入产品库
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:add")
    @Log(title = "Docker制品移入产品库", businessType = BusinessType.INSERT)
    @PostMapping("/{productRepoName}/dockerComponentToProductRepo/{repoName}")
    @ApiOperation(value = "选择Docker制品移入产品库")
    public AjaxResult addDockerComponentToProductRepo(@PathVariable String enterpriseIdentifier,
                                                      @ApiParam("产品库名称") @PathVariable String productRepoName,
                                                      @ApiParam("制品库名称") @PathVariable String repoName,
                                                      @Valid @RequestBody DockerComponentAddToProductRepoVo dockerComponentAddToProductRepoVo) {
        dockerComponentAddToProductRepoVo.setEnterpriseIdentifier(enterpriseIdentifier);
        dockerComponentAddToProductRepoVo.setProductRepoName(productRepoName);
        dockerComponentAddToProductRepoVo.setRepoName(repoName);
        return toAjax(pmsProductLibraryService.addDockerComponentToProductRepo(dockerComponentAddToProductRepoVo));
    }

    /**
     * 产品库打包
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "产品库打包", businessType = BusinessType.INSERT)
    @PostMapping("/packaged/{productRepoName}")
    @ApiOperation(value = "产品库打包")
    public AjaxResult packagedProduct(@PathVariable String enterpriseIdentifier,
                                      @ApiParam(value = "产品库标识", required = true) @PathVariable String productRepoName) {
        return toAjax(pmsProductLibraryService.packagedProduct(enterpriseIdentifier, productRepoName));
    }

    /**
     * 获取产品库打包下载地址
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @GetMapping("/packaged/{productRepoName}")
    @ApiOperation(value = "获取产品库打包下载地址")
    public GenericsAjaxResult<ProductPackagedResultVo> getPackagedProduct(@PathVariable String enterpriseIdentifier,
                                                                          @ApiParam(value = "产品库标识", required = true) @PathVariable String productRepoName) {
        return genericsSuccess(pmsProductLibraryService.getPackagedProduct(enterpriseIdentifier, productRepoName));
    }
}
