using System; using System.Collections.Generic; using System.Text; using System.Data; using SysBaseLibs; namespace SysDataLibs { public class rsDDColumn { // private private string _ColumnId = ""; private string _ColumnName = ""; private int _Width = 0; //private int _ColumnLength = 0; private rsDataType _DataType = null; private bool _IsAuto = false; private bool _IsPrimaryKey = false; private bool _isNull = true; private DataRow _DataRow=null; private string _ColumnDesc = ""; private string _DefValue = ""; private bool _IsVisable = false; private int _Sequence = -1; private string _RelationColumn = ""; private Dictionary _SaveObject = null; private string _DisplayValue = ""; private string _IsSelect = "Y"; private string _Format = ""; private DBConnSql _DBConn = null; public rsDDColumn(DataRow poRow) { _DataRow = poRow; if (_DataRow != null) { _ColumnId = _DataRow["ColumnId"].ToString().Trim(); _DataType = new rsDataType(_DataRow["DataType"].ToString()); _DataType.ColLength = Utils.ValI(_DataRow["CharNum"].ToString()); _DataType.ColPrecision = Utils.ValI(_DataRow["ColPrecision"].ToString()); _DataType.ColDecimal = Utils.ValI(_DataRow["ColDecimal"].ToString()); _IsAuto = UtilStr.StrToBool(_DataRow["AutoGen"].ToString()); _IsPrimaryKey = UtilStr.StrToBool(_DataRow["PrimaryKey"].ToString()); _isNull = UtilStr.StrToBool(_DataRow["AllowNull"].ToString()); _ColumnDesc = _DataRow["ColDesc"].ToString(); if (_DataRow["DefValue"] != null) _DefValue = _DataRow["DefValue"].ToString(); if (_DefValue.Trim().Length == 0) _DefValue = _DataType.DBEmptyValue; if (_DataRow["width"] != null) _Width = Utils.ValI(_DataRow["width"].ToString()); if (_DataRow["IsVisable"] != null) _IsVisable = UtilStr.StrToBool(_DataRow["IsVisable"].ToString()); if (_DataRow["Sequence"] != null) _Sequence = Utils.ValI(_DataRow["Sequence"].ToString()); if (_DataRow["ColumnName"] != null) _ColumnName = _DataRow["ColumnName"].ToString().Trim(); if (_DataRow["RelationColumns"] != null) _RelationColumn = _DataRow["RelationColumns"].ToString().Trim(); if (_DataRow["DisplayValue"] != null) _DisplayValue = _DataRow["DisplayValue"].ToString().Trim(); if (_DataRow["IsSelect"] != null) _IsSelect = _DataRow["IsSelect"].ToString().Trim(); if (_DataRow["Format"] != null) _Format = _DataRow["Format"].ToString().Trim(); } } public rsDDColumn(string pcColumnId, string pcColumnName, int piWidth, int piColumnLength, string pcDataType, bool pbIsAuto, bool pbIsPrimaryKey,bool pbisNull) { _ColumnId = pcColumnId; _ColumnName = pcColumnName; _Width = piWidth; _DataType.ColLength = piColumnLength; _DataType = new rsDataType(pcDataType); _IsAuto = pbIsAuto; _IsPrimaryKey = pbIsPrimaryKey; _isNull = pbisNull; } /// /// 字段名称 /// public string ColumnName { get { return _ColumnName; } set { _ColumnName = value; } } /// /// 字段编号 /// public string ColumnId { get { return _ColumnId; } } /// /// 字段显示宽度 /// public int Width { get { return _Width; } set { _Width = value; } } /// /// 字段长度 /// public int ColumnLength { get { return _DataType.ColLength; } } /// /// 字段类型 /// public rsDataType DataType { get { return _DataType; } } /// /// 是否自动增长 /// public bool IsAuto { get { return _IsAuto; } } /// /// 是否是主键 /// public bool IsPrimaryKey { get { return _IsPrimaryKey; } } /// /// 是否可见 /// public bool IsVisable { get { return _IsVisable; } set { _IsVisable = value; } } /// /// 是否是数字 /// public bool IsNum { get { return _DataType.IsNumeric; } } /// /// 字段排序 /// public int Sequence { get { return _Sequence; } } /// /// 关联的字段 /// public string RelationColunm { get { return _RelationColumn; } } /// /// 显示的字段 如[[1,男],[0,女]] /// public string DisplayValue { get { return _DisplayValue; } set { _DisplayValue = value; } } /// /// 进行自动拼装sql 语句的时候,该字段是否参与 /// public string IsSelect { get { return _IsSelect; } set { _IsSelect = value; } } /// /// 字段数据格式化,主要针对 日期 和 数值 /// public string Format { get { return _Format; } set { _Format = value; } } public void putValue(string pcKey, object poValue) { if (_SaveObject == null) _SaveObject = new Dictionary(); if (_SaveObject.ContainsKey(pcKey)) _SaveObject[pcKey] = poValue; else _SaveObject.Add(pcKey, poValue); } public string GetValue(string pcKey) { string lcRetVal = ""; object loObj = GetValueObj(pcKey); if (loObj != null) lcRetVal = loObj.ToString(); return lcRetVal; } public object GetValueObj(string pcKey) { object loRetVal = null; if (_SaveObject != null && _SaveObject.ContainsKey(pcKey)) { loRetVal = _SaveObject[pcKey]; } return loRetVal; } private rsQuery _RelationTable = null; private string _RelationTableId = ""; private string _RelationColumnId = ""; private string GetTableId { get { if (_RelationTableId == "") { if (_RelationColumn != null && _RelationColumn.Trim().Length > 0) { _RelationTableId = DALibs.GetTableId(_RelationColumn); } } return _RelationTableId; } } private string GetColumnId { get { if (_RelationColumnId == "") if (_RelationColumn != null && _RelationColumn.Trim().Length > 0) { _RelationColumnId = DALibs.GetColumnId(_RelationColumn); } return _RelationColumnId; } } /// /// /// private Dictionary _DisplayValues = null; /// /// 获取字段的显示值 比如在表 DisplayValue 定义 [1:男],[2:女] 则 传递 1 返回 男 /// /// /// public string GetDisplayValue(string pcValue) { string lcRetVal = pcValue; if (_DisplayValues == null) { if (_DisplayValue.Trim().Length > 0) { Array loArr = UtilStr.StrToArray(_DisplayValue); if (loArr != null && loArr.Length > 0) { _DisplayValues = new Dictionary(); foreach (string lcStr in loArr) { Array loArr2 = UtilStr.StrToArrayEx(lcStr, ":"); if (loArr2 != null && loArr2.Length == 2) { string lcKey = UtilStr.UAndT(loArr2.GetValue(0).ToString().Trim()); if (!_DisplayValues.ContainsKey(lcKey)) _DisplayValues.Add(loArr2.GetValue(0).ToString(), loArr2.GetValue(1).ToString()); else _DisplayValues[lcKey] = loArr2.GetValue(1).ToString(); } } } } } if (_DisplayValues != null) { if (_DisplayValues.ContainsKey(UtilStr.UAndT(pcValue))) lcRetVal = _DisplayValues[UtilStr.UAndT(pcValue)].ToString().Trim(); } return lcRetVal; } /// /// 想关联字段赋值 比如 该字段 是MarketID 在User表中,要获取相应的MarketName 则 RelationClumn 为 Markets.MarketName /// /// public void SetDbConn(DBConnSql poDbConn) { if (GetTableId.Trim().Length > 0) { _DBConn = poDbConn; string lcSql = " select " + this.ColumnId + "," + GetColumnId + " from " + GetTableId; _RelationTable = _DBConn.OpenQuery(lcSql); } } /// /// 获取关联或真实显示的信息 优先显示 DisplayValue 再显示 RelationValue /// /// /// public string DispalyValueTranslate(string pcValue) { string lcRetVal = pcValue; string lcValue = ""; lcValue = GetDisplayValue(pcValue); if (string.IsNullOrEmpty(lcValue)) { lcValue = GetRelationValue(pcValue); if (lcValue != pcValue) { lcRetVal = lcValue; } } else { if (lcValue != pcValue) { lcRetVal = lcValue; } } return lcRetVal; } /// /// 获取RelationValue /// /// /// public string GetRelationValue(string pcStr) { string lcRetVal = pcStr; if (pcStr.Trim().Length > 0) { if (_RelationTable != null && _RelationTable.IsOpened) { if (_RelationTable.GoToRecordByFieldsAndValues(this.ColumnId, pcStr)) lcRetVal = _RelationTable.GetString(GetColumnId); } } return lcRetVal; } /// /// 判断与当前的字段是否匹配 /// /// /// public bool MatchColumnId(string pcColumnId) { pcColumnId = UtilStr.UAndT(pcColumnId); return UtilStr.UAndT(this.ColumnId) == pcColumnId; } /// /// 获取 字段的对象 /// /// /// /// public static rsDDColumn GetColumnById(List poColumns, string pcColumnId) { rsDDColumn loRetVal = null; if (poColumns != null && pcColumnId != null) { pcColumnId=UtilStr.UAndT(pcColumnId); foreach (rsDDColumn loCol in poColumns) { if (UtilStr.UAndT(loCol.ColumnId) == pcColumnId) { loRetVal = loCol; break; } } } return loRetVal; } } }