rsDDColumn.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Data;
  5. using SysBaseLibs;
  6. namespace SysDataLibs
  7. {
  8. public class rsDDColumn
  9. {
  10. // private
  11. private string _ColumnId = "";
  12. private string _ColumnName = "";
  13. private int _Width = 0;
  14. //private int _ColumnLength = 0;
  15. private rsDataType _DataType = null;
  16. private bool _IsAuto = false;
  17. private bool _IsPrimaryKey = false;
  18. private bool _isNull = true;
  19. private DataRow _DataRow=null;
  20. private string _ColumnDesc = "";
  21. private string _DefValue = "";
  22. private bool _IsVisable = false;
  23. private int _Sequence = -1;
  24. private string _RelationColumn = "";
  25. private Dictionary<string,object> _SaveObject = null;
  26. private string _DisplayValue = "";
  27. private string _IsSelect = "Y";
  28. private string _Format = "";
  29. private DBConnSql _DBConn = null;
  30. public rsDDColumn(DataRow poRow)
  31. {
  32. _DataRow = poRow;
  33. if (_DataRow != null)
  34. {
  35. _ColumnId = _DataRow["ColumnId"].ToString().Trim();
  36. _DataType = new rsDataType(_DataRow["DataType"].ToString());
  37. _DataType.ColLength = Utils.ValI(_DataRow["CharNum"].ToString());
  38. _DataType.ColPrecision = Utils.ValI(_DataRow["ColPrecision"].ToString());
  39. _DataType.ColDecimal = Utils.ValI(_DataRow["ColDecimal"].ToString());
  40. _IsAuto = UtilStr.StrToBool(_DataRow["AutoGen"].ToString());
  41. _IsPrimaryKey = UtilStr.StrToBool(_DataRow["PrimaryKey"].ToString());
  42. _isNull = UtilStr.StrToBool(_DataRow["AllowNull"].ToString());
  43. _ColumnDesc = _DataRow["ColDesc"].ToString();
  44. if (_DataRow["DefValue"] != null)
  45. _DefValue = _DataRow["DefValue"].ToString();
  46. if (_DefValue.Trim().Length == 0)
  47. _DefValue = _DataType.DBEmptyValue;
  48. if (_DataRow["width"] != null)
  49. _Width = Utils.ValI(_DataRow["width"].ToString());
  50. if (_DataRow["IsVisable"] != null)
  51. _IsVisable = UtilStr.StrToBool(_DataRow["IsVisable"].ToString());
  52. if (_DataRow["Sequence"] != null)
  53. _Sequence = Utils.ValI(_DataRow["Sequence"].ToString());
  54. if (_DataRow["ColumnName"] != null)
  55. _ColumnName = _DataRow["ColumnName"].ToString().Trim();
  56. if (_DataRow["RelationColumns"] != null)
  57. _RelationColumn = _DataRow["RelationColumns"].ToString().Trim();
  58. if (_DataRow["DisplayValue"] != null)
  59. _DisplayValue = _DataRow["DisplayValue"].ToString().Trim();
  60. if (_DataRow["IsSelect"] != null)
  61. _IsSelect = _DataRow["IsSelect"].ToString().Trim();
  62. if (_DataRow["Format"] != null)
  63. _Format = _DataRow["Format"].ToString().Trim();
  64. }
  65. }
  66. public rsDDColumn(string pcColumnId, string pcColumnName, int piWidth,
  67. int piColumnLength, string pcDataType, bool pbIsAuto, bool pbIsPrimaryKey,bool pbisNull)
  68. {
  69. _ColumnId = pcColumnId;
  70. _ColumnName = pcColumnName;
  71. _Width = piWidth;
  72. _DataType.ColLength = piColumnLength;
  73. _DataType = new rsDataType(pcDataType);
  74. _IsAuto = pbIsAuto;
  75. _IsPrimaryKey = pbIsPrimaryKey;
  76. _isNull = pbisNull;
  77. }
  78. /// <summary>
  79. /// 字段名称
  80. /// </summary>
  81. public string ColumnName
  82. {
  83. get {
  84. return _ColumnName;
  85. }
  86. set { _ColumnName = value; }
  87. }
  88. /// <summary>
  89. /// 字段编号
  90. /// </summary>
  91. public string ColumnId
  92. {
  93. get
  94. {
  95. return _ColumnId;
  96. }
  97. }
  98. /// <summary>
  99. /// 字段显示宽度
  100. /// </summary>
  101. public int Width
  102. {
  103. get { return _Width; }
  104. set { _Width = value; }
  105. }
  106. /// <summary>
  107. /// 字段长度
  108. /// </summary>
  109. public int ColumnLength
  110. {
  111. get { return _DataType.ColLength; }
  112. }
  113. /// <summary>
  114. /// 字段类型
  115. /// </summary>
  116. public rsDataType DataType
  117. {
  118. get { return _DataType; }
  119. }
  120. /// <summary>
  121. /// 是否自动增长
  122. /// </summary>
  123. public bool IsAuto
  124. {
  125. get { return _IsAuto; }
  126. }
  127. /// <summary>
  128. /// 是否是主键
  129. /// </summary>
  130. public bool IsPrimaryKey
  131. {
  132. get { return _IsPrimaryKey; }
  133. }
  134. /// <summary>
  135. /// 是否可见
  136. /// </summary>
  137. public bool IsVisable
  138. {
  139. get { return _IsVisable; }
  140. set { _IsVisable = value; }
  141. }
  142. /// <summary>
  143. /// 是否是数字
  144. /// </summary>
  145. public bool IsNum
  146. {
  147. get
  148. {
  149. return _DataType.IsNumeric;
  150. }
  151. }
  152. /// <summary>
  153. /// 字段排序
  154. /// </summary>
  155. public int Sequence
  156. {
  157. get { return _Sequence; }
  158. }
  159. /// <summary>
  160. /// 关联的字段
  161. /// </summary>
  162. public string RelationColunm
  163. {
  164. get { return _RelationColumn; }
  165. }
  166. /// <summary>
  167. /// 显示的字段 如[[1,男],[0,女]]
  168. /// </summary>
  169. public string DisplayValue
  170. {
  171. get { return _DisplayValue; }
  172. set { _DisplayValue = value; }
  173. }
  174. /// <summary>
  175. /// 进行自动拼装sql 语句的时候,该字段是否参与
  176. /// </summary>
  177. public string IsSelect
  178. {
  179. get { return _IsSelect; }
  180. set { _IsSelect = value; }
  181. }
  182. /// <summary>
  183. /// 字段数据格式化,主要针对 日期 和 数值
  184. /// </summary>
  185. public string Format
  186. {
  187. get { return _Format; }
  188. set { _Format = value; }
  189. }
  190. public void putValue(string pcKey, object poValue)
  191. {
  192. if (_SaveObject == null)
  193. _SaveObject = new Dictionary<string, object>();
  194. if (_SaveObject.ContainsKey(pcKey))
  195. _SaveObject[pcKey] = poValue;
  196. else
  197. _SaveObject.Add(pcKey, poValue);
  198. }
  199. public string GetValue(string pcKey)
  200. {
  201. string lcRetVal = "";
  202. object loObj = GetValueObj(pcKey);
  203. if (loObj != null)
  204. lcRetVal = loObj.ToString();
  205. return lcRetVal;
  206. }
  207. public object GetValueObj(string pcKey)
  208. {
  209. object loRetVal = null;
  210. if (_SaveObject != null && _SaveObject.ContainsKey(pcKey))
  211. {
  212. loRetVal = _SaveObject[pcKey];
  213. }
  214. return loRetVal;
  215. }
  216. private rsQuery _RelationTable = null;
  217. private string _RelationTableId = "";
  218. private string _RelationColumnId = "";
  219. private string GetTableId
  220. {
  221. get
  222. {
  223. if (_RelationTableId == "")
  224. {
  225. if (_RelationColumn != null && _RelationColumn.Trim().Length > 0)
  226. {
  227. _RelationTableId = DALibs.GetTableId(_RelationColumn);
  228. }
  229. }
  230. return _RelationTableId;
  231. }
  232. }
  233. private string GetColumnId
  234. {
  235. get
  236. {
  237. if (_RelationColumnId == "")
  238. if (_RelationColumn != null && _RelationColumn.Trim().Length > 0)
  239. {
  240. _RelationColumnId = DALibs.GetColumnId(_RelationColumn);
  241. }
  242. return _RelationColumnId;
  243. }
  244. }
  245. /// <summary>
  246. ///
  247. /// </summary>
  248. private Dictionary<string, string> _DisplayValues = null;
  249. /// <summary>
  250. /// 获取字段的显示值 比如在表 DisplayValue 定义 [1:男],[2:女] 则 传递 1 返回 男
  251. /// </summary>
  252. /// <param name="pcValue"></param>
  253. /// <returns></returns>
  254. public string GetDisplayValue(string pcValue)
  255. {
  256. string lcRetVal = pcValue;
  257. if (_DisplayValues == null)
  258. {
  259. if (_DisplayValue.Trim().Length > 0)
  260. {
  261. Array loArr = UtilStr.StrToArray(_DisplayValue);
  262. if (loArr != null && loArr.Length > 0)
  263. {
  264. _DisplayValues = new Dictionary<string, string>();
  265. foreach (string lcStr in loArr)
  266. {
  267. Array loArr2 = UtilStr.StrToArrayEx(lcStr, ":");
  268. if (loArr2 != null && loArr2.Length == 2)
  269. {
  270. string lcKey = UtilStr.UAndT(loArr2.GetValue(0).ToString().Trim());
  271. if (!_DisplayValues.ContainsKey(lcKey))
  272. _DisplayValues.Add(loArr2.GetValue(0).ToString(), loArr2.GetValue(1).ToString());
  273. else
  274. _DisplayValues[lcKey] = loArr2.GetValue(1).ToString();
  275. }
  276. }
  277. }
  278. }
  279. }
  280. if (_DisplayValues != null)
  281. {
  282. if (_DisplayValues.ContainsKey(UtilStr.UAndT(pcValue)))
  283. lcRetVal = _DisplayValues[UtilStr.UAndT(pcValue)].ToString().Trim();
  284. }
  285. return lcRetVal;
  286. }
  287. /// <summary>
  288. /// 想关联字段赋值 比如 该字段 是MarketID 在User表中,要获取相应的MarketName 则 RelationClumn 为 Markets.MarketName
  289. /// </summary>
  290. /// <param name="poDbConn"></param>
  291. public void SetDbConn(DBConnSql poDbConn)
  292. {
  293. if (GetTableId.Trim().Length > 0)
  294. {
  295. _DBConn = poDbConn;
  296. string lcSql = " select " + this.ColumnId + "," + GetColumnId + " from " + GetTableId;
  297. _RelationTable = _DBConn.OpenQuery(lcSql);
  298. }
  299. }
  300. /// <summary>
  301. /// 获取关联或真实显示的信息 优先显示 DisplayValue 再显示 RelationValue
  302. /// </summary>
  303. /// <param name="pcValue"></param>
  304. /// <returns></returns>
  305. public string DispalyValueTranslate(string pcValue)
  306. {
  307. string lcRetVal = pcValue;
  308. string lcValue = "";
  309. lcValue = GetDisplayValue(pcValue);
  310. if (string.IsNullOrEmpty(lcValue))
  311. {
  312. lcValue = GetRelationValue(pcValue);
  313. if (lcValue != pcValue)
  314. {
  315. lcRetVal = lcValue;
  316. }
  317. }
  318. else
  319. {
  320. if (lcValue != pcValue)
  321. {
  322. lcRetVal = lcValue;
  323. }
  324. }
  325. return lcRetVal;
  326. }
  327. /// <summary>
  328. /// 获取RelationValue
  329. /// </summary>
  330. /// <param name="pcStr"></param>
  331. /// <returns></returns>
  332. public string GetRelationValue(string pcStr)
  333. {
  334. string lcRetVal = pcStr;
  335. if (pcStr.Trim().Length > 0)
  336. {
  337. if (_RelationTable != null && _RelationTable.IsOpened)
  338. {
  339. if (_RelationTable.GoToRecordByFieldsAndValues(this.ColumnId, pcStr))
  340. lcRetVal = _RelationTable.GetString(GetColumnId);
  341. }
  342. }
  343. return lcRetVal;
  344. }
  345. /// <summary>
  346. /// 判断与当前的字段是否匹配
  347. /// </summary>
  348. /// <param name="pcColumnId"></param>
  349. /// <returns></returns>
  350. public bool MatchColumnId(string pcColumnId)
  351. {
  352. pcColumnId = UtilStr.UAndT(pcColumnId);
  353. return UtilStr.UAndT(this.ColumnId) == pcColumnId;
  354. }
  355. /// <summary>
  356. /// 获取 字段的对象
  357. /// </summary>
  358. /// <param name="poColumns"></param>
  359. /// <param name="pcColumnId"></param>
  360. /// <returns></returns>
  361. public static rsDDColumn GetColumnById(List<rsDDColumn> poColumns, string pcColumnId)
  362. {
  363. rsDDColumn loRetVal = null;
  364. if (poColumns != null && pcColumnId != null)
  365. {
  366. pcColumnId=UtilStr.UAndT(pcColumnId);
  367. foreach (rsDDColumn loCol in poColumns)
  368. {
  369. if (UtilStr.UAndT(loCol.ColumnId) == pcColumnId)
  370. {
  371. loRetVal = loCol;
  372. break;
  373. }
  374. }
  375. }
  376. return loRetVal;
  377. }
  378. }
  379. }