CrazyIter_Bin 3 rokov pred
rodič
commit
10acb6c3b0

+ 81 - 29
TEAMModelOS.SDK/Models/Service/TriggerStuActivity.cs

@@ -142,14 +142,15 @@ namespace TEAMModelFunction
             return classInfos;
         }
        
-        public static async Task<(List<TmdInfo> tmdids, List<StuInfo> students,List<ClassListInfo>classInfo)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
+        public static async Task<(List<TmdInfo> tmdinfos, List<StuInfo> students,List<ClassListInfo>classInfo)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
         {
             try {
                 //classes.Add("d25f3dbf-c7d5-da58-6155-cc584439aac8");
-                List<TmdInfo> tmdids = new List<TmdInfo>();
+                List<TmdInfo> tmdinfos = new List<TmdInfo>();
                 List<Students> studentss = new List<Students>();
+                List<string> tmdids = new List<string>();
                 List<StuInfo> stuInfos = new List<StuInfo>();
-                if (!classes.IsNotEmpty()) { return (tmdids, new List<StuInfo>(),null); }
+                if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(),null); }
                 List<string> sqlList = new List<string>();
                 classes.ForEach(x => { sqlList.Add($" '{x}' "); });
                 string sql = string.Join(" , ", sqlList);
@@ -181,16 +182,9 @@ namespace TEAMModelFunction
                     }
                     if (x.tmids.IsNotEmpty())
                     {
-                        List<TmdInfo> infos = new List<TmdInfo>();
-                        List<string> inids = new List<string>();
-                        x.tmids.ForEach(x => { inids.Add($"'{x}'"); });
-                        var insql = string.Join(",", inids);
-                        var queryslt = $"SELECT  value(c) FROM c where c.id in ({insql})";
-                        await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") })) {
-                            infos.Add(item);
-                        }
-                        tmdids.AddRange(infos);
+                        tmdids.AddRange(x.tmids);
                     }
+                    classes.Remove(x.id);
                 }
                 foreach (var x in tchLists) 
                 {
@@ -199,19 +193,23 @@ namespace TEAMModelFunction
                     }
                     if (x.tmids.IsNotEmpty())
                     {
-                        List<TmdInfo> infos = new List<TmdInfo>();
-                        List<string> inids = new List<string>();
-                        x.tmids.ForEach(x => { inids.Add($"'{x}'"); });
-                        var insql = string.Join(",", inids);
-                        var queryslt = $"SELECT  value(c) FROM c where c.id in ({insql})";
-                        await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
-                        {
-                            infos.Add(item);
-                        }
-                        tmdids.AddRange(infos);
+                        tmdids.AddRange(x.tmids);
                     }
+                    classes.Remove(x.id);
                 }
 
+                if (tmdids.IsNotEmpty()) {
+                    List<TmdInfo> infos = new List<TmdInfo>();
+                    List<string> inids = new List<string>();
+                    tmdids.ForEach(x => { inids.Add($"'{x}'"); });
+                    var insql = string.Join(",", inids);
+                    var queryslt = $"SELECT  value(c) FROM c where c.id in ({insql})";
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
+                    {
+                        infos.Add(item);
+                    }
+                    tmdinfos.AddRange(infos);
+                }
                 if (studentss.IsNotEmpty()) {
                     List<string> inidstus = new List<string>();
                     studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
@@ -228,23 +226,77 @@ namespace TEAMModelFunction
                         stuInfos.Add(new StuInfo { id = x.id, code = x.code, schoolId = x.schoolId, classId = x.classId, name = x.name, picture = x.picture, year = x.year });
                     }
                 });
-                return (tmdids, stuInfos, null) ;
+                List<ClassListInfo> classInfo = new List<ClassListInfo>();
+                schList.ForEach(x => {
+                    ClassListInfo classListInfo = new ClassListInfo {id=x.id,name=x.name };
+                    if (x.students.IsNotEmpty()) {
+                        x.students.ForEach(y => {
+                            var stuinfo= stuInfos.Where(z => z.id == y.id).FirstOrDefault();
+                            if (stuinfo != null) {
+                                classListInfo.stuInfos.Add(stuinfo);
+                            }
+                        });
+                    }
+                    if (x.tmids.IsNotEmpty())
+                    {
+                        x.tmids.ForEach(y => {
+                            var tmdinfo = tmdinfos.Where(z => z.tmdid == y).FirstOrDefault();
+                            if (tmdinfo != null)
+                            {
+                                classListInfo.tmdInfos.Add(tmdinfo);
+                            }
+                        });
+                    }
+                    classInfo.Add(classListInfo);
+                });
+                tchLists.ForEach(x => {
+                    ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
+                    if (x.students.IsNotEmpty())
+                    {
+                        x.students.ForEach(y => {
+                            var stuinfo = stuInfos.Where(z => z.id == y.id).FirstOrDefault();
+                            if (stuinfo != null)
+                            {
+                                classListInfo.stuInfos.Add(stuinfo);
+                            }
+                        });
+                    }
+                    if (x.tmids.IsNotEmpty())
+                    {
+                        x.tmids.ForEach(y => {
+                            var tmdinfo = tmdinfos.Where(z => z.tmdid == y).FirstOrDefault();
+                            if (tmdinfo != null)
+                            {
+                                classListInfo.tmdInfos.Add(tmdinfo);
+                            }
+                        });
+                    }
+                    classInfo.Add(classListInfo);
+                });
+                //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
+                List<ClassInfo> classInfos= await  GetClassInfo(client, _dingDing, classes, school);
+                classInfos.ForEach(x =>
+                {
+                    ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
+                    var list= students.Where(y => y.classId == x.id).ToList();
+                    list.ForEach(z => { classListInfo.stuInfos.Add(new StuInfo { id = z.id, code = z.code, schoolId = z.schoolId, classId = z.classId, name = z.name, picture = z.picture, year = z.year }); });
+                    classInfo.Add(classListInfo);
+                });
+                return (tmdinfos, stuInfos, classInfo) ;
             }
             catch (Exception ex)
             {
                 await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
             }
                 return (null, null,null);
-            }
+        }
     }
     public class ClassListInfo
     {
         public string id { get; set; }
         public string name { get; set; }
-        public string from { get; set; }
-        public string periodId { get; set; }
-        public int year { get; set; }
-        public List<StuInfo> stuInfos { get; set; }
-        public List<TmdInfo> tmdInfos { get; set; }
+        // public string from { get; set; }
+        public List<StuInfo> stuInfos { get; set; } = new List<StuInfo>();
+        public List<TmdInfo> tmdInfos { get; set; } = new List<TmdInfo>();
     }
 }

+ 91 - 122
TEAMModelOS/Controllers/Client/HiScanController.cs

@@ -81,7 +81,7 @@ namespace TEAMModelOS.Controllers.Core
             if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
             var client = _azureCosmos.GetCosmosClient();
             var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync($"{id}", new PartitionKey("Base"));
-            if (response.Status == 200) 
+            if (response.Status == 200)
             {
                 var json = await JsonDocument.ParseAsync(response.ContentStream);
                 string name = $"{json.RootElement.GetProperty("name")}";
@@ -109,15 +109,18 @@ namespace TEAMModelOS.Controllers.Core
                             }
                             if (count > 0)
                             {
-                                schools.Add(new   { name = $"{schoolName}", picture = $"{schoolPicture}", id = $"{schoolId}" });
+                                schools.Add(new { name = $"{schoolName}", picture = $"{schoolPicture}", id = $"{schoolId}" });
                             }
                         }
                     }
                 }
                 var (tblob_uri, tblob_sas) = _azureStorage.GetBlobContainerSAS($"{id}", BlobContainerSasPermissions.Write | BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Delete);
-                return Ok(new { schools, teacher=new { name, picture, id, bloburl = tblob_uri, blobsas = tblob_sas , status = response.Status } });
+                return Ok(new { schools, teacher = new { name, picture, id, bloburl = tblob_uri, blobsas = tblob_sas } });
             }
-            return Ok(new { status = response.Status });
+            else {
+                return BadRequest();
+            }
+           
         }
         ///<summary>
         ///查询教师的阅卷任务列表
@@ -128,110 +131,85 @@ namespace TEAMModelOS.Controllers.Core
         /// <param name="request"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
-        [HttpPost("verify-qrcode")]
+        [HttpPost("get-examinfo")]
         // [AuthToken(Roles = "teacher,admin")]
         public async Task<IActionResult> VerifyQrcode(JsonElement request)
         {
             try
             {
-                 
-                List<ScanSchool> schools = new List<ScanSchool>();
+                List<SheetConfig> configs = new List<SheetConfig>();
+                HashSet<string> classesSet = new HashSet<string>();
+                ScanSchool  scanSchool = new   ScanSchool ();
                 if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
-                //TODO 取得Teacher 個人相關數據(課程清單、虛擬教室清單、歷史紀錄清單等),學校數據另外API處理,多校切換時不同
+                if (!request.TryGetProperty("schoolId", out JsonElement _schoolId)) return BadRequest();
                 var client = _azureCosmos.GetCosmosClient();
-                var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync($"{id}", new PartitionKey("Base"));
-                int size = 0;
-                //老師個人資料(含初始化)
-                if (response.Status == 200)
+                School school= await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>($"{_schoolId}", new PartitionKey("Base"));
+                var schoolId = $"{_schoolId}";
+                var schoolName = school.name;
+                var schoolPicture = school.picture;
+                //检查学校购买的模组是否包含阅卷模组
+                int count = 0;
+                string sql = $" SELECT value(count(product))  FROM c join product in c.service.product      where c.id ='{schoolId}'  and  c.pk='Product'  and product.prodCode='YMPCVCIM' ";
+                await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<int>(sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Product") }))
                 {
-                    HashSet<string> classesSet = new HashSet<string>();
-                    List<SheetConfig> configs = new List<SheetConfig>();
-                    var json = await JsonDocument.ParseAsync(response.ContentStream);
-                    string name = $"{json.RootElement.GetProperty("name")}";
-                    string picture = $"{json.RootElement.GetProperty("picture")}";
-                    if (json.RootElement.TryGetProperty("schools", out JsonElement value))
-                    {
-
-                        if (json.RootElement.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
-                        {
-                            size = _size.GetInt32();
-                        }
-
-                        foreach (var obj in value.EnumerateArray())
-                        {
-                            string statusNow = obj.GetProperty("status").ToString();
-                            //正式加入才会有
-                            if (statusNow == "join")
-                            {
-                                //dynamic schoolExtobj = new ExpandoObject();
-                                var schoolJson = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{obj.GetProperty("schoolId")}", new PartitionKey("Base"));
-                                var school = await JsonDocument.ParseAsync(schoolJson.ContentStream);
-                                var schoolId = obj.GetProperty("schoolId");
-                                var schoolName = obj.GetProperty("name");
-                                var schoolPicture = school.RootElement.GetProperty("picture");
-                                //检查学校购买的模组是否包含阅卷模组
-                                int count = 0;
-                                string sql = $" SELECT value(count(product))  FROM c join product in c.service.product      where c.id ='{schoolId}'  and  c.pk='Product'  and product.prodCode='YMPCVCIM' ";
-                                await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<int>(sql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Product") }))
-                                {
-                                    count = item;
-                                }
-                                if (count > 0)
-                                {
-                                    //生成token
-                                    var stoken = JwtAuthExtension.CreateAuthToken(_option.HostName, $"{id}", name?.ToString(), picture?.ToString(), _option.JwtSecretKey, roles: new[] { "teacher" }, schoolID: $"{schoolId}");
-                                    //获取学校线下阅卷评测
-                                    var sexams = await GetExam($"{schoolId}", client, configs, classesSet);
-                                    var (sblob_uri, sblob_sas) = _azureStorage.GetBlobContainerSAS($"{schoolId}", BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Write);
-                                    schools.Add(new ScanSchool { name = $"{schoolName}", picture = $"{schoolPicture}", id = $"{schoolId}", exams = sexams.schools.Select(x => x.Value).ToList(), bloburl = sblob_uri, blobsas = sblob_sas });
-                                }
-                            }
+                    count = item;
+                }
+                if (count > 0)
+                {
+                    //获取学校线下阅卷评测
+                    var sexams = await GetExam($"{schoolId}", client, configs, classesSet);
+                    var (sblob_uri, sblob_sas) = _azureStorage.GetBlobContainerSAS($"{schoolId}", BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Write);
+                    (List<TmdInfo> tmdids, List<StuInfo> students, List<ClassListInfo> classInfo)= await TriggerStuActivity.GetStuList(client, _dingDing, classesSet.ToList(), $"{schoolId}");
+                    List<SheetConfig> configsN = new List<SheetConfig>();
+                    foreach (var config in configs) {
+                        if (config.scope == "school") {
+                            try {
+                                SheetConfig con = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<SheetConfig>(config.id, new PartitionKey(config.code));
+                                configsN.Add(con);
+                            } catch (CosmosException ex) { }
                         }
                     }
-                    var ttoken = JwtAuthExtension.CreateAuthToken(_option.HostName, $"{id}", name?.ToString(), picture?.ToString(), _option.JwtSecretKey, roles: new[] { "teacher" });
-
-                    //获取个人线下阅卷评测
-                    var exams = await GetExam($"{id}", client, configs, classesSet);
-                    List<dynamic> teacher = new List<dynamic>();
-                    var list = exams.schools.GroupBy(x => x.Key).Select(y => new { key = y.Key, val = y.ToList() }).ToList();
-                    foreach (var s in list)
-                    {
-                        var schoolJson = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{s.key}", new PartitionKey("Base"));
-                        if (schoolJson.Status == 200)
+                    sexams.schools.SelectMany(y => y.classes).ToList().ForEach(z => {
+                        var a = classInfo.Where(m => m.id == z.id).FirstOrDefault();
+                        if (a != null) {
+                            z.name = a.name;
+                            z.stuInfos = a.stuInfos;
+                            z.tmdInfos = a.tmdInfos;
+                        }
+                    });
+                    sexams.schools.SelectMany(y => y.papers).ToList().ForEach(z => {
+                        if (z.sheet != null)
                         {
-                            var school = await JsonDocument.ParseAsync(schoolJson.ContentStream);
-                            var schoolName = school.RootElement.GetProperty("name");
-                            var schoolPicture = school.RootElement.GetProperty("picture");
-                            var (sblob_uri, sblob_sas) = _azureStorage.GetBlobContainerSAS($"{s.key}", BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Write);
-                            teacher.Add(new { name = $"{schoolName}", picture = $"{schoolPicture}", id = $"{s.key}", exams = s.val.Select(x => x.Value).ToList(), bloburl = sblob_uri, blobsas = sblob_sas });
+                            var a = configsN.Where(m => m.id == z.sheet.id).FirstOrDefault();
+                            if (a != null) {
+                                z.sheet = a;
+                            }
                         }
-                    }
-                    var (tblob_uri, tblob_sas) = _azureStorage.GetBlobContainerSAS($"{id}", BlobContainerSasPermissions.Write | BlobContainerSasPermissions.Read | BlobContainerSasPermissions.List | BlobContainerSasPermissions.Delete);
-                    teacher.Add(new { name, picture, id, exams = exams.personal, bloburl = tblob_uri, blobsas = tblob_sas });
-                    //換取AuthToken,提供給前端
-                    var lists=  await TriggerStuActivity.GetStuList(client, _dingDing, classesSet.ToList(), "hbcn");
-
-                    var data = new { schools, teacher, lists.students,lists.tmdids};
-                    return Ok(data);
-
+                    });
+                   scanSchool = new ScanSchool { name = $"{schoolName}", picture = $"{schoolPicture}", id = $"{schoolId}", exams = sexams.schools.ToList(), bloburl = sblob_uri, blobsas = sblob_sas };
                 }
+                
+                var pexams = await GetExam($"{id}", client, configs, classesSet);
+                return Ok(new { school=scanSchool });
+ 
             }
             catch (Exception ex)
             {
                 await _dingDing.SendBotMsg($"IES5,{_option.Location},hiscan/verify-qrcode()\n{ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
                 return BadRequest();
             }
-            return Ok();
         }
-        private async Task<(List<KeyValuePair<string, ExamRcd>> schools, List<ExamRcd> personal)> GetExam(string code, CosmosClient client, List<SheetConfig> sheet, HashSet<string> classesSet)
+        private async Task<(List< ExamRcd> schools, List<ExamRcd> personal)> GetExam(string code, CosmosClient client, List<SheetConfig> sheet, HashSet<string> classesSet)
         {
-            List<KeyValuePair<string, ExamRcd>> schools = new List<KeyValuePair<string, ExamRcd>>();
+            List<ExamRcd> schools = new List<ExamRcd>();
             List<ExamRcd> personal = new List<ExamRcd>();
+            int i= 1;
             //await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Correct>(queryText: "SELECT  * FROM c where c.source='2' and c.progress='going' order by c.createTime  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Correct-{code}") }))
             await foreach (var exam in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<ExamInfo>(queryText: "SELECT  * FROM c  where   c.progress='going'  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Exam-{code}") }))
             {
-                List<ExamClass> classes = new List<ExamClass>();
-                exam.classes.ForEach(x => { classes.Add(new ExamClass { classId = x }); classesSet.Add(x); } );
+                Console.WriteLine(i++);
+                List<ClassListInfo> classes = new List<ClassListInfo>();
+                exam.classes.ForEach(x => { classes.Add(new ClassListInfo { id = x }); classesSet.Add(x); } );
                 List<PaperRcd> dys = new List<PaperRcd>();
                 foreach (var pap in exam.papers)
                 {
@@ -254,34 +232,36 @@ namespace TEAMModelOS.Controllers.Core
                     {
                         dys.Add(new PaperRcd { name = pap.name, answers = pap.answers, point = pap.point, sheet = null });
                     }
-                    if (exam.scope == "school")
+                }
+                if (exam.scope == "school")
+                {
+                    schools.Add(new ExamRcd
                     {
-                        schools.Add(new KeyValuePair<string, ExamRcd>(exam.school, new ExamRcd
-                        {
-                            name = exam.name,
-                            startTime = exam.startTime,
-                            endTime = exam.endTime,
-                            period = exam.period,
-                            grades = exam.grades,
-                            subjects = exam.subjects,
-                            papers = dys,
-                            classes = classes
-                        }));
-                    }
-                    else
+                        id = exam.id,
+                        name = exam.name,
+                        startTime = exam.startTime,
+                        endTime = exam.endTime,
+                        period = exam.period,
+                        grades = exam.grades,
+                        subjects = exam.subjects,
+                        papers = dys,
+                        classes = classes
+                    });
+                }
+                else
+                {
+                    personal.Add(new ExamRcd
                     {
-                        personal.Add(new ExamRcd
-                        {
-                            name = exam.name,
-                            startTime = exam.startTime,
-                            endTime = exam.endTime,
-                            period = exam.period,
-                            grades = exam.grades,
-                            subjects = exam.subjects,
-                            papers = dys,
-                            classes = classes
-                        });
-                    }
+                        id = exam.id,
+                        name = exam.name,
+                        startTime = exam.startTime,
+                        endTime = exam.endTime,
+                        period = exam.period,
+                        grades = exam.grades,
+                        subjects = exam.subjects,
+                        papers = dys,
+                        classes = classes
+                    });
                 }
             }
             return (schools, personal);
@@ -294,9 +274,9 @@ namespace TEAMModelOS.Controllers.Core
         public SheetConfig sheet { get; set; }
     }
 
-    // pap.name, pap.answers, pap.point, sheet =pap.sheet
     public record ExamRcd
     {
+        public string id { get; set; }
         public string name { get; set; }
         public long startTime { get; set; }
         public long endTime { get; set; }
@@ -304,7 +284,7 @@ namespace TEAMModelOS.Controllers.Core
         public List<Grade> grades { get; set; }
         public List<ExamSubject> subjects { get; set; }
         public List<PaperRcd> papers { get; set; }
-        public List<ExamClass> classes { get; set; }
+        public List<ClassListInfo> classes { get; set; }
     }
 
     public class ScanSchool
@@ -314,17 +294,6 @@ namespace TEAMModelOS.Controllers.Core
         public string name { get; set; }
         public string picture { get; set; }
         public string id { get; set; }
-       // public string status { get; set; }
-        //public string token { get; set; }
         public List<ExamRcd> exams { get; set; }
-       // public List<(Correct,ExamInfo, (List<string> tmdids, List<Students> stulist))> exams{ get; set; }
-    }
-
-   
-    public class ExamClass {
-        public string classId { get; set; }
-        public string className { get; set; }
-        public List<TmdInfo> tmdids { get; set; } = new List<TmdInfo>();
-        public List<StuInfo> stulist { get; set; } = new List<StuInfo>();
     }
 }