package cn.estsh.i3plus.pojo.form.bean;

import cn.estsh.i3plus.pojo.base.annotation.AnnoOutputColumn;
import cn.estsh.i3plus.pojo.base.bean.BaseBean;
import cn.estsh.i3plus.pojo.base.enumutil.BlockFormEnumUtil;
import cn.estsh.i3plus.pojo.base.enumutil.CommonEnumUtil;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.List;

/**
 * @Description : 数据对象
 * @Reference :
 * @Author : alwaysfrin
 * @CreateDate : 2019-02-27 10:53
 * @Modify:
 **/
@Data
@Entity
@DynamicInsert
@DynamicUpdate
@EqualsAndHashCode(callSuper = true)
@Table(name = "bf_data_object_property")
@Api(value = "数据对象属性表", description = "数据对象的属性明细表")
public class BfDataObjectProperty extends BaseBean {

    private static final long serialVersionUID = 2607149135728621568L;
    //外键关联数据对象主键
    @Column(name = "DATA_OBJECT_ID")
    @ApiParam(value = "数据对象ID", example = "-1")
    @JsonSerialize(using = ToStringSerializer.class)
    private Long dataObjectId;

    @Column(name="OBJECT_NAME_RDD")
    @ApiParam(value ="数据对象名称")
    private String objectNameRdd;

    @Column(name="OBJECT_TABLE_NAME_RDD")
    @ApiParam(value ="数据表名称")
    private String objectTableNameRdd;

    @Column(name="OBJECT_COLUMN_NAME")
    @ApiParam(value ="数据对象列名称")
    private String objectColumnName;

    // 暂用来区分存储过程出入参数信息
    @AnnoOutputColumn(refClass = BlockFormEnumUtil.ORIGIN_PROPERTY_TYPE.class)
    @ColumnDefault("10")
    @Column(name="ORIGIN_PROPERTY_TYPE")
    @ApiParam(value ="原始列类型")
    private Integer originPropertyType;

    public String getOriginPropertyTypeTxt() {
        return originPropertyType == null ? BlockFormEnumUtil.ORIGIN_PROPERTY_TYPE.NORMAL_PROPERTY.getDescription()
                : BlockFormEnumUtil.ORIGIN_PROPERTY_TYPE.valueOfDescription(originPropertyType);
    }

    @Column(name="OBJECT_COLUMN_TYPE")
    @ApiParam(value ="数据对象类型")
    private Integer objectColumnType;

    @Column(name="OBJECT_COLUMN_DEFAULT_WHERE")
    @ApiParam(value ="默认查询条件")
    private Integer objectColumnDefaultWhere;

    @Column(name="IS_PRIMARY_KEY")
    @ApiParam(value ="是否主键")
    private Integer isPrimaryKey;

    public boolean isPrimaryKey(){
        return isPrimaryKey != null && CommonEnumUtil.TRUE_OR_FALSE.valueOfBoolean(isPrimaryKey);
    }

    @Column(name="IS_NULLABLE")
    @ApiParam(value = "是否允许为空")
    private Integer isNullable;

    public Integer getIsNullableVal(){
        return isNullable == null ? CommonEnumUtil.TRUE_OR_FALSE.FALSE.getValue() : isNullable;
    }

    // 字段长度
    @ApiParam(value = "字段长度")
    @Column(name="OBJECT_COLUMN_PRECISION")
    private Integer objectColumnPrecision;

    // 字段精确长度
    @ApiParam(value = "字段精确长度")
    @Column(name="OBJECT_COLUMN_SCALE")
    private Integer objectColumnScale;

    @Column(name="OBJECT_COLUMN_TYPE_NAME")
    @ApiParam(value ="数据对象类型名称")
    private String objectColumnTypeName;

    @Column(name="PROPERTY_NAME")
    @ApiParam(value ="类属性名称")
    private String propertyName;

    @Column(name="PROPERTY_CODE")
    @ApiParam(value ="类属性名称")
    private String propertyCode;

    @Column(name="PROPERTY_TYPE")
    @ApiParam(value ="属性类型")
    private Integer propertyType;

    @Column(name="PROPERTY_TYPE_NAME_RDD")
    @ApiParam(value ="属性类型名称")
    private String propertyTypeNameRdd;

    @Column(name="PROPERTY_DESCRIPTION")
    @ApiParam(value ="属性描述")
    private String propertyDescription;

    @Transient
    @ApiParam(value ="默认查询条件")
    private Integer objectColumnCustomWhere;

    public int getObjectColumnCustomWhereVal(){
        if(objectColumnCustomWhere == null){
            objectColumnCustomWhere = -1;
        }
        return objectColumnCustomWhere;
    }

    @Transient
    @ApiParam(value ="是否为数据有效字段")
    private transient Integer isValidProperty;

    @Transient
    @ApiParam(value ="是否为弱删除字段")
    private transient Integer isDeleteWeaklyProperty;

    @Transient
    @ApiParam(value ="是否为唯一约束字段")
    private transient Integer isUniqueProperty;

    @Transient
    @ApiParam(value ="元素值")
    private transient Object propertyFormValue;

    @Transient
    @ApiParam(value ="元素值集合")
    private transient List<Object> propertyFormValueList;

}
