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.log.annotation.Log;
import com.microservices.common.log.enums.BusinessType;
import com.microservices.pms.productLibrary.domain.ProductLibraryRepositoriesFormatEnum;
import com.microservices.pms.productLibrary.domain.vo.*;
import com.microservices.pms.productLibrary.service.IPmsProductLibraryComponentService;
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-27
 */
@RestController
@Api(tags = "制品库-制品相关接口")
@RequestMapping("/{enterpriseIdentifier}/pmsProductLibraryComponent")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "组织标识", paramType = "path", dataType = "String")
})
public class PmsProductLibraryComponentController extends BaseController {
    @Autowired
    private IPmsProductLibraryComponentService pmsProductLibraryComponentService;

    /**
     * 查询制品库-制品列表
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:list")
    @GetMapping("/{repoName}/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, false);
        return success(result);
    }

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

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

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

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

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

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

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

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


    /**
     * 上传制品-maven制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "制品-maven制品", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}/maven")
    @ApiOperation(value = "maven类型制品库上传制品")
    public AjaxResult uploadMavenAsset(@PathVariable String enterpriseIdentifier,
                                       @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                          @Valid @RequestBody ComponentMavenInputVo componentMavenInputVo) {
        componentMavenInputVo.setRepoName(repoName);
        componentMavenInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return toAjax(pmsProductLibraryComponentService.insertMavenComponent(componentMavenInputVo));
    }

    /**
     * 上传制品-npm制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "制品-npm制品", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}/npm")
    @ApiOperation(value = "npm类型制品库上传制品")
    public AjaxResult uploadNpmAsset(@PathVariable String enterpriseIdentifier,
                                     @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                     @Valid @RequestBody ComponentInputVo componentInputVo) {
        componentInputVo.setRepoName(repoName);
        componentInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return toAjax(pmsProductLibraryComponentService.insertNpmComponent(componentInputVo));
    }

    /**
     * 上传制品-pypi制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "制品-pypi制品", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}/pypi")
    @ApiOperation(value = "pypi类型制品库上传制品")
    public AjaxResult uploadPypiAsset(@PathVariable String enterpriseIdentifier,
                                      @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                      @Valid @RequestBody ComponentInputVo componentInputVo) {
        componentInputVo.setRepoName(repoName);
        componentInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return toAjax(pmsProductLibraryComponentService.insertPypiComponent(componentInputVo));
    }

    /**
     * 上传制品-raw制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "制品-raw制品", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}/raw")
    @ApiOperation(value = "raw类型制品库上传制品")
    public AjaxResult uploadRawAsset(@PathVariable String enterpriseIdentifier,
                                     @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                     @Valid @RequestBody ComponentRawInputVo componentRawInputVo) {
        componentRawInputVo.setRepoName(repoName);
        componentRawInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        return toAjax(pmsProductLibraryComponentService.insertRawComponent(componentRawInputVo));
    }

    /**
     * 上传制品-go制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "制品-go制品", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}/go")
    @ApiOperation(value = "go类型制品库上传制品")
    public AjaxResult uploadGoAsset(@PathVariable String enterpriseIdentifier,
                                    @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                    @Valid @RequestBody ComponentRawInputVo componentRawInputVo) {
        componentRawInputVo.setRepoName(repoName);
        componentRawInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        componentRawInputVo.setFormat(ProductLibraryRepositoriesFormatEnum.GO.getKey());
        return toAjax(pmsProductLibraryComponentService.insertRawComponent(componentRawInputVo));
    }

    /**
     * 上传制品-conda制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "制品-conda制品", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}/conda")
    @ApiOperation(value = "conda类型制品库上传制品")
    public AjaxResult uploadCondaAsset(@PathVariable String enterpriseIdentifier,
                                       @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                       @Valid @RequestBody ComponentRawInputVo componentRawInputVo) {
        componentRawInputVo.setRepoName(repoName);
        componentRawInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        componentRawInputVo.setFormat(ProductLibraryRepositoriesFormatEnum.CONDA.getKey());
        return toAjax(pmsProductLibraryComponentService.insertRawComponent(componentRawInputVo));
    }

    /**
     * 上传制品-xxxxx制品
     */
//    @RequiresPermissions("pms:pmsProductLibraryComponent:add")
    @Log(title = "制品-xxxxx制品", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}/xxxxx")
    @ApiOperation(value = "xxxxx类型制品库上传制品")
    public AjaxResult uploadXxxxxAsset(@PathVariable String enterpriseIdentifier,
                                       @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                       @Valid @RequestBody ComponentRawInputVo componentRawInputVo) {
        componentRawInputVo.setRepoName(repoName);
        componentRawInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        componentRawInputVo.setFormat(ProductLibraryRepositoriesFormatEnum.XXXXX.getKey());
        return toAjax(pmsProductLibraryComponentService.insertRawComponent(componentRawInputVo));
    }
}
