using Microsoft.Azure.Cosmos; using Microsoft.Extensions.Configuration; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.Json; using System.Threading.Tasks; using TEAMModelOS.SDK.DI; using TEAMModelOS.SDK.Extension; using TEAMModelOS.SDK.Models; using TEAMModelOS.SDK.Services; namespace TEAMModelOS.SDK { public static class HomeworkService { public static async Task> AnswerRecordAll(CosmosClient client, CoreAPIHttpService _coreAPIHttpService,DingDing _dingDing, Homework homework, string userid, string tbname,List listIds , string _targetType= "student") { List typeUsers = new List(); (List tmdinfos, List classInfo) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, listIds, homework.school, null, -1, homework.startTime); var addStudentsCls = tmdinfos.FindAll(x => x.type == 2); var addTmdidsCls = tmdinfos.FindAll(x => x.type == 1); if ($"{_targetType}".Equals("research", StringComparison.OrdinalIgnoreCase) || $"{_targetType}".Equals("yxtrain", StringComparison.OrdinalIgnoreCase)) { if (tmdinfos.IsNotEmpty()) { tmdinfos.ForEach(x => { var clases = classInfo.Where(y => y.members.Select(z => z.id).Contains(x.id)).ToList(); typeUsers.Add(new TypeUser() { irs=x.irs, no=x.no, nickname =x.nickname, userid = x.id, userType = "tmdid", username = x.name, classes = clases.Select(x => new TypeUserClass { id = x.id, name = x.name }).ToList() }); }); } } else { if (addTmdidsCls.IsNotEmpty()) { addTmdidsCls.ForEach(x => { var clases = classInfo.Where(y => y.members.Select(z => z.id).Contains(x.id)).ToList(); typeUsers.Add(new TypeUser() { irs = x.irs, no = x.no, nickname = x.nickname, userid = x.id, userType = "tmdid", username = x.name, classes = clases.Select(x => new TypeUserClass { id = x.id, name = x.name }).ToList() }); }); } if (addStudentsCls.IsNotEmpty()) { addStudentsCls.ForEach(x => { var clases = classInfo.Where(y => y.members.Select(z => z.id).Contains(x.id)).ToList(); typeUsers.Add(new TypeUser() { irs = x.irs, no = x.no, nickname = x.nickname, userid = x.id, userType = "student", username = x.name, userSchool = x.code.Replace("Base-", ""), classes = clases.Select(x => new TypeUserClass { id = x.id, name = x.name }).ToList() }); }); } } List rscs = new List(); string debateCode = ""; string debateTbname = ""; if (homework.scope.Equals("school")) { debateTbname = "School"; debateCode = $"Debate-{homework.school}"; } else { debateTbname = "Teacher"; debateCode = $"Debate-{homework.creatorId}"; } List debates = new List(); string sql = $"select value(c) from c where c.comid='{homework.id}'"; await foreach (var item in client.GetContainer(Constant.TEAMModelOS, debateTbname).GetItemQueryIteratorSql(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey(debateCode) })) { debates.Add(item); } List users = new List(); foreach (var typeuser in typeUsers) { var us = users.Find(x => x.userid.Equals(typeuser.userid)); if (us != null) { continue; } HomeworkUser user = new HomeworkUser() { irs = typeuser.irs, no = typeuser.no, nickname = typeuser.nickname, userid = typeuser.userid, userSchool = typeuser.userSchool, userType = typeuser.userType, username = typeuser.username, classes = typeuser.classes }; string partition = $"HomeworkRecord-{typeuser.userid}"; if (typeuser.userType.Equals("student")) { partition = $"HomeworkRecord-{typeuser.userSchool}-{typeuser.userid}"; } else { partition = $"HomeworkRecord-{typeuser.userid}"; } HomeworkRecord record = null; try { record = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync($"{homework.id}", new PartitionKey(partition)); double star = -1; List stars = record.comments.FindAll(s => s.identity.Equals("student")).Select(x => x.star).ToList(); if (stars.IsNotEmpty()) { star = stars.Sum() / stars.Count; } user.submit = true; user.studentStar = star; user.score = record.score; user.submitTime = record.time; user.content = record.content; // user.username = typeuser.username; var teacherComment = record.comments.Where(x => x.identity.Equals("teacher") && record.teacher.Equals(userid)).FirstOrDefault(); if (teacherComment != null) { user.teacherStar = teacherComment.star; List replies = new List(); teacherComment.replyIds.ForEach(x => { var reply = debates.SelectMany(x => x.replies).ToList().Find(r => r.id.Equals(x)); if (reply != null) { replies.Add(reply.comment); } }); user.replies = replies; } } catch (CosmosException ex) { user.submit = false; record = null; } users.Add(user); } return users; } public static async Task saveMoreAsync(CosmosClient client, DingDing _dingDing, Homework work, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration, AzureRedisFactory _azureRedis) { try { work.ttl = -1; work.code = "Homework-" + work.school; long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(); work.createTime = now; string blobcntr = null; blobcntr = work.school; work.size = await _azureStorage.GetBlobContainerClient(work.school).GetBlobsSize($"homework/{work.id}"); await BlobService.RefreshBlobRoot(new BlobRefreshMessage { progress = "insert", root = $"homework", name = $"{blobcntr}" }, _serviceBus, _configuration, _azureRedis); work.recordUrl = $"/homework/{work.id}/record.json"; var cods = new { records = new List(), userids = new List(), question = new List() }; await _azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(cods.ToJsonString(), "homework", $"{work.id}/record.json"); work.id = Guid.NewGuid().ToString(); if (string.IsNullOrEmpty(work.id)) { if (work.publish == 1) { work.progress = "pending"; } else { if (work.startTime > now) { work.progress = "pending"; } else { work.progress = "going"; } } await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(work, new PartitionKey($"{work.code}")); } else { await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(work, new PartitionKey($"{work.code}")); } return work.id; } catch (Exception e) { await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-HomeworkService-saveMore\n{e.Message}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組); return ""; } } } public class TypeUser { public string irs { get; set; } public string no { get; set; } public string nickname { get; set; } public string userid { get; set; } public string userType { get; set; } public string userSchool { get; set; } public string username { get; set; } public List classes { get; set; } = new List(); } public class TypeUserClass { public string id { get; set; } public string name { get; set; } } public class HomeworkUser { public string irs { get; set; } public string no { get; set; } public string nickname { get; set; } public string userid { get; set; } public string userType { get; set; } public string userSchool { get; set; } public long submitTime { get; set; } public double score { get; set; } = -1; public double studentStar { get; set; } = -1; public double teacherStar { get; set; } = -1; public bool submit { get; set; } public string username { get; set; } public List classes { get; set; } = new List(); public List replies { get; set; } = new List(); public List content { get; set; } = new List(); } }