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

import com.alibaba.fastjson2.JSONObject;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.pms.productLibrary.domain.PmsProductLibraryRepositories;
import com.microservices.pms.productLibrary.domain.ProductLibraryRepositoriesFormatEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotBlank;

/**
 * 制品库-仓库对象 pms_product_library_repositories
 *
 * @author otto
 * @date 2024-06-20
 */
@Data
@ApiModel("快照库输入对象")
public class ProductRepositoriesSnapshotInputVo {
    @ApiModelProperty(value = "快照库标识", hidden = true)
    private String name;
    @ApiModelProperty(value = "快照库名称")
    @NotBlank(message = "快照库名称不能为空")
    private String alias;
    @ApiModelProperty(value = "制品库标识", hidden = true)
    private String repoName;
    @ApiModelProperty(value = "关联组织标识", hidden = true)
    private String enterpriseIdentifier;

    public NexusHostedRepoInputVo toNexusRepoDataVo(PmsProductLibraryRepositories repo) {
        NexusHostedRepoInputVo target = new NexusHostedRepoInputVo();
        BeanUtils.copyProperties(this, target);
        target.setName(PmsProductLibraryRepositories.getNexusSnapshotRepositoriesName(this.enterpriseIdentifier, repo.getName(), this.name));
        if (ProductLibraryRepositoriesFormatEnum.MAVEN.getKey().equals(repo.getFormat())) {
            JSONObject attributes = JSONObject.parseObject(repo.getAttributes());
            target.setMaven(attributes.getObject("maven", NexusRepoMavenVo.class));
        }
        target.setCleanup(new NexusRepoCleanupVo());
        target.setStorage(new NexusRepoStorageVo());
        target.setOnline(true);
        target.setFormat(repo.getFormat());
        target.setType(repo.getType());
        return target;
    }

    public PmsProductLibraryRepositories toPmsProductLibraryRepositories(PmsProductLibraryRepositories repo) {
        PmsProductLibraryRepositories target = new PmsProductLibraryRepositories();
        BeanUtils.copyProperties(repo, target);
        target.setSnapshotName(this.name);
        target.setFormat(repo.getFormat());
        target.setAlias(this.alias);
        return target;
    }
}
