Browse Source

个人题目数量统计

CrazyIter_Bin 3 years ago
parent
commit
9e1e6c088f

+ 4 - 4
TEAMModelOS.SDK/Models/Service/ItemService.cs

@@ -56,7 +56,7 @@ namespace TEAMModelOS.Services.Common
                 //检查两个对象是否是同一条记录
                 //检查两个对象是否是同一条记录
                 if (newItem != null && odlItem == null)
                 if (newItem != null && odlItem == null)
                 {
                 {
-                    string newKey = $"{newItem.creator}";
+                    string newKey = $"{newItem.code}".Replace("Item-", "");
                     List<string> grade = new List<string> { newKey };
                     List<string> grade = new List<string> { newKey };
                     UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
                     UpdateItemCond(cond, true, newKey, grade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
                 }
                 }
@@ -66,18 +66,18 @@ namespace TEAMModelOS.Services.Common
                     if (newItem.id == odlItem.id && newItem.code == odlItem.code)
                     if (newItem.id == odlItem.id && newItem.code == odlItem.code)
                     {
                     {
                         //先增加
                         //先增加
-                        string newKey = $"{newItem.creator}";
+                        string newKey = $"{newItem.code}".Replace("Item-","");
                         List<string> newGrade = new List<string> { newKey };
                         List<string> newGrade = new List<string> { newKey };
                         UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
                         UpdateItemCond(cond, true, newKey, newGrade, newItem.type, newItem.level, newItem.field.HasValue ? newItem.field.Value : 0, newItem);
                         //后变更删除
                         //后变更删除
-                        string oldKey = $"{odlItem.creator}";
+                        string oldKey = $"{odlItem.code}".Replace("Item-", "");
                         List<string> oldGrade = new List<string> { oldKey };
                         List<string> oldGrade = new List<string> { oldKey };
                         UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, newItem);
                         UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, newItem);
                     }
                     }
                 }
                 }
                 else if (newItem == null && odlItem != null)
                 else if (newItem == null && odlItem != null)
                 {
                 {
-                    string oldKey = $"{odlItem.creator}";
+                    string oldKey = $"{odlItem.code}".Replace("Item-", "");
                     List<string> oldGrade = new List<string> { oldKey };
                     List<string> oldGrade = new List<string> { oldKey };
                     UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, null);
                     UpdateItemCond(cond, false, oldKey, oldGrade, odlItem.type, odlItem.level, odlItem.field.HasValue ? odlItem.field.Value : 0, null);
                 }
                 }

+ 61 - 24
TEAMModelOS/Controllers/Item/ItemController.cs

@@ -57,13 +57,15 @@ namespace TEAMModelOS.Controllers
             {
             {
                 var client = _azureCosmos.GetCosmosClient();
                 var client = _azureCosmos.GetCosmosClient();
                 request.TryGetProperty("periodId", out JsonElement periodId);
                 request.TryGetProperty("periodId", out JsonElement periodId);
-                if (!request.TryGetProperty("schoolCode", out JsonElement schoolCode)) return BadRequest();
-                if (periodId.ValueKind.Equals(JsonValueKind.String) && !string.IsNullOrEmpty(periodId.GetString()))
+                request.TryGetProperty("schoolCode", out JsonElement schoolCode);
+                request.TryGetProperty("tmdid", out JsonElement tmdid);
+                if (!string.IsNullOrEmpty($"{periodId}") && !string.IsNullOrEmpty($"{schoolCode}"))
                 {
                 {
                     ItemCond itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>($"{periodId}", new PartitionKey($"ItemCond-{schoolCode}"));
                     ItemCond itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>($"{periodId}", new PartitionKey($"ItemCond-{schoolCode}"));
                     return Ok(new { itemConds = new List<ItemCond>() { itemCond } });
                     return Ok(new { itemConds = new List<ItemCond>() { itemCond } });
                 }
                 }
-                else {
+                else if (!string.IsNullOrEmpty($"{schoolCode}"))
+                {
                     List<ItemCond> items = new List<ItemCond>();
                     List<ItemCond> items = new List<ItemCond>();
                     var queryslt = $"SELECT  value(c) FROM c ";
                     var queryslt = $"SELECT  value(c) FROM c ";
                     await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemCond>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ItemCond-{schoolCode}") }))
                     await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ItemCond>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"ItemCond-{schoolCode}") }))
@@ -72,9 +74,17 @@ namespace TEAMModelOS.Controllers
                     }
                     }
                     return Ok(new { itemConds = items });
                     return Ok(new { itemConds = items });
                 }
                 }
+                else if (!string.IsNullOrEmpty($"{tmdid}"))
+                {
+                    ItemCond itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>($"{tmdid}", new PartitionKey($"ItemCond"));
+                    return Ok(new { itemConds = new List<ItemCond>() { itemCond } });
+                }
+                else {
+                    return Ok(new { itemConds = new List<ItemCond>() { } });
+                }
             } catch (Exception ex) {
             } catch (Exception ex) {
                 await _dingDing.SendBotMsg($"OS,{_option.Location},item/cond-count()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
                 await _dingDing.SendBotMsg($"OS,{_option.Location},item/cond-count()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
-                return BadRequest();
+                return Ok(new { itemConds = new List<ItemCond>() {  } });
             }
             }
         }
         }
         [ProducesDefaultResponseType]
         [ProducesDefaultResponseType]
@@ -104,11 +114,9 @@ namespace TEAMModelOS.Controllers
                 if (option.ToString().Equals("insert"))
                 if (option.ToString().Equals("insert"))
                 {
                 {
                     itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                     itemInfo.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
-                    //DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                     if (!itemInfo.code.Contains("Item"))
                     if (!itemInfo.code.Contains("Item"))
                     {
                     {
                         itemInfo.code = "Item-" + itemInfo.code;
                         itemInfo.code = "Item-" + itemInfo.code;
-                        // itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
                     }
                     }
                     var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
                     var response = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
                     if (response.Status == 200)
                     if (response.Status == 200)
@@ -119,19 +127,29 @@ namespace TEAMModelOS.Controllers
                     {
                     {
                         if (itemInfo.scope.Equals("private"))
                         if (itemInfo.scope.Equals("private"))
                         {
                         {
-                            itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
+                            itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
+                            string condId = itemInfo.code.Replace("Item-","");
+                            ItemCond itemCond = null;
+                            try
+                            {
+                                itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>(condId, new PartitionKey("ItemCond"));
+                            }
+                            catch (Exception)
+                            {
+                                itemCond = new ItemCond() { id = condId, code = $"ItemCond", pk = "ItemCond", ttl = -1, count = 0 };
+                            };
+                            ItemService.CountItemCond(itemInfo, null, itemCond);
+                            await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
                         }
                         }
                         else
                         else
                         {
                         {
                             // itemInfo.periodId
                             // itemInfo.periodId
-                            itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
+                            itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync(itemInfo, new PartitionKey($"{itemInfo.code}"));
                             ItemCond itemCond = null;
                             ItemCond itemCond = null;
                             try {
                             try {
-
                                 itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>(itemInfo.periodId, new PartitionKey($"ItemCond-{itemInfo.code.Replace("Item-", "")}"));
                                 itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>(itemInfo.periodId, new PartitionKey($"ItemCond-{itemInfo.code.Replace("Item-", "")}"));
                             } catch (Exception  ) {
                             } catch (Exception  ) {
                                 itemCond = new ItemCond() { id = itemInfo.periodId, code = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
                                 itemCond = new ItemCond() { id = itemInfo.periodId, code = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
-
                             };
                             };
                             ItemService.CountItemCond(itemInfo, null, itemCond);
                             ItemService.CountItemCond(itemInfo, null, itemCond);
                             await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
                             await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
@@ -147,21 +165,23 @@ namespace TEAMModelOS.Controllers
                         {
                         {
                             itemInfo.code = "Item-" + itemInfo.code;
                             itemInfo.code = "Item-" + itemInfo.code;
                         }
                         }
+                        string condId = itemInfo.code.Replace("Item-", "");
                         itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
                         itemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(itemInfo, itemInfo.id, new PartitionKey($"{itemInfo.code}"));
+                        ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>(itemInfo.id, new PartitionKey($"{itemInfo.code}"));
                         //更新题目数量
                         //更新题目数量
-                        //ItemCond itemCond = null;
-                        //try
-                        //{
-
-                        //    itemCond = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemCond>(itemInfo.periodId, new PartitionKey($"ItemCond-{itemInfo.code.Replace("Item-", "")}"));
-                        //}
-                        //catch (Exception)
-                        //{
-                        //    itemCond = new ItemCond() { id = itemInfo.periodId, code = $"ItemCond-{itemInfo.code.Replace("Item-", "")}", pk = "ItemCond", ttl = -1, count = 0, grades = new List<GradeCount>(), subjects = new List<SubjectCount>() };
-
-                        //};
-                        //ItemService.CountItemCond(itemInfo, olditemInfo, itemCond);
-                        //await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
+                        ItemCond itemCond = null;
+                        try
+                        {
+
+                            itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>(condId, new PartitionKey("ItemCond"));
+                        }
+                        catch (Exception)
+                        {
+                            itemCond = new ItemCond() { id = condId, code = $"ItemCond", pk = "ItemCond", ttl = -1, count = 0};
+
+                        };
+                        ItemService.CountItemCond(itemInfo, olditemInfo, itemCond);
+                        await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
                     }
                     }
                     else
                     else
                     {
                     {
@@ -310,7 +330,6 @@ namespace TEAMModelOS.Controllers
                 if (scope.ToString().Equals("school"))
                 if (scope.ToString().Equals("school"))
                 {
                 {
                     ItemInfo itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(id.ToString(), new PartitionKey($"{code}"));
                     ItemInfo itemInfo = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<ItemInfo>(id.ToString(), new PartitionKey($"{code}"));
-                    var response = await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
                     //更新题目数量
                     //更新题目数量
                     ItemCond itemCond = null;
                     ItemCond itemCond = null;
                     try
                     try
@@ -325,10 +344,28 @@ namespace TEAMModelOS.Controllers
                     };
                     };
                     ItemService.CountItemCond(null, itemInfo, itemCond);
                     ItemService.CountItemCond(null, itemInfo, itemCond);
                     await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
                     await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
+                    var response = await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
                     return Ok(new { code = response.Status });
                     return Ok(new { code = response.Status });
                 }
                 }
                 else
                 else
                 {
                 {
+
+                    ItemInfo olditemInfo = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemInfo>($"{id}", new PartitionKey($"{code}"));
+                    //更新题目数量
+                    ItemCond itemCond = null;
+                    string condId = code.ToString().Replace("Item-", "");
+                    try
+                    {
+
+                        itemCond = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<ItemCond>(condId, new PartitionKey("ItemCond"));
+                    }
+                    catch (Exception)
+                    {
+                        itemCond = new ItemCond() { id = condId, code = $"ItemCond", pk = "ItemCond", ttl = -1, count = 0 };
+
+                    };
+                    ItemService.CountItemCond(null, olditemInfo, itemCond);
+                    await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<ItemCond>(itemCond, new PartitionKey(itemCond.code));
                     var response = await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
                     var response = await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
                     return Ok(new { code = response.Status });
                     return Ok(new { code = response.Status });
                 }
                 }

+ 1 - 1
TEAMModelOS/Controllers/Research/AbilityController.cs

@@ -213,7 +213,7 @@ namespace TEAMModelOS.Controllers.Research
 
 
             request.pk = "Ability";
             request.pk = "Ability";
             request.ttl = -1;
             request.ttl = -1;
-            if (string.IsNullOrEmpty(request.code))
+            if (!string.IsNullOrEmpty(request.code))
             {
             {
                 request.code = request.code.StartsWith("Ability-") ? request.code : $"Ability-{request.code}";
                 request.code = request.code.StartsWith("Ability-") ? request.code : $"Ability-{request.code}";
             }
             }

+ 3 - 2
TEAMModelOS/Controllers/Research/ResearchStatisticsController.cs

@@ -640,9 +640,10 @@ namespace TEAMModelOS.Controllers.Research
         [AuthToken(Roles = "teacher,student,admin,area")]
         [AuthToken(Roles = "teacher,student,admin,area")]
         public async Task<IActionResult> GetSchoolStatistics(JsonElement request)
         public async Task<IActionResult> GetSchoolStatistics(JsonElement request)
         {
         {
+            var (userid, _, _, __school) = HttpContext.GetAuthTokenInfo();
             try
             try
             {
             {
-                var (userid, _, _, __school) = HttpContext.GetAuthTokenInfo();
+              
                 if (!request.TryGetProperty("school", out JsonElement school)) return BadRequest();
                 if (!request.TryGetProperty("school", out JsonElement school)) return BadRequest();
                 var client = _azureCosmos.GetCosmosClient();
                 var client = _azureCosmos.GetCosmosClient();
                 AreaSetting setting = null;
                 AreaSetting setting = null;
@@ -1149,7 +1150,7 @@ namespace TEAMModelOS.Controllers.Research
             }
             }
             catch (Exception ex)
             catch (Exception ex)
             {
             {
-                await _dingDing.SendBotMsg($"OS,{_option.Location},AbilityController/GetSubsStatistics()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
+                await _dingDing.SendBotMsg($"OS,{_option.Location},AbilityController/ResearchStatisticsController()\n{ex.Message}{ex.StackTrace}{request.ToJsonString()}{userid}{__school}", GroupNames.醍摩豆服務運維群組);
                 return BadRequest();
                 return BadRequest();
             }
             }
         }
         }

+ 31 - 0
TEAMModelOS/Controllers/XTest/FixDataController.cs

@@ -235,9 +235,40 @@ namespace TEAMModelOS.Controllers
                     await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(cond, new PartitionKey(cond.code));
                     await client.GetContainer(Constant.TEAMModelOS, "School").UpsertItemAsync<ItemCond>(cond, new PartitionKey(cond.code));
                 });
                 });
             }
             }
+
             return Ok(new {  });
             return Ok(new {  });
         }
         }
+        [ProducesDefaultResponseType]
+        //[AuthToken(Roles = "teacher")]
+        [HttpPost("fix-item-teacher")]
+        public async Task<IActionResult> FixItemTeacher(JsonElement data)
+        {
 
 
+            var client = _azureCosmos.GetCosmosClient();
+            List<Teacher > teachers = new List<Teacher>();
+            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Teacher>(queryText: "SELECT  value(c) FROM c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
+            {
+                teachers.Add(item);
+            }
+            Random random = new Random();
+            foreach (var teacher in teachers)
+            {
+                List<ItemInfo> items = new List<ItemInfo>();
+                var queryslt = $"SELECT  value(c) FROM c where  c.pid = null ";
+                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<ItemInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{teacher.id}") }))
+                {
+                    items.Add(item);
+                }
+                List<ItemInfo> unMatch = new List<ItemInfo>();
+                ItemCond cond = new ItemCond() { id = teacher.id, code = $"ItemCond", pk = "ItemCond", ttl = -1, count = items.Count };
+                items.ForEach(z =>
+                {
+                    ItemService.CountItemCond(z, null, cond);
+                });
+                await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<ItemCond>(cond, new PartitionKey(cond.code));
+            }
+            return Ok(new { });
+        }
 
 
         /// <summary>
         /// <summary>
         /// 修复名单的scope,school,creatorid
         /// 修复名单的scope,school,creatorid