工作流

BaseBusiness.cs 17KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.Data.SqlClient;
  5. using System.Linq;
  6. using System.Reflection;
  7. using WorkFlowApi.DB;
  8. using WorkFlowApi.Utility;
  9. namespace WorkFlowApi.Business
  10. {
  11. /// <summary>
  12. /// 描述:业务处理基类
  13. /// </summary>
  14. /// <typeparam name="T">泛型约束(数据库实体)</typeparam>
  15. public abstract class BaseBusiness<T> where T : class, new()
  16. {
  17. #region 构造函数
  18. /// <summary>
  19. /// 构造函数
  20. /// </summary>
  21. /// <param name="db">注入数据库</param>
  22. public BaseBusiness()
  23. {
  24. type= typeof(T);
  25. Prop = type.GetProperties();
  26. KeyProp = Prop.Where(p => p.Name == KeyField).FirstOrDefault();
  27. }
  28. #endregion
  29. #region 私有成员
  30. /// <summary>
  31. /// 主键
  32. /// </summary>
  33. protected virtual string KeyField { get; } = "F_Id";
  34. /// <summary>
  35. /// 类
  36. /// </summary>
  37. protected Type type { get; set; }
  38. /// <summary>
  39. /// 属性
  40. /// </summary>
  41. protected PropertyInfo[] Prop { get; set; }
  42. /// <summary>
  43. /// 主键属性
  44. /// </summary>
  45. protected PropertyInfo KeyProp { get; set; }
  46. #endregion
  47. #region 存在数据
  48. /// <summary>
  49. /// 是否存在
  50. /// </summary>
  51. /// <param name="entity">实体对象</param>
  52. /// <param name="field">存在字段</param>
  53. /// <returns></returns>
  54. public bool Exists(T entity, string field)
  55. {
  56. PropertyInfo pi = Prop.Where(p => p.Name == field).FirstOrDefault();
  57. string sql = string.Format("select count(1) from [{0}] where [{1}]={2} ", type.Name, field, string.Format("@{0}", field));
  58. int keyValue = KeyProp.GetValue(entity, null)?.ToString().ToInt() ?? 0;
  59. if (keyValue > 0) { sql += " and [" + KeyField + "]!=" + keyValue; }
  60. SqlParameter[] ps = {
  61. new SqlParameter(string.Format("@{0}", field), pi.GetValue(entity, null) ?? DBNull.Value)
  62. };
  63. object obj = DbHelperSQL.GetSingle(sql, ps.ToArray());
  64. if (obj == null)
  65. {
  66. return false;
  67. }
  68. else
  69. {
  70. return Convert.ToInt32(obj) > 0;
  71. }
  72. }
  73. #endregion
  74. #region 增加数据
  75. /// <summary>
  76. /// 添加数据
  77. /// </summary>
  78. /// <param name="entity">实体对象</param>
  79. public int Insert(T entity)
  80. {
  81. string fields = string.Empty;
  82. string values = string.Empty;
  83. List<SqlParameter> ps = new List<SqlParameter>();
  84. foreach (var p in Prop.Where(p => p.Name.ToLower() != KeyField.ToLower()))
  85. {
  86. if (string.IsNullOrEmpty(fields)) { fields = string.Format("[{0}]", p.Name); }
  87. else { fields += string.Format(",[{0}]", p.Name); }
  88. if (string.IsNullOrEmpty(values)) { values = string.Format("@{0}", p.Name); }
  89. else { values += string.Format(",@{0}", p.Name); }
  90. ps.Add(new SqlParameter(string.Format("@{0}", p.Name), p.GetValue(entity, null) ?? DBNull.Value));
  91. }
  92. string sql = string.Format("insert into [{0}]({1}) values({2});select @@identity", type.Name, fields, values);
  93. object obj = DbHelperSQL.GetSingle(sql, ps.ToArray());
  94. if (obj == null)
  95. {
  96. return 0;
  97. }
  98. else
  99. {
  100. return Convert.ToInt32(obj);
  101. }
  102. }
  103. public int InsertExe(T entity)
  104. {
  105. string fields = string.Empty;
  106. string values = string.Empty;
  107. List<SqlParameter> ps = new List<SqlParameter>();
  108. foreach (var p in Prop)
  109. {
  110. if (string.IsNullOrEmpty(fields)) { fields = string.Format("[{0}]", p.Name); }
  111. else { fields += string.Format(",[{0}]", p.Name); }
  112. if (string.IsNullOrEmpty(values)) { values = string.Format("@{0}", p.Name); }
  113. else { values += string.Format(",@{0}", p.Name); }
  114. ps.Add(new SqlParameter(string.Format("@{0}", p.Name), p.GetValue(entity, null) ?? DBNull.Value));
  115. }
  116. string sql = string.Format("insert into [{0}]({1}) values({2})", type.Name, fields, values);
  117. object obj = DbHelperSQL.GetSingle(sql, ps.ToArray());
  118. if (obj == null)
  119. {
  120. return 0;
  121. }
  122. else
  123. {
  124. return Convert.ToInt32(obj);
  125. }
  126. }
  127. /// <summary>
  128. /// 添加多条数据
  129. /// </summary>
  130. /// <param name="entities">实体对象集合</param>
  131. public int InsertList(List<T> entities)
  132. {
  133. List<CommandInfo> cmdList = new List<CommandInfo>();
  134. var pps = Prop.Where(p => p.Name.ToLower() != KeyField.ToLower());
  135. foreach (var entity in entities)
  136. {
  137. string fields = string.Empty;
  138. string values = string.Empty;
  139. List<SqlParameter> ps = new List<SqlParameter>();
  140. foreach (var p in pps)
  141. {
  142. if (string.IsNullOrEmpty(fields)) { fields = string.Format("[{0}]", p.Name); }
  143. else { fields += string.Format(",[{0}]", p.Name); }
  144. if (string.IsNullOrEmpty(values)) { values = string.Format("@{0}", p.Name); }
  145. else { values += string.Format(",@{0}", p.Name); }
  146. ps.Add(new SqlParameter(string.Format("@{0}", p.Name), p.GetValue(entity, null) ?? DBNull.Value));
  147. }
  148. string sql = string.Format("insert into [{0}]({1}) values({2});", type.Name, fields, values);
  149. cmdList.Add(new CommandInfo(sql, ps.ToArray()));
  150. }
  151. return DbHelperSQL.ExecuteSqlTran(cmdList);
  152. }
  153. public int InsertListExe(List<T> entities)
  154. {
  155. List<CommandInfo> cmdList = new List<CommandInfo>();
  156. foreach (var entity in entities)
  157. {
  158. string fields = string.Empty;
  159. string values = string.Empty;
  160. List<SqlParameter> ps = new List<SqlParameter>();
  161. foreach (var p in Prop)
  162. {
  163. if (string.IsNullOrEmpty(fields)) { fields = string.Format("[{0}]", p.Name); }
  164. else { fields += string.Format(",[{0}]", p.Name); }
  165. if (string.IsNullOrEmpty(values)) { values = string.Format("@{0}", p.Name); }
  166. else { values += string.Format(",@{0}", p.Name); }
  167. ps.Add(new SqlParameter(string.Format("@{0}", p.Name), p.GetValue(entity, null) ?? DBNull.Value));
  168. }
  169. string sql = string.Format("insert into [{0}]({1}) values({2});", type.Name, fields, values);
  170. cmdList.Add(new CommandInfo(sql, ps.ToArray()));
  171. }
  172. return DbHelperSQL.ExecuteSqlTran(cmdList);
  173. }
  174. #endregion
  175. #region 删除数据
  176. /// <summary>
  177. /// 删除指定主键数据
  178. /// </summary>
  179. /// <param name="key"></param>
  180. public int Delete(string key)
  181. {
  182. string sql = string.Format("delete from [{0}] where [{1}]={2};", type.Name,KeyField, string.Format("@{0}", KeyField));
  183. SqlParameter[] ps = {
  184. new SqlParameter(string.Format("@{0}", KeyField), key)
  185. };
  186. return DbHelperSQL.ExecuteSql(sql, ps);
  187. }
  188. public int DeleteExe(string key,string Field)
  189. {
  190. string sql = string.Format("delete from [{0}] where [{1}]={2};", type.Name, Field, string.Format("@{0}", Field));
  191. SqlParameter[] ps = {
  192. new SqlParameter(string.Format("@{0}", Field), key)
  193. };
  194. return DbHelperSQL.ExecuteSql(sql, ps);
  195. }
  196. /// <summary>
  197. /// 通过主键删除多条数据
  198. /// </summary>
  199. /// <param name="keys"></param>
  200. public int DeleteList(List<string> keys)
  201. {
  202. string fields = string.Empty;
  203. List<SqlParameter> ps = new List<SqlParameter>();
  204. for (int i = 0; i < keys.Count; i++)
  205. {
  206. if (string.IsNullOrEmpty(fields)) { fields = string.Format("@{0}" + i, KeyField); }
  207. else { fields += string.Format(",@{0}" + i, KeyField); }
  208. ps.Add(new SqlParameter(string.Format("@{0}"+i, KeyField), keys[i]));
  209. }
  210. string sql = string.Format("delete from [{0}] where [{1}] in ({2});", type.Name, KeyField, fields);
  211. return DbHelperSQL.ExecuteSql(sql,ps.ToArray());
  212. }
  213. /// <summary>
  214. /// 删除单条数据
  215. /// </summary>
  216. /// <param name="entity">实体对象</param>
  217. public int DeleteEntity(T entity)
  218. {
  219. string sql = string.Format("delete from [{0}] where [{1}]={2};", type.Name, KeyField, string.Format("@{0}", KeyField));
  220. SqlParameter[] ps = {
  221. new SqlParameter(string.Format("@{0}", KeyField), KeyProp?.GetValue(entity, null)??null)
  222. };
  223. return DbHelperSQL.ExecuteSql(sql, ps);
  224. }
  225. /// <summary>
  226. /// 删除多条数据
  227. /// </summary>
  228. /// <param name="entities">实体对象集合</param>
  229. public int DeleteEntityList(List<T> entities)
  230. {
  231. string fields = string.Empty;
  232. List<SqlParameter> ps = new List<SqlParameter>();
  233. for (int i = 0; i < entities.Count; i++)
  234. {
  235. if (string.IsNullOrEmpty(fields)) { fields = string.Format("@{0}" + i, KeyField); }
  236. else { fields += string.Format(",@{0}" + i, KeyField); }
  237. ps.Add(new SqlParameter(string.Format("@{0}" + i, KeyField), KeyProp?.GetValue(entities[i], null) ?? null));
  238. }
  239. string sql = string.Format("delete from [{0}] where [{1}] in ({2});", type.Name, KeyField, fields);
  240. return DbHelperSQL.ExecuteSql(sql);
  241. }
  242. #endregion
  243. #region 更新数据
  244. /// <summary>
  245. /// 更新一条数据
  246. /// </summary>
  247. /// <param name="entity">实体对象</param>
  248. public int Update(T entity)
  249. {
  250. string fieldvalues = string.Empty;
  251. List<SqlParameter> ps = new List<SqlParameter>();
  252. foreach (var p in Prop.Where(p => p.Name.ToLower() != KeyField.ToLower()))
  253. {
  254. if (string.IsNullOrEmpty(fieldvalues)) { fieldvalues = string.Format("[{0}]=@{1}", p.Name, p.Name); }
  255. else { fieldvalues += string.Format(",[{0}]=@{1}", p.Name, p.Name); }
  256. ps.Add(new SqlParameter(string.Format("@{0}", p.Name), p.GetValue(entity, null) ?? DBNull.Value));
  257. }
  258. string sql = string.Format("update [{0}] set {1} where [{2}]={3};", type.Name, fieldvalues, KeyField, KeyProp?.GetValue(entity, null) ?? null);
  259. return DbHelperSQL.ExecuteSql(sql, ps.ToArray());
  260. }
  261. public int UpdateExe(T entity)
  262. {
  263. string fieldvalues = string.Empty;
  264. List<SqlParameter> ps = new List<SqlParameter>();
  265. foreach (var p in Prop.Where(p => p.Name.ToLower() != KeyField.ToLower()))
  266. {
  267. var value = p.GetValue(entity, null) ?? DBNull.Value;
  268. if (value != null)
  269. {
  270. if (value.ToString() != "")
  271. {
  272. if (string.IsNullOrEmpty(fieldvalues)) { fieldvalues = string.Format("[{0}]=@{1}", p.Name, p.Name); }
  273. else { fieldvalues += string.Format(",[{0}]=@{1}", p.Name, p.Name); }
  274. ps.Add(new SqlParameter(string.Format("@{0}", p.Name), p.GetValue(entity, null) ?? DBNull.Value));
  275. }
  276. }
  277. }
  278. string sql = string.Format("update [{0}] set {1} where [{2}]='{3}';", type.Name, fieldvalues, KeyField, KeyProp?.GetValue(entity, null) ?? null);
  279. return DbHelperSQL.ExecuteSql(sql, ps.ToArray());
  280. }
  281. /// <summary>
  282. /// 更新多条数据
  283. /// </summary>
  284. /// <param name="entities">数据列表</param>
  285. public int UpdateList(List<T> entities)
  286. {
  287. List<CommandInfo> cmdList = new List<CommandInfo>();
  288. var pps = Prop.Where(p => p.Name.ToLower() != KeyField.ToLower());
  289. foreach (var entity in entities)
  290. {
  291. string fieldvalues = string.Empty;
  292. List<SqlParameter> ps = new List<SqlParameter>();
  293. foreach (var p in pps)
  294. {
  295. if (string.IsNullOrEmpty(fieldvalues)) { fieldvalues = string.Format("[{0}]=@{1}", p.Name, p.Name); }
  296. else { fieldvalues += string.Format(",[{0}]=@{1}", p.Name, p.Name); }
  297. ps.Add(new SqlParameter(string.Format("@{0}", p.Name), p.GetValue(entity, null) ?? DBNull.Value));
  298. }
  299. string sql = string.Format("update [{0}] set {1} where [{2}]={3};", type.Name, fieldvalues, KeyField, KeyProp?.GetValue(entity, null) ?? null);
  300. cmdList.Add(new CommandInfo(sql, ps.ToArray()));
  301. }
  302. return DbHelperSQL.ExecuteSqlTran(cmdList);
  303. }
  304. #endregion
  305. #region 查询数据
  306. /// <summary>
  307. /// 获取实体
  308. /// </summary>
  309. /// <param name="keyValue">主键</param>
  310. /// <returns></returns>
  311. public T GetEntity(string keyValue)
  312. {
  313. string fields =string.Join(",", Prop.Select(p=> string.Format("[{0}]", p.Name)));
  314. string sql = string.Format("select top 1 {0} from [{1}] with(nolock) where [{2}]={3};", fields,type.Name, KeyField, string.Format("@{0}", KeyField));
  315. SqlParameter[] ps = {
  316. new SqlParameter(string.Format("@{0}", KeyField), keyValue)
  317. };
  318. DataTable dt = DbHelperSQL.Query(sql, ps).Tables[0];
  319. return dt.ConvertToList<T>().FirstOrDefault();
  320. }
  321. /// <summary>
  322. /// 获取表的所有数据
  323. /// </summary>
  324. /// <returns></returns>
  325. public List<T> GetAllList()
  326. {
  327. string fields = string.Join(",", Prop.Select(p => string.Format("[{0}]", p.Name)));
  328. string sql = string.Format("select {0} from [{1}] with(nolock);", fields, type.Name);
  329. DataTable dt = DbHelperSQL.Query(sql).Tables[0];
  330. return dt.ConvertToList<T>();
  331. }
  332. /// <summary>
  333. /// 查询列表
  334. /// </summary>
  335. /// <param name="WhereStr"></param>
  336. /// <param name="OrderStr"></param>
  337. /// <returns></returns>
  338. public List<T> GetList(string WhereStr, string OrderStr)
  339. {
  340. string fields = string.Join(",", Prop.Select(p => string.Format("[{0}]", p.Name)));
  341. string sql = string.Format("select {0} from [{1}] with(nolock) ", fields, type.Name);
  342. if (!string.IsNullOrEmpty( WhereStr.Trim()))
  343. {
  344. sql += " where " + WhereStr;
  345. }
  346. if (!string.IsNullOrEmpty(OrderStr.Trim()))
  347. {
  348. sql += " order by " + OrderStr;
  349. }
  350. DataTable dt = DbHelperSQL.Query(sql).Tables[0];
  351. return dt.ConvertToList<T>();
  352. }
  353. /// <summary>
  354. /// 分页列表返回DataTable
  355. /// </summary>
  356. /// <param name="WhereStr"></param>
  357. /// <param name="OrderStr"></param>
  358. /// <param name="PageSize"></param>
  359. /// <param name="PageIndex"></param>
  360. /// <param name="RecordCount"></param>
  361. /// <returns></returns>
  362. public DataTable GetDataTablePage(string WhereStr, string OrderStr, int PageSize, int PageIndex, out int RecordCount)
  363. {
  364. string fields = string.Join(",", Prop.Select(p => string.Format("[{0}]", p.Name)));
  365. return DbHelperSQL.RunPagination(type.Name, KeyField, fields, WhereStr, OrderStr, PageSize, PageIndex, true, out RecordCount);
  366. }
  367. /// <summary>
  368. /// 分页列表返回List
  369. /// </summary>
  370. /// <param name="WhereStr"></param>
  371. /// <param name="OrderStr"></param>
  372. /// <param name="PageSize"></param>
  373. /// <param name="PageIndex"></param>
  374. /// <param name="RecordCount"></param>
  375. /// <returns></returns>
  376. public List<T> GetListPage(string WhereStr, string OrderStr, int PageSize, int PageIndex, out int RecordCount)
  377. {
  378. return GetDataTablePage(WhereStr, OrderStr, PageSize, PageIndex,out RecordCount).ConvertToList<T>();
  379. }
  380. #endregion
  381. }
  382. }