123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Collections;
- using SysBaseLibs;
- using System.Data;
- using System.Web.UI.WebControls ;
- namespace SysDataLibs
- {
- public class rsDDTable
- {
- protected string _TableName = "";
- public rsDDTable()
- {
- Initialize();
- }
- public rsDDTable(string pcTableName,List<rsDDColumn> poList)
- {
- _TableName = pcTableName;
- _ColumnList = poList;
- }
- public rsDDTable(string pcTableName, DBConnSql poDBConn)
- {
- _TableName = pcTableName;
- if (poDBConn != null && poDBConn.IsOpened)
- {
- _ColumnList = GetTableColsByTableName(pcTableName, poDBConn);
- }
- }
- public static List<rsDDColumn> GetTableColsByTableName(string pcTableName, DBConnSql poDBConn)
- {
- List<rsDDColumn> loRetVal = new List<rsDDColumn>();
- string lcSql = "select * from "+TableClass.Tn.Sys_TableColumns+" where TableId = '" + pcTableName + "' order by Sequence";
- DataTable loTable = poDBConn.OpenDataTable(lcSql);
- if (loTable != null)
- {
- foreach (DataRow lodr in loTable.Rows)
- {
- rsDDColumn loColumn = new rsDDColumn(lodr);
- loColumn.SetDbConn(poDBConn);
- loRetVal.Add(loColumn);
- }
- }
- return loRetVal;
- }
- public string TableName
- {
- get { return _TableName; }
- }
- protected List<rsDDColumn> _ColumnList = null;
- public List<rsDDColumn> ColumnList
- {
- get
- {
- if (_ColumnList == null)
- _ColumnList = new List<rsDDColumn>();
- return _ColumnList;
- }
- }
- public List<rsDDColumn> AvaiColumnList
- {
- get
- {
- List<rsDDColumn> loRetVal = new List<rsDDColumn>();
- foreach (rsDDColumn column in ColumnList)
- {
- if (column.IsVisable)
- {
- loRetVal.Add(column);
- }
- }
- return loRetVal;
- }
- }
- public List<rsDDColumn> GetUserAvaiColumnList(UserSession poUserSession)
- {
- List<rsDDColumn> loRetVal = new List<rsDDColumn>();
- string lcUserColumn = GetUserDefaultCols(poUserSession);
- Array loArr = UtilStr.StrToArray(lcUserColumn);
- foreach (string column in loArr)
- {
- rsDDColumn loColumn = GetColumnByColumnId(column);
- if (loColumn != null)
- loRetVal.Add(loColumn);
- }
- return loRetVal;
- }
- public static Array GetArrayByString(string pcString, int piWidth)
- {
- Array loArr = UtilStr.StrToArray(pcString);
- int liAll = 0;
- piWidth = piWidth - 22;
- foreach (string lcWd in loArr)
- {
- int liWd = Convert.ToInt32(lcWd);
- liAll += liWd;
- }
- ArrayList loList = new ArrayList();
- foreach (string lcWd in loArr)
- {
- int liWd = Convert.ToInt32(lcWd);
- loList.Add((piWidth * liWd) / liAll);
- }
- int liCount = 0;
- foreach (int liWd in loList)
- {
- liCount += liWd;
- }
- loList[loList.Count - 1] = Convert.ToInt32(loList[loList.Count - 1]) + (piWidth - liCount);
- Array array1 = Array.CreateInstance(typeof(int), loList.Count);
- loList.CopyTo(array1);
- return array1;
- }
- //public int GetColumnWith(UserSession poSession, rsDDColumn poColumn, int AllWidth)
- //{
- // int liRetVal = 0;
-
- // return liRetVal;
- //}
- public string AllColumnsStr
- {
- get {
- string lcRetVal = "";
- if (_ColumnList != null)
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- lcRetVal += (lcRetVal==""?"":",")+loColumn.ColumnId;
- }
- }
- return lcRetVal;
- }
- }
- /// <summary>
- /// 所有的非自动增长的字段
- /// </summary>
- public virtual string VisiableColumns
- {
- get
- {
- string lcRetVal = "";
- if (_ColumnList != null)
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (!loColumn.IsAuto)
- lcRetVal += (lcRetVal == ""?"":",") + loColumn.ColumnId;
- }
- }
- return lcRetVal;
- }
- }
- /// <summary>
- /// 所有的可见字段
- /// </summary>
- public virtual string AvaiColumns
- {
- get
- {
- string lcRetVal = "";
- if (_ColumnList != null)
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (loColumn.IsVisable)
- lcRetVal += (lcRetVal == ""?"":",") + loColumn.ColumnId;
- }
- }
- return lcRetVal;
- }
- }
-
- /// <summary>
- /// 所有的可见显示的字段名称
- /// </summary>
- public virtual string DisplayColumns
- {
- get
- {
- string lcRetVal = "";
- if (_ColumnList != null)
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (loColumn.IsVisable)
- lcRetVal += (lcRetVal == "" ? "" : ",") + loColumn.ColumnName;
- }
- }
- return lcRetVal;
- }
- }
- public rsQuery GetUserDefaultQuery(DBConnSql poDBConn, string pcUserId)
- {
- string lcSql = "select * from "+SysDataLibs.TableClass.Tn.Sys_UserDefaultColumns +" where TableId= '" + _TableName + "' and UserId= '" + pcUserId + "' order by Sequence ";
- return poDBConn.OpenQuery(lcSql);
- }
- /// <summary>
- /// 得到用户默认的显示字段的ID field1,field2
- /// </summary>
- /// <param name="poSession"></param>
- /// <returns></returns>
- public string GetUserDefaultCols(UserSession poSession)
- {
- string lcRetVal = AvaiColumns;
- rsQuery loQuery = GetUserDefaultQuery(poSession.DBConn, poSession.UserInfo.UserID);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- lcRetVal = "";
- loQuery.MoveFirst();
- for (int i = 0; i < loQuery.RecCount; i++)
- {
- string lcColumnId = loQuery.GetString("ColumnId");
- rsDDColumn loCol = GetColumnByColumnId(lcColumnId);
- if (loCol != null)
- {
- lcRetVal += (lcRetVal == "" ? "" : ",") + loCol.ColumnId.Trim();
- }
- loQuery.MoveNext();
- }
- }
- return lcRetVal;
- }
- /// <summary>
- /// 得到用户默认的显示字段的名称 name1,name2
- /// </summary>
- /// <param name="poSession"></param>
- /// <returns></returns>
- public string GetUserDefaultColNames(UserSession poSession)
- {
- string lcRetVal = DisplayColumns;
- rsQuery loQuery = GetUserDefaultQuery(poSession.DBConn, poSession.UserInfo.UserID);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- lcRetVal = "";
- loQuery.MoveFirst();
- for (int i = 0; i < loQuery.RecCount; i++)
- {
- string lcColumnId = loQuery.GetString("ColumnId");
- rsDDColumn loCol = GetColumnByColumnId(lcColumnId);
- if (loCol != null)
- {
- lcRetVal += (lcRetVal == "" ? "" : ",") + loCol.ColumnName.Trim();
- }
- loQuery.MoveNext();
- }
- }
- return lcRetVal;
- }
- /// <summary>
- /// 得到用户默认的显示字段的宽度
- /// </summary>
- /// <param name="poSession"></param>
- /// <returns></returns>
- public string GetUserDefaultColWidths(UserSession poSession)
- {
- string lcRetVal = ColumnWidths;
- rsQuery loQuery = GetUserDefaultQuery(poSession.DBConn, poSession.UserInfo.UserID);
- if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
- {
- lcRetVal = "";
- loQuery.MoveFirst();
- for (int i = 0; i < loQuery.RecCount; i++)
- {
- string lcColumnId = loQuery.GetString("ColumnId");
- rsDDColumn loCol = GetColumnByColumnId(lcColumnId);
- if (loCol != null)
- {
- lcRetVal += (lcRetVal == "" ? "" : ",") + loCol.Width;
- }
- loQuery.MoveNext();
- }
- }
- return lcRetVal;
- }
- public virtual string ColumnWidths
- {
- get
- {
- string lcRetVal = "";
- if (_ColumnList != null)
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (loColumn.IsVisable)
- lcRetVal += (lcRetVal == "" ? "" : ",") + loColumn.Width;
- }
- }
- return lcRetVal;
- }
- }
- public rsDDColumn GetColumnByColumnId(string pcColumnId)
- {
- rsDDColumn loRetVal = null;
- if (_ColumnList != null)
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (UtilStr.UAndT(loColumn.ColumnId) == UtilStr.UAndT(pcColumnId))
- {
- loRetVal = loColumn;
- break;
- }
- }
- }
- return loRetVal;
- }
-
- public void SetTableColumn(rsDDColumn poColumn)
- {
- if (_ColumnList == null)
- _ColumnList = new List<rsDDColumn>();
- if (IsEixst(poColumn))
- _ColumnList.Add(poColumn);
- }
- private string _PrimaryKey = "";
- public string PrimaryKeys
- {
- get {
- if (_PrimaryKey == "")
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (loColumn.IsPrimaryKey)
- _PrimaryKey += (_PrimaryKey == ""?"":"," )+ loColumn.ColumnId;
- }
- }
- return _PrimaryKey;
- }
- }
- public bool IsAutoGen
- {
- get
- {
- bool lbRetVal = false;
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (loColumn.IsAuto)
- {
- lbRetVal = true;
- break;
- }
- }
- return lbRetVal;
- }
- }
- private bool IsEixst(rsDDColumn poColumn)
- {
- bool lbRetVal = false;
- if (_ColumnList != null&&poColumn!=null)
- {
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (UtilStr.UAndT(loColumn.ColumnId) == UtilStr.UAndT(poColumn.ColumnId))
- {
- lbRetVal = true;
- break;
- }
- }
- }
- return lbRetVal;
- }
- private string _InsertSql = "";
- private string _UpdateSql = "";
- private string _DeleteSql = "";
- public string InsertSql
- {
- get
- {
- if (_InsertSql.Trim().Length == 0)
- CreateSqlStr();
- return _InsertSql;
- }
- }
- public string UpdateSql
- {
- get
- {
- if (_UpdateSql.Trim().Length == 0)
- CreateSqlStr();
- return _UpdateSql;
- }
- }
- public string DeleteSql
- {
- get
- {
- if (_DeleteSql.Trim().Length == 0)
- CreateSqlStr();
- return _DeleteSql;
- }
- }
- private void CreateSqlStr()
- {
- string lcInsertFields = "";
- string lcInsertValues = "";
- string lcUpdateSet = "";
- string lcWhere = "";
- foreach (rsDDColumn loColumn in _ColumnList)
- {
- if (!loColumn.IsPrimaryKey)
- lcUpdateSet += (lcUpdateSet == "" ? "" : ",") + loColumn.ColumnId + "=" + "@" + loColumn.ColumnId;
- else
- lcWhere += (lcWhere == "" ? "" : " AND ") + loColumn.ColumnId + "=" + "@OOLLDD" + loColumn.ColumnId;
- if (!loColumn.IsAuto)
- {
- lcInsertFields += (lcInsertFields == "" ? "" : ",") + loColumn.ColumnId;
- lcInsertValues += (lcInsertValues == "" ? "" : ",") + "@" + loColumn.ColumnId;
- }
- }
- _InsertSql = "INSERT INTO " + this._TableName + " (" + lcInsertFields + ") VALUES (" + lcInsertValues + ")";
- _UpdateSql = "UPDATE " + _TableName + " SET " + lcUpdateSet + " WHERE " + lcWhere;
- _DeleteSql = "DELETE FROM " + _TableName + " WHERE " + lcWhere;
- }
- public virtual void Initialize()
- {
- }
- }
- }
|