BaseServices.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Data;
  6. using System.Reflection;
  7. using Common;
  8. using Interface;
  9. using Models;
  10. using System.Data.SqlClient;
  11. using System.Linq.Expressions;
  12. namespace Services
  13. {
  14. /// <summary>
  15. /// 基础服务
  16. /// </summary>
  17. public class BaseServices: BaseInterface
  18. {
  19. /// <summary>
  20. /// 装箱单个数据对象
  21. /// </summary>
  22. /// <typeparam name="T">装箱对象</typeparam>
  23. /// <param name="dr">装箱数据行</param>
  24. /// <returns></returns>
  25. protected T LoadData<T>(DataRow dr)
  26. {
  27. if (dr == null) return default(T);
  28. var t = typeof(T);
  29. var obj = Activator.CreateInstance(t);
  30. var properts = t.GetProperties();
  31. foreach (var pi in properts)
  32. {
  33. if (!dr.Table.Columns.Contains(pi.Name)) continue;
  34. pi.SetValue(obj, CommonHelper.ChangeType(dr[pi.Name], pi.PropertyType), null);
  35. }
  36. return (T)obj;
  37. }
  38. /// <summary>
  39. /// 装箱列表数据对象
  40. /// </summary>
  41. /// <typeparam name="T">装箱对象</typeparam>
  42. /// <param name="dt">装箱数据来源表格</param>
  43. /// <returns></returns>
  44. protected List<T> LoadDataList<T>(DataTable dt)
  45. {
  46. List<T> result = new List<T>();
  47. var t = typeof(T);
  48. var properts = t.GetProperties();
  49. object obj;
  50. foreach (DataRow dr in dt.Rows)
  51. {
  52. obj = Activator.CreateInstance(t);
  53. foreach (var pi in properts)
  54. {
  55. if (!dt.Columns.Contains(pi.Name)) continue;
  56. pi.SetValue(obj, CommonHelper.ChangeType(dr[pi.Name], pi.PropertyType), null);
  57. }
  58. result.Add((T)obj);
  59. }
  60. return result;
  61. }
  62. protected List<string> Loadstringist(DataTable dt)
  63. {
  64. List<string> result = new List<string>();
  65. foreach (DataRow dr in dt.Rows)
  66. {
  67. result.Add(dr[0].ToString());
  68. }
  69. return result;
  70. }
  71. public Boolean Add<T>(T obj)
  72. {
  73. if (obj==null)
  74. {
  75. return false;
  76. }
  77. string tablename= EnumHelper.GetZXTableName<T>();
  78. var tbnameary = tablename.Split('.');
  79. List<string> fildlist = new List<string>();
  80. List<string> paramsql = new List<string>();
  81. List<SqlParameter> paramlist = new List<SqlParameter>();
  82. foreach (PropertyInfo pi in obj.GetType().GetProperties())
  83. {
  84. object value1 = pi.GetValue(obj, null);
  85. var attribute = pi.GetCustomAttributes(typeof(IsInsert), false).FirstOrDefault();
  86. if (attribute != null)
  87. {
  88. if (((IsInsert)attribute).FildName)
  89. {
  90. fildlist.Add(pi.Name);
  91. paramsql.Add("@" + pi.Name);
  92. paramlist.Add(new SqlParameter("@"+pi.Name, value1));
  93. }
  94. }
  95. }
  96. string insertsql = string.Format(AddItemSql, tbnameary[1], string.Join(",", fildlist), string.Join(",", paramsql));
  97. var conn = SqlHelper.GetConnection(tbnameary[0]);
  98. var result = SqlHelper.ExecuteNonQuery(conn,CommandType.Text, insertsql, paramlist.ToArray());
  99. return result > 0;
  100. }
  101. public bool CBAdd(string sql, SqlParameter[] para)
  102. {
  103. var conn = SqlHelper.GetConnection("CBDataBase");
  104. var result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, sql, para);
  105. return result > 0;
  106. }
  107. public Boolean DeleteItemBykey<T>(object key)
  108. {
  109. string tablename = EnumHelper.GetZXTableName<T>();
  110. var tbnameary = tablename.Split('.');
  111. List<SqlParameter> paramlist = new List<SqlParameter>();
  112. var deleteSql = string.Empty;
  113. foreach (PropertyInfo pi in typeof(T).GetProperties())
  114. {
  115. var attribute = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  116. if (attribute != null)
  117. {
  118. if (((Key)attribute).KEY)
  119. {
  120. paramlist.Add(new SqlParameter("@" + pi.Name, key));
  121. deleteSql = string.Format(DeleteItemSql, tbnameary[1], pi.Name, "@" + pi.Name);
  122. }
  123. }
  124. }
  125. var conn = SqlHelper.GetConnection(tbnameary[0]);
  126. var result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, deleteSql, paramlist.ToArray());
  127. return result > 0;
  128. }
  129. public T QueryItembyKey<T>(object key )
  130. {
  131. string tablename = EnumHelper.GetZXTableName<T>();
  132. var tbnameary = tablename.Split('.');
  133. List<SqlParameter> paramlist = new List<SqlParameter>();
  134. var deleteSql = string.Empty;
  135. foreach (PropertyInfo pi in typeof(T).GetProperties())
  136. {
  137. var attribute = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  138. if (attribute != null)
  139. {
  140. if (((Key)attribute).KEY)
  141. {
  142. paramlist.Add(new SqlParameter("@" + pi.Name, key));
  143. deleteSql = string.Format(QueryItemSql, tbnameary[1], pi.Name, "@" + pi.Name);
  144. }
  145. }
  146. }
  147. var conn = SqlHelper.GetConnection(tbnameary[0]);
  148. var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, deleteSql, paramlist.ToArray());
  149. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  150. {
  151. var result = LoadData<T>(ds.Tables[0].Rows[0]);
  152. return result;
  153. }
  154. else
  155. {
  156. return default(T);
  157. }
  158. }
  159. public bool Update<T>(T data)
  160. {
  161. if (data == null)
  162. {
  163. return false;
  164. }
  165. string tablename = EnumHelper.GetZXTableName<T>();
  166. var tbnameary = tablename.Split('.');
  167. List<string> paramsql = new List<string>();
  168. var key = new object() ;
  169. string keyname = string.Empty;
  170. List<SqlParameter> paramlist = new List<SqlParameter>();
  171. foreach (PropertyInfo pi in data.GetType().GetProperties())
  172. {
  173. object value1 = pi.GetValue(data, null);
  174. var attribute = pi.GetCustomAttributes(typeof(IsInsert), false).FirstOrDefault();
  175. if (attribute != null)
  176. {
  177. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  178. if (attributekey != null)
  179. {
  180. if (((Key)attributekey).KEY)
  181. {
  182. key = value1;
  183. keyname = pi.Name;
  184. }
  185. }
  186. if (((IsInsert)attribute).FildName)
  187. {
  188. paramsql.Add(string.Format(" {0} = @{0}",pi.Name));
  189. paramlist.Add(new SqlParameter("@" + pi.Name, value1));
  190. }
  191. }
  192. }
  193. string insertsql = string.Format(UpdateItemsql, tbnameary[1], string.Join(" ,", paramsql),keyname,key.GetType()==typeof(int)?key:"'"+key+"'");
  194. var conn = SqlHelper.GetConnection(tbnameary[0]);
  195. var result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, insertsql, paramlist.ToArray());
  196. return result > 0;
  197. }
  198. public List<T> GetList<T>(int page, int rows, string order, List<EExpression> expression, bool isDesc = true)
  199. {
  200. int startindex = (page-1) * rows;
  201. int endindex = startindex + rows+1;
  202. string tablename = EnumHelper.GetZXTableName<T>();
  203. var tbnameary = tablename.Split('.');
  204. List<string> fildlist = new List<string>();
  205. List<string> expressionlist = new List<string>();
  206. string keyname = string.Empty;
  207. foreach (PropertyInfo pi in typeof(T).GetProperties())
  208. {
  209. fildlist.Add(pi.Name);
  210. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  211. if (attributekey != null)
  212. {
  213. if (((Key)attributekey).KEY)
  214. {
  215. keyname = pi.Name;
  216. }
  217. }
  218. }
  219. for (int i = 0; i < expression.Count; i++)
  220. {
  221. expressionlist.Add(@" AND "+expression[i].GetSql());
  222. }
  223. //"AND OpenCode1>3 AND IsChecked=1"
  224. string pageListSql = string.Format(QueryListPageSql, string.Join(",", fildlist),order==null?keyname: order, isDesc?"desc":"asc",tbnameary[1],string.Join("", expressionlist),startindex,endindex);
  225. var conn = SqlHelper.GetConnection(tbnameary[0]);
  226. var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, pageListSql);
  227. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  228. {
  229. var result = LoadDataList<T>(ds.Tables[0]);
  230. return result;
  231. }
  232. else
  233. {
  234. return new List<T>();
  235. }
  236. }
  237. public List<T> GetList<T>(string order,List<EExpression> expression, bool isDesc = true)
  238. {
  239. string tablename = EnumHelper.GetZXTableName<T>();
  240. var tbnameary = tablename.Split('.');
  241. string keyname = string.Empty;
  242. List<string> expressionlist = new List<string>();
  243. foreach (PropertyInfo pi in typeof(T).GetProperties())
  244. {
  245. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  246. if (attributekey != null)
  247. {
  248. if (((Key)attributekey).KEY)
  249. {
  250. keyname = pi.Name;
  251. }
  252. }
  253. }
  254. for (int i = 0; i < expression.Count; i++)
  255. {
  256. expressionlist.Add(@" AND " + expression[i].GetSql());
  257. }
  258. string listSql = string.Format(QueryListSql, tbnameary[1], string.Join("", expressionlist), order == null ? keyname : order, isDesc ? "desc" : "asc");
  259. var conn = SqlHelper.GetConnection(tbnameary[0]);
  260. var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, listSql);
  261. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  262. {
  263. var result = LoadDataList<T>(ds.Tables[0]);
  264. return result;
  265. }
  266. else
  267. {
  268. return new List<T>();
  269. }
  270. }
  271. public IEnumerable<T> FindeList<T>(string sql, SqlParameter[] para=null)
  272. {
  273. var conn = SqlHelper.GetConnection("CBDataBase");
  274. var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, sql,para);
  275. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  276. {
  277. var result = LoadDataList<T>(ds.Tables[0]);
  278. return result;
  279. }
  280. else
  281. {
  282. return new List<T>();
  283. }
  284. }
  285. public int GetCount<T>()
  286. {
  287. string tablename = EnumHelper.GetZXTableName<T>();
  288. var tbnameary = tablename.Split('.');
  289. string insertsql = string.Format(SqlCount, tbnameary[1]);
  290. var conn = SqlHelper.GetConnection(tbnameary[0]);
  291. var result = SqlHelper.ExecuteScalar(conn, CommandType.Text, insertsql);
  292. return (int)result;
  293. }
  294. public int GetPageListCount<T>(List<EExpression> expression)
  295. {
  296. string tablename = EnumHelper.GetZXTableName<T>();
  297. var tbnameary = tablename.Split('.');
  298. string keyname = string.Empty;
  299. List<string> expressionlist = new List<string>();
  300. foreach (PropertyInfo pi in typeof(T).GetProperties())
  301. {
  302. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  303. if (attributekey != null)
  304. {
  305. if (((Key)attributekey).KEY)
  306. {
  307. keyname = pi.Name;
  308. }
  309. }
  310. }
  311. for (int i = 0; i < expression.Count; i++)
  312. {
  313. expressionlist.Add(@" AND " + expression[i].GetSql());
  314. }
  315. string listSql = string.Format(PageListCount, keyname, tbnameary[1], string.Join("", expressionlist));
  316. var conn = SqlHelper.GetConnection(tbnameary[0]);
  317. var ds = SqlHelper.ExecuteScalar(conn, CommandType.Text, listSql);
  318. return (int)ds;
  319. }
  320. private static string AddItemSql = @"INSERT INTO {0} ({1}) VALUES ({2})";
  321. private static string DeleteItemSql = @"DELETE FROM {0} WHERE {1} = {2}";
  322. private static string QueryItemSql = @"SELECT * FROM {0} WHERE {1} = {2}";
  323. private static string UpdateItemsql = @"UPDATE {0} SET {1} WHERE {2} = {3}";
  324. private static string QueryListPageSql = @"SELECT {0} FROM
  325. (SELECT ROW_NUMBER()OVER(ORDER BY {1} {2})ROWNUMBER, * FROM {3} WHERE 1=1 {4} )A
  326. WHERE ROWNUMBER>{5} AND ROWNUMBER<{6}";
  327. private static string QueryListSql = @"SELECT * FROM {0} WHERE 1=1 {1} ORDER BY {2} {3}";
  328. private static string SqlCount = @"SELECT COUNT(*)
  329. FROM {0}";
  330. private static string PageListCount = @"SELECT Count({0}) FROM {1} WHERE 1=1 {2}";
  331. }
  332. }