AzureTableDBRepository.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. using TEAMModelOS.SDK.Module.AzureTable.Configuration;
  2. using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
  3. using TEAMModelOS.SDK.Extension.DataResult.PageToken;
  4. using Microsoft.WindowsAzure.Storage.Table;
  5. using System;
  6. using System.Collections.Concurrent;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.Helper.Security.AESCrypt;
  12. using TEAMModelOS.SDK.Context.Exception;
  13. using System.Reflection;
  14. using TEAMModelOS.SDK.Context.Attributes.Azure;
  15. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  16. using TEAMModelOS.SDK.Context.Configuration;
  17. using Microsoft.Extensions.Configuration;
  18. namespace TEAMModelOS.SDK.Module.AzureTable.Implements
  19. {
  20. public class AzureTableDBRepository : IAzureTableDBRepository
  21. {
  22. private readonly string china = "417A7572654368696E612020202020202020202020202020202020202020202097EB27FCC1F03349787DCD35F4DE22BBDFEDC90F24738B1D7FB9167A2C191BE671B512E17D48B73A002FC98867345CD59D3250AF59FD5FDFFC67976108F9E3BC9E9F75EDE605B058C1821D16BD9EB753B8E7D39FF48163430C1B5F3B6150195B880C3FCB87D35BF3540432734B768EC28C77B4CF0D556E794DE57979C1E01C429E66F7B2794D9940CF287F2B22A22E5F266B949D5E523E709FF37229E45D1A8FC8C4341E0A8346BB976CCB3D91802FFE5A4A28577898B4E942B5BA3A4A7B796FA673782D405060E7F2CBA4F67DF59F47";
  23. private readonly string global = "417A757265476C6F62616C2020202020202020202020202020202020202020206956019D195ED330AFA660D369B9464FC5E90AB3A106FDDD7978A2772DB186CDAE21C6CBFDE2B6739F089E853B3171A27841026E61C51666347F63FDF63E4377448D493B05CF6CDB3791946B9145825DD7756392EB8EA36DBF42E5C1C0021CEC2CDB5F4EA57EBCFA98B17D7236FA2CDCA6E7FCBE1DDC45BEAF691A2462A8BC3C429CBC4BCCA3192E554D23758AA8EA5937F988C927534C70A4769ED33878BEC10E2550F121E4AEB5A2DA213F2902D602A758C7D93D5DED368544F8A86D2A0CAA7813D1D950EC81D544EE41A8EDC84173";
  24. private readonly CloudTableClient tableClient;
  25. private CloudTable Table { get; set; }
  26. //public AzureTableDBRepository(AzureTableOptions options)
  27. //{
  28. // if (!string.IsNullOrEmpty(options.ConnectionString))
  29. // {
  30. // tableClient = TableClientSingleton.getInstance(options.ConnectionString).GetTableClient();
  31. // }
  32. // else if (AzureTableConfig.AZURE_CHINA.Equals(options.AzureTableDialect))
  33. // {
  34. // AESCrypt crypt = new AESCrypt();
  35. // tableClient = TableClientSingleton.getInstance(crypt.Decrypt(china, options.AzureTableDialect)).GetTableClient();
  36. // }
  37. // else if (AzureTableConfig.AZURE_GLOBAL.Equals(options.AzureTableDialect))
  38. // {
  39. // AESCrypt crypt = new AESCrypt();
  40. // tableClient = TableClientSingleton.getInstance(crypt.Decrypt(global, options.AzureTableDialect)).GetTableClient();
  41. // }
  42. // else { throw new BizException("请设置正确的AzureTable数据库配置信息!"); }
  43. //}
  44. public AzureTableDBRepository()
  45. {
  46. AzureTableOptions options= BaseConfigModel.Configuration.GetSection("Azure:Table").Get<AzureTableOptions>();
  47. if (!string.IsNullOrEmpty(options.ConnectionString))
  48. {
  49. tableClient = TableClientSingleton.getInstance(options.ConnectionString).GetTableClient();
  50. }
  51. else { throw new BizException("请设置正确的AzureTable数据库配置信息!"); }
  52. }
  53. private async Task<string> InitializeTable<T>()
  54. {
  55. string TableName = GetTableSpace<T>();
  56. if (Table == null || !Table.Name.Equals(TableName))
  57. {
  58. Table = tableClient.GetTableReference(TableName);
  59. await Table.CreateIfNotExistsAsync();
  60. }
  61. return TableName;
  62. }
  63. private string GetTableSpace<T>()
  64. {
  65. Type type = typeof(T);
  66. string Name = type.Name;
  67. object[] attributes = type.GetCustomAttributes(true);
  68. foreach (object attribute in attributes) //2.通过映射,找到成员属性上关联的特性类实例,
  69. {
  70. if (attribute is TableSpaceAttribute tableSpace)
  71. {
  72. Name = tableSpace.Name + Name;
  73. }
  74. }
  75. return Name;
  76. }
  77. public async Task<List<T>> FindAll<T>() where T : TableEntity, new()
  78. {
  79. string TableName = await InitializeTable<T>();
  80. var exQuery = new TableQuery<T>();
  81. return await QueryList<T>(exQuery, TableName);
  82. }
  83. private async Task<List<T>> QueryList<T>(TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
  84. {
  85. TableContinuationToken continuationToken = null;
  86. List<T> entitys = new List<T>();
  87. do
  88. {
  89. var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
  90. if (result.Results.Count > 0)
  91. {
  92. entitys.AddRange(result.ToList());
  93. }
  94. continuationToken = result.ContinuationToken;
  95. } while (continuationToken != null);
  96. return entitys;
  97. }
  98. private async Task<T> QueryObject<T>(TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
  99. {
  100. TableContinuationToken continuationToken = null;
  101. T entity = new T();
  102. do
  103. {
  104. var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
  105. if (result.Results.Count > 0)
  106. {
  107. entity = result.ToList().Single();
  108. }
  109. continuationToken = result.ContinuationToken;
  110. } while (continuationToken != null);
  111. return entity;
  112. }
  113. public async Task<int> Count<T>(TableContinuationToken continuationToken) where T : TableEntity, new()
  114. {
  115. string TableName = await InitializeTable<T>();
  116. var exQuery = new TableQuery<T>();
  117. List<T> entitys = new List<T>();
  118. do
  119. {
  120. var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
  121. if (result.Results.Count > 0)
  122. {
  123. entitys.AddRange(result.ToList());
  124. }
  125. continuationToken = result.ContinuationToken;
  126. } while (continuationToken != null);
  127. return entitys.Count;
  128. }
  129. public async Task<int> Count<T>() where T : TableEntity, new()
  130. {
  131. string TableName = await InitializeTable<T>();
  132. TableContinuationToken continuationToken = null;
  133. var exQuery = new TableQuery<T>();
  134. List<T> entitys = new List<T>();
  135. do
  136. {
  137. var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
  138. if (result.Results.Count > 0)
  139. {
  140. entitys.AddRange(result.ToList());
  141. }
  142. continuationToken = result.ContinuationToken;
  143. } while (continuationToken != null);
  144. return entitys.Count;
  145. }
  146. public async Task<T> FindByRowKey<T>(string id) where T : TableEntity, new()
  147. {
  148. string TableName = await InitializeTable<T>();
  149. var exQuery = new TableQuery<T>();
  150. if (!string.IsNullOrEmpty(id))
  151. {
  152. string typeStr = SwitchType<T>(id, "RowKey");
  153. if (string.IsNullOrEmpty(typeStr))
  154. {
  155. return null;
  156. }
  157. exQuery.Where(typeStr);
  158. // exQuery.Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, id));
  159. return await QueryObject<T>(exQuery, TableName);
  160. }
  161. else
  162. {
  163. return null;
  164. }
  165. }
  166. public async Task<List<T>> FindListByDict<T>(Dictionary<string, object> dict) where T : TableEntity, new()
  167. {
  168. string TableName = await InitializeTable<T>();
  169. var exQuery = new TableQuery<T>();
  170. StringBuilder builder = new StringBuilder();
  171. if (null != dict && dict.Count > 0)
  172. {
  173. var keys = dict.Keys;
  174. int index = 1;
  175. foreach (string key in keys)
  176. {
  177. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  178. {
  179. string typeStr = SwitchType<T>(dict[key], key);
  180. if(string.IsNullOrEmpty(typeStr))
  181. {
  182. continue;
  183. }
  184. if (index == 1)
  185. {
  186. //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  187. builder.Append(typeStr);
  188. }
  189. else
  190. {
  191. //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  192. builder.Append(" " + TableOperators.And + " " + typeStr);
  193. }
  194. index++;
  195. }
  196. else {
  197. throw new Exception("The parameter must have value!");
  198. }
  199. }
  200. exQuery.Where(builder.ToString());
  201. return await QueryList<T>(exQuery, TableName);
  202. }
  203. else
  204. {
  205. return null;
  206. }
  207. }
  208. public async Task<List<T>> FindListByKey<T>(string key, object value) where T : TableEntity, new()
  209. {
  210. string TableName = await InitializeTable<T>();
  211. var exQuery = new TableQuery<T>();
  212. if (!string.IsNullOrEmpty(key) && value != null && !string.IsNullOrEmpty(value.ToString()))
  213. {
  214. string typeStr = SwitchType<T>(value, key);
  215. if (string.IsNullOrEmpty(typeStr))
  216. {
  217. return null;
  218. }
  219. exQuery.Where(typeStr);
  220. //exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, value));
  221. return await QueryList<T>(exQuery, TableName);
  222. }
  223. else
  224. {
  225. return null;
  226. }
  227. }
  228. public async Task<T> FindOneByDict<T>(IDictionary<string, object> dict) where T : TableEntity, new()
  229. {
  230. string TableName = await InitializeTable<T>();
  231. var exQuery = new TableQuery<T>();
  232. StringBuilder builder = new StringBuilder();
  233. if (null != dict && dict.Count > 0)
  234. {
  235. var keys = dict.Keys;
  236. int index = 1;
  237. foreach (string key in keys)
  238. {
  239. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  240. {
  241. string typeStr = SwitchType<T>(dict[key], key);
  242. if (string.IsNullOrEmpty(typeStr))
  243. {
  244. continue;
  245. }
  246. if (index == 1)
  247. {
  248. //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  249. builder.Append(typeStr);
  250. }
  251. else
  252. {
  253. // builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  254. builder.Append(" " + TableOperators.And + " " + typeStr);
  255. }
  256. index++;
  257. }
  258. else
  259. {
  260. throw new Exception("The parameter must have value!");
  261. }
  262. }
  263. exQuery.Where(builder.ToString());
  264. return await QueryObject<T>(exQuery, TableName);
  265. }
  266. else
  267. {
  268. return null;
  269. }
  270. }
  271. private static string SwitchType<T>(object obj, string key)
  272. {
  273. Type objType = typeof(T);
  274. PropertyInfo property = objType.GetProperty(key);
  275. //Type s = obj.GetType();
  276. //TypeCode typeCode = Type.GetTypeCode(s);
  277. if (property == null) {
  278. //return null;
  279. throw new Exception(objType.FullName+" PropertyInfo doesn't include this parameter :" +key);
  280. }
  281. TypeCode typeCode = Type.GetTypeCode(property.PropertyType);
  282. switch (typeCode)
  283. {
  284. case TypeCode.String: return TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, obj.ToString());
  285. case TypeCode.Int32: return TableQuery.GenerateFilterConditionForInt(key, QueryComparisons.Equal, int.Parse( obj.ToString()));
  286. case TypeCode.Double: return TableQuery.GenerateFilterConditionForDouble(key, QueryComparisons.Equal, (double)obj);
  287. case TypeCode.Byte: return TableQuery.GenerateFilterConditionForBinary(key, QueryComparisons.Equal, (byte[])obj);
  288. case TypeCode.Boolean: return TableQuery.GenerateFilterConditionForBool(key, QueryComparisons.Equal, (bool)obj);
  289. case TypeCode.DateTime: return TableQuery.GenerateFilterConditionForDate(key, QueryComparisons.Equal, (DateTimeOffset)obj);
  290. case TypeCode.Int64: return TableQuery.GenerateFilterConditionForLong(key, QueryComparisons.Equal, long.Parse(obj.ToString()));
  291. default: return null;
  292. }
  293. }
  294. public async Task<T> FindOneByKey<T>(string key, object value) where T : TableEntity, new()
  295. {
  296. string TableName = await InitializeTable<T>();
  297. var exQuery = new TableQuery<T>();
  298. if (!string.IsNullOrEmpty(key) && value != null && !string.IsNullOrEmpty(value.ToString()))
  299. {
  300. string typeStr = SwitchType<T>(value, key);
  301. if (string.IsNullOrEmpty(typeStr))
  302. {
  303. return null;
  304. }
  305. exQuery.Where(typeStr);
  306. //exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal,
  307. // value));
  308. return await QueryObject<T>(exQuery, TableName);
  309. }
  310. else
  311. {
  312. return null;
  313. }
  314. }
  315. public async Task<List<T>> GetEntities<T>(IDictionary<string, object> dict) where T : TableEntity, new()
  316. {
  317. string TableName = await InitializeTable<T>();
  318. var exQuery = new TableQuery<T>();
  319. StringBuilder builder = new StringBuilder();
  320. if (null != dict && dict.Count > 0)
  321. {
  322. var keys = dict.Keys;
  323. int index = 1;
  324. foreach (string key in keys)
  325. {
  326. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  327. {
  328. string typeStr = SwitchType<T>(dict, key);
  329. if (string.IsNullOrEmpty(typeStr))
  330. {
  331. continue;
  332. }
  333. if (index == 1)
  334. {
  335. //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  336. builder.Append(typeStr);
  337. }
  338. else
  339. {
  340. // builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  341. builder.Append(" " + TableOperators.And + " " + typeStr);
  342. }
  343. index++;
  344. }
  345. else
  346. {
  347. throw new Exception("The parameter must have value!");
  348. }
  349. }
  350. exQuery.Where(builder.ToString());
  351. return await QueryList<T>(exQuery, TableName);
  352. }
  353. else
  354. {
  355. return null;
  356. }
  357. }
  358. public async Task<List<T>> SaveAll<T>(List<T> entitys) where T : TableEntity, new()
  359. {
  360. if (entitys.IsEmpty())
  361. {
  362. return null;
  363. }
  364. string TableName = await InitializeTable<T>();
  365. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  366. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  367. {
  368. Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
  369. {
  370. { group.Key, group.ToList() }
  371. };
  372. listInfo.Add(dictInfo);
  373. }
  374. foreach (Dictionary<string, List<T>> dict in listInfo)
  375. {
  376. IList<TableResult> result = null;
  377. foreach (string key in dict.Keys)
  378. {
  379. List<T> values = dict[key];
  380. //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
  381. // async range =>
  382. // {
  383. // TableBatchOperation batchOperation = new TableBatchOperation();
  384. // for (Int32 i = range.Item1; i < range.Item2; i++)
  385. // batchOperation.Insert(values[i]);
  386. // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  387. // });
  388. int pageSize = 100;
  389. int pages = (int)Math.Ceiling((double)values.Count / pageSize);
  390. for (int i = 0; i < pages; i++)
  391. {
  392. List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
  393. TableBatchOperation batchOperation = new TableBatchOperation();
  394. for (int j = 0; j < lists.Count; j++)
  395. {
  396. batchOperation.Insert(lists[j]);
  397. }
  398. result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  399. }
  400. }
  401. }
  402. return entitys;
  403. }
  404. public async Task<List<T>> UpdateAll<T>(List<T> entitys) where T : TableEntity, new()
  405. {
  406. if (entitys.IsEmpty())
  407. {
  408. return null;
  409. }
  410. string TableName = await InitializeTable<T>();
  411. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  412. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  413. {
  414. Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
  415. {
  416. { group.Key, group.ToList() }
  417. };
  418. listInfo.Add(dictInfo);
  419. }
  420. foreach (Dictionary<string, List<T>> dict in listInfo)
  421. {
  422. IList<TableResult> result = null;
  423. foreach (string key in dict.Keys)
  424. {
  425. List<T> values = dict[key];
  426. //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
  427. // async range =>
  428. // {
  429. // TableBatchOperation batchOperation = new TableBatchOperation();
  430. // for (Int32 i = range.Item1; i < range.Item2; i++)
  431. // batchOperation.Replace(values[i]);
  432. // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  433. // });
  434. int pageSize = 100;
  435. int pages = (int)Math.Ceiling((double)values.Count / pageSize);
  436. for (int i = 0; i < pages; i++)
  437. {
  438. List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
  439. TableBatchOperation batchOperation = new TableBatchOperation();
  440. for (int j = 0; j < lists.Count; j++)
  441. {
  442. batchOperation.Replace(lists[j]);
  443. }
  444. result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  445. }
  446. }
  447. }
  448. return entitys;
  449. }
  450. public async Task<List<T>> SaveOrUpdateAll<T>(List<T> entitys) where T : TableEntity, new()
  451. {
  452. if (entitys.IsEmpty())
  453. {
  454. return null;
  455. }
  456. string TableName = await InitializeTable<T>();
  457. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  458. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  459. {
  460. Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
  461. {
  462. { group.Key, group.ToList() }
  463. };
  464. listInfo.Add(dictInfo);
  465. }
  466. foreach (Dictionary<string, List<T>> dict in listInfo)
  467. {
  468. IList<TableResult> result = null;
  469. foreach (string key in dict.Keys)
  470. {
  471. List<T> values = dict[key];
  472. //Parallel.ForEach(Partitioner.Create(0, values.Count, 50),
  473. // async range =>
  474. // {
  475. // TableBatchOperation batchOperation = new TableBatchOperation();
  476. // for (Int32 i = range.Item1; i < range.Item2; i++)
  477. // batchOperation.InsertOrReplace(values[i]);
  478. // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  479. // });
  480. int pageSize = 100;
  481. int pages = (int)Math.Ceiling((double)values.Count/ pageSize);
  482. for (int i= 0; i < pages; i++) {
  483. List<T> lists= values.Skip((i) * pageSize).Take(pageSize).ToList();
  484. TableBatchOperation batchOperation = new TableBatchOperation();
  485. for (int j = 0; j < lists.Count; j++)
  486. {
  487. batchOperation.InsertOrReplace(lists[j]);
  488. }
  489. result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  490. }
  491. }
  492. }
  493. return entitys;
  494. }
  495. public async Task<List<T>> DeleteAll<T>(List<T> entitys) where T : TableEntity, new()
  496. {
  497. if (entitys.IsEmpty())
  498. {
  499. return null;
  500. }
  501. string TableName = await InitializeTable<T>();
  502. IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
  503. foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
  504. {
  505. Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
  506. {
  507. { group.Key, group.ToList() }
  508. };
  509. listInfo.Add(dictInfo);
  510. }
  511. foreach (Dictionary<string, List<T>> dict in listInfo)
  512. {
  513. IList<TableResult> result = null;
  514. foreach (string key in dict.Keys)
  515. {
  516. List<T> values = dict[key];
  517. //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
  518. // async range =>
  519. // {
  520. // TableBatchOperation batchOperation = new TableBatchOperation();
  521. // for (Int32 i = range.Item1; i < range.Item2; i++)
  522. // batchOperation.Delete(values[i]);
  523. // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  524. // });
  525. int pageSize = 100;
  526. int pages = (int)Math.Ceiling((double)values.Count / pageSize);
  527. for (int i = 0; i < pages; i++)
  528. {
  529. List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
  530. TableBatchOperation batchOperation = new TableBatchOperation();
  531. for (int j = 0; j < lists.Count; j++)
  532. {
  533. batchOperation.Delete(lists[j]);
  534. }
  535. result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
  536. }
  537. }
  538. }
  539. return entitys;
  540. }
  541. public async Task<T> Save<T>(TableEntity entity) where T : TableEntity, new()
  542. {
  543. string TableName = await InitializeTable<T>();
  544. TableOperation operation = TableOperation.Insert(entity);
  545. TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
  546. return (T)result.Result;
  547. }
  548. public async Task<T> SaveOrUpdate<T>(TableEntity entity) where T : TableEntity, new()
  549. {
  550. string TableName = await InitializeTable<T>();
  551. TableOperation operation = TableOperation.InsertOrReplace(entity);
  552. TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
  553. return (T)result.Result;
  554. }
  555. public async Task<T> Update<T>(TableEntity entity) where T : TableEntity, new()
  556. {
  557. string TableName = await InitializeTable<T>();
  558. TableOperation operation = TableOperation.Replace(entity);
  559. TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
  560. return (T)result.Result;
  561. }
  562. public async Task<T> Delete<T>(TableEntity entity) where T : TableEntity, new()
  563. {
  564. string TableName = await InitializeTable<T>();
  565. TableOperation operation = TableOperation.Delete(entity);
  566. TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
  567. return (T)result.Result;
  568. }
  569. //public async Task<List<T>> FindListByDictAndLike<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict) where T : TableEntity, new()
  570. //{
  571. // throw new NotImplementedException();
  572. //}
  573. //public async Task<List<T>> FindListByDictAndLikeAndNotEQ<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict, Dictionary<string, object> notEQDict) where T : TableEntity, new()
  574. //{
  575. // throw new NotImplementedException();
  576. //}
  577. //public async Task<List<T>> FindListByDictAndLikeAndStartWith<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict, Dictionary<string, object> startDict) where T : TableEntity, new()
  578. //{
  579. // throw new NotImplementedException();
  580. //}
  581. public async Task<AzurePagination<T>> FindListByDict<T>(Dictionary<string, object> dict, AzureTableToken azureTableToken) where T : TableEntity, new()
  582. {
  583. string TableName = await InitializeTable<T>();
  584. var exQuery = new TableQuery<T>();
  585. StringBuilder builder = new StringBuilder();
  586. if (null != dict && dict.Count > 0)
  587. {
  588. var keys = dict.Keys;
  589. int index = 1;
  590. foreach (string key in keys)
  591. {
  592. if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
  593. {
  594. string typeStr = SwitchType<T>(dict, key);
  595. if (string.IsNullOrEmpty(typeStr))
  596. {
  597. continue;
  598. }
  599. if (index == 1)
  600. {
  601. // builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  602. builder.Append(typeStr);
  603. }
  604. else
  605. {
  606. builder.Append(" " + TableOperators.And + " " + typeStr);
  607. //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
  608. }
  609. index++;
  610. }
  611. else
  612. {
  613. throw new Exception("The parameter must have value!");
  614. }
  615. }
  616. exQuery.Where(builder.ToString());
  617. return await QueryList<T>(azureTableToken, exQuery, TableName);
  618. }
  619. else
  620. {
  621. return null;
  622. }
  623. }
  624. private async Task<AzurePagination<T>> QueryList<T>(AzureTableToken azureTableToken, TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
  625. {
  626. TableContinuationToken tableToken = new HaBookTableContinuationToken(azureTableToken).GetContinuationToken();
  627. List<T> entitys = new List<T>();
  628. var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, tableToken);
  629. if (result.Results.Count > 0)
  630. {
  631. entitys.AddRange(result.ToList());
  632. }
  633. tableToken = result.ContinuationToken;
  634. AzurePagination<T> pagination = new AzurePagination<T>
  635. {
  636. token = new HaBookTableContinuationToken(tableToken).GetAzureTableToken(),
  637. data = entitys
  638. };
  639. return pagination;
  640. }
  641. }
  642. }