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.IPmsProductLibrarySnapshotRepoService;
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;

/**
 * 快照库-相关接口
 *
 * @author otto
 * @date 2024-07-11
 */
@RestController
@Api(tags = "快照库-相关接口")
@RequestMapping("/{enterpriseIdentifier}/pmsProductLibrarySnapshot")
@ApiImplicitParams({
        @ApiImplicitParam(name = "enterpriseIdentifier", value = "组织标识", paramType = "path", dataType = "String")
})
public class PmsProductLibrarySnapshotController extends BaseController {
    @Autowired
    private IPmsProductLibraryComponentService pmsProductLibraryComponentService;
    @Autowired
    private IPmsProductLibrarySnapshotRepoService pmsProductLibrarySnapshotRepoService;

    /**
     * 获取快照库详情
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:list")
    @GetMapping("/byRepoName/{repoName}/{snapshotName}")
    @ApiOperation(value = "获取快照库详情")
    public GenericsAjaxResult<PmsProductLibrarySnapshotSimpleVo> getSnapshotByRepoName(@PathVariable String enterpriseIdentifier,
                                                                                         @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                                                                         @ApiParam(value = "快照库标识", required = true) @PathVariable String snapshotName) {
        return GenericsAjaxResult.success(pmsProductLibrarySnapshotRepoService.selectPmsProductLibrarySnapshotDataByName(enterpriseIdentifier, repoName, snapshotName));
    }

    /**
     * 查询产品库列表
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:list")
    @GetMapping("/{repoName}/list")
    @ApiOperation(value = "查询快照库列表")
    public GenericsTableDataInfo<PmsProductLibrarySnapshotSimpleVo> list(
            @PathVariable String enterpriseIdentifier,
            @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
            @ApiParam("模糊搜索快照库标识") String snapshotName) {
        startPage();
        return pmsProductLibrarySnapshotRepoService.selectPmsProductLibrarySnapshotPageList(enterpriseIdentifier, repoName, snapshotName);
    }

    /**
     * 新增快照库
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:add")
    @Log(title = "快照库", businessType = BusinessType.INSERT)
    @PostMapping("/{repoName}")
    @ApiOperation(value = "新增快照库")
    public AjaxResult addSnapshot(@PathVariable String enterpriseIdentifier,
                                  @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                                  @Valid @RequestBody ProductRepositoriesSnapshotInputVo productRepositoriesSnapshotInputVo) {
        productRepositoriesSnapshotInputVo.setEnterpriseIdentifier(enterpriseIdentifier);
        productRepositoriesSnapshotInputVo.setRepoName(repoName);
        return success(pmsProductLibrarySnapshotRepoService.insertPmsProductLibrarySnapshot(productRepositoriesSnapshotInputVo));
    }

    /**
     * 删除快照库
     */
//    @RequiresPermissions("pms:pmsProductLibraryRepositories:remove")
    @Log(title = "快照库", businessType = BusinessType.DELETE)
    @DeleteMapping("/{repoName}/{snapshotName}")
    @ApiOperation(value = "删除快照库")
    public AjaxResult remove(@PathVariable String enterpriseIdentifier,
                             @ApiParam(value = "制品库标识", required = true) @PathVariable String repoName,
                             @ApiParam(value = "快照库标识", required = true) @PathVariable String snapshotName) {
        return toAjax(pmsProductLibrarySnapshotRepoService.deletePmsProductLibrarySnapshotRepoByName(enterpriseIdentifier, repoName, snapshotName));
    }

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

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

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