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.

317 lines
9.4 KiB

2 years ago
  1. using EasyNet.DBUtility;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. namespace EasyNet.Common
  6. {
  7. public class ParamMap : Map
  8. {
  9. private bool isPage;
  10. public ParamMap()
  11. {
  12. isPage = true;
  13. }
  14. public string Entity { get; set; }
  15. public string OrderFields { get; set; }
  16. public string OrderType { get; set; } = "Desc";
  17. public bool ParamNull { get; set; } = true;
  18. public int PageIndex { get; set; } = 1;
  19. public int PageSize { get; set; } = 10;
  20. public bool IsDesc { get; set; } = true;
  21. public static ParamMap NewMap()
  22. {
  23. return new ParamMap();
  24. }
  25. public bool IsPage
  26. {
  27. get
  28. {
  29. return isPage;
  30. }
  31. }
  32. public int PageOffset
  33. {
  34. get
  35. {
  36. var pageIndex = this.PageIndex;
  37. var pageSize = this.PageSize;
  38. if (pageIndex <= 0) pageIndex = 1;
  39. if (pageSize <= 0) pageSize = 1;
  40. return (pageIndex - 1) * pageSize;
  41. }
  42. }
  43. public int PageLimit
  44. {
  45. get
  46. {
  47. return this.PageSize;
  48. }
  49. }
  50. public int GetInt(string key)
  51. {
  52. var value = this[key];
  53. return Convert.ToInt32(value);
  54. }
  55. public String GetString(string key)
  56. {
  57. var value = this[key];
  58. return Convert.ToString(value);
  59. }
  60. public Double ToDouble(string key)
  61. {
  62. var value = this[key];
  63. return Convert.ToDouble(value);
  64. }
  65. public Int64 ToLong(string key)
  66. {
  67. var value = this[key];
  68. return Convert.ToInt64(value);
  69. }
  70. public Decimal ToDecimal(string key)
  71. {
  72. var value = this[key];
  73. return Convert.ToDecimal(value);
  74. }
  75. public DateTime ToDateTime(string key)
  76. {
  77. var value = this[key];
  78. return Convert.ToDateTime(value);
  79. }
  80. public void SetOrderFields(string orderFields, bool isDesc, string orderType)
  81. {
  82. this.OrderFields = orderFields;
  83. this.IsDesc = isDesc;
  84. this.OrderType = orderType;
  85. }
  86. /// <summary>
  87. /// 分頁參數設置
  88. /// </summary>
  89. /// <param name="page">第幾頁,從0開始</param>
  90. /// <param name="limit">每頁最多顯示幾條資料</param>
  91. public void SetPageParamters()
  92. {
  93. SetPages();
  94. }
  95. /// <summary>
  96. /// 分頁參數設置
  97. /// </summary>
  98. /// <param name="opm">todo: describe opm parameter on SetPageParamters</param>
  99. public void SetPageParamters(Object opm)
  100. {
  101. SetParamters(opm);
  102. SetPages();
  103. }
  104. /// <summary>
  105. /// 分頁參數設置
  106. /// </summary>
  107. /// <param name="opm">參數隊列</param>
  108. public void Paramters(Object opm)
  109. {
  110. SetParamters(opm);
  111. }
  112. /// <summary>
  113. /// 分頁參數設置
  114. /// </summary>
  115. /// <param name="opm">參數隊列</param>
  116. public void ParamtersForPrc(Object opm)
  117. {
  118. if (opm is Dictionary<string, object>)
  119. {
  120. var dic = opm as Dictionary<string, object>;
  121. if (dic.Keys.Count > 0)
  122. {
  123. switch (AdoHelper.DbType)
  124. {
  125. case DatabaseType.MYSQL:
  126. break;
  127. case DatabaseType.SQLSERVER:
  128. foreach (string key in dic.Keys)
  129. {
  130. this.Add(key, dic[key]);
  131. }
  132. break;
  133. case DatabaseType.ACCESS:
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. }
  140. }
  141. /// <summary>
  142. /// 分頁參數設置
  143. /// </summary>
  144. /// <param name="page">第幾頁,從0開始</param>
  145. /// <param name="limit">每頁最多顯示幾條資料</param>
  146. public void SetPageParamters(int page, int limit)
  147. {
  148. this.PageIndex = page;
  149. this.PageIndex = limit;
  150. SetPages();
  151. }
  152. private void SetPages()
  153. {
  154. this.isPage = true;
  155. switch (AdoHelper.DbType)
  156. {
  157. case DatabaseType.MYSQL:
  158. this["offset"] = this.PageOffset;
  159. this["limit"] = this.PageLimit;
  160. break;
  161. case DatabaseType.SQLSERVER:
  162. var pageIndex = this.PageIndex;
  163. var pageSize = this.PageSize;
  164. if (pageIndex <= 0) pageIndex = 1;
  165. if (pageSize <= 0) pageSize = 1;
  166. this["pageStart"] = (pageIndex - 1) * pageSize + 1;
  167. this["pageEnd"] = pageIndex * pageSize;
  168. break;
  169. case DatabaseType.ACCESS:
  170. var pageIndex_ac = this.PageIndex;
  171. var pageSize_ac = this.PageSize;
  172. this["offset"] = pageIndex_ac * pageSize_ac;
  173. this["limit"] = pageSize_ac;
  174. break;
  175. default:
  176. break;
  177. }
  178. //int start = (pageIndex-1) * pageSize + 1;
  179. //int end = pageIndex * pageSize;
  180. }
  181. private void SetParamters(Object p)
  182. {
  183. if (p is Dictionary<string, object>)
  184. {
  185. var dic = p as Dictionary<string, object>;
  186. if (dic.Keys.Count > 0)
  187. {
  188. this.isPage = true;
  189. switch (AdoHelper.DbType)
  190. {
  191. case DatabaseType.MYSQL:
  192. break;
  193. case DatabaseType.SQLSERVER:
  194. foreach (string key in dic.Keys)
  195. {
  196. switch (key)
  197. {
  198. case nameof(Entity):
  199. {
  200. this.Entity = dic[key].ToString();
  201. break;
  202. }
  203. case nameof(PageIndex):
  204. {
  205. this.PageIndex = Convert.ToInt32(dic[key]);
  206. break;
  207. }
  208. case nameof(PageSize):
  209. {
  210. this.PageSize = Convert.ToInt32(dic[key]);
  211. break;
  212. }
  213. case nameof(OrderFields):
  214. {
  215. this.OrderFields = dic[key].ToString();
  216. break;
  217. }
  218. case nameof(OrderType):
  219. {
  220. this.OrderType = dic[key].ToString();
  221. break;
  222. }
  223. case nameof(IsDesc):
  224. {
  225. this.IsDesc = (Boolean)dic[key];
  226. break;
  227. }
  228. default:
  229. {
  230. if (key.StartsWith("ISBLANK_") || key.StartsWith("ISNULL_"))
  231. {
  232. this.Add(key, dic[key]);
  233. }
  234. else if (dic[key] != null && dic[key].ToString() != "")
  235. {
  236. this.Add(key, dic[key]);
  237. }
  238. break;
  239. }
  240. }
  241. }
  242. break;
  243. case DatabaseType.ACCESS:
  244. break;
  245. }
  246. }
  247. }
  248. }
  249. public IDbDataParameter[] ToDbParameters()
  250. {
  251. var i = 0;
  252. var paramArr = DbFactory.CreateDbParameters(this.Keys.Count);
  253. foreach (string key in this.Keys)
  254. {
  255. if (!string.IsNullOrEmpty(key.Trim()))
  256. {
  257. var value = this[key];
  258. if (value == null) value = DBNull.Value;
  259. paramArr[i].ParameterName = key;
  260. paramArr[i].Value = value;
  261. i++;
  262. }
  263. }
  264. return paramArr;
  265. }
  266. }
  267. }