package com.pictc.datalog;

import java.lang.reflect.Field;

import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.pictc.annotations.datalog.LogField;
import com.pictc.jdbc.JdbcContextUtils;
import com.pictc.utils.SpringAnnotationUtils;

import lombok.Data;
import lombok.experimental.Accessors;

@Data
@Accessors(chain = true)
public class LogFieldInfo implements Comparable<LogFieldInfo>{
	
	private LogField annotation;
	
	private Field field;
	
	private TableFieldInfo fieldInfo;
	
	public static LogFieldInfo ofField(Field field) {
		return new LogFieldInfo().setField(field).setAnnotation(SpringAnnotationUtils.findAnnotation(field, LogField.class));
	}

	@Override
	public int compareTo(LogFieldInfo o) {
		return annotation.index() - o.annotation.index();
	}

	public String getFieldName() {
		return field.getName();
	}
	
	
	public String getName() {
		return annotation.name();
	}
	
	public String getColumn() {
		return fieldInfo!=null?fieldInfo.getColumn():JdbcContextUtils.fieldToColumn(field.getName());
	}
	
	public Class<?> getJavaType(){
		return field.getType();
	}

}
