Storageable.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Linq.Expressions;
  5. using System.Reflection;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace SqlSugar
  10. {
  11. public class Storageable<T> : IStorageable<T> where T : class, new()
  12. {
  13. SqlSugarProvider Context { get; set; }
  14. internal ISqlBuilder Builder;
  15. List<SugarParameter> Parameters;
  16. List<StorageableInfo<T>> allDatas = new List<StorageableInfo<T>>();
  17. List<T> dbDataList = new List<T>();
  18. List<KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>> whereFuncs = new List<KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>>();
  19. Expression<Func<T, object>> whereExpression;
  20. Func<DateTime, string> formatTime;
  21. DbLockType? lockType;
  22. private string asname { get; set; }
  23. private bool isDisableFilters = false;
  24. public Storageable(List<T> datas, SqlSugarProvider context)
  25. {
  26. this.Context = context;
  27. if (datas == null)
  28. datas = new List<T>();
  29. this.allDatas = datas.Select(it => new StorageableInfo<T>()
  30. {
  31. Item = it
  32. }).ToList();
  33. }
  34. Expression<Func<T, bool>> queryableWhereExp;
  35. public IStorageable<T> TableDataRange(Expression<Func<T, bool>> exp)
  36. {
  37. this.queryableWhereExp = exp;
  38. return this;
  39. }
  40. public IStorageable<T> SplitInsert(Func<StorageableInfo<T>, bool> conditions, string message = null)
  41. {
  42. whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Insert, conditions, message));
  43. return this;
  44. }
  45. public IStorageable<T> SplitDelete(Func<StorageableInfo<T>, bool> conditions, string message = null)
  46. {
  47. whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Delete, conditions, message));
  48. return this;
  49. }
  50. public IStorageable<T> SplitUpdate(Func<StorageableInfo<T>, bool> conditions, string message = null)
  51. {
  52. whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Update, conditions, message));
  53. return this;
  54. }
  55. public IStorageable<T> Saveable(string inserMessage = null,string updateMessage=null)
  56. {
  57. return this
  58. .SplitUpdate(it => it.Any(),updateMessage)
  59. .SplitInsert(it => true, inserMessage);
  60. }
  61. public IStorageable<T> SplitError(Func<StorageableInfo<T>, bool> conditions, string message = null)
  62. {
  63. whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Error, conditions, message));
  64. return this;
  65. }
  66. public IStorageable<T> SplitIgnore(Func<StorageableInfo<T>, bool> conditions, string message = null)
  67. {
  68. whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Ignore, conditions, message));
  69. return this;
  70. }
  71. public IStorageable<T> DisableFilters()
  72. {
  73. this.isDisableFilters = true;
  74. return this;
  75. }
  76. public IStorageable<T> TranLock(DbLockType dbLockType = DbLockType.Wait)
  77. {
  78. this.lockType = dbLockType;
  79. return this;
  80. }
  81. public IStorageable<T> TranLock(DbLockType? LockType)
  82. {
  83. if (LockType!=null)
  84. {
  85. this.lockType = LockType;
  86. return this;
  87. }
  88. else
  89. {
  90. return this;
  91. }
  92. }
  93. public IStorageable<T> SplitOther(Func<StorageableInfo<T>, bool> conditions, string message = null)
  94. {
  95. whereFuncs.Add(new KeyValuePair<StorageType, Func<StorageableInfo<T>, bool>, string>(StorageType.Other, conditions, message));
  96. return this;
  97. }
  98. public StorageablePage<T> PageSize(int PageSize,Action<int> ActionCallBack=null)
  99. {
  100. if (PageSize > 10000)
  101. {
  102. Check.ExceptionEasy("Advanced save page Settings should not exceed 10,000, and the reasonable number of pages is about 2000", "高级保存分页设置不要超过1万,合理分页数在2000左右");
  103. }
  104. StorageablePage<T> page = new StorageablePage<T>();
  105. page.Context = this.Context;
  106. page.PageSize = PageSize;
  107. page.Data = this.allDatas.Select(it => it.Item).ToList();
  108. page.ActionCallBack = ActionCallBack;
  109. page.TableName = this.asname;
  110. page.whereExpression = this.whereExpression;
  111. page.lockType = this.lockType;
  112. return page;
  113. }
  114. public StorageableSplitProvider<T> SplitTable()
  115. {
  116. StorageableSplitProvider<T> result = new StorageableSplitProvider<T>();
  117. result.Context = this.Context;
  118. result.SaveInfo = this;
  119. result.List = allDatas.Select(it=>it.Item).ToList();
  120. result.EntityInfo = this.Context.EntityMaintenance.GetEntityInfoWithAttr(typeof(T));
  121. return result;
  122. }
  123. public IStorageable<T> DefaultAddElseUpdate()
  124. {
  125. var column = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.FirstOrDefault(it=>it.IsPrimarykey);
  126. if (column == null) Check.ExceptionEasy("DefaultAddElseUpdate() need primary key", "DefaultAddElseUpdate()这个方法只能用于主键");
  127. return this.SplitUpdate(it =>
  128. {
  129. var itemPkValue = column.PropertyInfo.GetValue(it.Item);
  130. var defaultValue =UtilMethods.GetDefaultValue(column.PropertyInfo.PropertyType);
  131. var result= itemPkValue != null && itemPkValue.ObjToString() != defaultValue.ObjToString();
  132. return result;
  133. }).SplitInsert(it => true);
  134. }
  135. public int ExecuteCommand()
  136. {
  137. var result = 0;
  138. var x = this.ToStorage();
  139. result+=x.AsInsertable.ExecuteCommand();
  140. var updateRow = x.AsUpdateable.ExecuteCommand();
  141. if (updateRow < 0) updateRow = 0;
  142. result += updateRow;
  143. return result;
  144. }
  145. public T ExecuteReturnEntity()
  146. {
  147. var x = this.ToStorage();
  148. if (x.InsertList?.Any()==true)
  149. {
  150. var data = x.AsInsertable.ExecuteReturnEntity();
  151. x.AsUpdateable.ExecuteCommand();
  152. return data;
  153. }
  154. else
  155. {
  156. x.AsInsertable.ExecuteCommand();
  157. x.AsUpdateable.ExecuteCommand();
  158. return x.UpdateList.FirstOrDefault()?.Item;
  159. }
  160. }
  161. public async Task<T> ExecuteReturnEntityAsync()
  162. {
  163. var x = this.ToStorage();
  164. if (x.InsertList.Any())
  165. {
  166. var data = await x.AsInsertable.ExecuteReturnEntityAsync();
  167. await x.AsUpdateable.ExecuteCommandAsync();
  168. return data;
  169. }
  170. else
  171. {
  172. await x.AsInsertable.ExecuteCommandAsync();
  173. await x.AsUpdateable.ExecuteCommandAsync();
  174. return x.UpdateList.FirstOrDefault()?.Item;
  175. }
  176. }
  177. public Task<int> ExecuteCommandAsync(CancellationToken cancellationToken)
  178. {
  179. this.Context.Ado.CancellationToken=cancellationToken;
  180. return ExecuteCommandAsync();
  181. }
  182. public async Task<int> ExecuteCommandAsync()
  183. {
  184. var result = 0;
  185. var x = await this.ToStorageAsync();
  186. result +=await x.AsInsertable.ExecuteCommandAsync();
  187. var updateCount=await x.AsUpdateable.ExecuteCommandAsync();
  188. if (updateCount < 0)
  189. updateCount = 0;
  190. result += updateCount;
  191. return result;
  192. }
  193. public int ExecuteSqlBulkCopy()
  194. {
  195. var storage = this.ToStorage();
  196. return storage.BulkCopy() + storage.BulkUpdate();
  197. }
  198. public async Task<int> ExecuteSqlBulkCopyAsync()
  199. {
  200. var storage =await this.ToStorageAsync();
  201. return await storage.BulkCopyAsync() + await storage.BulkUpdateAsync();
  202. }
  203. public StorageableResult<T> ToStorage()
  204. {
  205. if (whereFuncs == null || whereFuncs.Count == 0)
  206. {
  207. return this.Saveable().ToStorage();
  208. }
  209. if (this.allDatas.Count == 0)
  210. return new StorageableResult<T>() {
  211. AsDeleteable = this.Context.Deleteable<T>().AS(asname).Where(it => false),
  212. AsInsertable = this.Context.Insertable(new List<T>()).AS(asname),
  213. AsUpdateable = this.Context.Updateable(new List<T>()).AS(asname),
  214. InsertList = new List<StorageableMessage<T>>(),
  215. UpdateList = new List<StorageableMessage<T>>(),
  216. DeleteList = new List<StorageableMessage<T>>(),
  217. ErrorList = new List<StorageableMessage<T>>(),
  218. IgnoreList = new List<StorageableMessage<T>>(),
  219. OtherList=new List<StorageableMessage<T>>(),
  220. TotalList=new List<StorageableMessage<T>>()
  221. };
  222. var pkInfos = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsPrimarykey);
  223. if (whereExpression==null&&!pkInfos.Any())
  224. {
  225. Check.ExceptionEasy(true, "Need primary key or WhereColumn", "使用Storageable实体需要主键或者使用WhereColumn指定条件列");
  226. }
  227. if (whereExpression == null && pkInfos.Any())
  228. {
  229. this.Context.Utilities.PageEach(allDatas, 300, item => {
  230. var addItems=this.Context.Queryable<T>().Filter(null, this.isDisableFilters).TranLock(this.lockType).AS(asname).WhereClassByPrimaryKey(item.Select(it => it.Item).ToList()).ToList();
  231. dbDataList.AddRange(addItems);
  232. });
  233. }
  234. var pkProperties = GetPkProperties(pkInfos);
  235. var messageList = allDatas.Select(it => new StorageableMessage<T>()
  236. {
  237. Item = it.Item,
  238. Database = dbDataList,
  239. PkFields= pkProperties
  240. }).ToList();
  241. foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
  242. {
  243. List<StorageableMessage<T>> whereList = messageList.Where(it => it.StorageType == null).ToList();
  244. Func<StorageableMessage<T>, bool> exp = item.value1;
  245. var list = whereList.Where(exp).ToList();
  246. foreach (var it in list)
  247. {
  248. it.StorageType = item.key;
  249. it.StorageMessage = item.value2;
  250. }
  251. }
  252. var delete = messageList.Where(it => it.StorageType == StorageType.Delete).ToList();
  253. var update = messageList.Where(it => it.StorageType == StorageType.Update).ToList();
  254. var inset = messageList.Where(it => it.StorageType == StorageType.Insert).ToList();
  255. var error = messageList.Where(it => it.StorageType == StorageType.Error).ToList();
  256. var ignore = messageList.Where(it => it.StorageType == StorageType.Ignore || it.StorageType == null).ToList();
  257. var other = messageList.Where(it => it.StorageType == StorageType.Other).ToList();
  258. StorageableResult<T> result = new StorageableResult<T>()
  259. {
  260. _WhereColumnList= wherecolumnList,
  261. _AsName =asname,
  262. _Context=this.Context,
  263. AsDeleteable = this.Context.Deleteable<T>().AS(asname),
  264. AsUpdateable = this.Context.Updateable(update.Select(it => it.Item).ToList()).AS(asname),
  265. AsInsertable = this.Context.Insertable(inset.Select(it => it.Item).ToList()).AS(asname),
  266. OtherList = other,
  267. InsertList = inset,
  268. DeleteList = delete,
  269. UpdateList = update,
  270. ErrorList = error,
  271. IgnoreList = ignore,
  272. TotalList = messageList
  273. };
  274. if (this.whereExpression != null)
  275. {
  276. result.AsUpdateable.WhereColumns(whereExpression);
  277. result.AsDeleteable.WhereColumns(update.Select(it => it.Item).ToList(),whereExpression);
  278. }
  279. if (this.whereExpression != null)
  280. {
  281. result.AsDeleteable.WhereColumns(delete.Select(it => it.Item).ToList(), whereExpression);
  282. }
  283. else
  284. {
  285. result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
  286. }
  287. return result;
  288. }
  289. public StorageableResult<T> GetStorageableResult()
  290. {
  291. if (whereFuncs == null || whereFuncs.Count == 0)
  292. {
  293. return this.Saveable().GetStorageableResult();
  294. }
  295. if (this.allDatas.Count == 0)
  296. return new StorageableResult<T>()
  297. {
  298. //AsDeleteable = this.Context.Deleteable<T>().AS(asname).Where(it => false),
  299. //AsInsertable = this.Context.Insertable(new List<T>()).AS(asname),
  300. //AsUpdateable = this.Context.Updateable(new List<T>()).AS(asname),
  301. InsertList = new List<StorageableMessage<T>>(),
  302. UpdateList = new List<StorageableMessage<T>>(),
  303. DeleteList = new List<StorageableMessage<T>>(),
  304. ErrorList = new List<StorageableMessage<T>>(),
  305. IgnoreList = new List<StorageableMessage<T>>(),
  306. OtherList = new List<StorageableMessage<T>>(),
  307. TotalList = new List<StorageableMessage<T>>()
  308. };
  309. var pkInfos = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsPrimarykey);
  310. if (whereExpression == null && !pkInfos.Any())
  311. {
  312. Check.ExceptionEasy(true, "Need primary key or WhereColumn", "使用Storageable实体需要主键或者使用WhereColumn指定条件列");
  313. }
  314. if (whereExpression == null && pkInfos.Any())
  315. {
  316. this.Context.Utilities.PageEach(allDatas, 300, item => {
  317. var addItems = this.Context.Queryable<T>().Filter(null, this.isDisableFilters).TranLock(this.lockType).AS(asname).WhereClassByPrimaryKey(item.Select(it => it.Item).ToList()).ToList();
  318. dbDataList.AddRange(addItems);
  319. });
  320. }
  321. var pkProperties = GetPkProperties(pkInfos);
  322. var messageList = allDatas.Select(it => new StorageableMessage<T>()
  323. {
  324. Item = it.Item,
  325. Database = dbDataList,
  326. PkFields = pkProperties
  327. }).ToList();
  328. foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
  329. {
  330. List<StorageableMessage<T>> whereList = messageList.Where(it => it.StorageType == null).ToList();
  331. Func<StorageableMessage<T>, bool> exp = item.value1;
  332. var list = whereList.Where(exp).ToList();
  333. foreach (var it in list)
  334. {
  335. it.StorageType = item.key;
  336. it.StorageMessage = item.value2;
  337. }
  338. }
  339. var delete = messageList.Where(it => it.StorageType == StorageType.Delete).ToList();
  340. var update = messageList.Where(it => it.StorageType == StorageType.Update).ToList();
  341. var inset = messageList.Where(it => it.StorageType == StorageType.Insert).ToList();
  342. var error = messageList.Where(it => it.StorageType == StorageType.Error).ToList();
  343. var ignore = messageList.Where(it => it.StorageType == StorageType.Ignore || it.StorageType == null).ToList();
  344. var other = messageList.Where(it => it.StorageType == StorageType.Other).ToList();
  345. StorageableResult<T> result = new StorageableResult<T>()
  346. {
  347. _WhereColumnList = wherecolumnList,
  348. _AsName = asname,
  349. _Context = this.Context,
  350. //AsDeleteable = this.Context.Deleteable<T>().AS(asname),
  351. //AsUpdateable = this.Context.Updateable(update.Select(it => it.Item).ToList()).AS(asname),
  352. //AsInsertable = this.Context.Insertable(inset.Select(it => it.Item).ToList()).AS(asname),
  353. OtherList = other,
  354. InsertList = inset,
  355. DeleteList = delete,
  356. UpdateList = update,
  357. ErrorList = error,
  358. IgnoreList = ignore,
  359. TotalList = messageList
  360. };
  361. //if (this.whereExpression != null)
  362. //{
  363. // result.AsUpdateable.WhereColumns(whereExpression);
  364. // result.AsDeleteable.WhereColumns(update.Select(it => it.Item).ToList(), whereExpression);
  365. //}
  366. //result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
  367. return result;
  368. }
  369. public async Task<StorageableResult<T>> ToStorageAsync()
  370. {
  371. if (whereFuncs == null || whereFuncs.Count == 0)
  372. {
  373. return await this.Saveable().ToStorageAsync();
  374. }
  375. if (this.allDatas.Count == 0)
  376. return new StorageableResult<T>()
  377. {
  378. AsDeleteable = this.Context.Deleteable<T>().AS(asname).Where(it => false),
  379. AsInsertable = this.Context.Insertable(new List<T>()).AS(asname),
  380. AsUpdateable = this.Context.Updateable(new List<T>()).AS(asname),
  381. InsertList = new List<StorageableMessage<T>>(),
  382. UpdateList = new List<StorageableMessage<T>>(),
  383. DeleteList = new List<StorageableMessage<T>>(),
  384. ErrorList = new List<StorageableMessage<T>>(),
  385. IgnoreList = new List<StorageableMessage<T>>(),
  386. OtherList = new List<StorageableMessage<T>>(),
  387. TotalList = new List<StorageableMessage<T>>()
  388. };
  389. var pkInfos = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsPrimarykey);
  390. if (whereExpression == null && !pkInfos.Any())
  391. {
  392. Check.Exception(true, "Need primary key or WhereColumn");
  393. }
  394. if (whereExpression == null && pkInfos.Any())
  395. {
  396. await this.Context.Utilities.PageEachAsync(allDatas, 300,async item => {
  397. var addItems =await this.Context.Queryable<T>().Filter(null,this.isDisableFilters).AS(asname).TranLock(this.lockType).WhereClassByPrimaryKey(item.Select(it => it.Item).ToList()).ToListAsync();
  398. dbDataList.AddRange(addItems);
  399. });
  400. }
  401. var pkProperties = GetPkProperties(pkInfos);
  402. var messageList = allDatas.Select(it => new StorageableMessage<T>()
  403. {
  404. Item = it.Item,
  405. Database = dbDataList,
  406. PkFields = pkProperties
  407. }).ToList();
  408. foreach (var item in whereFuncs.OrderByDescending(it => (int)it.key))
  409. {
  410. List<StorageableMessage<T>> whereList = messageList.Where(it => it.StorageType == null).ToList();
  411. Func<StorageableMessage<T>, bool> exp = item.value1;
  412. var list = whereList.Where(exp).ToList();
  413. foreach (var it in list)
  414. {
  415. it.StorageType = item.key;
  416. it.StorageMessage = item.value2;
  417. }
  418. }
  419. var delete = messageList.Where(it => it.StorageType == StorageType.Delete).ToList();
  420. var update = messageList.Where(it => it.StorageType == StorageType.Update).ToList();
  421. var inset = messageList.Where(it => it.StorageType == StorageType.Insert).ToList();
  422. var error = messageList.Where(it => it.StorageType == StorageType.Error).ToList();
  423. var ignore = messageList.Where(it => it.StorageType == StorageType.Ignore || it.StorageType == null).ToList();
  424. var other = messageList.Where(it => it.StorageType == StorageType.Other).ToList();
  425. StorageableResult<T> result = new StorageableResult<T>()
  426. {
  427. _WhereColumnList = wherecolumnList,
  428. _AsName = asname,
  429. _Context = this.Context,
  430. AsDeleteable = this.Context.Deleteable<T>().AS(asname),
  431. AsUpdateable = this.Context.Updateable(update.Select(it => it.Item).ToList()).AS(asname),
  432. AsInsertable = this.Context.Insertable(inset.Select(it => it.Item).ToList()).AS(asname),
  433. OtherList = other,
  434. InsertList = inset,
  435. DeleteList = delete,
  436. UpdateList = update,
  437. ErrorList = error,
  438. IgnoreList = ignore,
  439. TotalList = messageList
  440. };
  441. if (this.whereExpression != null)
  442. {
  443. result.AsUpdateable.WhereColumns(whereExpression);
  444. result.AsDeleteable.WhereColumns(delete.Select(it => it.Item).ToList(),whereExpression);
  445. }
  446. result.AsDeleteable.Where(delete.Select(it => it.Item).ToList());
  447. return result;
  448. }
  449. private string[] GetPkProperties(IEnumerable<EntityColumnInfo> pkInfos)
  450. {
  451. if (whereExpression == null)
  452. {
  453. return pkInfos.Select(it => it.PropertyName).ToArray();
  454. }
  455. else
  456. {
  457. return wherecolumnList.Select(it => it.PropertyName).ToArray();
  458. }
  459. }
  460. List<EntityColumnInfo> wherecolumnList;
  461. public IStorageable<T> WhereColumns(Expression<Func<T, object>> columns, Func<DateTime, string> formatTime)
  462. {
  463. this.formatTime = formatTime;
  464. return WhereColumns(columns);
  465. }
  466. public IStorageable<T> WhereColumns(Expression<Func<T, object>> columns)
  467. {
  468. if (columns == null)
  469. return this;
  470. else if (asname == null && typeof(T).GetCustomAttribute<SplitTableAttribute>() != null)
  471. {
  472. whereExpression = columns;
  473. return this;
  474. }
  475. else
  476. {
  477. List<string> list = GetExpressionValue(columns, ResolveExpressType.ArraySingle).GetResultArray().Select(it => Builder.GetNoTranslationColumnName(it)).ToList();
  478. var dbColumns = this.Context.EntityMaintenance.GetEntityInfo<T>().Columns.Where(it => it.IsIgnore == false);
  479. var whereColumns = dbColumns.Where(it => list.Any(y =>
  480. it.DbColumnName.Equals(y, StringComparison.CurrentCultureIgnoreCase) ||
  481. it.PropertyName.Equals(y, StringComparison.CurrentCultureIgnoreCase))
  482. ).ToList();
  483. wherecolumnList = whereColumns;
  484. if (whereColumns.Count == 0)
  485. {
  486. whereColumns = dbColumns.Where(it => it.IsPrimarykey).ToList();
  487. }
  488. if (whereColumns.Count > 0)
  489. {
  490. if (queryableWhereExp == null)
  491. {
  492. this.Context.Utilities.PageEach(allDatas, 200, itemList =>
  493. {
  494. List<IConditionalModel> conditList = new List<IConditionalModel>();
  495. SetConditList(itemList, whereColumns, conditList);
  496. var addItem = this.Context.Queryable<T>().AS(asname)
  497. .Filter(null, this.isDisableFilters)
  498. .TranLock(this.lockType)
  499. .Where(conditList,true).ToList();
  500. this.dbDataList.AddRange(addItem);
  501. });
  502. }
  503. else
  504. {
  505. this.dbDataList.AddRange(this.Context.Queryable<T>().AS(asname).Where(queryableWhereExp).ToList());
  506. }
  507. }
  508. this.whereExpression = columns;
  509. return this;
  510. }
  511. }
  512. public IStorageable<T> WhereColumns(string [] columns)
  513. {
  514. var list = columns.Select(it=>this.Context.EntityMaintenance.GetPropertyName<T>(it)).ToList();
  515. var exp=ExpressionBuilderHelper.CreateNewFields<T>(this.Context.EntityMaintenance.GetEntityInfo<T>(), list);
  516. return this.WhereColumns(exp);
  517. }
  518. public IStorageable<T> WhereColumns(string[] columns, Func<DateTime, string> formatTime)
  519. {
  520. this.formatTime = formatTime;
  521. return WhereColumns(columns);
  522. }
  523. private void SetConditList(List<StorageableInfo<T>> itemList, List<EntityColumnInfo> whereColumns, List<IConditionalModel> conditList)
  524. {
  525. ;
  526. foreach (var dataItem in itemList)
  527. {
  528. var condition = new ConditionalCollections()
  529. {
  530. ConditionalList = new List<KeyValuePair<WhereType, SqlSugar.ConditionalModel>>()
  531. };
  532. conditList.Add(condition);
  533. int i = 0;
  534. foreach (var item in whereColumns)
  535. {
  536. var value = item.PropertyInfo.GetValue(dataItem.Item, null);
  537. if (value != null&&value.GetType().IsEnum())
  538. {
  539. if (this.Context.CurrentConnectionConfig.MoreSettings?.TableEnumIsString == true)
  540. {
  541. value = value.ToString();
  542. }
  543. else
  544. {
  545. value = Convert.ToInt64(value);
  546. }
  547. }
  548. if (item.SqlParameterDbType != null && item.SqlParameterDbType is Type && UtilMethods.HasInterface((Type)item.SqlParameterDbType, typeof(ISugarDataConverter)))
  549. {
  550. var columnInfo = item;
  551. var type = columnInfo.SqlParameterDbType as Type;
  552. var ParameterConverter = type.GetMethod("ParameterConverter").MakeGenericMethod(columnInfo.PropertyInfo.PropertyType);
  553. var obj = Activator.CreateInstance(type);
  554. var p = ParameterConverter.Invoke(obj, new object[] { value, 100 }) as SugarParameter;
  555. value = p.Value;
  556. }
  557. condition.ConditionalList.Add(new KeyValuePair<WhereType, ConditionalModel>(i==0?WhereType.Or :WhereType.And, new ConditionalModel()
  558. {
  559. FieldName = item.DbColumnName,
  560. ConditionalType = ConditionalType.Equal,
  561. CSharpTypeName=UtilMethods.GetTypeName(value),
  562. FieldValue = value==null?"null":value.ObjToString(formatTime),
  563. FieldValueConvertFunc=this.Context.CurrentConnectionConfig.DbType==DbType.PostgreSQL?
  564. UtilMethods.GetTypeConvert(value):null
  565. }));
  566. ++i;
  567. }
  568. }
  569. }
  570. public virtual ExpressionResult GetExpressionValue(Expression expression, ResolveExpressType resolveType)
  571. {
  572. ILambdaExpressions resolveExpress = InstanceFactory.GetLambdaExpressions(this.Context.CurrentConnectionConfig); ;
  573. if (this.Context.CurrentConnectionConfig.MoreSettings != null)
  574. {
  575. resolveExpress.TableEnumIsString = this.Context.CurrentConnectionConfig.MoreSettings.TableEnumIsString;
  576. resolveExpress.PgSqlIsAutoToLower = this.Context.CurrentConnectionConfig.MoreSettings.PgSqlIsAutoToLower;
  577. }
  578. else
  579. {
  580. resolveExpress.PgSqlIsAutoToLower = true;
  581. }
  582. resolveExpress.MappingColumns = Context.MappingColumns;
  583. resolveExpress.MappingTables = Context.MappingTables;
  584. resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
  585. resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
  586. resolveExpress.InitMappingInfo = Context.InitMappingInfo;
  587. resolveExpress.RefreshMapping = () =>
  588. {
  589. resolveExpress.MappingColumns = Context.MappingColumns;
  590. resolveExpress.MappingTables = Context.MappingTables;
  591. resolveExpress.IgnoreComumnList = Context.IgnoreColumns;
  592. resolveExpress.SqlFuncServices = Context.CurrentConnectionConfig.ConfigureExternalServices == null ? null : Context.CurrentConnectionConfig.ConfigureExternalServices.SqlFuncServices;
  593. };
  594. resolveExpress.Resolve(expression, resolveType);
  595. if (this.Parameters == null)
  596. this.Parameters = new List<SugarParameter>();
  597. this.Parameters.AddRange(resolveExpress.Parameters);
  598. var result = resolveExpress.Result;
  599. return result;
  600. }
  601. public IStorageable<T> As(string tableName)
  602. {
  603. this.asname = tableName;
  604. return this;
  605. }
  606. }
  607. }