123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720 |
- using TEAMModelOS.SDK.Module.AzureTable.Configuration;
- using TEAMModelOS.SDK.Module.AzureTable.Interfaces;
- using TEAMModelOS.SDK.Extension.DataResult.PageToken;
- using Microsoft.WindowsAzure.Storage.Table;
- using System;
- using System.Collections.Concurrent;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Helper.Security.AESCrypt;
- using TEAMModelOS.SDK.Context.Exception;
- using System.Reflection;
- using TEAMModelOS.SDK.Context.Attributes.Azure;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.Context.Configuration;
- using Microsoft.Extensions.Configuration;
- namespace TEAMModelOS.SDK.Module.AzureTable.Implements
- {
- public class AzureTableDBRepository : IAzureTableDBRepository
- {
- private readonly string china = "417A7572654368696E612020202020202020202020202020202020202020202097EB27FCC1F03349787DCD35F4DE22BBDFEDC90F24738B1D7FB9167A2C191BE671B512E17D48B73A002FC98867345CD59D3250AF59FD5FDFFC67976108F9E3BC9E9F75EDE605B058C1821D16BD9EB753B8E7D39FF48163430C1B5F3B6150195B880C3FCB87D35BF3540432734B768EC28C77B4CF0D556E794DE57979C1E01C429E66F7B2794D9940CF287F2B22A22E5F266B949D5E523E709FF37229E45D1A8FC8C4341E0A8346BB976CCB3D91802FFE5A4A28577898B4E942B5BA3A4A7B796FA673782D405060E7F2CBA4F67DF59F47";
- private readonly string global = "417A757265476C6F62616C2020202020202020202020202020202020202020206956019D195ED330AFA660D369B9464FC5E90AB3A106FDDD7978A2772DB186CDAE21C6CBFDE2B6739F089E853B3171A27841026E61C51666347F63FDF63E4377448D493B05CF6CDB3791946B9145825DD7756392EB8EA36DBF42E5C1C0021CEC2CDB5F4EA57EBCFA98B17D7236FA2CDCA6E7FCBE1DDC45BEAF691A2462A8BC3C429CBC4BCCA3192E554D23758AA8EA5937F988C927534C70A4769ED33878BEC10E2550F121E4AEB5A2DA213F2902D602A758C7D93D5DED368544F8A86D2A0CAA7813D1D950EC81D544EE41A8EDC84173";
- private readonly CloudTableClient tableClient;
- private CloudTable Table { get; set; }
- //public AzureTableDBRepository(AzureTableOptions options)
- //{
- // if (!string.IsNullOrEmpty(options.ConnectionString))
- // {
- // tableClient = TableClientSingleton.getInstance(options.ConnectionString).GetTableClient();
- // }
- // else if (AzureTableConfig.AZURE_CHINA.Equals(options.AzureTableDialect))
- // {
- // AESCrypt crypt = new AESCrypt();
- // tableClient = TableClientSingleton.getInstance(crypt.Decrypt(china, options.AzureTableDialect)).GetTableClient();
- // }
- // else if (AzureTableConfig.AZURE_GLOBAL.Equals(options.AzureTableDialect))
- // {
- // AESCrypt crypt = new AESCrypt();
- // tableClient = TableClientSingleton.getInstance(crypt.Decrypt(global, options.AzureTableDialect)).GetTableClient();
- // }
- // else { throw new BizException("请设置正确的AzureTable数据库配置信息!"); }
- //}
- public AzureTableDBRepository()
- {
- AzureTableOptions options= BaseConfigModel.Configuration.GetSection("Azure:Table").Get<AzureTableOptions>();
- if (!string.IsNullOrEmpty(options.ConnectionString))
- {
- tableClient = TableClientSingleton.getInstance(options.ConnectionString).GetTableClient();
- }
- else if (AzureTableConfig.AZURE_CHINA.Equals(options.AzureTableDialect))
- {
- AESCrypt crypt = new AESCrypt();
- tableClient = TableClientSingleton.getInstance(crypt.Decrypt(china, options.AzureTableDialect)).GetTableClient();
- }
- else if (AzureTableConfig.AZURE_GLOBAL.Equals(options.AzureTableDialect))
- {
- AESCrypt crypt = new AESCrypt();
- tableClient = TableClientSingleton.getInstance(crypt.Decrypt(global, options.AzureTableDialect)).GetTableClient();
- }
- else { throw new BizException("请设置正确的AzureTable数据库配置信息!"); }
- }
- private async Task<string> InitializeTable<T>()
- {
- string TableName = GetTableSpace<T>();
- if (Table == null || !Table.Name.Equals(TableName))
- {
- Table = tableClient.GetTableReference(TableName);
- await Table.CreateIfNotExistsAsync();
- }
- return TableName;
- }
- private string GetTableSpace<T>()
- {
- Type type = typeof(T);
- string Name = type.Name;
- object[] attributes = type.GetCustomAttributes(true);
- foreach (object attribute in attributes) //2.通过映射,找到成员属性上关联的特性类实例,
- {
- if (attribute is TableSpaceAttribute tableSpace)
- {
- Name = tableSpace.Name + Name;
- }
- }
- return Name;
- }
- public async Task<List<T>> FindAll<T>() where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- return await QueryList<T>(exQuery, TableName);
- }
- private async Task<List<T>> QueryList<T>(TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
- {
- TableContinuationToken continuationToken = null;
- List<T> entitys = new List<T>();
- do
- {
- var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
- if (result.Results.Count > 0)
- {
- entitys.AddRange(result.ToList());
- }
- continuationToken = result.ContinuationToken;
- } while (continuationToken != null);
- return entitys;
- }
- private async Task<T> QueryObject<T>(TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
- {
- TableContinuationToken continuationToken = null;
- T entity = new T();
- do
- {
- var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
- if (result.Results.Count > 0)
- {
- entity = result.ToList().Single();
- }
- continuationToken = result.ContinuationToken;
- } while (continuationToken != null);
- return entity;
- }
- public async Task<int> Count<T>(TableContinuationToken continuationToken) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- List<T> entitys = new List<T>();
- do
- {
- var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
- if (result.Results.Count > 0)
- {
- entitys.AddRange(result.ToList());
- }
- continuationToken = result.ContinuationToken;
- } while (continuationToken != null);
- return entitys.Count;
- }
- public async Task<int> Count<T>() where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- TableContinuationToken continuationToken = null;
- var exQuery = new TableQuery<T>();
- List<T> entitys = new List<T>();
- do
- {
- var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, continuationToken);
- if (result.Results.Count > 0)
- {
- entitys.AddRange(result.ToList());
- }
- continuationToken = result.ContinuationToken;
- } while (continuationToken != null);
- return entitys.Count;
- }
- public async Task<T> FindByRowKey<T>(string id) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- if (!string.IsNullOrEmpty(id))
- {
- string typeStr = SwitchType<T>(id, "RowKey");
- if (string.IsNullOrEmpty(typeStr))
- {
- return null;
- }
- exQuery.Where(typeStr);
- // exQuery.Where(TableQuery.GenerateFilterCondition("RowKey", QueryComparisons.Equal, id));
- return await QueryObject<T>(exQuery, TableName);
- }
- else
- {
- return null;
- }
- }
- public async Task<List<T>> FindListByDict<T>(Dictionary<string, object> dict) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- StringBuilder builder = new StringBuilder();
- if (null != dict && dict.Count > 0)
- {
- var keys = dict.Keys;
- int index = 1;
- foreach (string key in keys)
- {
- if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
- {
- string typeStr = SwitchType<T>(dict[key], key);
- if(string.IsNullOrEmpty(typeStr))
- {
- continue;
- }
- if (index == 1)
- {
- //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- builder.Append(typeStr);
- }
- else
- {
- //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- builder.Append(" " + TableOperators.And + " " + typeStr);
- }
- index++;
- }
- else {
- throw new Exception("The parameter must have value!");
- }
- }
- exQuery.Where(builder.ToString());
- return await QueryList<T>(exQuery, TableName);
- }
- else
- {
- return null;
- }
- }
- public async Task<List<T>> FindListByKey<T>(string key, object value) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- if (!string.IsNullOrEmpty(key) && value != null && !string.IsNullOrEmpty(value.ToString()))
- {
- string typeStr = SwitchType<T>(value, key);
- if (string.IsNullOrEmpty(typeStr))
- {
- return null;
- }
- exQuery.Where(typeStr);
- //exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, value));
- return await QueryList<T>(exQuery, TableName);
- }
- else
- {
- return null;
- }
- }
- public async Task<T> FindOneByDict<T>(IDictionary<string, object> dict) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- StringBuilder builder = new StringBuilder();
- if (null != dict && dict.Count > 0)
- {
- var keys = dict.Keys;
- int index = 1;
- foreach (string key in keys)
- {
- if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
- {
- string typeStr = SwitchType<T>(dict[key], key);
- if (string.IsNullOrEmpty(typeStr))
- {
- continue;
- }
- if (index == 1)
- {
- //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- builder.Append(typeStr);
- }
- else
- {
- // builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- builder.Append(" " + TableOperators.And + " " + typeStr);
- }
- index++;
- }
- else
- {
- throw new Exception("The parameter must have value!");
- }
- }
- exQuery.Where(builder.ToString());
- return await QueryObject<T>(exQuery, TableName);
- }
- else
- {
- return null;
- }
- }
- private static string SwitchType<T>(object obj, string key)
- {
- Type objType = typeof(T);
- PropertyInfo property = objType.GetProperty(key);
- //Type s = obj.GetType();
- //TypeCode typeCode = Type.GetTypeCode(s);
- if (property == null) {
- //return null;
- throw new Exception(objType.FullName+" PropertyInfo doesn't include this parameter :" +key);
- }
- TypeCode typeCode = Type.GetTypeCode(property.PropertyType);
- return typeCode switch
- {
- TypeCode.String => TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, obj.ToString()),
- TypeCode.Int32 => TableQuery.GenerateFilterConditionForInt(key, QueryComparisons.Equal, int.Parse(obj.ToString())),
- TypeCode.Double => TableQuery.GenerateFilterConditionForDouble(key, QueryComparisons.Equal, (double)obj),
- TypeCode.Byte => TableQuery.GenerateFilterConditionForBinary(key, QueryComparisons.Equal, (byte[])obj),
- TypeCode.Boolean => TableQuery.GenerateFilterConditionForBool(key, QueryComparisons.Equal, (bool)obj),
- TypeCode.DateTime => TableQuery.GenerateFilterConditionForDate(key, QueryComparisons.Equal, (DateTimeOffset)obj),
- TypeCode.Int64 => TableQuery.GenerateFilterConditionForLong(key, QueryComparisons.Equal, long.Parse(obj.ToString())),
- _ => null,
- };
- }
- public async Task<T> FindOneByKey<T>(string key, object value) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- if (!string.IsNullOrEmpty(key) && value != null && !string.IsNullOrEmpty(value.ToString()))
- {
- string typeStr = SwitchType<T>(value, key);
- if (string.IsNullOrEmpty(typeStr))
- {
- return null;
- }
- exQuery.Where(typeStr);
- //exQuery.Where(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal,
- // value));
- return await QueryObject<T>(exQuery, TableName);
- }
- else
- {
- return null;
- }
- }
- public async Task<List<T>> GetEntities<T>(IDictionary<string, object> dict) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- StringBuilder builder = new StringBuilder();
- if (null != dict && dict.Count > 0)
- {
- var keys = dict.Keys;
- int index = 1;
- foreach (string key in keys)
- {
- if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
- {
- string typeStr = SwitchType<T>(dict, key);
- if (string.IsNullOrEmpty(typeStr))
- {
- continue;
- }
- if (index == 1)
- {
- //builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- builder.Append(typeStr);
- }
- else
- {
- // builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- builder.Append(" " + TableOperators.And + " " + typeStr);
- }
- index++;
- }
- else
- {
- throw new Exception("The parameter must have value!");
- }
- }
- exQuery.Where(builder.ToString());
- return await QueryList<T>(exQuery, TableName);
- }
- else
- {
- return null;
- }
- }
- public async Task<List<T>> SaveAll<T>(List<T> entitys) where T : TableEntity, new()
- {
- if (entitys.IsEmpty())
- {
- return null;
- }
- string TableName = await InitializeTable<T>();
- IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
- foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
- {
- Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
- {
- { group.Key, group.ToList() }
- };
- listInfo.Add(dictInfo);
- }
- foreach (Dictionary<string, List<T>> dict in listInfo)
- {
- IList<TableResult> result = null;
- foreach (string key in dict.Keys)
- {
- List<T> values = dict[key];
- //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
- // async range =>
- // {
- // TableBatchOperation batchOperation = new TableBatchOperation();
- // for (Int32 i = range.Item1; i < range.Item2; i++)
- // batchOperation.Insert(values[i]);
- // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- // });
- int pageSize = 100;
- int pages = (int)Math.Ceiling((double)values.Count / pageSize);
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
- TableBatchOperation batchOperation = new TableBatchOperation();
- for (int j = 0; j < lists.Count; j++)
- {
- batchOperation.Insert(lists[j]);
- }
- result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- }
- }
- }
- return entitys;
- }
- public async Task<List<T>> UpdateAll<T>(List<T> entitys) where T : TableEntity, new()
- {
- if (entitys.IsEmpty())
- {
- return null;
- }
- string TableName = await InitializeTable<T>();
- IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
- foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
- {
- Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
- {
- { group.Key, group.ToList() }
- };
- listInfo.Add(dictInfo);
- }
- foreach (Dictionary<string, List<T>> dict in listInfo)
- {
- IList<TableResult> result = null;
- foreach (string key in dict.Keys)
- {
- List<T> values = dict[key];
- //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
- // async range =>
- // {
- // TableBatchOperation batchOperation = new TableBatchOperation();
- // for (Int32 i = range.Item1; i < range.Item2; i++)
- // batchOperation.Replace(values[i]);
- // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- // });
- int pageSize = 100;
- int pages = (int)Math.Ceiling((double)values.Count / pageSize);
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
- TableBatchOperation batchOperation = new TableBatchOperation();
- for (int j = 0; j < lists.Count; j++)
- {
- batchOperation.Replace(lists[j]);
- }
- result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- }
- }
- }
- return entitys;
- }
- public async Task<List<T>> SaveOrUpdateAll<T>(List<T> entitys) where T : TableEntity, new()
- {
- if (entitys.IsEmpty())
- {
- return null;
- }
- string TableName = await InitializeTable<T>();
- IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
- foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
- {
- Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
- {
- { group.Key, group.ToList() }
- };
- listInfo.Add(dictInfo);
- }
- foreach (Dictionary<string, List<T>> dict in listInfo)
- {
- IList<TableResult> result = null;
- foreach (string key in dict.Keys)
- {
- List<T> values = dict[key];
- //Parallel.ForEach(Partitioner.Create(0, values.Count, 50),
- // async range =>
- // {
- // TableBatchOperation batchOperation = new TableBatchOperation();
- // for (Int32 i = range.Item1; i < range.Item2; i++)
- // batchOperation.InsertOrReplace(values[i]);
- // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- // });
-
- int pageSize = 100;
- int pages = (int)Math.Ceiling((double)values.Count/ pageSize);
- for (int i= 0; i < pages; i++) {
- List<T> lists= values.Skip((i) * pageSize).Take(pageSize).ToList();
- TableBatchOperation batchOperation = new TableBatchOperation();
- for (int j = 0; j < lists.Count; j++)
- {
- batchOperation.InsertOrReplace(lists[j]);
- }
- result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- }
- }
- }
- return entitys;
- }
- public async Task<List<T>> DeleteAll<T>(List<T> entitys) where T : TableEntity, new()
- {
- if (entitys.IsEmpty())
- {
- return null;
- }
- string TableName = await InitializeTable<T>();
- IList<Dictionary<string, List<T>>> listInfo = new List<Dictionary<string, List<T>>>();
- foreach (IGrouping<string, T> group in entitys.GroupBy(c => c.PartitionKey))
- {
- Dictionary<string, List<T>> dictInfo = new Dictionary<string, List<T>>
- {
- { group.Key, group.ToList() }
- };
- listInfo.Add(dictInfo);
- }
- foreach (Dictionary<string, List<T>> dict in listInfo)
- {
- IList<TableResult> result = null;
- foreach (string key in dict.Keys)
- {
- List<T> values = dict[key];
- //Parallel.ForEach(Partitioner.Create(0, values.Count, 100),
- // async range =>
- // {
- // TableBatchOperation batchOperation = new TableBatchOperation();
- // for (Int32 i = range.Item1; i < range.Item2; i++)
- // batchOperation.Delete(values[i]);
- // result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- // });
- int pageSize = 100;
- int pages = (int)Math.Ceiling((double)values.Count / pageSize);
- for (int i = 0; i < pages; i++)
- {
- List<T> lists = values.Skip((i) * pageSize).Take(pageSize).ToList();
- TableBatchOperation batchOperation = new TableBatchOperation();
- for (int j = 0; j < lists.Count; j++)
- {
- batchOperation.Delete(lists[j]);
- }
- result = await tableClient.GetTableReference(TableName).ExecuteBatchAsync(batchOperation);
- }
- }
- }
- return entitys;
- }
- public async Task<T> Save<T>(TableEntity entity) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- TableOperation operation = TableOperation.Insert(entity);
- TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
- return (T)result.Result;
- }
- public async Task<T> SaveOrUpdate<T>(TableEntity entity) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- TableOperation operation = TableOperation.InsertOrReplace(entity);
- TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
- return (T)result.Result;
- }
- public async Task<T> Update<T>(TableEntity entity) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- TableOperation operation = TableOperation.Replace(entity);
- TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
- return (T)result.Result;
- }
- public async Task<T> Delete<T>(TableEntity entity) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- TableOperation operation = TableOperation.Delete(entity);
- TableResult result = await tableClient.GetTableReference(TableName).ExecuteAsync(operation);
- return (T)result.Result;
- }
- //public async Task<List<T>> FindListByDictAndLike<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict) where T : TableEntity, new()
- //{
- // throw new NotImplementedException();
- //}
- //public async Task<List<T>> FindListByDictAndLikeAndNotEQ<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict, Dictionary<string, object> notEQDict) where T : TableEntity, new()
- //{
- // throw new NotImplementedException();
- //}
- //public async Task<List<T>> FindListByDictAndLikeAndStartWith<T>(Dictionary<string, object> dict, Dictionary<string, object> likeDict, Dictionary<string, object> startDict) where T : TableEntity, new()
- //{
- // throw new NotImplementedException();
- //}
- public async Task<AzurePagination<T>> FindListByDict<T>(Dictionary<string, object> dict, AzureTableToken azureTableToken) where T : TableEntity, new()
- {
- string TableName = await InitializeTable<T>();
- var exQuery = new TableQuery<T>();
- StringBuilder builder = new StringBuilder();
- if (null != dict && dict.Count > 0)
- {
- var keys = dict.Keys;
- int index = 1;
- foreach (string key in keys)
- {
- if (dict[key] != null && !string.IsNullOrEmpty(dict[key].ToString()))
- {
- string typeStr = SwitchType<T>(dict, key);
- if (string.IsNullOrEmpty(typeStr))
- {
- continue;
- }
- if (index == 1)
- {
- // builder.Append(TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- builder.Append(typeStr);
- }
- else
- {
- builder.Append(" " + TableOperators.And + " " + typeStr);
- //builder.Append(" " + TableOperators.And + " " + TableQuery.GenerateFilterCondition(key, QueryComparisons.Equal, dict[key].ToString()));
- }
- index++;
- }
- else
- {
- throw new Exception("The parameter must have value!");
- }
- }
- exQuery.Where(builder.ToString());
- return await QueryList<T>(azureTableToken, exQuery, TableName);
- }
- else
- {
- return null;
- }
- }
- private async Task<AzurePagination<T>> QueryList<T>(AzureTableToken azureTableToken, TableQuery<T> exQuery, string TableName) where T : TableEntity, new()
- {
- TableContinuationToken tableToken = new HaBookTableContinuationToken(azureTableToken).GetContinuationToken();
- List<T> entitys = new List<T>();
- var result = await tableClient.GetTableReference(TableName).ExecuteQuerySegmentedAsync(exQuery, tableToken);
- if (result.Results.Count > 0)
- {
- entitys.AddRange(result.ToList());
- }
- tableToken = result.ContinuationToken;
- AzurePagination<T> pagination = new AzurePagination<T>
- {
- token = new HaBookTableContinuationToken(tableToken).GetAzureTableToken(),
- data = entitys
- };
- return pagination;
- }
- }
- }
|