Explorar o código

新增cosmosDB查找表的长度接口

李思淳 %!s(int64=5) %!d(string=hai) anos
pai
achega
aed590af89

+ 107 - 0
TEAMModelOS.SDK/Module/AzureCosmosDB/Implements/AzureCosmosDBRepository.cs

@@ -826,5 +826,112 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Implements
             }
             
         }
+
+
+        public IQueryable<dynamic> FindCountByDict(string CollectionName, Dictionary<string, object> dict)
+        {
+            if (DocumentCollectionDict.TryGetValue(CollectionName, out DocumentCollection collection))
+            {
+                //  collection = await InitializeCollection(CollectionName, "");
+                /*    StringBuilder sql = new StringBuilder("select * from " + CollectionName + " c where 1=1 ");
+                    if (dict != null)
+                    {
+                        foreach (string key in dict.Keys)
+                        {
+                            sql.Append(GenSql(dict[key], key));
+                        }
+                    }*/
+                StringBuilder sql = new StringBuilder("select value count(c) from c");
+                if (dict != null)
+                {
+
+                    string Join = " join ";
+                    string instring = "in ";
+                    Dictionary<string, string> keyValues = new Dictionary<string, string>();
+                    StringBuilder ps = new StringBuilder();
+                    int heada = 0;
+                    string[] sqlHead = new string[] { "A", "B", "C", "D", "E", "F" };
+                    int kslength = 0;
+                    foreach (KeyValuePair<string, object> item in dict)
+                    {
+
+                        int index = 0;
+                        string[] ks = item.Key.Split("[*]");
+
+                        if (ks.Length > 1)
+                        {
+                            kslength += ks.Length;
+                            if (kslength < (7 + heada))
+                            {
+                                StringBuilder sqlitem = new StringBuilder();
+                                for (int i = 0; i < ks.Length - 1; i++)
+                                {
+
+                                    //Console.WriteLine(ks[i]);
+                                    if (i == 0)
+                                    {
+                                        sqlitem.Append(Join);
+                                        string a = sqlHead[heada] + index;
+                                        sqlitem.Append(a + " ");
+                                        //keyValues.Add(ks[i], a);
+                                        keyValues[ks[i]] = a;
+                                        sqlitem.Append(instring);
+                                        sqlitem.Append("c.");
+                                        sqlitem.Append(ks[i]);
+                                    }
+                                    else
+                                    {
+                                        sqlitem.Append(Join + " ");
+                                        string a = sqlHead[heada] + index;
+                                        sqlitem.Append(a + " ");
+                                        //keyValues.Add(ks[i], a);
+                                        keyValues[ks[i]] = a;
+                                        sqlitem.Append(instring);
+                                        sqlitem.Append(keyValues[ks[i - 1]]);
+                                        sqlitem.Append(ks[i]);
+                                    }
+                                    index += 1;
+                                }
+                                // Console.WriteLine(sqlitem);
+                                sql.Append(sqlitem);
+                                string s;
+                                s = "and " + sqlHead[heada] + (ks.Length - 2) + ks[index] + " = " + "\"" + item.Value.ToString() + "\"";
+                                ps.Append(s);
+                            }
+                            else
+                            {
+                                throw new BizException("数组总共深度不能超过5层", ResponseCode.PARAMS_ERROR);
+
+                            }
+
+                        }
+                        else
+                        {
+                            ps.Append(GenSql(dict[item.Key], item.Key));
+                        }
+                        heada += 1;
+                    }
+                    sql.Append(" where 1=1 ").Append(ps);
+                }
+                FeedOptions queryOptions;
+                if (collection.PartitionKey.Paths.Count > 0)
+                {
+                    queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = true };
+                }
+                else
+                {
+                    queryOptions = new FeedOptions { MaxItemCount = -1, EnableCrossPartitionQuery = false };
+                }
+                //查询条数 -1是全部
+                var query = CosmosClient.CreateDocumentQuery(UriFactory.CreateDocumentCollectionUri(Database, CollectionName), sql.ToString(), queryOptions);
+                return query;
+            }
+            else
+            {
+                throw new BizException("CollectionName named:" + CollectionName + " dose not exsit in Database!");
+            }
+
+        }
+
     }
 }

+ 2 - 1
TEAMModelOS.SDK/Module/AzureCosmosDB/Interfaces/IAzureCosmosDBRepository.cs

@@ -24,6 +24,7 @@ namespace TEAMModelOS.SDK.Module.AzureCosmosDB.Interfaces
         Task<List<T>> SaveAll<T>(List<T> enyites);
         Task<List<T>> UpdateAll<T>(Dictionary<string, object> dict, Dictionary<string, object> updateFilters, List<string> deleteKeys = null);
         Task<List<T>> DeleteAll<T>(Dictionary<string, object> dict);
-        IQueryable<dynamic> FindByDict(string CollectionName, Dictionary<string, object> dict); 
+        IQueryable<dynamic> FindByDict(string CollectionName, Dictionary<string, object> dict);
+        IQueryable<dynamic> FindCountByDict(string CollectionName, Dictionary<string, object> dict);
     }
 }

+ 9 - 0
TEAMModelOS/Controllers/Core/CommonController.cs

@@ -128,5 +128,14 @@ namespace TEAMModelOS.Controllers.Syllabus
             IQueryable<dynamic>  data =   azureCosmosDBRepository.FindByDict(request.@params.collectionName,request.@params.queryDict);
             return builder.Data(data).build();
         }
+
+
+        [HttpPost("FindCollectionCount")]
+        public BaseJosnRPCResponse FindCollectionCount(JosnRPCRequest<CommonQuery> request)
+        {
+            JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
+            IQueryable<dynamic> data = azureCosmosDBRepository.FindCountByDict(request.@params.collectionName, request.@params.queryDict);
+            return builder.Data(data).build();
+        }
     }
 }

+ 3 - 3
TEAMModelOS/Controllers/Syllabus/KnowledgeController.cs

@@ -350,9 +350,9 @@ namespace TEAMModelOS.Controllers.Syllabus
             JsonRPCResponseBuilder builder = JsonRPCResponseBuilder.custom();
             if (request.@params != null)
             {
-               // IQueryable<dynamic> data = _cosmos.FindByDict("SchoolBlock", request.@params);
-              List<Knowledge> knowledges =await  _cosmos.FindSQL<Knowledge>("SELECT value(c) FROM c join a in c.points where Contains(a, 'bbd677cafb8dd84b22213af7675a4f27dbdd9541') = true ",true);
-                return builder.Data(knowledges).build();
+                IQueryable<dynamic> data = _cosmos.FindCountByDict("Knowledge", request.@params);
+                //List<Knowledge> knowledges = await _cosmos.FindSQL<Knowledge>("SELECT value(c) FROM c join a in c.points where Contains(a, 'bbd677cafb8dd84b22213af7675a4f27dbdd9541') = true ", true);
+                return builder.Data(data).build();
 
             }
             else return builder.Error(false, ResponseCode.PARAMS_ERROR, "参数为空").build();