package cn.reghao.oss.console.app.model.po;

import cn.reghao.oss.console.util.db.BaseEntity;
import cn.reghao.oss.storeapi.dto.StoreNodeDto;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;

import javax.persistence.Column;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2024-02-23 11:29:14
 */
@NoArgsConstructor
@Getter
@Setter
@Entity
public class StoreNode extends BaseEntity {
    @Column(nullable = false, unique = true)
    private String nodeAddr;
    @NotNull
    private Integer httpPort;
    @NotNull
    private Integer rpcPort;
    @ElementCollection(targetClass = StoreDisk.class)
    @LazyCollection(LazyCollectionOption.FALSE)
    private List<StoreDisk> storeDisks;
    private String domain;
    private String referer;
    private String secretKey;
    private Boolean enabled;

    public StoreNode(StoreNodeDto storeNodeDto) {
        this.nodeAddr = storeNodeDto.getNodeAddr();
        this.httpPort = storeNodeDto.getHttpPort();
        this.rpcPort = storeNodeDto.getRpcPort();
        this.storeDisks = storeNodeDto.getDiskDtoList().stream().map(StoreDisk::new).collect(Collectors.toList());
        this.enabled = false;
    }
}
