package com.microservices.pms.productLibrary.domain;

import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONValidator;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.microservices.common.core.utils.StringUtils;
import com.microservices.common.core.utils.bean.BeanUtils;
import com.microservices.common.core.web.domain.BaseEntity;
import com.microservices.pms.productLibrary.domain.vo.*;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * 制品库-仓库对象 pms_product_library_repositories
 *
 * @author otto
 * @date 2024-06-20
 */
@Data
@ApiModel("制品仓库对象")
public class PmsProductLibraryRepositories extends BaseEntity {
    private static final long serialVersionUID = 1L;

    /**
     * 制品仓库ID
     */
    private Long id;

    /**
     * 制品仓库标识
     */
    @ApiModelProperty(value = "制品仓库标识")
    private String name;

    /**
     * 制品仓库名称类型
     */
    @ApiModelProperty(value = "制品库类型")
    private String type;


    @ApiModelProperty(value = "制品库格式")
    private String format;

    /**
     * 制品仓库访问路径
     */
    @ApiModelProperty(value = "制品仓库访问路径")
    private String url;

    /**
     * 关联组织ID
     */
    @ApiModelProperty(value = "关联组织ID")
    private Long deptId;

    /**
     * 关联组织ID
     */
    @ApiModelProperty(value = "关联组织标识", hidden = true)
    private String pmsEnterpriseIdentifier;

    /**
     * 关联组织ID
     */
    @ApiModelProperty(value = "关联组织ID")
    private Long pmsEnterpriseId;

    /**
     * 删除标志（0代表存在 2代表删除）
     */
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String delFlag;

    /**
     * 其他属性（JSON字符串）
     */
    @ApiModelProperty(value = "其他属性(JSON字符串)")
    private String attributes;


    @ApiModelProperty(value = "制品仓库名称")
    private String alias;


    @ApiModelProperty(value = "快照标识")
    private String snapshotName;


    @ApiModelProperty(value = "打包的文件标识")
    private String packagedFileIdentifier;

    public RepoAttributesVo getRepoAttributes() {
        return JSONObject.parseObject(this.attributes, RepoAttributesVo.class);
    }

    public PmsProductLibraryRepositoriesDataVo toPmsProductLibraryRepositoriesDataVo() {
        PmsProductLibraryRepositoriesDataVo target = new PmsProductLibraryRepositoriesDataVo();
        BeanUtils.copyProperties(this, target);
        target.setAttributes(JSONObject.parseObject(attributes));
        return target;
    }

    public PmsProductLibraryRepositoriesSimpleVo toPmsProductLibraryRepositoriesSimpleVo() {
        PmsProductLibraryRepositoriesSimpleVo target = new PmsProductLibraryRepositoriesSimpleVo();
        BeanUtils.copyProperties(this, target);
        return target;
    }

    /**
     * 在Nexus上创建的产品库标识应该为用户输入的product+制品库标识+组织标识
     *
     * @return Nexus产品库标识
     */
    public static String getNexusProductRepositoriesName(String enterpriseIdentifier, String name) {
        return String.format("product-%s-%s", enterpriseIdentifier, name);
    }

    /**
     * 在Nexus上创建的快照库标识应该为用户输入的snapshot+组织标识+制品库标识+快照库标识
     *
     * @return Nexus快照库标识
     */
    public static String getNexusSnapshotRepositoriesName(String enterpriseIdentifier, String repoName, String snapshotName) {
        return String.format("snapshot-%s-%s-%s", enterpriseIdentifier, repoName, snapshotName);
    }

    /**
     * 在Nexus上创建的制品库标识应该为用户输入的制品库标识+组织标识
     *
     * @return Nexus制品库标识
     */
    public static String getNexusRepositoriesName(String enterpriseIdentifier, String name) {
        return String.format("%s-%s", enterpriseIdentifier, name);
    }

    public static String getNexusRepositoriesName(String format, String enterpriseIdentifier, String repoName, String snapshotName) {
        ProductLibraryRepositoriesFormatEnum formatEnum = ProductLibraryRepositoriesFormatEnum.getByKey(format);
        if (formatEnum == ProductLibraryRepositoriesFormatEnum.PRODUCT) {
            return getNexusProductRepositoriesName(enterpriseIdentifier, repoName);
        }
        if (StringUtils.isNotEmpty(snapshotName)) {
            return getNexusSnapshotRepositoriesName(enterpriseIdentifier, repoName, snapshotName);
        } else {
            return getNexusRepositoriesName(enterpriseIdentifier, repoName);
        }
    }

    public PmsProductLibrarySimpleVo toPmsProductLibrarySimpleVo() {
        PmsProductLibrarySimpleVo target = new PmsProductLibrarySimpleVo();
        BeanUtils.copyProperties(this, target);
        if (JSONValidator.from(this.attributes).validate()) {
            target.setAttributes(JSONObject.parseObject(this.attributes));
        }
        return target;
    }

    public PmsProductLibrarySnapshotSimpleVo toPmsProductLibrarySnapshotSimpleVo() {
        PmsProductLibrarySnapshotSimpleVo target = new PmsProductLibrarySnapshotSimpleVo();
        BeanUtils.copyProperties(this, target);
        if (JSONValidator.from(this.attributes).validate()) {
            target.setAttributes(JSONObject.parseObject(this.attributes));
        }
        target.setSnapshotName(this.snapshotName);
        return target;
    }

    public String getNexusRepositoriesName() {
        if (ProductLibraryRepositoriesFormatEnum.PRODUCT.getKey().equals(this.format)) {
            return getNexusProductRepositoriesName(pmsEnterpriseIdentifier, name);
        } else if (StringUtils.isEmpty(snapshotName)) {
            return getNexusRepositoriesName(pmsEnterpriseIdentifier, name);
        } else {
            return getNexusSnapshotRepositoriesName(pmsEnterpriseIdentifier, name, snapshotName);
        }
    }
}
