package com.microservices.pms.productLibrary.domain.vo;

import com.microservices.common.core.utils.StringUtils;
import com.microservices.pms.productLibrary.domain.PmsProductLibraryComponent;
import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 制品Maven输入对象
 *
 * @author otto
 * @date 2024-06-28
 */
@Data
@ApiModel("制品Maven输入对象")
public class ComponentMavenInputVo {
    @ApiModelProperty(value = "制品库标识", hidden = true)
    private String repoName;
    @ApiModelProperty(value = "快照库标识", hidden = true)
    private String snapshotName;
    @ApiModelProperty(value = "组织标识", hidden = true)
    private String enterpriseIdentifier;
    @ApiModelProperty(value = "文件列表")
    @Size(min = 1, message = "最少上传一个文件")
    @Valid
    private List<AssetMavenInputVo> assetList;
    @ApiModelProperty(value = "组Id")
    @NotNull(message = "组Id不能为空")
    private String groupId;
    @ApiModelProperty(value = "制品Id")
    @NotNull(message = "制品Id不能为空")
    private String artifactId;
    @ApiModelProperty(value = "版本")
    @NotNull(message = "版本不能为空")
    private String version;
    @ApiModelProperty(value = "是否生成pom文件")
    private Boolean generatePom;
    @ApiModelProperty(value = "包装")
    private String packaging;

    public String getNexusRepoName() {
        if (StringUtils.isEmpty(snapshotName)) {
            return PmsProductLibraryRepositories.getNexusRepositoriesName(
                    enterpriseIdentifier,
                    repoName
            );
        } else {
            return PmsProductLibraryRepositories.getNexusSnapshotRepositoriesName(
                    enterpriseIdentifier,
                    repoName,
                    snapshotName
            );
        }

    }

    public String getPomComponentName() {
        return String.format("%s-%s.pom", artifactId, version);
    }

    public PmsProductLibraryComponent toPmsProductLibraryComponent(Long repoId, String name) {
        PmsProductLibraryComponent pmsProductLibraryComponent = new PmsProductLibraryComponent();
        pmsProductLibraryComponent.setRepoId(repoId);
        pmsProductLibraryComponent.setName(name);
        pmsProductLibraryComponent.setPath(String.format("%s/%s/%s/%s", groupId, artifactId, version, name));
        pmsProductLibraryComponent.setGroup(groupId);
        pmsProductLibraryComponent.setVersion(version);
        pmsProductLibraryComponent.setPmsEnterpriseIdentifier(enterpriseIdentifier);
        return pmsProductLibraryComponent;
    }
}
