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.ProductLibraryRepositoriesTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * 制品库-仓库对象 pms_product_library_repositories
 *
 * @author otto
 * @date 2024-06-20
 */
@Data
@ApiModel("虚拟仓库操作对象")
public class GroupRepositoriesOperateVo {
    @ApiModelProperty(value = "制品库Id", hidden = true)
    private Long id;
    @ApiModelProperty(value = "虚拟仓库标识", hidden = true)
    private String name;
    @ApiModelProperty(value = "虚拟仓库名称")
    private String alias;
    @ApiModelProperty(value = "组织标识", hidden = true)
    private String enterpriseIdentifier;
    @ApiModelProperty(value = "制品库类型", hidden = true)
    private String type = ProductLibraryRepositoriesTypeEnum.GROUP.getKey();
    @ApiModelProperty(value = "制品库格式", hidden = true)
    private String format;
    @ApiModelProperty(value = "是否在线", hidden = true)
    private Boolean online = true;
    @Valid
    @ApiModelProperty(value = "成员对象")
    @NotNull(message = "虚拟仓库成员对象不能为空")
    private NexusRepoGroupVo group;
    @Valid
    @ApiModelProperty(value = "Docker制品库输入对象")
    private NexusRepoDockerVo docker;

    public NexusGroupRepoInputVo toNexusGroupRepoInputVo() {
        NexusGroupRepoInputVo target = new NexusGroupRepoInputVo();
        target.setOnline(this.getOnline());
        target.setName(PmsProductLibraryRepositories.getNexusRepositoriesName(this.enterpriseIdentifier, this.name));
        target.setGroup(group.toNexusRepoGroupVo(this.enterpriseIdentifier));
        target.setStorage(new NexusRepoStorageVo());
        target.setDocker(this.getDocker());
        return target;
    }

    public PmsProductLibraryRepositories toPmsProductLibraryRepositories() {
        PmsProductLibraryRepositories pmsProductLibraryRepositories = new PmsProductLibraryRepositories();
        BeanUtils.copyProperties(this, pmsProductLibraryRepositories);
        JSONObject attributeJson = new JSONObject();
        attributeJson.put("online", online);
        attributeJson.put("group", group);
        if (docker != null) {
            attributeJson.put("docker", docker);
        }
        pmsProductLibraryRepositories.setAttributes(attributeJson.toJSONString());
        return pmsProductLibraryRepositories;
    }
}
