StandardStrategyStringBuilder.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace WebAPIBase.NetCore.BusinessCore
  8. {
  9. public sealed class StandardStrategyStringBuilder
  10. {
  11. public static string BuildStrategyString(Strategy strategy)
  12. {
  13. string result = "";
  14. switch (strategy.Type)
  15. {
  16. case StrategyType.StringEqual:
  17. result = BuildStringEqualString(strategy);
  18. break;
  19. case StrategyType.StringEqualEx:
  20. result = BuildStringEqualStringEx(strategy);
  21. break;
  22. case StrategyType.StringRange:
  23. result = BuildStringRangeString(strategy);
  24. break;
  25. case StrategyType.StringIn:
  26. result = BuildStringInString(strategy);
  27. break;
  28. case StrategyType.StringLike:
  29. result = BuildStringLikeString(strategy);
  30. break;
  31. case StrategyType.StringLikeEx0:
  32. result = BuildStringLikeEx0String(strategy);
  33. break;
  34. case StrategyType.StringLikeEx1:
  35. result = BuildStringLikeEx1String(strategy);
  36. break;
  37. case StrategyType.NumberIn:
  38. result = BuildNumberInString(strategy);
  39. break;
  40. case StrategyType.DateTimeEqual:
  41. result = BuildDateTimeEqualString(strategy);
  42. break;
  43. case StrategyType.DateTimeEqualOnlyDate:
  44. result = BuildDateTimeEqualOnlyDateString(strategy);
  45. break;
  46. case StrategyType.DateTimeRange:
  47. result = BuildDateTimeRangeString(strategy);
  48. break;
  49. case StrategyType.DateTimeRangeOnlyDate:
  50. result = BuildDateTimeRangeOnlyDateString(strategy);
  51. break;
  52. case StrategyType.DateTimeEqualMonth:
  53. result = BuildDateTimeEqualMonthString(strategy);
  54. break;
  55. case StrategyType.DateTimeEqualYear:
  56. result = BuildDateTimeEqualYearString(strategy);
  57. break;
  58. case StrategyType.IntegerEqual:
  59. result = BuildIntegerEqualString(strategy);
  60. break;
  61. case StrategyType.IntegerRange:
  62. result = BuildIntegerRangeString(strategy);
  63. break;
  64. case StrategyType.FloatRange:
  65. result = BuildFloatRangeString(strategy);
  66. break;
  67. }
  68. return result;
  69. }
  70. public static string BuildStringEqualString(Strategy strategy)
  71. {
  72. return " " + strategy.RelationFieldName + " = '" + strategy.GetParameter(0) + "' ";
  73. }
  74. public static string BuildStringEqualStringEx(Strategy strategy)
  75. {
  76. string parameter = strategy.GetParameter(0);
  77. string relationFieldName = strategy.RelationFieldName;
  78. string text = "";
  79. if (parameter == BlankType._Blank.ToString("F"))
  80. {
  81. return $" len( isnull({relationFieldName},'')) = 0 ";
  82. }
  83. if (parameter == BlankType._Not_Blank.ToString("F"))
  84. {
  85. return $" len( isnull({relationFieldName},'')) > 0 ";
  86. }
  87. return string.Format(" {1} = '{0}' ", parameter, relationFieldName);
  88. }
  89. public static string BuildStringRangeString(Strategy strategy)
  90. {
  91. return " " + strategy.RelationFieldName + " in (" + strategy.GetParameter(0) + ") ";
  92. }
  93. public static string BuildStringInString(Strategy strategy)
  94. {
  95. string parameter = strategy.GetParameter(0);
  96. if (parameter.Length == 0)
  97. {
  98. return " 1=1 ";
  99. }
  100. string[] array = parameter.Split(',', ',');
  101. int num = array.Length;
  102. string text = "";
  103. string relationFieldName = strategy.RelationFieldName;
  104. for (int i = 0; i < num; i++)
  105. {
  106. string arg = array[i];
  107. if (text != "")
  108. {
  109. text += ", ";
  110. }
  111. text += string.Format("'{1}'", relationFieldName, arg);
  112. }
  113. return " " + strategy.RelationFieldName + " in (" + text + ") ";
  114. }
  115. public static string BuildStringLikeString(Strategy strategy)
  116. {
  117. return " " + strategy.RelationFieldName + " like ('" + strategy.GetParameter(0) + "') ";
  118. }
  119. public static string BuildStringLikeEx0String(Strategy strategy)
  120. {
  121. if (strategy.GetParameterCount() == 0)
  122. {
  123. return " 1=1 ";
  124. }
  125. string text = "";
  126. string relationFieldName = strategy.RelationFieldName;
  127. IEnumerator parameterEnumerator = strategy.GetParameterEnumerator();
  128. while (parameterEnumerator.MoveNext())
  129. {
  130. string arg = (string)parameterEnumerator.Current;
  131. if (text != "")
  132. {
  133. text += " or ";
  134. }
  135. text += $" {relationFieldName} like ('{arg}' ) ";
  136. }
  137. return "( " + text + " )";
  138. }
  139. private static string AddLikeChar(string val, string type)
  140. {
  141. string text = val;
  142. switch (type.ToUpper())
  143. {
  144. case "L":
  145. text += "%";
  146. break;
  147. case "R":
  148. text = "%" + text;
  149. break;
  150. case "F":
  151. text = "%" + text + "%";
  152. break;
  153. }
  154. return text;
  155. }
  156. public static string BuildStringLikeEx1String(Strategy strategy)
  157. {
  158. string parameter = strategy.GetParameter(0);
  159. if (parameter.Length == 0)
  160. {
  161. return " 1=1 ";
  162. }
  163. string[] array = parameter.Split(',', ',');
  164. int num = array.Length;
  165. string text = "";
  166. string relationFieldName = strategy.RelationFieldName;
  167. string type = "";
  168. if (strategy.GetParameterCount() > 1)
  169. {
  170. type = strategy.GetParameter(1);
  171. }
  172. for (int i = 0; i < num; i++)
  173. {
  174. string val = array[i];
  175. if (text != "")
  176. {
  177. text += " or ";
  178. }
  179. val = AddLikeChar(val, type);
  180. text += $" {relationFieldName} like ('{val}' ) ";
  181. }
  182. return "( " + text + " )";
  183. }
  184. public static string BuildNumberInString(Strategy strategy)
  185. {
  186. string parameter = strategy.GetParameter(0);
  187. return $" {strategy.RelationFieldName} in ( {parameter} ) ";
  188. }
  189. public static string BuildIntegerEqualString(Strategy strategy)
  190. {
  191. return " " + strategy.RelationFieldName + " = " + strategy.GetParameter(0) + " ";
  192. }
  193. public static string BuildIntegerRangeString(Strategy strategy)
  194. {
  195. string parameter = strategy.GetParameter(0);
  196. string parameter2 = strategy.GetParameter(1);
  197. string text = "";
  198. if (parameter == "" && parameter2 == "")
  199. {
  200. return " 1=1 ";
  201. }
  202. if (parameter == "")
  203. {
  204. return " " + strategy.RelationFieldName + " <= " + parameter2 + " ";
  205. }
  206. if (parameter2 == "")
  207. {
  208. return " " + strategy.RelationFieldName + " >= " + parameter + " ";
  209. }
  210. return " " + strategy.RelationFieldName + " BETWEEN " + parameter + " and " + parameter2 + " ";
  211. }
  212. public static string BuildFloatRangeString(Strategy strategy)
  213. {
  214. string parameter = strategy.GetParameter(0);
  215. string parameter2 = strategy.GetParameter(1);
  216. string text = "";
  217. if (parameter == "" && parameter2 == "")
  218. {
  219. return " 1=1 ";
  220. }
  221. if (parameter == "")
  222. {
  223. return " " + strategy.RelationFieldName + " <= " + parameter2 + " ";
  224. }
  225. if (parameter2 == "")
  226. {
  227. return " " + strategy.RelationFieldName + " >= " + parameter + " ";
  228. }
  229. return " " + strategy.RelationFieldName + " BETWEEN " + parameter + " and " + parameter2 + " ";
  230. }
  231. public static string BuildDateTimeEqualString(Strategy strategy)
  232. {
  233. return " " + strategy.RelationFieldName + " = '" + strategy.GetParameter(0) + "' ";
  234. }
  235. public static string BuildDateTimeEqualMonthString(Strategy strategy)
  236. {
  237. return string.Format(" Year({0})={1} and Month({0})={2} ", strategy.RelationFieldName, strategy.GetParameter(0), strategy.GetParameter(1));
  238. }
  239. public static string BuildDateTimeEqualYearString(Strategy strategy)
  240. {
  241. return " Year(" + strategy.RelationFieldName + ") = " + strategy.GetParameter(0) + " ";
  242. }
  243. public static string BuildDateTimeEqualOnlyDateString(Strategy strategy)
  244. {
  245. return " convert(datetime,convert(varchar(10)," + strategy.RelationFieldName + ",121)) = '" + strategy.GetParameter(0) + "' ";
  246. }
  247. public static string BuildDateTimeRangeString(Strategy strategy)
  248. {
  249. string text = strategy.GetParameter(0).Trim();
  250. string text2 = strategy.GetParameter(1).Trim();
  251. string result = "";
  252. if (text == "" && text2 == "")
  253. {
  254. result = " 1=1 ";
  255. }
  256. else if (text != "" && text2 != "")
  257. {
  258. result = " " + strategy.RelationFieldName + " BETWEEN '" + text + "' and '" + text2 + "' ";
  259. }
  260. else if (text != "")
  261. {
  262. result = " " + strategy.RelationFieldName + " >= '" + text + "' ";
  263. }
  264. else if (text2 != "")
  265. {
  266. result = " " + strategy.RelationFieldName + " <= '" + text2 + "' ";
  267. }
  268. return result;
  269. }
  270. public static string BuildDateTimeRangeOnlyDateString(Strategy strategy)
  271. {
  272. string text = strategy.GetParameter(0).Trim();
  273. string text2 = strategy.GetParameter(1).Trim();
  274. string result = "";
  275. if (text == "" && text2 == "")
  276. {
  277. result = " 1=1 ";
  278. }
  279. else if (text != "" && text2 != "")
  280. {
  281. result = " convert(datetime,convert(varchar(10)," + strategy.RelationFieldName + ",121)) BETWEEN '" + text + "' and '" + text2 + "' ";
  282. }
  283. else if (text != "")
  284. {
  285. result = " convert(datetime,convert(varchar(10)," + strategy.RelationFieldName + ",121)) >= '" + text + "' ";
  286. }
  287. else if (text2 != "")
  288. {
  289. result = " convert(datetime,convert(varchar(10)," + strategy.RelationFieldName + ",121)) <= '" + text2 + "' ";
  290. }
  291. return result;
  292. }
  293. }
  294. }