package org.example.Entity.Control.Fields;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.example.Entity.Control.Base.RowComponent;
import org.example.Entity.Control.Data.DataStore;
import org.example.Enums.SystemTypeEnums;
import org.example.Utils.DataTableUtil;
import org.example.Utils.IPublicUtil;
import org.example.Utils.PublicUtil;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


import static org.example.Utils.DataTableUtil.get;
import static org.example.Utils.NativeExtensionUtils.*;

@JsonInclude(JsonInclude.Include.NON_NULL)
public class Field extends RowComponent {
    private static final Logger log = LoggerFactory.getLogger(Field.class);



    public Field(Map<String, Object> _row) {
        super(_row);
        this.defaultXtype = "textfield";
    }

    public Field(Map<String, Object> _row, IPublicUtil util) {
        this(_row);
        this.setUtil(util);
    }


    @JsonProperty("FieldId")
    public int getFieldId() {
        return ToInt32(get(_row, "id", 0));
    }

    private String _ftype;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getFtype() {
        _ftype = (_ftype != null) ? _ftype : DataTableUtil.getStringValue(_row, "ftype", "");
        if (_ftype != null && _ftype.isEmpty()) {
            return null;
        }
        return _ftype;
    }

    public void setFtype(String ftype) {
        this._ftype = ftype;
    }


    private String _name;

    public String getName() {
        if (_name != null) {
            return _name;
        } else {
//            return (DataTableUtil.getRowVal(this._row, new String[]{"FieldName", "id"}, _name) != null) ? DataTableUtil.getRowVal(this._row, new String[]{"FieldName", "id"}, _name) + "" : "";
            return Objects.toString(DataTableUtil.getRowVal(this._row, new String[]{"FieldName", "id"}, _name), "").toLowerCase();
        }
    }

    public void setName(String name) {
        this._name = name;
    }

    private String _fieldLabel;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getFieldLabel() {
        String label = (String) get(_row, "FieldCaption", _fieldLabel, null);
        if (label != null && !label.isEmpty()) {
            if (label.indexOf('|') > -1) {
                String[] ls = label.split("\\|");
                LabelSuffix = ls[1];
                log.debug(String.valueOf(LabelSuffix + " " + ls[1]));
                return ls[0];
            }
        }
        return label;
    }

    public void setFieldLabel(String fieldLabel) {
        this._fieldLabel = fieldLabel;
    }

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public DataStore store;


    private String LabelSuffix;

    @JsonProperty("LabelSuffix")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getLabelSuffix() {
        return LabelSuffix;
    }

    private int _labelfontsize = 12;

    protected int getLabelfontsize() {
        Integer fontSize = getFontSize();
        return fontSize == null ? 12 : Math.max(fontSize, 12);
    }

    private Integer _labelWidth;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Integer getLabelWidth() {
        if (_labelWidth != null) {
            return _labelWidth;
        }
//        _labelWidth = parseInt((String) get(_row, "labelwidth", null));
        Object labelWidthObj = get(_row, "labelwidth", null);
        _labelWidth = parseIntSafe(labelWidthObj, 0);
        if (_labelWidth != null && _labelWidth > 0) {
            return _labelWidth;
        }
        if (getFieldLabel() == null || getFieldLabel().isEmpty()) {
            return null;
        }
        if (getName() == null || getName().isEmpty()) {
            return getWidth();
        }
        Pattern reg = Pattern.compile("[^\\x00-\\xFF]");
        Matcher matcher = reg.matcher(getFieldLabel());
        int tlen = getFieldLabel().length();
        int clen = matcher.replaceAll("").length();
        int totlen = tlen - clen + clen / 2 + 1;
        _labelWidth = (totlen * getLabelfontsize()) + (clen % 2 > 0 ? 6 : 0) + 6;
        return _labelWidth;
    }

    public void setLabelWidth(Integer labelWidth) {
        this._labelWidth = labelWidth;
    }

    private String _labelAlign;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getLabelAlign() {
        if (_labelAlign != null) {
            return _labelAlign;
        }
        _labelAlign = DataTableUtil.getStringValue(_row, "labelalign", null);
        if (_labelAlign == null || _labelAlign.isEmpty()) {
            return null;
        }
        return _labelAlign;
    }

    public void setLabelAlign(String labelAlign) {
        this._labelAlign = labelAlign;
    }

    private Integer _width;


    public Integer getWidth() {
        if (_width == null) {
            // 从 _row 中获取 "ControlWidth"，使用 DataTableUtil 工具类（代码库中统一的取值方式）
            Object value = get(_row, "ControlWidth", null);
            // 转换为 Integer，处理 null 情况（参考 Field.java 中的转换逻辑）
            _width = (value != null) ? Integer.parseInt(value.toString()) : null; // 或根据业务设为 null
        }
        return _width;
    }


    public void setWidth(Integer width) {
        this._width = width;
    }

    private Integer _height;


    public Integer getHeight() {
        if (_height == null) {
            // 1. 先获取原始值（可能为null）
            Object value = get(_row, "ControlHeight", null);

            // 2. 处理null值：设置默认值0（或根据业务需求调整）
            if (value == null) {
                _height = null; // 数据库为NULL时，默认值为0
            } else {
                // 3. 确保非null时再转换
                _height = Integer.parseInt(value.toString());
            }
        }
        return _height;
    }


    public void setHeight(Integer height) {
        this._height = height;
    }


    @JsonIgnore
    public Integer PopupWidth;
    @JsonIgnore
    public Integer PopupHeight;

    @JsonProperty("hidden")
    @Override
    public Boolean getHidden() {
        return getControlHidden();
    }

    private Boolean _uppercaseHidden;

    @JsonProperty("Hidden")
    public boolean isUppercaseHidden() {
        if (_uppercaseHidden == null) {
            _uppercaseHidden = toBoolean(get(_row, "Disabled2", false));
        }
        if (!_uppercaseHidden) {
            _uppercaseHidden = getPurviewWidth() == 0;
        }
        return _uppercaseHidden;
    }

    @JsonProperty("Hidden")
    public void setUppercaseHidden(Boolean hidden) {
        this._uppercaseHidden = hidden;
    }

    private Boolean _issearchcontrol;

    @JsonIgnore
    public boolean isIsSearchcontrol() {
        if (_issearchcontrol == null && (_issearchcontrol = toBoolean(DataTableUtil.getRowVal(_row, "searchcontrol", false)))) {
            return true;
        }
        return _issearchcontrol;
    }

    public void setIssearchcontrol(Boolean issearchcontrol) {
        this._issearchcontrol = issearchcontrol;
    }


    @JsonProperty("ColHidden")
    public Boolean getColHidden() {
        boolean colHidden = toBoolean(DataTableUtil.getRowVal(this._row, "Disabled", false));
        if (!colHidden) {
            colHidden = getPurviewWidth() == 0;
        }
        return colHidden;
    }

    private Boolean _canCopy = null;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Boolean getCanCopy() {
        if (_canCopy == null) {
            _canCopy = DataTableUtil.getBooleanValue(_row, "cancopy", false);
        }
        if (_canCopy) {
            return true;
        }
        return null;
    }


    @JsonIgnore
    public boolean isDisabled2() {
        return DataTableUtil.getBooleanValue(_row, "disabled2", false);
    }

    private Boolean _hidden;

    @JsonProperty("ControlHidden")
    public Boolean getControlHidden() {
        if (isIsSearchcontrol()) {
            return isDisabled2() || (getWidth() != null && getWidth() == 0);
        }
        if (_hidden == null) {
            _hidden = toBoolean(get(_row, "disabled2", false)) || (getWidth() != null && getWidth() == 0) || (getTop() != null && getTop() < -3) || (getLeft() != null && getLeft() < -3) || (PopupWidth != null && PopupWidth > 30 && getLeft() > PopupWidth) || (PopupHeight != null && PopupHeight > 30 && getTop() > PopupHeight);
        }
        if (_hidden == false) {
            _hidden = getPurviewWidth() == 0;
        }
        return _hidden != null ? _hidden : false;
    }


    @JsonIgnore
    public int getFieldType() {
        return ToInt32(get(_row, "FieldType", 0));
    }


    @JsonProperty("FieldDataType")
    public int getFieldDataType() {
        return ToInt32(get(_row, "FieldDataType", 0));
    }


    @Override
    public Integer getTop() {
        if (top == null) {
            // 1. 先获取原始值（可能为null）
            Object value = get(_row, "ControlTop", null);

            // 2. 处理null值：设置默认值0（或根据业务需求调整）
            if (value == null) {
                top = null; // 数据库为NULL时，默认值为0
            } else {
                // 3. 确保非null时再转换
                top = Integer.parseInt(value.toString());
            }
        }
        return top;
    }

    @Override
    public void setTop(Integer top) {
        this.top = top;
    }


    @Override
//    public Integer getLeft() {
//        if (left == null) {
//            // 1. 先获取原始值（可能为null）
//            Object value = get(_row, "ControlLeft", null);
//
//            // 2. 处理null值：设置默认值0（或根据业务需求调整）
//            if (value == null) {
//                left = null;
//            } else {
//                // 3. 确保非null时再转换
//                left = Integer.parseInt(value.toString());
//            }
//        }
//        return left;
//    }
    public Integer getLeft() {
        if (left == null) {
            Object value = get(_row, "ControlLeft", null);
            if (value == null) {
                left = 0;   // 默认值 0
            } else {
                try {
                    left = Integer.parseInt(value.toString());
                } catch (NumberFormatException e) {
                    left = 0; // 转换失败也给默认值
                }
            }
        }
        return left;
    }

    @Override
    public void setLeft(Integer left) {
        this.left = left;
    }

    private Integer _tabIndex;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Integer getTabIndex() {
        if (_tabIndex == null) {
            _tabIndex = (Integer) get(_row, "TabOrderId", null);
        }
        return _tabIndex;
    }

    public void setTabIndex(Integer tabIndex) {
        this._tabIndex = tabIndex;
    }

    private Boolean _allowblank = null;

    public boolean isAllowBlank() {
        if (_allowblank == null) {
            _allowblank = !toBoolean(get(_row, "Nullable", null));
        }
        return _allowblank;
    }

    public void setAllowBlank(Boolean allowblank) {
        this._allowblank = allowblank;
    }


    private String _enableCond;

    @JsonProperty("EnableCond")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getEnableCond() {
        if (_enableCond != null && !_enableCond.isEmpty()) {
            return _enableCond;
        }
        _enableCond = (String) get(_row, "disableCond", _enableCond, null);
        _enableCond = PublicUtil.ReqSqlPms(null, null, PublicUtil.SqlToCode(_enableCond), org.example.Enums.SystemTypeEnums.PmType.ignorenull, null);
        return _enableCond;
    }

    public void setEnableCond(String enableCond) {
        this._enableCond = enableCond;
    }


    @JsonProperty
    public Integer getCondEnableType() {
        int tp = ToInt32(get(_row, "disableType", null));
        if (tp == 0) {
            return null;
        }
        return tp;
    }

    public boolean enableDisValue;

    private Boolean _readOnly = null;

    public boolean isReadOnly() {
        if (_readOnly == null) {
            _readOnly = toBoolean(DataTableUtil.getRowVal(_row, "Edit", false));
        }
        if (!_readOnly) {
            _readOnly = !getOperPurview();
        }
        return _readOnly;
    }

    public void setReadOnly(Boolean readOnly) {
        this._readOnly = readOnly;
    }

    private Boolean _copyable = null;

    @JsonProperty("CopyAble")
    public boolean isCopyAble() {
        if (_copyable == null) {
            _copyable = DataTableUtil.getBooleanValue(_row, "copy", false);
        }
        return _copyable;
    }

    public void setCopyAble(Boolean copyable) {
        this._copyable = copyable;
    }

    private Boolean _multiple = null;

    @JsonProperty("Multiple")
    public boolean isMultiple() {
        if (_multiple == null) {
            _multiple = toBoolean(get(_row, "Multiple", "0"));
        }
        return _multiple;
    }

    public void setMultiple(Boolean multiple) {
        this._multiple = multiple;
    }

    private Boolean _sum = null;

    @JsonIgnore
    public boolean isIsSum() {
        if (_sum == null) {
            _sum = DataTableUtil.getBooleanValue(_row, "sum", false);
        }
        return _sum;
    }

    public void setSum(Boolean sum) {
        this._sum = sum;
    }


    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getSummaryType() {
        if (isIsSum()) {
            return "sum";
        }
        return null;
    }


    private String _sumText;

    @JsonProperty("SumText")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getSumText() {
        return (String) get(_row, "SumText", _sumText, null);
    }

    public void setSumText(String sumText) {
        this._sumText = sumText;
    }

    private String _sumCond;

    @JsonProperty("SumCond")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getSumCond() {
        String cond = (String) DataTableUtil.getRowVal(_row, "SumCond", _sumCond, null);
        if (cond != null && !cond.isEmpty()) {
            return PublicUtil.SqlToCode(cond.toLowerCase());
        }
        return null;
    }

    public void setSumCond(String sumCond) {
        this._sumCond = sumCond;
    }

    private String _sumCalc;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getSumCalc() {
        String calc = (String) DataTableUtil.getRowVal(_row, "sumCalc", _sumCalc, null);
        if (calc != null && !calc.isEmpty()) {
            return PublicUtil.SqlToCode(calc.toLowerCase());
        }
        return null;
    }

    public void setSumCalc(String sumCalc) {
        this._sumCalc = sumCalc;
    }

    private String _dataSource;

    @JsonIgnore
    @JsonProperty("DataSource")
    public String getDataSource() {
        if (_dataSource == null) {
            _dataSource = (String) get(_row, "FieldSQL", "");
        }
        return _dataSource;
    }

    public void setDataSource(String dataSource) {
        this._dataSource = dataSource;
    }

    private String _valueField;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getValueField() {
        if (_valueField != null && !_valueField.isEmpty()) {
            return _valueField.toLowerCase();
        }
        _valueField = (String) get(_row, "ValueMember", null);
        if (_valueField != null && !_valueField.isEmpty()) {
            return _valueField.toLowerCase();
        }
        return "dm";
    }

    public void setValueField(String valueField) {
        this._valueField = valueField;
    }

    private String _displayField;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getDisplayField() {
        if (_displayField != null && !_displayField.isEmpty()) {
            return _displayField;
        }
        _displayField = (String) get(_row, "DisplayMember", null);
        if (_displayField != null && !_displayField.isEmpty()) {
            _displayField = _displayField.toLowerCase();
            return _displayField;
        }
        return "mc";
    }

    public void setDisplayField(String displayField) {
        this._displayField = displayField;
    }

    private String _calcExpr;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getCalcExpr() {
        if (_calcExpr == null) {
            _calcExpr = (String) get(_row, "CalcExpress", null);
        }
        if (_calcExpr != null && !_calcExpr.isEmpty()) {
            return PublicUtil.SqlToCode(PublicUtil.ReqSqlPms(null, null, _calcExpr.toLowerCase(), org.example.Enums.SystemTypeEnums.PmType.ignorenull, null));
        }
        return _calcExpr;
    }


    public int getCalcOrder() {
        return ToInt32(get(_row, "CalcOrderId", 0));
    }

    private String _unionSQL;

    @JsonIgnore
    public String getUnionSQL() {
        if (_unionSQL == null) {
            _unionSQL = (String) DataTableUtil.getRowVal(_row, "UnionSQL", null);
        }
        return _unionSQL;
    }

    public void setUnionSQL(String unionSQL) {
        this._unionSQL = unionSQL;
    }

    private String _unionFields;

    @JsonProperty("UnionFields")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getUnionFields() {
        if (_unionFields != null && !_unionFields.isEmpty()) {
            return _unionFields.toLowerCase();
        }
        _unionFields = DataTableUtil.getStringValue(_row, "UnionField", null).toLowerCase();
        return _unionFields;
    }

    public void setUnionFields(String unionFields) {
        this._unionFields = unionFields;
    }

    protected String dataFormat;

    protected String getDataFormat() {
        return getFormat();
    }


    private String _format;

    @JsonProperty
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getFormat() {
        if (_format == null) {
            _format = DataTableUtil.getStringValue(_row, "dataformat", null);
        }
        if (_format == null || _format.isEmpty()) {
            _format = IPublicUtil.GetFormatByCType(getFieldType());
        }
        if (_format != null && _format.indexOf("#,") > -1) {
            _format = "###,###,###,###," + _format.substring(_format.indexOf(",") + 1);
        }
        return _format;
    }

    public void setFormat(String format) {
        this._format = format;
    }


    private String _addModule;

    @JsonProperty("AddModule")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getAddModule() {
        if (_addModule == null) {
            _addModule = (String) get(_row, "addModuleId", null);
        }
        return _addModule;
    }

    public void setAddModule(String addModule) {
        this._addModule = addModule;
    }

    private Object _enterBind;

    @JsonProperty
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Object getEnterBind() {
        if (_enterBind == null) {
            _enterBind = get(_row, "IsAddControl", null);
        }
        return _enterBind;
    }

    public void setEnterBind(Object enterBind) {
        this._enterBind = enterBind;
    }

    private String _addModuleSpec;

    @JsonProperty("AddModuleSpec")
    public String getAddModuleSpec() {
        if (_addModuleSpec == null) {
            _addModuleSpec = (String) get(_row, "AddModuleSpec", null);
        }
        return _addModuleSpec;
    }

    public void setAddModuleSpec(String addModuleSpec) {
        this._addModuleSpec = addModuleSpec;
    }

    private String _bandTitle;

    @JsonProperty("BandTitle")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getBandTitle() {
        if (_bandTitle == null) {
            _bandTitle = (String) get(_row, "BandTitle", null);
        }
        return _bandTitle;
    }

    public void setBandTitle(String bandTitle) {
        this._bandTitle = bandTitle;
    }

    private String _bandField;

    @JsonProperty("BandField")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getBandField() {
        if (_bandField == null) {
            _bandField = (String) get(_row, "BandField", null);
        }
        return _bandField;
    }

    public void setBandField(String bandField) {
        this._bandField = bandField;
    }

    private String _hintText;


    @JsonProperty("PlaceHolder")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getPlaceHolder() {
        if (_hintText == null) {
            _hintText = (String) get(_row, "HintText", null);
        }
        return _hintText;
    }

    public void setPlaceHolder(String hintText) {
        this._hintText = hintText;
    }

    private String _hintColor;

    @JsonProperty("HolderColor")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getHolderColor() {
        if (_hintColor == null) {
            _hintColor = (String) get(_row, "HintColor", null);
        }
        return _hintColor;
    }

    public void setHolderColor(String hintColor) {
        this._hintColor = hintColor;
    }

    private String _fontColor;

    @JsonProperty("LabelColor")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getLabelColor() {
        if (_fontColor == null) {
            _fontColor = (String) get(_row, "FontColor", null);
        }
        if ("0".equals(_fontColor)) {
            return null;
        }
        return _fontColor;
    }

    public void setLabelColor(String fontColor) {
        this._fontColor = fontColor;
    }

    private int _fontSize;

    @JsonProperty("FontSize")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Integer getFontSize() {
        _fontSize = (_fontSize <= 0) ? ToInt32(get(_row, "FontSize", null)) : _fontSize;
        return _fontSize;
    }

    public void setFontSize(Integer fontSize) {
        this._fontSize = ToInt32(fontSize);
    }

    private String _fcolor;

    @JsonProperty("FColor")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getFColor() {
        if (_fcolor == null) {
            _fcolor = (String) get(_row, "FColor", null);
        }
        return _fcolor;
    }


    public String _bcolor;

    @JsonProperty
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String getBColor() {
        if (_bcolor == null) {
            _bcolor = (String) get(_row, "BColor", null);
        }
        return _bcolor;
    }


    public Boolean _bold;

    @JsonProperty("Bold")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Boolean getBold() {
        if (_bold == null) {
            _bold = toBoolean(DataTableUtil.getRowVal(_row, "bold", null));
        }
        return _bold ? _bold : null;
    }

    public void setBold(Boolean bold) {
        this._bold = bold;
    }

    private Boolean _operPurview;

    @JsonIgnore
    public boolean getOperPurview() {
        if (_operPurview != null) {
            return _operPurview;
        }
        String strpur = (String) DataTableUtil.getRowVal(_row, "OperPurview", "");
        _operPurview = strpur.isEmpty() || strpur.contains(getUser().UserId);
        return _operPurview;
    }

    public void setOperPurview(Boolean operPurview) {
        this._operPurview = operPurview;
    }


    @JsonIgnore
    public Integer getPurviewWidth() {
        String purviewWidth = Objects.toString(get(_row, "PurviewWidth", null), "");
        if (isNullOrEmpty(purviewWidth)) return -1;
        else return ToInt32(purviewWidth);
    }

    private Boolean _readPurview;

    @JsonIgnore
    public Boolean getReadPurview() {
        if (_readPurview != null) {
            return _readPurview;
        }
        String strpur = (String) get(_row, "ReadPurview", "");
        _readPurview = strpur.isEmpty() || strpur.contains(getUser().UserId);
        return _readPurview;
    }

    public void setReadPurview(Boolean readPurview) {
        this._readPurview = readPurview;
    }

    private String _whereCond;

    @JsonIgnore
    public String getWhereCond() {
        if (_whereCond == null) {
            _whereCond = (String) get(_row, "WhereCond", null);
        }
        return _whereCond;
    }

    public void setWhereCond(String whereCond) {
        this._whereCond = whereCond;
    }


    @JsonInclude(JsonInclude.Include.NON_NULL)
    public String[] linknames;

    private Map<String, Object> _popPms;

    @JsonProperty("PopPms")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    public Map<String, Object> getPopPms() {
        boolean linkNamesCheck = false;
        if (_popPms != null && linknames != null && linknames.length > 0) {
            for (String name : linknames) {
                if (_popPms.containsKey(name)) {
                    linkNamesCheck = true;
                    break; // 找到一个匹配就跳出循环
                }
            }
        }
        boolean hasPlaceholder = (getUnionSQL() != null && !getUnionSQL().isEmpty() && getUnionSQL().indexOf("{#p") > -1)
                || (!getDataSource().isEmpty() && getDataSource().indexOf("{#p") > -1) ||
                (!getEnableCond().isEmpty() && getEnableCond().indexOf("{#p") > -1) ||
                (linkNamesCheck);
        if (hasPlaceholder) {
            return _popPms;
        }
        return null;
    }

    public void setPopPms(Map<String, Object> popPms) {
        this._popPms = popPms;
    }

    private Object _value;

    public Object getValue() {
        String defaultSource = getDefaultsource() == null ? "" : getDefaultsource();
        String defaultVal = getDefaultval() == null ? "" : getDefaultval().toString();
        return defaultSource.startsWith("{#p_") && defaultVal.isEmpty() ? defaultSource : getDefaultval();
    }

    public void setValue(Object value) {
        this._value = value;
    }

    private String _limitMaxValue;

    @JsonIgnore
    public Object getLimitMaxValue() {
        Object value = DataTableUtil.getRowVal(_row, "LimitMaxValue", null);
        if (_limitMaxValue == null) {
            _limitMaxValue = getUtil().GetDefaultValue(value == null ? "" : value.toString(), null, SystemTypeEnums.PmType.sql);
        }
        return isNullOrEmpty(_limitMaxValue) ? null : _limitMaxValue;
    }

    private String _limitMinValue;

    @JsonIgnore
    @JsonProperty("LimitMinValue")
    public Object getLimitMinValue() {
        Object value = DataTableUtil.getRowVal(_row, "LimitMinValue", null);
        if (_limitMinValue == null) {
            _limitMinValue = getUtil().GetDefaultValue(value == null ? "" : value.toString(), null, SystemTypeEnums.PmType.sql);
        }
        return isNullOrEmpty(_limitMinValue) ? null : _limitMinValue;
    }

    protected String pspec;

    @JsonIgnore
    public String getDefaultsource() {
        return (String) get(_row, "defaultvalue", null);
    }

    private Object _defaultval;

    public Object getDefaultval() {
        return _defaultval;
    }

    public void setDefaultval(Object defaultval) {
        this._defaultval = defaultval;
    }

    public void setDefaultVal(Map<String, Object> updrow) {
        this.setDefaultval(DataTableUtil.getRowVal(updrow, getName(), ""));
    }

    private int parseIntSafe(Object value, int defaultValue) {
        if (value == null) {
            return defaultValue;
        }

        if (value instanceof Integer) {
            return (Integer) value;
        }

        if (value instanceof Number) {
            return ((Number) value).intValue();
        }

        String str = value.toString().trim();
        if (str.isEmpty()) {
            return defaultValue;
        }

        try {
            return Integer.parseInt(str);
        } catch (NumberFormatException e) {
            return defaultValue;
        }
    }
}