|
@@ -0,0 +1,86 @@
|
|
|
+using Azure.Cosmos;
|
|
|
+using StackExchange.Redis;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Text.Json;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using TEAMModelOS.SDK.DI;
|
|
|
+
|
|
|
+namespace TEAMModelOS.SDK.Models.Service
|
|
|
+{
|
|
|
+ public static class ErrorItemsService
|
|
|
+ {
|
|
|
+ public static async Task cntStuErrorItemsAsync(AzureRedisFactory _azureRedis, CosmosClient _azureCosmosClient)
|
|
|
+ {
|
|
|
+ var redisClinet8 = _azureRedis.GetRedisClient(8);
|
|
|
+ //取得ErrorItems錯題
|
|
|
+ Dictionary<string, Dictionary<string, List<ErrorItemsStuRow>>> ErrorItemsDic = new Dictionary<string, Dictionary<string, List<ErrorItemsStuRow>>>();
|
|
|
+ string qry = "SELECT SUM(ARRAY_LENGTH(c.its)) AS number, c.stuId, c.school, c.subjectId FROM c WHERE CONTAINS(c.code, 'ErrorItems') GROUP BY c.stuId, c.school, c.subjectId";
|
|
|
+ await foreach (var item in _azureCosmosClient.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: qry, requestOptions: null))
|
|
|
+ {
|
|
|
+ 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())
|
|
|
+ {
|
|
|
+ //{"number":2,"stuId":"202101001","school":"hbcn","subjectId":"8b94c6b6-2572-41e5-89b9-a82fcf13891e"}
|
|
|
+ string stuId = Convert.ToString(obj.GetProperty("stuId"));
|
|
|
+ string schoolId = Convert.ToString(obj.GetProperty("school"));
|
|
|
+ schoolId = (!string.IsNullOrWhiteSpace(schoolId)) ? schoolId : "noschoolid";
|
|
|
+ string subjectId = Convert.ToString(obj.GetProperty("subjectId"));
|
|
|
+ int number = obj.GetProperty("number").GetInt32();
|
|
|
+ if(ErrorItemsDic.ContainsKey(schoolId))
|
|
|
+ {
|
|
|
+ if (ErrorItemsDic[schoolId].ContainsKey(stuId))
|
|
|
+ {
|
|
|
+ List<ErrorItemsStuRow> ErrorItemsStuRowList = ErrorItemsDic[schoolId][stuId];
|
|
|
+ ErrorItemsStuRow ErrorItemsStuRow = ErrorItemsStuRowList.Where(s => s.subjectId.Equals(subjectId)).FirstOrDefault();
|
|
|
+ if (ErrorItemsStuRow == null)
|
|
|
+ {
|
|
|
+ ErrorItemsDic[schoolId][stuId].Add(new ErrorItemsStuRow() { subjectId = subjectId, number = number });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ List<ErrorItemsStuRow> ErrorItemsStuRowList = new List<ErrorItemsStuRow>();
|
|
|
+ ErrorItemsStuRow ErrorItemsStuRow = new ErrorItemsStuRow() { subjectId = subjectId, number = number };
|
|
|
+ ErrorItemsStuRowList.Add(ErrorItemsStuRow);
|
|
|
+ ErrorItemsDic[schoolId].Add(stuId, ErrorItemsStuRowList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ List<ErrorItemsStuRow> ErrorItemsStuRowList = new List<ErrorItemsStuRow>();
|
|
|
+ ErrorItemsStuRow ErrorItemsStuRow = new ErrorItemsStuRow() { subjectId = subjectId, number = number };
|
|
|
+ ErrorItemsStuRowList.Add(ErrorItemsStuRow);
|
|
|
+ Dictionary<string, List<ErrorItemsStuRow>> ErrorItemsSchRow = new Dictionary<string, List<ErrorItemsStuRow>>{{ stuId, ErrorItemsStuRowList }};
|
|
|
+ ErrorItemsDic.Add(schoolId, ErrorItemsSchRow);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //寫入Redis
|
|
|
+ foreach(var SchKey in ErrorItemsDic)
|
|
|
+ {
|
|
|
+ string schoolId = SchKey.ToString();
|
|
|
+ string hkey = $"ErrorItems:{schoolId}";
|
|
|
+ List<HashEntry> hvalList = new List<HashEntry>();
|
|
|
+ Dictionary<string, List<ErrorItemsStuRow>> itemSch = SchKey.Value;
|
|
|
+ foreach(var itemStu in itemSch)
|
|
|
+ {
|
|
|
+ string stuId = itemStu.Key;
|
|
|
+ string stuVal = JsonSerializer.Serialize(SchKey.Value);
|
|
|
+ hvalList.Add(new HashEntry($"{stuId}", stuVal));
|
|
|
+ }
|
|
|
+ await redisClinet8.HashSetAsync(hkey, hvalList.ToArray());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ public class ErrorItemsStuRow
|
|
|
+ {
|
|
|
+ public string subjectId { get; set; }
|
|
|
+ public int number { get; set; }
|
|
|
+ }
|
|
|
+}
|