Browse Source

知识点数量缓存

CrazyIter_Bin 4 years ago
parent
commit
5ee357ab67

+ 2 - 2
TEAMModelFunction/TriggerVote.cs

@@ -176,7 +176,7 @@ namespace TEAMModelFunction
                                 progress = "finish",
                                 owner = vote.owner
                             };
-                            await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
+                            await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
                         }
                         else if (vote.scope == "private")
                         {
@@ -196,7 +196,7 @@ namespace TEAMModelFunction
                                 owner = vote.owner
                                 // tmdids = vote.tmdids
                             };
-                            await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
+                            await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
                         }
                         break;
                 }

+ 73 - 1
TEAMModelOS/Controllers/Knowledge/KnowledgesController.cs

@@ -30,12 +30,14 @@ namespace TEAMModelOS.Controllers
         private readonly AzureCosmosFactory _azureCosmos;
         private readonly DingDing _dingDing;
         private readonly Option _option;
-        public KnowledgesController(AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option)
+        private readonly AzureRedisFactory _azureRedis;
+        public KnowledgesController(AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis)
         {
             _azureCosmos = azureCosmos;
             _snowflakeId = snowflakeId;
             _dingDing = dingDing;
             _option = option?.Value;
+            _azureRedis = azureRedis;
         }
         /**
          * 
@@ -94,8 +96,78 @@ namespace TEAMModelOS.Controllers
                 knowledge.id = Guid.NewGuid().ToString();
                 knowledge = await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(knowledge, new PartitionKey($"{knowledge.code}"));
             }
+            var count= new { pcount = knowledge.points!=null ? knowledge.points.Count:0,bcount= knowledge.blocks!=null? knowledge.blocks.Count:0 };
+            //处理知识点,知识块计数问题
+            await _azureRedis.GetRedisClient(8).HashSetAsync($"Knowledge:Count:{knowledge.owner}-{knowledge.subjectId}", knowledge.periodId, count.ToJsonString());
             return Ok(knowledge);
         }
+        /// <summary>
+        /**
+        
+        {
+            !"hbcn-ac73f07d-2cc8-4174-85ae-b39cc5b6beef":"ca484aa8-e7b5-4a7c-8ef3-bd9e7b7d4fp2",
+        }
+       
+        单个Item查询一个学校某个科目知识点,知识块数量,如果需要确定某一个学段,则需要加学段。
+         **/
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("find-count")]
+        public async Task<IActionResult> FindCount(Dictionary<string,string> request) 
+        {
+            List<dynamic> datas = new List<dynamic>();
+            foreach (var kp in request) {
+                var countPoint = 0;
+                var countBlock = 0;
+                if (!string.IsNullOrWhiteSpace(kp.Value))
+                {
+                    var value = _azureRedis.GetRedisClient(8).HashGet($"Knowledge:Count:{kp.Key}", kp.Value);
+                    if (value != default && !value.IsNullOrEmpty)
+                    {
+                        
+                        JsonElement record = value.ToString().ToObject<JsonElement>();
+                        if (record.TryGetProperty("pcount", out JsonElement pcout))
+                        {
+                            int.TryParse($"{pcout}", out countPoint);
+                        }
+                        if (record.TryGetProperty("bcount", out JsonElement bcout))
+                        {
+                            int.TryParse($"{bcout}", out countBlock);
+                        }
+                    }
+                }
+                else {
+                    var values = _azureRedis.GetRedisClient(8).HashGetAll($"Knowledge:Count:{kp.Key}");
+                    if (values != null) {
+                        foreach (var value in values) 
+                        {
+                            JsonElement record = value.ToString().ToObject<JsonElement>();
+                            if (record.TryGetProperty("pcount", out JsonElement pcout))
+                            {
+                                if (int.TryParse($"{pcout}", out int countP)) 
+                                {
+                                    countPoint = countPoint + countP;
+                                }
+                            }
+                            if (record.TryGetProperty("bcount", out JsonElement bcout))
+                            {
+                                if(int.TryParse($"{bcout}", out int countB)) 
+                                {
+                                    countBlock = countBlock + countB;
+                                }
+                            }
+                        }
+                    }
+                }
+                datas.Add( new { key = kp.Key, countPoint, countBlock });
+            }
+            return Ok(new { datas });
+        }
+
+
+          
         /**
          * {
             ?"periodId": "ca484aa8-e7b5-4a7c-8ef3-bd9e7b7d4fp2",