package org.springblade.sqlfly.config;

import java.io.Serializable;

import cn.hutool.core.util.StrUtil;

/**
 * 数据库的列
 * 
 * @author William
 * @date 2022年10月28日 上午11:26:41
 */
public class Column implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 字段名
	 */
	private String columnName;

	/**
	 * 类型名称
	 */
	private String dataType;

	/**
	 * 字段注释
	 */
	private String columnComment;

	/**
	 * 是否可为空
	 */
	private String isNullable;

	/**
	 * 额外信息
	 */
	private String extra;

	/**
	 * 长度
	 */
	private int size;

	/**
	 * 精度
	 */
	private int scale;

	/**
	 * 对应的java类型
	 */
	private String javaType;

	/**
	 * 是否必填，不可为空即为必填
	 */
	private boolean required;

	/**
	 * 是否为主键字段
	 */
	private boolean primaryKey;

	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}

	public String getColumnComment() {
		String realComment = columnComment;
		if (realComment.contains("[")) {
			String[] array = realComment.split("\\[");
			if (array != null && array.length > 0) {
				realComment = array[0];
			}
		}
		return realComment;
	}

	public void setColumnComment(String columnComment) {
		this.columnComment = columnComment;
	}

	public String getCommentExtra() {
		String commentExtra = null;
		if (columnComment.contains("[")) {
			String[] array = columnComment.split("\\[");
			if (array != null && array.length > 0) {
				commentExtra = array[1];
				if (commentExtra != null) {
					commentExtra = commentExtra.replace("]", "");
				}
			}
		}
		return commentExtra;
	}

	public String getIsNullable() {
		return isNullable;
	}

	public void setIsNullable(String isNullable) {
		this.isNullable = isNullable;
	}

	public String getExtra() {
		return extra;
	}

	public void setExtra(String extra) {
		this.extra = extra;
	}

	public int getSize() {
		return size;
	}

	public void setSize(int size) {
		this.size = size;
	}

	public int getScale() {
		return scale;
	}

	public void setScale(int scale) {
		this.scale = scale;
	}

	public String getJavaType() {
		return javaType;
	}

	public void setJavaType(String javaType) {
		this.javaType = javaType;
	}

	public boolean getPrimaryKey() {
		return primaryKey;
	}

	public void setPrimaryKey(boolean primaryKey) {
		this.primaryKey = primaryKey;
	}

	public void setRequire(boolean required) {
		this.required = required;
	}

	public boolean getRequired() {
		return StrUtil.isNotBlank(isNullable) ? "NO".equalsIgnoreCase(isNullable) : required;
	}

	@Override
	public String toString() {
		return "Column{" + "columnName='" + columnName + '\'' + ", dataType='" + dataType + '\'' + ", columnComment='"
				+ columnComment + '\'' + ", isNullable='" + isNullable + '\'' + ", extra='" + extra + '\'' + ", size="
				+ size + ", scale=" + scale + ", javaType='" + javaType + '\'' + ", required=" + required
				+ ", primaryKey=" + primaryKey + '}';
	}

}
