ReportableProvider.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading.Tasks;
  8. namespace SqlSugar
  9. {
  10. public class ReportableProvider<T> : IReportable<T>
  11. {
  12. public SqlSugarProvider Context { get; set; }
  13. private List<T> datas = new List<T>();
  14. private List<DateTime> dates = new List<DateTime>();
  15. private bool isDates = false;
  16. internal QueryBuilder queryBuilder;
  17. internal InsertBuilder formatBuilder { get; set; }
  18. public ReportableProvider(T data)
  19. {
  20. datas.Add(data);
  21. Init();
  22. }
  23. public ReportableProvider(List<T> list)
  24. {
  25. datas = list;
  26. Init();
  27. }
  28. //public IReportable<T> MakeUp(Func<T, object> auto)
  29. //{
  30. // throw new NotImplementedException();
  31. //}
  32. public ISugarQueryable<T> ToQueryable()
  33. {
  34. StringBuilder sb = new StringBuilder();
  35. if (datas.Any())
  36. {
  37. if (isDates)
  38. {
  39. var da = this.dates;
  40. Each(sb, da);
  41. }
  42. else
  43. {
  44. var da = this.datas;
  45. Each(sb, da);
  46. }
  47. }
  48. else
  49. {
  50. if (typeof(T).IsClass())
  51. {
  52. var result = (T)Activator.CreateInstance(typeof(T), true);
  53. datas.Add(result);
  54. ClassMethod(result, sb, true);
  55. }
  56. else
  57. {
  58. sb.Append("SELECT NULL as ColumnName ");
  59. sb.Append(GetNextSql);
  60. }
  61. }
  62. return this.Context.SqlQueryable<object>(sb.ToString()).Select<T>();
  63. }
  64. public ISugarQueryable<SingleColumnEntity<Y>> ToQueryable<Y>()
  65. {
  66. return ToQueryable().Select<SingleColumnEntity<Y>>();
  67. }
  68. private bool _isOnlySelectEntity = false;
  69. public ISugarQueryable<SingleColumnEntity<Y>> ToQueryable<Y>(bool isOnlySelectEntity)
  70. {
  71. _isOnlySelectEntity = isOnlySelectEntity;
  72. return ToQueryable<Y>();
  73. }
  74. private void Each<Y>(StringBuilder sb, List<Y> list)
  75. {
  76. int i = 0;
  77. foreach (var item in list)
  78. {
  79. ++i;
  80. var isLast = i == list.Count ;
  81. var isClass = typeof(T).IsClass();
  82. if (isClass)
  83. {
  84. ClassMethod(item, sb, isLast);
  85. }
  86. else
  87. {
  88. NoClassMethod(item, sb, isLast);
  89. }
  90. }
  91. }
  92. private void ClassMethod<Y>(Y data, StringBuilder sb,bool isLast)
  93. {
  94. var columns = new StringBuilder();
  95. var entity=this.Context.EntityMaintenance.GetEntityInfo<T>();
  96. columns.Append(string.Join(",",entity.Columns.Where(it=>it.IsIgnore==false).Select(it=>GetSelect(it,data))));
  97. if (_isOnlySelectEntity==false)
  98. {
  99. columns.Append(",null as NoCacheColumn");
  100. }
  101. sb.AppendLine(" SELECT " + columns.ToString());
  102. sb.Append(GetNextSql);
  103. if (!isLast)
  104. {
  105. sb.AppendLine(" UNION ALL ");
  106. }
  107. }
  108. private object GetSelect<Y>(EntityColumnInfo it,Y data)
  109. {
  110. return string.Format(" {0} AS {1} ", FormatValue(it.PropertyInfo.GetValue(data,null), it),queryBuilder.Builder.GetTranslationColumnName(it.PropertyName));
  111. }
  112. private void NoClassMethod<Y>(Y data, StringBuilder sb,bool isLast)
  113. {
  114. sb.AppendLine(" SELECT "+ FormatValue(data));
  115. sb.Append(" AS "+this.queryBuilder.Builder.GetTranslationColumnName("ColumnName") +" ");
  116. sb.Append(GetNextSql);
  117. if (!isLast)
  118. {
  119. sb.AppendLine(" UNION ALL ");
  120. }
  121. }
  122. public string GetNextSql
  123. {
  124. get
  125. {
  126. var isDual=queryBuilder.Builder.FullSqlDateNow?.ToLower()?.Contains(" dual")==true;
  127. if (isDual)
  128. {
  129. return " from dual ";
  130. }
  131. else
  132. {
  133. return null;
  134. }
  135. }
  136. }
  137. private void Init()
  138. {
  139. if (datas.Count == 1)
  140. {
  141. var data=datas.First();
  142. isDates = data is ReportableDateType;
  143. if (data is ReportableDateType)
  144. {
  145. var type = UtilMethods.ChangeType2(data, typeof(ReportableDateType));
  146. switch (type)
  147. {
  148. case ReportableDateType.MonthsInLast1years:
  149. dates.AddRange(GetMonths(1));
  150. break;
  151. case ReportableDateType.MonthsInLast3years:
  152. dates.AddRange(GetMonths(3));
  153. break;
  154. case ReportableDateType.MonthsInLast10years:
  155. dates.AddRange(GetMonths(10));
  156. break;
  157. case ReportableDateType.years1:
  158. dates.AddRange(GetYears(1));
  159. break;
  160. case ReportableDateType.years3:
  161. dates.AddRange(GetYears(3));
  162. break;
  163. case ReportableDateType.years10:
  164. dates.AddRange(GetYears(10));
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. }
  171. }
  172. private List<DateTime> GetYears(int v)
  173. {
  174. List<DateTime> result = new List<DateTime>();
  175. for (int i = 0; i < v; i++)
  176. {
  177. var year= (DateTime.Now.AddYears(i * -1).Year+"-01"+"-01").ObjToDate();
  178. result.Add(year);
  179. }
  180. return result;
  181. }
  182. private List<DateTime> GetMonths(int v)
  183. {
  184. List<DateTime> result = new List<DateTime>();
  185. var years = GetYears(v);
  186. foreach (var item in years)
  187. {
  188. for (int i = 0; i < 12; i++)
  189. {
  190. result.Add(item.AddMonths(i));
  191. }
  192. }
  193. return result;
  194. }
  195. private object FormatValue(object value,EntityColumnInfo entityColumnInfo=null)
  196. {
  197. if (entityColumnInfo != null&&entityColumnInfo.UnderType==UtilConstants.DateType && value == null&&this.Context.CurrentConnectionConfig.DbType==DbType.SqlServer)
  198. {
  199. return $" CAST( NULL AS DATETIME) ";
  200. }
  201. else if (entityColumnInfo != null && entityColumnInfo.UnderType == UtilConstants.DateType && value == null && this.Context.CurrentConnectionConfig.DbType == DbType.PostgreSQL)
  202. {
  203. return $" CAST( NULL AS timestamp) ";
  204. }
  205. if (value == null)
  206. return "null";
  207. var type =UtilMethods.GetUnderType(value.GetType());
  208. if (type.IsIn(typeof(int), typeof(uint), typeof(long), typeof(ulong), typeof(short), typeof(ushort)))
  209. {
  210. return value;
  211. }
  212. else if (type.IsIn(typeof(decimal), typeof(double)))
  213. {
  214. Expression<Func<SingleColumnEntity<object>, object>> exp = it => Convert.ToDecimal(it.ColumnName);
  215. var result = queryBuilder.LambdaExpressions.DbMehtods.ToDecimal(new MethodCallExpressionModel() {
  216. Args=new List<MethodCallExpressionArgs>() {
  217. new MethodCallExpressionArgs(){
  218. IsMember=true,
  219. MemberName= formatBuilder.FormatValue(value)
  220. }
  221. }
  222. });
  223. return result;
  224. }
  225. else if (type.IsIn(typeof(DateTime))||type.Name== "DateOnly")
  226. {
  227. if (this.Context.CurrentConnectionConfig.DbType == DbType.Oracle)
  228. {
  229. return queryBuilder.LambdaExpressions.DbMehtods.Oracle_ToDate(new MethodCallExpressionModel()
  230. {
  231. Args = new List<MethodCallExpressionArgs>() {
  232. new MethodCallExpressionArgs(){
  233. IsMember=true,
  234. MemberName= value.ObjToDate().ToString("yyyy-MM-dd HH:mm:ss").ToSqlValue()
  235. },
  236. new MethodCallExpressionArgs(){
  237. IsMember=true,
  238. MemberName= "yyyy-mm-dd hh24:mi:ss".ToSqlValue()
  239. }
  240. }
  241. }); ;
  242. }
  243. //Expression<Func<SingleColumnEntity<object>, object>> exp = it => Convert.ToDecimal(it.ColumnName);
  244. var result = queryBuilder.LambdaExpressions.DbMehtods.ToDate(new MethodCallExpressionModel()
  245. {
  246. Args = new List<MethodCallExpressionArgs>() {
  247. new MethodCallExpressionArgs(){
  248. IsMember=true,
  249. MemberName= formatBuilder.FormatValue(value)
  250. }
  251. }
  252. });
  253. return result;
  254. }
  255. else
  256. {
  257. return formatBuilder.FormatValue(value);
  258. }
  259. }
  260. }
  261. }