package com.xydl.cac.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@JsonInclude(JsonInclude.Include.NON_NULL)
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name = "remote_config")
@ApiModel("远端服务器信息表")
public class RemoteConfig {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Integer id;

    @NotBlank(message = "名称不能为空")
    @ApiModelProperty("名称")
    @Column(name = "name")
    private String name;

    @NotBlank(message = "IP不能为空")
    @ApiModelProperty("IP")
    @Column(name = "ip")
    private String ip;

    @NotNull(message = "端口不能为空")
    @ApiModelProperty("端口")
    @Column(name = "port")
    private Integer port;

    @NotBlank(message = "用户名不能为空")
    @ApiModelProperty("用户名")
    @Column(name = "user")
    private String user;

    @NotBlank(message = "密码不能为空")
    @ApiModelProperty("密码")
    @Column(name = "passwd")
    private String passwd;

    @JsonIgnore
    @ApiModelProperty("目录")
    @Column(name = "path")
    private String path;

    @ApiModelProperty("后缀名")
    @Column(name = "suffix")
    private String suffix;

    @NotNull(message = "是否删除不能为空")
    @ApiModelProperty("下载后是否删除")
    @Column(name = "todel")
    private Integer todel;

    @NotNull(message = "状态不能为空")
    @ApiModelProperty("状态 0:停用 1:启用")
    @Column(name = "active")
    private Integer active;

    @Transient
    @ApiModelProperty("目录")
    List<String> pathList;

    public void toList() {
        if (path != null) {
            pathList = Arrays.asList(path.split(","));
        }
    }

    public void fromList() {
        if (pathList != null) {
            path = pathList.stream().collect(Collectors.joining(","));
        }
    }
}