/**
 * 
 */
package org.springblade.datasource.enums;

import static java.sql.Types.ARRAY;
import static java.sql.Types.BINARY;
import static java.sql.Types.BIT;
import static java.sql.Types.BLOB;
import static java.sql.Types.BOOLEAN;
import static java.sql.Types.CHAR;
import static java.sql.Types.CLOB;
import static java.sql.Types.DATE;
import static java.sql.Types.DECIMAL;
import static java.sql.Types.DOUBLE;
import static java.sql.Types.FLOAT;
import static java.sql.Types.INTEGER;
import static java.sql.Types.LONGNVARCHAR;
import static java.sql.Types.LONGVARBINARY;
import static java.sql.Types.LONGVARCHAR;
import static java.sql.Types.NCHAR;
import static java.sql.Types.NCLOB;
import static java.sql.Types.NUMERIC;
import static java.sql.Types.NVARCHAR;
import static java.sql.Types.REAL;
import static java.sql.Types.SMALLINT;
import static java.sql.Types.TIME;
import static java.sql.Types.TIMESTAMP;
import static java.sql.Types.TINYINT;
import static java.sql.Types.VARBINARY;
import static java.sql.Types.VARCHAR;

import java.util.stream.Stream;

import com.baomidou.mybatisplus.annotation.EnumValue;

/**
 * @author wookvn
 *
 * 显示的字段类型
 */
public enum ViewColumnTypeEnum {

	STRING(1, new Integer[] {CHAR, VARCHAR, LONGVARCHAR, BINARY, VARBINARY, LONGVARBINARY, BLOB, CLOB, NCHAR, NVARCHAR, LONGNVARCHAR, NCLOB, ARRAY}),
	
	DATETIME(2, new Integer[] {DATE, TIME, TIMESTAMP}),
	
	NUMBER(3, new Integer[] {BIT, TINYINT, SMALLINT, INTEGER, java.sql.Types.BIGINT, FLOAT, REAL, DOUBLE, NUMERIC, DECIMAL, BOOLEAN }),
	
	//特殊处理
	BIGINT(4, new Integer[] {});
	
	@EnumValue
	private int value;
	private Integer[] types;

	/**
	 * @param value
	 */
	private ViewColumnTypeEnum(int value, Integer[] types) {
		this.value = value;
		this.types = types;
	}

	/**
	 * @return the value
	 */
	public int getValue() {
		return value;
	}
	
	/**
	 * @return the types
	 */
	public Integer[] getTypes() {
		return types;
	}
	
	public static ViewColumnTypeEnum resolve(Integer type) {
		ViewColumnTypeEnum[] values = ViewColumnTypeEnum.values();
		for(ViewColumnTypeEnum columnType : values) {
			if(Stream.of(columnType.getTypes()).anyMatch(f -> type != null && f.intValue() == type)) {
				return columnType;
			}
		}
		return ViewColumnTypeEnum.STRING;
	}
}
