package com.jy.log.model.wrapper;

import com.jy.framework.model.annotation.LogTag;
import lombok.Getter;
import lombok.Setter;

import java.lang.reflect.Field;

@Getter
@Setter
public class FieldWrapper {

    /**
     * 属性名称
     */
    private String attributeName;

    /**
     * 注解的属性名称,如果不存在则使用attributeName
     */
    private String attributeAlias;

    /**
     * 属性的旧值
     */
    private Object oldValue;

    /**
     * 属性的新值
     */
    private Object newValue;

    /**
     * 属性旧值字符串
     */
    private String oldValueString;

    /**
     * 属性新值字符串
     */
    private String newValueString;

    /**
     * 是否有注解
     */
    private boolean withLogTag;

    /**
     * 属性注解
     */
    private LogTag logTag;

    public FieldWrapper(Field field, Object oldValue, Object newValue) {
        this.attributeName = field.getName();
        this.oldValue = oldValue;
        this.newValue = newValue;
        this.oldValueString = oldValue == null ? "" : oldValue.toString();
        this.newValueString = newValue == null ? "" : newValue.toString();
        this.logTag = field.getAnnotation(LogTag.class);
        this.withLogTag = logTag != null;
        this.attributeAlias = (withLogTag && logTag.alias().length() != 0) ? logTag.alias() : field.getName();
    }
}
