BaseServices.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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 Boolean DeleteItemBykey<T>(object key)
  102. {
  103. string tablename = EnumHelper.GetZXTableName<T>();
  104. var tbnameary = tablename.Split('.');
  105. List<SqlParameter> paramlist = new List<SqlParameter>();
  106. var deleteSql = string.Empty;
  107. foreach (PropertyInfo pi in typeof(T).GetProperties())
  108. {
  109. var attribute = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  110. if (attribute != null)
  111. {
  112. if (((Key)attribute).KEY)
  113. {
  114. paramlist.Add(new SqlParameter("@" + pi.Name, key));
  115. deleteSql = string.Format(DeleteItemSql, tbnameary[1], pi.Name, "@" + pi.Name);
  116. }
  117. }
  118. }
  119. var conn = SqlHelper.GetConnection(tbnameary[0]);
  120. var result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, deleteSql, paramlist.ToArray());
  121. return result > 0;
  122. }
  123. public T QueryItembyKey<T>(object key )
  124. {
  125. string tablename = EnumHelper.GetZXTableName<T>();
  126. var tbnameary = tablename.Split('.');
  127. List<SqlParameter> paramlist = new List<SqlParameter>();
  128. var deleteSql = string.Empty;
  129. foreach (PropertyInfo pi in typeof(T).GetProperties())
  130. {
  131. var attribute = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  132. if (attribute != null)
  133. {
  134. if (((Key)attribute).KEY)
  135. {
  136. paramlist.Add(new SqlParameter("@" + pi.Name, key));
  137. deleteSql = string.Format(QueryItemSql, tbnameary[1], pi.Name, "@" + pi.Name);
  138. }
  139. }
  140. }
  141. var conn = SqlHelper.GetConnection(tbnameary[0]);
  142. var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, deleteSql, paramlist.ToArray());
  143. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  144. {
  145. var result = LoadData<T>(ds.Tables[0].Rows[0]);
  146. return result;
  147. }
  148. else
  149. {
  150. return default(T);
  151. }
  152. }
  153. public bool Update<T>(T data)
  154. {
  155. if (data == null)
  156. {
  157. return false;
  158. }
  159. string tablename = EnumHelper.GetZXTableName<T>();
  160. var tbnameary = tablename.Split('.');
  161. List<string> paramsql = new List<string>();
  162. var key = new object() ;
  163. string keyname = string.Empty;
  164. List<SqlParameter> paramlist = new List<SqlParameter>();
  165. foreach (PropertyInfo pi in data.GetType().GetProperties())
  166. {
  167. object value1 = pi.GetValue(data, null);
  168. var attribute = pi.GetCustomAttributes(typeof(IsInsert), false).FirstOrDefault();
  169. if (attribute != null)
  170. {
  171. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  172. if (attributekey != null)
  173. {
  174. if (((Key)attributekey).KEY)
  175. {
  176. key = value1;
  177. keyname = pi.Name;
  178. }
  179. }
  180. if (((IsInsert)attribute).FildName)
  181. {
  182. paramsql.Add(string.Format(" {0} = @{0}",pi.Name));
  183. paramlist.Add(new SqlParameter("@" + pi.Name, value1));
  184. }
  185. }
  186. }
  187. string insertsql = string.Format(UpdateItemsql, tbnameary[1], string.Join(" ,", paramsql),keyname,key.GetType()==typeof(int)?key:"'"+key+"'");
  188. var conn = SqlHelper.GetConnection(tbnameary[0]);
  189. var result = SqlHelper.ExecuteNonQuery(conn, CommandType.Text, insertsql, paramlist.ToArray());
  190. return result > 0;
  191. }
  192. public List<T> GetList<T>(int page, int rows, string order, List<EExpression> expression, bool isDesc = true)
  193. {
  194. int startindex = (page-1) * rows;
  195. int endindex = startindex + rows+1;
  196. string tablename = EnumHelper.GetZXTableName<T>();
  197. var tbnameary = tablename.Split('.');
  198. List<string> fildlist = new List<string>();
  199. List<string> expressionlist = new List<string>();
  200. string keyname = string.Empty;
  201. foreach (PropertyInfo pi in typeof(T).GetProperties())
  202. {
  203. fildlist.Add(pi.Name);
  204. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  205. if (attributekey != null)
  206. {
  207. if (((Key)attributekey).KEY)
  208. {
  209. keyname = pi.Name;
  210. }
  211. }
  212. }
  213. for (int i = 0; i < expression.Count; i++)
  214. {
  215. expressionlist.Add(@" AND "+expression[i].GetSql());
  216. }
  217. //"AND OpenCode1>3 AND IsChecked=1"
  218. string pageListSql = string.Format(QueryListPageSql, string.Join(",", fildlist),order==null?keyname: order, isDesc?"desc":"asc",tbnameary[1],string.Join("", expressionlist),startindex,endindex);
  219. var conn = SqlHelper.GetConnection(tbnameary[0]);
  220. var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, pageListSql);
  221. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  222. {
  223. var result = LoadDataList<T>(ds.Tables[0]);
  224. return result;
  225. }
  226. else
  227. {
  228. return new List<T>();
  229. }
  230. }
  231. public List<T> GetList<T>(string order,List<EExpression> expression, bool isDesc = true)
  232. {
  233. string tablename = EnumHelper.GetZXTableName<T>();
  234. var tbnameary = tablename.Split('.');
  235. string keyname = string.Empty;
  236. List<string> expressionlist = new List<string>();
  237. foreach (PropertyInfo pi in typeof(T).GetProperties())
  238. {
  239. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  240. if (attributekey != null)
  241. {
  242. if (((Key)attributekey).KEY)
  243. {
  244. keyname = pi.Name;
  245. }
  246. }
  247. }
  248. for (int i = 0; i < expression.Count; i++)
  249. {
  250. expressionlist.Add(@" AND " + expression[i].GetSql());
  251. }
  252. string listSql = string.Format(QueryListSql, tbnameary[1], string.Join("", expressionlist), order == null ? keyname : order, isDesc ? "desc" : "asc");
  253. var conn = SqlHelper.GetConnection(tbnameary[0]);
  254. var ds = SqlHelper.ExecuteDataset(conn, CommandType.Text, listSql);
  255. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  256. {
  257. var result = LoadDataList<T>(ds.Tables[0]);
  258. return result;
  259. }
  260. else
  261. {
  262. return new List<T>();
  263. }
  264. }
  265. public int GetCount<T>()
  266. {
  267. string tablename = EnumHelper.GetZXTableName<T>();
  268. var tbnameary = tablename.Split('.');
  269. string insertsql = string.Format(SqlCount, tbnameary[1]);
  270. var conn = SqlHelper.GetConnection(tbnameary[0]);
  271. var result = SqlHelper.ExecuteScalar(conn, CommandType.Text, insertsql);
  272. return (int)result;
  273. }
  274. public int GetPageListCount<T>(List<EExpression> expression)
  275. {
  276. string tablename = EnumHelper.GetZXTableName<T>();
  277. var tbnameary = tablename.Split('.');
  278. string keyname = string.Empty;
  279. List<string> expressionlist = new List<string>();
  280. foreach (PropertyInfo pi in typeof(T).GetProperties())
  281. {
  282. var attributekey = pi.GetCustomAttributes(typeof(Key), false).FirstOrDefault();
  283. if (attributekey != null)
  284. {
  285. if (((Key)attributekey).KEY)
  286. {
  287. keyname = pi.Name;
  288. }
  289. }
  290. }
  291. for (int i = 0; i < expression.Count; i++)
  292. {
  293. expressionlist.Add(@" AND " + expression[i].GetSql());
  294. }
  295. string listSql = string.Format(PageListCount, keyname, tbnameary[1], string.Join("", expressionlist));
  296. var conn = SqlHelper.GetConnection(tbnameary[0]);
  297. var ds = SqlHelper.ExecuteScalar(conn, CommandType.Text, listSql);
  298. return (int)ds;
  299. }
  300. private static string AddItemSql = @"INSERT INTO {0} ({1}) VALUES ({2})";
  301. private static string DeleteItemSql = @"DELETE FROM {0} WHERE {1} = {2}";
  302. private static string QueryItemSql = @"SELECT * FROM {0} WHERE {1} = {2}";
  303. private static string UpdateItemsql = @"UPDATE {0} SET {1} WHERE {2} = {3}";
  304. private static string QueryListPageSql = @"SELECT {0} FROM
  305. (SELECT ROW_NUMBER()OVER(ORDER BY {1} {2})ROWNUMBER, * FROM {3} WHERE 1=1 {4} )A
  306. WHERE ROWNUMBER>{5} AND ROWNUMBER<{6}";
  307. private static string QueryListSql = @"SELECT * FROM {0} WHERE 1=1 {1} ORDER BY {2} {3}";
  308. private static string SqlCount = @"SELECT COUNT(*)
  309. FROM {0}";
  310. private static string PageListCount = @"SELECT Count({0}) FROM {1} WHERE 1=1 {2}";
  311. }
  312. }