Parcourir la source

册别 课纲API处理

zhouj1203@hotmail.com il y a 4 ans
Parent
commit
13c0b72db2

+ 4 - 4
TEAMModelOS.SDK/DI/AzureCosmos/AzureCosmosExtensions.cs

@@ -449,8 +449,8 @@ namespace TEAMModelOS.SDK.DI
             StringBuilder sql;
             sql = SQLHelper.GetSQLSelect(propertys);
             string pk = typeof(T).Name;
-            Dictionary<string, object> dict = new Dictionary<string, object>();
-            var emobj = jsonElement.EnumerateObject();
+            //Dictionary<string, object> dict = new Dictionary<string, object>();
+           /* var emobj = jsonElement.EnumerateObject();
             while (emobj.MoveNext())
             {
                 dict[emobj.Current.Name] = emobj.Current.Value;
@@ -459,8 +459,8 @@ namespace TEAMModelOS.SDK.DI
             if (dict.TryGetValue("code", out object _))
             {
                 dict.Remove("code");
-            }
-            AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql, pk);
+            }*/
+            AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(jsonElement, sql, pk);
             QueryRequestOptions queryRequestOptions = GetDefaultQueryRequestOptions(itemsPerPage: GetEffectivePageSize(-1, null));
             return await ResultsFromQueryAndOptions<T>(azureCosmosFactory, cosmosDbQuery, queryRequestOptions);
         }

+ 77 - 11
TEAMModelOS/Controllers/Syllabus/VolumeController.cs

@@ -130,7 +130,9 @@ namespace TEAMModelOS.Controllers
             }
             if (string.IsNullOrEmpty(request.id))
             {
-                request.code = typeof(Volume).Name + "-" + request.code;
+                request.ttl = -1;
+                request.pk = typeof(Volume).Name;
+                //request.code = typeof(Volume).Name + "-" + request.code;
                 if (request.type == 0)
                 {
                     List<Volume> volumesFind = await _azureCosmos.FindByDict<Volume>(new Dictionary<string, object>() {
@@ -147,6 +149,9 @@ namespace TEAMModelOS.Controllers
                     string key = request.code + _snowflakeId.NextId();
                     request.id = key;
                     request.volumeCode = key;
+                    string code = request.code;
+                    request.code = typeof(Volume).Name + "-" + request.code;
+                    request = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").CreateItemAsync(request, new PartitionKey($"Volume-{code}"));
                 }
                 else if (request.type == 1)
                 {
@@ -164,6 +169,9 @@ namespace TEAMModelOS.Controllers
                     string key = "dynamic" + request.TEAMModelId.Replace("#", "") + _snowflakeId.NextId();
                     request.id = key;
                     request.volumeCode = key;
+                    string code = request.code;
+                    request.code = typeof(Volume).Name + "-" + request.code;
+                    request = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(request, new PartitionKey($"{code}"));
                 }
                 else if (request.type == 2)
                 {
@@ -173,6 +181,8 @@ namespace TEAMModelOS.Controllers
                     request.id = key;
                     request.volumeCode = key;
                 }
+               
+
             }
             else
             {
@@ -191,6 +201,7 @@ namespace TEAMModelOS.Controllers
                             return Ok(new { V = "已存在!", error = ResponseCode.DATA_EXIST });
                         }
                     }
+                    request = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync(request, new PartitionKey($"{request.code}"));
                 }
                 else if (request.type == 1)
                 {
@@ -211,16 +222,18 @@ namespace TEAMModelOS.Controllers
                     else if (request.type == 2)
                     {
                     }
+                    request = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(request, new PartitionKey($"{request.code}"));
 
-                }                
+                }
+                //request = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").UpsertItemAsync(request, new PartitionKey($"{request.code}"));
             }
-            List<Volume> volumes = new List<Volume>
+           /* List<Volume> volumes = new List<Volume>
                 {
                     request
                 };
 
-            List<Volume> volume = await _azureCosmos.SaveOrUpdateAll<Volume>(volumes);
-            return Ok(new { volume });           
+            List<Volume> volume = await _azureCosmos.SaveOrUpdateAll<Volume>(volumes);*/
+            return Ok(new { request });           
         }
 
 
@@ -269,13 +282,66 @@ namespace TEAMModelOS.Controllers
         [HttpPost("find")]
         public async Task<IActionResult> Find(JsonElement request)
         {
-            ResponseBuilder builder = ResponseBuilder.custom();
-            List<Volume> volumes ;
+            /*ResponseBuilder builder = ResponseBuilder.custom();
+            List<Volume> volumes ;*/
+            List<Syllabus> syllabuses = new List<Syllabus>();
+            List<Volume> volumes = new List<Volume>();
             if (request.TryGetProperty("code", out JsonElement code))
             {
-
-                List<Syllabus> syllabuses = await _azureCosmos.FindByDict<Syllabus>(new Dictionary<string, object> { { "code", "Syllabus-"+code.ToString() } });
-                volumes = await _azureCosmos.FindByDict<Volume>(request);
+               
+                request.TryGetProperty("type", out JsonElement type);
+                if (type.GetInt32() == 1)
+                {
+                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id,c.children from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{code}") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                        {
+                            foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                            {
+                                syllabuses.Add(obj.ToObject<Syllabus>());
+                            }
+                        }
+                    }
+                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id, c.volumeName, c.volumeCode,c.children from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Volume-{code}") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                        {
+                            foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                            {
+                                volumes.Add(obj.ToObject<Volume>());
+                            }
+                        }
+                    }
+                }
+                else {
+                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: $"select c.id,c.children from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{code}") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                        {
+                            foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                            {
+                                syllabuses.Add(obj.ToObject<Syllabus>());
+                            }
+                        }
+                    }
+                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: $"select c.id, c.volumeName, c.volumeCode from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Volume-{code}") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                        {  
+                            foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                            {
+                                volumes.Add(obj.ToObject<Volume>());
+                            }
+                        }
+                    }
+                }
+                //List<Syllabus> syllabuses = await _azureCosmos.FindByDict<Syllabus>(new Dictionary<string, object> { { "code", "Syllabus-"+code.ToString() } });
+                //课纲               
+                //volumes = await _azureCosmos.FindByDict<Volume>(request);
                 //处理单个册别下面的资源和题目
                 volumes.ForEach(v => {
                     
@@ -283,7 +349,7 @@ namespace TEAMModelOS.Controllers
                     HashSet<string> iCount = new HashSet<string>();
                     
                     syllabuses.ForEach(s => {
-                        if (v.volumeCode.Equals(s.code)) {
+                        if (v.volumeCode.Equals(s.id)) {
                             s.children.ForEach(c => {
                                 if (c.resources.IsNotEmpty())
                                 {

+ 1 - 1
TEAMModelOS/Models/CommonInfo/Syllabus.cs

@@ -10,7 +10,7 @@ namespace TEAMModelOS.Models.CommonInfo
     /// <summary>
     /// Teaching materials 教学材料 ---课纲
     /// </summary>
-    [CosmosDB(Database = "TEAMModelOS", Name = "Common")]
+    [CosmosDB(Database = "TEAMModelOS", Name = "School")]
     public class Syllabus : ID
     {
         [PartitionKey]