You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

334 lines
11 KiB

  1. using Learun.Util;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. namespace Learun.Application.Base.SystemModule
  6. {
  7. /// <summary>
  8. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  9. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  10. /// 创建人:陈彬彬
  11. /// 日 期:2017.03.08
  12. /// 描 述:数据库表管理
  13. /// </summary>
  14. public class DatabaseTableBLL : DatabaseTableIBLL
  15. {
  16. #region 属性
  17. private DatabaseTableService databaseTableService = new DatabaseTableService();
  18. private DatabaseLinkIBLL databaseLinkIBLL = new DatabaseLinkBLL();
  19. #endregion
  20. #region 获取数据
  21. /// <summary>
  22. /// 数据表列表
  23. /// </summary>
  24. /// <param name="databaseLinkId">数据库连接主键</param>
  25. /// <param name="tableName">表名</param>
  26. /// <returns></returns>
  27. public List<DatabaseTableModel> GetTableList(string databaseLinkId, string tableName)
  28. {
  29. try
  30. {
  31. DatabaseLinkEntity databaseLinkEntity = databaseLinkIBLL.GetEntity(databaseLinkId);
  32. List<DatabaseTableModel> list = (List<DatabaseTableModel>)databaseTableService.GetTableList(databaseLinkEntity);
  33. if (!string.IsNullOrEmpty(tableName))
  34. {
  35. list = list.FindAll(t => t.name.Contains(tableName));
  36. }
  37. return list;
  38. }
  39. catch (Exception ex)
  40. {
  41. if (ex is ExceptionEx)
  42. {
  43. throw;
  44. }
  45. else
  46. {
  47. throw ExceptionEx.ThrowBusinessException(ex);
  48. }
  49. }
  50. }
  51. /// <summary>
  52. /// 获取树形数据
  53. /// </summary>
  54. /// <param name="databaseLinkId">数据库连接主键</param>
  55. /// <returns></returns>
  56. public List<TreeModel> GetTreeList(string databaseLinkId)
  57. {
  58. try
  59. {
  60. List<TreeModel> list = null;
  61. if (string.IsNullOrEmpty(databaseLinkId))
  62. {
  63. list = databaseLinkIBLL.GetTreeListEx();
  64. }
  65. else
  66. {
  67. list = new List<TreeModel>();
  68. List<DatabaseTableModel> databaseTableList = GetTableList(databaseLinkId, "");
  69. foreach (var item in databaseTableList)
  70. {
  71. TreeModel node = new TreeModel();
  72. node.id = databaseLinkId + item.name;
  73. node.text = item.name;
  74. node.value = databaseLinkId;
  75. node.title = item.tdescription;
  76. node.complete = true;
  77. node.isexpand = false;
  78. node.hasChildren = false;
  79. list.Add(node);
  80. }
  81. }
  82. return list;
  83. }
  84. catch (Exception ex)
  85. {
  86. if (ex is ExceptionEx)
  87. {
  88. throw;
  89. }
  90. else
  91. {
  92. throw ExceptionEx.ThrowBusinessException(ex);
  93. }
  94. }
  95. }
  96. /// <summary>
  97. /// 数据表字段列表
  98. /// </summary>
  99. /// <param name="databaseLinkId">数据库连接主键</param>
  100. /// <param name="tableName">表名</param>
  101. /// <returns></returns>
  102. public IEnumerable<DatabaseTableFieldModel> GetTableFiledList(string databaseLinkId, string tableName)
  103. {
  104. try
  105. {
  106. DatabaseLinkEntity databaseLinkEntity = databaseLinkIBLL.GetEntity(databaseLinkId);
  107. return databaseTableService.GetTableFiledList(databaseLinkEntity, tableName);
  108. }
  109. catch (Exception ex)
  110. {
  111. if (ex is ExceptionEx)
  112. {
  113. throw;
  114. }
  115. else
  116. {
  117. throw ExceptionEx.ThrowBusinessException(ex);
  118. }
  119. }
  120. }
  121. /// <summary>
  122. /// 获取数据表字段树形数据
  123. /// </summary>
  124. /// <param name="databaseLinkId">数据库连接主键</param>
  125. /// <param name="tableName">表名</param>
  126. /// <returns></returns>
  127. public List<TreeModel> GetFiledTreeList(string databaseLinkId, string tableName)
  128. {
  129. try
  130. {
  131. var list = GetTableFiledList(databaseLinkId, tableName);
  132. List<TreeModel> treeList = new List<TreeModel>();
  133. foreach (var item in list)
  134. {
  135. TreeModel node = new TreeModel();
  136. node.id = item.f_column;
  137. node.text = item.f_column;
  138. node.value = item.f_column;
  139. node.title = item.f_remark;
  140. node.complete = true;
  141. node.isexpand = false;
  142. node.hasChildren = false;
  143. node.showcheck = true;
  144. treeList.Add(node);
  145. }
  146. return treeList;
  147. }
  148. catch (Exception ex)
  149. {
  150. if (ex is ExceptionEx)
  151. {
  152. throw;
  153. }
  154. else
  155. {
  156. throw ExceptionEx.ThrowBusinessException(ex);
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// 数据库表数据列表
  162. /// </summary>
  163. /// <param name="databaseLinkId">数据库连接主键</param>
  164. /// <param name="field">表明</param>
  165. /// <param name="switchWhere">条件</param>
  166. /// <param name="logic">逻辑</param>
  167. /// <param name="keyword">关键字</param>
  168. /// <param name="pagination">分页参数</param>
  169. /// <returns></returns>
  170. public DataTable GetTableDataList(string databaseLinkId, string tableName, string field, string logic, string keyword, Pagination pagination)
  171. {
  172. try
  173. {
  174. DatabaseLinkEntity databaseLinkEntity = databaseLinkIBLL.GetEntity(databaseLinkId);
  175. return databaseTableService.GetTableDataList(databaseLinkEntity, tableName, field, logic, keyword, pagination);
  176. }
  177. catch (Exception ex)
  178. {
  179. if (ex is ExceptionEx)
  180. {
  181. throw;
  182. }
  183. else
  184. {
  185. throw ExceptionEx.ThrowBusinessException(ex);
  186. }
  187. }
  188. }
  189. /// <summary>
  190. /// 数据库表数据列表
  191. /// </summary>
  192. /// <param name="databaseLinkId">数据库连接主键</param>
  193. /// <param name="tableName">表名</param>
  194. /// <returns></returns>
  195. public DataTable GetTableDataList(string databaseLinkId, string tableName)
  196. {
  197. try
  198. {
  199. DatabaseLinkEntity databaseLinkEntity = databaseLinkIBLL.GetEntity(databaseLinkId);
  200. return databaseTableService.GetTableDataList(databaseLinkEntity, tableName);
  201. }
  202. catch (Exception ex)
  203. {
  204. if (ex is ExceptionEx)
  205. {
  206. throw;
  207. }
  208. else
  209. {
  210. throw ExceptionEx.ThrowBusinessException(ex);
  211. }
  212. }
  213. }
  214. /// <summary>
  215. /// 给定查询语句查询字段
  216. /// </summary>
  217. /// <param name="databaseLinkId">数据库连接主键</param>
  218. /// <param name="strSql">表名</param>
  219. /// <returns></returns>
  220. public List<string> GetSqlColName(string databaseLinkId, string strSql)
  221. {
  222. try
  223. {
  224. DatabaseLinkEntity databaseLinkEntity = databaseLinkIBLL.GetEntity(databaseLinkId);
  225. return databaseTableService.GetSqlColName(databaseLinkEntity, strSql);
  226. }
  227. catch (Exception ex)
  228. {
  229. if (ex is ExceptionEx)
  230. {
  231. throw;
  232. }
  233. else
  234. {
  235. throw ExceptionEx.ThrowServiceException(ex);
  236. }
  237. }
  238. }
  239. #endregion
  240. #region 提交数据
  241. /// <summary>
  242. /// 创建数据库表
  243. /// </summary>
  244. /// <param name="databaseLinkEntity"></param>
  245. /// <param name="tableName"></param>
  246. /// <param name="tableRemark"></param>
  247. /// <param name="colList"></param>
  248. /// <returns></returns>
  249. public string CreateTable(string databaseLinkId, string tableName, string tableRemark, List<DatabaseTableFieldModel> colList)
  250. {
  251. try
  252. {
  253. DatabaseLinkEntity databaseLinkEntity = databaseLinkIBLL.GetEntity(databaseLinkId);
  254. return databaseTableService.CreateTable(databaseLinkEntity, tableName, tableRemark, colList);
  255. }
  256. catch (Exception ex)
  257. {
  258. if (ex is ExceptionEx)
  259. {
  260. throw;
  261. }
  262. else
  263. {
  264. throw ExceptionEx.ThrowBusinessException(ex);
  265. }
  266. }
  267. }
  268. #endregion
  269. #region 扩展方法
  270. /// <summary>
  271. /// C#实体数据类型
  272. /// </summary>
  273. /// <param name="datatype">数据库字段类型</param>
  274. /// <returns></returns>
  275. public string FindModelsType(string datatype)
  276. {
  277. string res = "string";
  278. datatype = datatype.ToLower();
  279. switch (datatype)
  280. {
  281. case "int":
  282. case "number":
  283. case "integer":
  284. case "smallint":
  285. res = "int?";
  286. break;
  287. case "tinyint":
  288. res = "byte?";
  289. break;
  290. case "numeric":
  291. case "real":
  292. case "float":
  293. case "decimal":
  294. case "number(8,2)":
  295. case "money":
  296. case "smallmoney":
  297. res = "decimal?";
  298. break;
  299. case "char":
  300. case "varchar":
  301. case "nvarchar2":
  302. case "text":
  303. case "nchar":
  304. case "nvarchar":
  305. case "ntext":
  306. res = "string";
  307. break;
  308. case "bit":
  309. res = "bool?";
  310. break;
  311. case "datetime":
  312. case "date":
  313. case "smalldatetime":
  314. res = "DateTime?";
  315. break;
  316. default:
  317. res = "string";
  318. break;
  319. }
  320. return res;
  321. }
  322. #endregion
  323. }
  324. }