package com.microservices.pms.productLibrary.domain;

import com.alibaba.fastjson2.JSONObject;
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.utils.sign.Base64;
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;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

/**
 * 制品库-制品对象 pms_product_library_component
 *
 * @author otto
 * @date 2024-06-27
 */
@Data
@ApiModel("制品对象")
public class PmsProductLibraryComponent extends BaseEntity {
    private static final long serialVersionUID = 1L;
    private static final Logger log = LoggerFactory.getLogger(PmsProductLibraryComponent.class);

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

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

    /**
     * 制品路径
     */
    @ApiModelProperty(value = "制品路径")
    private String path;

    /**
     * 制品所在组
     */
    @ApiModelProperty(value = "制品所在组")
    private String group;

    /**
     * 制品名称
     */
    @ApiModelProperty(value = "制品名称")
    private String name;

    /**
     * 制品库Id
     */
    @ApiModelProperty(value = "制品库Id")
    private Long repoId;

    /**
     * 制品版本
     */
    @ApiModelProperty(value = "制品版本")
    private String version;

    /**
     * 制品文件Id
     */
    @ApiModelProperty(value = "制品文件Id")
    private String fileIdentifier;

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

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

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

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

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

    /**
     * 预留字段1
     */
    @ApiModelProperty(value = "预留字段1")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String reservedField1;

    /**
     * 预留字段2
     */
    @ApiModelProperty(value = "预留字段2")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String reservedField2;

    /**
     * 预留字段3
     */
    @ApiModelProperty(value = "预留字段3")
    @JsonInclude(JsonInclude.Include.NON_EMPTY)
    private String reservedField3;

    public void setNexusMavenAssetVo(NexusAssetVo nexusAssetVo) {
        String composeId = Base64.base64ToDecoderString(nexusAssetVo.getId());
        if (StringUtils.isNotEmpty(composeId)) {
            String[] composeIds = composeId.split(":");
            this.setIdentifier(composeIds[1]);
        }

        this.setAttributes(JSONObject.toJSONString(nexusAssetVo));
    }

    public ComponentMavenInputVo toComponentMavenInputVo(PmsProductLibraryRepositories snapshotRepo) {
        ComponentMavenInputVo target = new ComponentMavenInputVo();
        target.setRepoName(snapshotRepo.getName());
        target.setSnapshotName(snapshotRepo.getSnapshotName());
        target.setEnterpriseIdentifier(snapshotRepo.getPmsEnterpriseIdentifier());
        JSONObject attributesObj = JSONObject.parseObject(this.getAttributes());
        JSONObject mavenObj = attributesObj.getJSONObject("maven2");
        AssetMavenInputVo assetMavenInputVo = new AssetMavenInputVo();
        assetMavenInputVo.setClassifier(mavenObj.getString("classifier"));
        assetMavenInputVo.setExtension(mavenObj.getString("extension"));
        assetMavenInputVo.setFileIdentifier(this.getFileIdentifier());
        List<AssetMavenInputVo> assetMavenInputVoList = new ArrayList<>();
        assetMavenInputVoList.add(assetMavenInputVo);
        target.setAssetList(assetMavenInputVoList);
        target.setGroupId(mavenObj.getString("groupId"));
        target.setArtifactId(mavenObj.getString("artifactId"));
        target.setVersion(this.getVersion());
        // 不自动生成POM文件
        target.setGeneratePom(false);
        return target;
    }

    public ComponentRawInputVo toComponentRawInputVo(PmsProductLibraryRepositories snapshotRepo) {
        ComponentRawInputVo target = new ComponentRawInputVo();
        target.setRepoName(snapshotRepo.getName());
        target.setSnapshotName(snapshotRepo.getSnapshotName());
        target.setEnterpriseIdentifier(snapshotRepo.getPmsEnterpriseIdentifier());
        target.setAttributes(this.getAttributes());
        AssetRawInputVo assetRawInputVo = new AssetRawInputVo();
        assetRawInputVo.setFileIdentifier(this.getFileIdentifier());
        assetRawInputVo.setFilename(this.getName());
        List<AssetRawInputVo> assetRawInputVoList = new ArrayList<>();
        assetRawInputVoList.add(assetRawInputVo);
        target.setAssetList(assetRawInputVoList);
        target.setVersion(this.getVersion());
        target.setDirectory(this.getGroup());
        return target;
    }

    public PmsProductLibraryComponentDataVo toPmsProductLibraryComponentDataVo() {
        PmsProductLibraryComponentDataVo target = new PmsProductLibraryComponentDataVo();
        BeanUtils.copyProperties(this, target);
        return target;
    }
}
