rsDDTable.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Collections;
  5. using SysBaseLibs;
  6. using System.Data;
  7. using System.Web.UI.WebControls ;
  8. namespace SysDataLibs
  9. {
  10. public class rsDDTable
  11. {
  12. protected string _TableName = "";
  13. public rsDDTable()
  14. {
  15. Initialize();
  16. }
  17. public rsDDTable(string pcTableName,List<rsDDColumn> poList)
  18. {
  19. _TableName = pcTableName;
  20. _ColumnList = poList;
  21. }
  22. public rsDDTable(string pcTableName, DBConnSql poDBConn)
  23. {
  24. _TableName = pcTableName;
  25. if (poDBConn != null && poDBConn.IsOpened)
  26. {
  27. _ColumnList = GetTableColsByTableName(pcTableName, poDBConn);
  28. }
  29. }
  30. public static List<rsDDColumn> GetTableColsByTableName(string pcTableName, DBConnSql poDBConn)
  31. {
  32. List<rsDDColumn> loRetVal = new List<rsDDColumn>();
  33. string lcSql = "select * from "+TableClass.Tn.Sys_TableColumns+" where TableId = '" + pcTableName + "' order by Sequence";
  34. DataTable loTable = poDBConn.OpenDataTable(lcSql);
  35. if (loTable != null)
  36. {
  37. foreach (DataRow lodr in loTable.Rows)
  38. {
  39. rsDDColumn loColumn = new rsDDColumn(lodr);
  40. loColumn.SetDbConn(poDBConn);
  41. loRetVal.Add(loColumn);
  42. }
  43. }
  44. return loRetVal;
  45. }
  46. public string TableName
  47. {
  48. get { return _TableName; }
  49. }
  50. protected List<rsDDColumn> _ColumnList = null;
  51. public List<rsDDColumn> ColumnList
  52. {
  53. get
  54. {
  55. if (_ColumnList == null)
  56. _ColumnList = new List<rsDDColumn>();
  57. return _ColumnList;
  58. }
  59. }
  60. public List<rsDDColumn> AvaiColumnList
  61. {
  62. get
  63. {
  64. List<rsDDColumn> loRetVal = new List<rsDDColumn>();
  65. foreach (rsDDColumn column in ColumnList)
  66. {
  67. if (column.IsVisable)
  68. {
  69. loRetVal.Add(column);
  70. }
  71. }
  72. return loRetVal;
  73. }
  74. }
  75. public List<rsDDColumn> GetUserAvaiColumnList(UserSession poUserSession)
  76. {
  77. List<rsDDColumn> loRetVal = new List<rsDDColumn>();
  78. string lcUserColumn = GetUserDefaultCols(poUserSession);
  79. Array loArr = UtilStr.StrToArray(lcUserColumn);
  80. foreach (string column in loArr)
  81. {
  82. rsDDColumn loColumn = GetColumnByColumnId(column);
  83. if (loColumn != null)
  84. loRetVal.Add(loColumn);
  85. }
  86. return loRetVal;
  87. }
  88. public static Array GetArrayByString(string pcString, int piWidth)
  89. {
  90. Array loArr = UtilStr.StrToArray(pcString);
  91. int liAll = 0;
  92. piWidth = piWidth - 22;
  93. foreach (string lcWd in loArr)
  94. {
  95. int liWd = Convert.ToInt32(lcWd);
  96. liAll += liWd;
  97. }
  98. ArrayList loList = new ArrayList();
  99. foreach (string lcWd in loArr)
  100. {
  101. int liWd = Convert.ToInt32(lcWd);
  102. loList.Add((piWidth * liWd) / liAll);
  103. }
  104. int liCount = 0;
  105. foreach (int liWd in loList)
  106. {
  107. liCount += liWd;
  108. }
  109. loList[loList.Count - 1] = Convert.ToInt32(loList[loList.Count - 1]) + (piWidth - liCount);
  110. Array array1 = Array.CreateInstance(typeof(int), loList.Count);
  111. loList.CopyTo(array1);
  112. return array1;
  113. }
  114. //public int GetColumnWith(UserSession poSession, rsDDColumn poColumn, int AllWidth)
  115. //{
  116. // int liRetVal = 0;
  117. // return liRetVal;
  118. //}
  119. public string AllColumnsStr
  120. {
  121. get {
  122. string lcRetVal = "";
  123. if (_ColumnList != null)
  124. {
  125. foreach (rsDDColumn loColumn in _ColumnList)
  126. {
  127. lcRetVal += (lcRetVal==""?"":",")+loColumn.ColumnId;
  128. }
  129. }
  130. return lcRetVal;
  131. }
  132. }
  133. /// <summary>
  134. /// 所有的非自动增长的字段
  135. /// </summary>
  136. public virtual string VisiableColumns
  137. {
  138. get
  139. {
  140. string lcRetVal = "";
  141. if (_ColumnList != null)
  142. {
  143. foreach (rsDDColumn loColumn in _ColumnList)
  144. {
  145. if (!loColumn.IsAuto)
  146. lcRetVal += (lcRetVal == ""?"":",") + loColumn.ColumnId;
  147. }
  148. }
  149. return lcRetVal;
  150. }
  151. }
  152. /// <summary>
  153. /// 所有的可见字段
  154. /// </summary>
  155. public virtual string AvaiColumns
  156. {
  157. get
  158. {
  159. string lcRetVal = "";
  160. if (_ColumnList != null)
  161. {
  162. foreach (rsDDColumn loColumn in _ColumnList)
  163. {
  164. if (loColumn.IsVisable)
  165. lcRetVal += (lcRetVal == ""?"":",") + loColumn.ColumnId;
  166. }
  167. }
  168. return lcRetVal;
  169. }
  170. }
  171. /// <summary>
  172. /// 所有的可见显示的字段名称
  173. /// </summary>
  174. public virtual string DisplayColumns
  175. {
  176. get
  177. {
  178. string lcRetVal = "";
  179. if (_ColumnList != null)
  180. {
  181. foreach (rsDDColumn loColumn in _ColumnList)
  182. {
  183. if (loColumn.IsVisable)
  184. lcRetVal += (lcRetVal == "" ? "" : ",") + loColumn.ColumnName;
  185. }
  186. }
  187. return lcRetVal;
  188. }
  189. }
  190. public rsQuery GetUserDefaultQuery(DBConnSql poDBConn, string pcUserId)
  191. {
  192. string lcSql = "select * from "+SysDataLibs.TableClass.Tn.Sys_UserDefaultColumns +" where TableId= '" + _TableName + "' and UserId= '" + pcUserId + "' order by Sequence ";
  193. return poDBConn.OpenQuery(lcSql);
  194. }
  195. /// <summary>
  196. /// 得到用户默认的显示字段的ID field1,field2
  197. /// </summary>
  198. /// <param name="poSession"></param>
  199. /// <returns></returns>
  200. public string GetUserDefaultCols(UserSession poSession)
  201. {
  202. string lcRetVal = AvaiColumns;
  203. rsQuery loQuery = GetUserDefaultQuery(poSession.DBConn, poSession.UserInfo.UserID);
  204. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  205. {
  206. lcRetVal = "";
  207. loQuery.MoveFirst();
  208. for (int i = 0; i < loQuery.RecCount; i++)
  209. {
  210. string lcColumnId = loQuery.GetString("ColumnId");
  211. rsDDColumn loCol = GetColumnByColumnId(lcColumnId);
  212. if (loCol != null)
  213. {
  214. lcRetVal += (lcRetVal == "" ? "" : ",") + loCol.ColumnId.Trim();
  215. }
  216. loQuery.MoveNext();
  217. }
  218. }
  219. return lcRetVal;
  220. }
  221. /// <summary>
  222. /// 得到用户默认的显示字段的名称 name1,name2
  223. /// </summary>
  224. /// <param name="poSession"></param>
  225. /// <returns></returns>
  226. public string GetUserDefaultColNames(UserSession poSession)
  227. {
  228. string lcRetVal = DisplayColumns;
  229. rsQuery loQuery = GetUserDefaultQuery(poSession.DBConn, poSession.UserInfo.UserID);
  230. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  231. {
  232. lcRetVal = "";
  233. loQuery.MoveFirst();
  234. for (int i = 0; i < loQuery.RecCount; i++)
  235. {
  236. string lcColumnId = loQuery.GetString("ColumnId");
  237. rsDDColumn loCol = GetColumnByColumnId(lcColumnId);
  238. if (loCol != null)
  239. {
  240. lcRetVal += (lcRetVal == "" ? "" : ",") + loCol.ColumnName.Trim();
  241. }
  242. loQuery.MoveNext();
  243. }
  244. }
  245. return lcRetVal;
  246. }
  247. /// <summary>
  248. /// 得到用户默认的显示字段的宽度
  249. /// </summary>
  250. /// <param name="poSession"></param>
  251. /// <returns></returns>
  252. public string GetUserDefaultColWidths(UserSession poSession)
  253. {
  254. string lcRetVal = ColumnWidths;
  255. rsQuery loQuery = GetUserDefaultQuery(poSession.DBConn, poSession.UserInfo.UserID);
  256. if (loQuery != null && loQuery.IsOpened && loQuery.RecCount > 0)
  257. {
  258. lcRetVal = "";
  259. loQuery.MoveFirst();
  260. for (int i = 0; i < loQuery.RecCount; i++)
  261. {
  262. string lcColumnId = loQuery.GetString("ColumnId");
  263. rsDDColumn loCol = GetColumnByColumnId(lcColumnId);
  264. if (loCol != null)
  265. {
  266. lcRetVal += (lcRetVal == "" ? "" : ",") + loCol.Width;
  267. }
  268. loQuery.MoveNext();
  269. }
  270. }
  271. return lcRetVal;
  272. }
  273. public virtual string ColumnWidths
  274. {
  275. get
  276. {
  277. string lcRetVal = "";
  278. if (_ColumnList != null)
  279. {
  280. foreach (rsDDColumn loColumn in _ColumnList)
  281. {
  282. if (loColumn.IsVisable)
  283. lcRetVal += (lcRetVal == "" ? "" : ",") + loColumn.Width;
  284. }
  285. }
  286. return lcRetVal;
  287. }
  288. }
  289. public rsDDColumn GetColumnByColumnId(string pcColumnId)
  290. {
  291. rsDDColumn loRetVal = null;
  292. if (_ColumnList != null)
  293. {
  294. foreach (rsDDColumn loColumn in _ColumnList)
  295. {
  296. if (UtilStr.UAndT(loColumn.ColumnId) == UtilStr.UAndT(pcColumnId))
  297. {
  298. loRetVal = loColumn;
  299. break;
  300. }
  301. }
  302. }
  303. return loRetVal;
  304. }
  305. public void SetTableColumn(rsDDColumn poColumn)
  306. {
  307. if (_ColumnList == null)
  308. _ColumnList = new List<rsDDColumn>();
  309. if (IsEixst(poColumn))
  310. _ColumnList.Add(poColumn);
  311. }
  312. private string _PrimaryKey = "";
  313. public string PrimaryKeys
  314. {
  315. get {
  316. if (_PrimaryKey == "")
  317. {
  318. foreach (rsDDColumn loColumn in _ColumnList)
  319. {
  320. if (loColumn.IsPrimaryKey)
  321. _PrimaryKey += (_PrimaryKey == ""?"":"," )+ loColumn.ColumnId;
  322. }
  323. }
  324. return _PrimaryKey;
  325. }
  326. }
  327. public bool IsAutoGen
  328. {
  329. get
  330. {
  331. bool lbRetVal = false;
  332. foreach (rsDDColumn loColumn in _ColumnList)
  333. {
  334. if (loColumn.IsAuto)
  335. {
  336. lbRetVal = true;
  337. break;
  338. }
  339. }
  340. return lbRetVal;
  341. }
  342. }
  343. private bool IsEixst(rsDDColumn poColumn)
  344. {
  345. bool lbRetVal = false;
  346. if (_ColumnList != null&&poColumn!=null)
  347. {
  348. foreach (rsDDColumn loColumn in _ColumnList)
  349. {
  350. if (UtilStr.UAndT(loColumn.ColumnId) == UtilStr.UAndT(poColumn.ColumnId))
  351. {
  352. lbRetVal = true;
  353. break;
  354. }
  355. }
  356. }
  357. return lbRetVal;
  358. }
  359. private string _InsertSql = "";
  360. private string _UpdateSql = "";
  361. private string _DeleteSql = "";
  362. public string InsertSql
  363. {
  364. get
  365. {
  366. if (_InsertSql.Trim().Length == 0)
  367. CreateSqlStr();
  368. return _InsertSql;
  369. }
  370. }
  371. public string UpdateSql
  372. {
  373. get
  374. {
  375. if (_UpdateSql.Trim().Length == 0)
  376. CreateSqlStr();
  377. return _UpdateSql;
  378. }
  379. }
  380. public string DeleteSql
  381. {
  382. get
  383. {
  384. if (_DeleteSql.Trim().Length == 0)
  385. CreateSqlStr();
  386. return _DeleteSql;
  387. }
  388. }
  389. private void CreateSqlStr()
  390. {
  391. string lcInsertFields = "";
  392. string lcInsertValues = "";
  393. string lcUpdateSet = "";
  394. string lcWhere = "";
  395. foreach (rsDDColumn loColumn in _ColumnList)
  396. {
  397. if (!loColumn.IsPrimaryKey)
  398. lcUpdateSet += (lcUpdateSet == "" ? "" : ",") + loColumn.ColumnId + "=" + "@" + loColumn.ColumnId;
  399. else
  400. lcWhere += (lcWhere == "" ? "" : " AND ") + loColumn.ColumnId + "=" + "@OOLLDD" + loColumn.ColumnId;
  401. if (!loColumn.IsAuto)
  402. {
  403. lcInsertFields += (lcInsertFields == "" ? "" : ",") + loColumn.ColumnId;
  404. lcInsertValues += (lcInsertValues == "" ? "" : ",") + "@" + loColumn.ColumnId;
  405. }
  406. }
  407. _InsertSql = "INSERT INTO " + this._TableName + " (" + lcInsertFields + ") VALUES (" + lcInsertValues + ")";
  408. _UpdateSql = "UPDATE " + _TableName + " SET " + lcUpdateSet + " WHERE " + lcWhere;
  409. _DeleteSql = "DELETE FROM " + _TableName + " WHERE " + lcWhere;
  410. }
  411. public virtual void Initialize()
  412. {
  413. }
  414. }
  415. }