Jelajahi Sumber

评语罐头接口。

CrazyIter_Bin 3 tahun lalu
induk
melakukan
f075d46a51

+ 11 - 7
TEAMModelOS.SDK/Models/Cosmos/Teacher/Comment.cs

@@ -11,19 +11,23 @@ namespace TEAMModelOS.SDK.Models
     /// <summary>
     /// <summary>
     /// 教师评语罐头
     /// 教师评语罐头
     /// </summary>
     /// </summary>
-    
+
     public class Comment : CosmosEntity
     public class Comment : CosmosEntity
     {
     {
-       
         public Comment(){
         public Comment(){
-            comment = new List<string>();
-        }        
-
+            pk = "Comment";
+        }
         /// <summary>
         /// <summary>
         /// 评语
         /// 评语
         /// </summary>
         /// </summary>
-        public List<string> comment { get; set; }
-     
+        public List<Box> boxes { get; set; } = new List<Box>();
         
         
     }
     }
+    public class Box {
+        /// <summary>
+        /// type="correct"  阅卷批注  "homework" 作业批注   "common": 常用的
+        /// </summary>
+        public string type { get; set; }
+        public List<string> comment { get; set; } = new List<string>();
+    }
 }
 }

+ 29 - 26
TEAMModelOS/Controllers/Research/ResearchStatisticsController.cs

@@ -45,14 +45,20 @@ namespace TEAMModelOS.Controllers.Research
         [AuthToken(Roles = "teacher,student,admin,area")]
         [AuthToken(Roles = "teacher,student,admin,area")]
         public async Task<IActionResult> GetAreasStatistics(JsonElement request) {
         public async Task<IActionResult> GetAreasStatistics(JsonElement request) {
             var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
             var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
-            if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
+            // if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
+            if (!request.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
             var client = _azureCosmos.GetCosmosClient();
             var client = _azureCosmos.GetCosmosClient();
+            //string sql = $"select value(c) from c where c.standard='{standard}'";
+            //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
+            //      requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
+            //{
+            //    area = item;
+            //}
             Area area = null;
             Area area = null;
-            string sql = $"select value(c) from c where c.standard='{standard}'";
-            await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
-                  requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
-            {
-                area = item;
+            try {
+                area = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Area>($"{areaId}", new PartitionKey("Base-Area"));
+            } catch (CosmosException  ex ) {
+                return Ok(new { error = 404,msg="区域不存在!" });
             }
             }
             AreaSetting setting = null;
             AreaSetting setting = null;
             if (area != null)
             if (area != null)
@@ -63,7 +69,7 @@ namespace TEAMModelOS.Controllers.Research
                 }
                 }
                 catch (CosmosException)
                 catch (CosmosException)
                 {
                 {
-                   
+                    return Ok(new { error = 404 ,msg="设置不存在!"});
                 }
                 }
             }
             }
             if (setting == null)
             if (setting == null)
@@ -89,7 +95,7 @@ namespace TEAMModelOS.Controllers.Research
             List<Ability> abilities = new List<Ability>();
             List<Ability> abilities = new List<Ability>();
             List<DimensionCount> areaDimensionCount = new List<DimensionCount>();
             List<DimensionCount> areaDimensionCount = new List<DimensionCount>();
             await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal")
             await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal")
-                           .GetItemQueryIterator<Ability>(queryText: $"select c.id, c.name,c.no,c.dimension, c.env  from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{standard}") }))
+                           .GetItemQueryIterator<Ability>(queryText: $"select c.id, c.name,c.no,c.dimension, c.env  from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{area.standard}") }))
             {
             {
                
                
                 dimensions.Add(item.dimension);
                 dimensions.Add(item.dimension);
@@ -636,29 +642,26 @@ namespace TEAMModelOS.Controllers.Research
         {
         {
             try
             try
             {
             {
-                var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
-                if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
+                var (userid, _, _, __school) = HttpContext.GetAuthTokenInfo();
+                if (!request.TryGetProperty("school", out JsonElement school)) return BadRequest();
                 var client = _azureCosmos.GetCosmosClient();
                 var client = _azureCosmos.GetCosmosClient();
-                Area area = null;
-                string sql = $"select value(c) from c where c.standard='{standard}'";
-                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
-                      requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
-                {
-                    area = item;
-                }
                 AreaSetting setting = null;
                 AreaSetting setting = null;
+                School schoolBase = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>($"{school}", new PartitionKey("Base"));
+                Area area = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<Area>(schoolBase.areaId, new PartitionKey("Base-Area"));
                 if (area != null)
                 if (area != null)
                 {
                 {
                     try
                     try
                     {
                     {
                         //优先找校级
                         //优先找校级
-                        setting = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<AreaSetting>(school, new PartitionKey("AreaSetting"));
+                        setting = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<AreaSetting>($"{school}", new PartitionKey("AreaSetting"));
                     }
                     }
                     catch (CosmosException)
                     catch (CosmosException)
                     {
                     {
                         try
                         try
                         {
                         {
-                            setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
+                            if (!string.IsNullOrEmpty(schoolBase.areaId)) {
+                                setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(schoolBase.areaId, new PartitionKey("AreaSetting"));
+                            }
                         }
                         }
                         catch (CosmosException)
                         catch (CosmosException)
                         {
                         {
@@ -678,13 +681,13 @@ namespace TEAMModelOS.Controllers.Research
                         lessonMinutes = 45,
                         lessonMinutes = 45,
                     };
                     };
                 }
                 }
-                if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
+               
                 HashSet<string> dimensions = new HashSet<string>();
                 HashSet<string> dimensions = new HashSet<string>();
              
              
                
                
                 List<Ability> abilities = new List<Ability>();
                 List<Ability> abilities = new List<Ability>();
                 await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal")
                 await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Normal")
-                           .GetItemQueryIterator<Ability>(queryText: $"select c.id, c.name,c.no,c.dimension, c.env  from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{standard}") }))
+                           .GetItemQueryIterator<Ability>(queryText: $"select c.id, c.name,c.no,c.dimension, c.env  from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{area.standard}") }))
                 {
                 {
                     dimensions.Add(item.dimension);
                     dimensions.Add(item.dimension);
                     abilities.Add(item);
                     abilities.Add(item);
@@ -692,7 +695,7 @@ namespace TEAMModelOS.Controllers.Research
                 List<string> abilitieIds = abilities.Select(x => x.id).ToList();
                 List<string> abilitieIds = abilities.Select(x => x.id).ToList();
                 List<ClassVideo> classVideos = new List<ClassVideo>();
                 List<ClassVideo> classVideos = new List<ClassVideo>();
                 List<AbilitySub> abilitySubs = new List<AbilitySub>();
                 List<AbilitySub> abilitySubs = new List<AbilitySub>();
-                string code = $"ClassVideo-{_school}";
+                string code = $"ClassVideo-{school}";
                 await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
                 await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
                         .GetItemQueryIterator<ClassVideo>(queryText: $"select value(c)  from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
                         .GetItemQueryIterator<ClassVideo>(queryText: $"select value(c)  from c  ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
                 {
                 {
@@ -702,7 +705,7 @@ namespace TEAMModelOS.Controllers.Research
                 List<SchoolGroup> groups = new List<SchoolGroup>();
                 List<SchoolGroup> groups = new List<SchoolGroup>();
                 List<TchList> tchLists = new List<TchList>();
                 List<TchList> tchLists = new List<TchList>();
                 await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<TchList>(queryText: $"SELECT value(c) FROM c  ",
                 await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<TchList>(queryText: $"SELECT value(c) FROM c  ",
-                    requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TchList-{_school}") }))
+                    requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TchList-{school}") }))
                 {
                 {
                     groups.Add(new SchoolGroup { groupId=item.id,groupName=item.name});
                     groups.Add(new SchoolGroup { groupId=item.id,groupName=item.name});
                     tchLists.Add(item);
                     tchLists.Add(item);
@@ -753,7 +756,7 @@ namespace TEAMModelOS.Controllers.Research
                 {
                 {
                     ids.Add("default");
                     ids.Add("default");
                 }
                 }
-                (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{_school}");
+                (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
                 if (groups.IsEmpty()) {
                 if (groups.IsEmpty()) {
                     groups.Add(new SchoolGroup { groupId = "default", groupName = "default" });
                     groups.Add(new SchoolGroup { groupId = "default", groupName = "default" });
                 }
                 }
@@ -841,7 +844,7 @@ namespace TEAMModelOS.Controllers.Research
                 foreach (var tmd in tmdInfos)
                 foreach (var tmd in tmdInfos)
                 {
                 {
                     await foreach (var sub in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
                     await foreach (var sub in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
-                         .GetItemQueryIterator<AbilitySub>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilitySub-{_school}-{tmd.id}") }))
+                         .GetItemQueryIterator<AbilitySub>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilitySub-{school}-{tmd.id}") }))
                     {
                     {
                         abilitySubs.Add(sub);
                         abilitySubs.Add(sub);
                     }
                     }
@@ -980,7 +983,7 @@ namespace TEAMModelOS.Controllers.Research
                         });
                         });
                     }
                     }
                     List<SubStatistic> subs = new();
                     List<SubStatistic> subs = new();
-                    abilitySubs.Where(x => x.code.Equals($"AbilitySub-{_school}-{tmd.id}")).ToList().ForEach(item =>
+                    abilitySubs.Where(x => x.code.Equals($"AbilitySub-{school}-{tmd.id}")).ToList().ForEach(item =>
                     {
                     {
                         subCount += 1;
                         subCount += 1;
                         thcSubCount += 1;
                         thcSubCount += 1;

+ 71 - 64
TEAMModelOS/Controllers/Teacher/CommentController.cs

@@ -47,27 +47,46 @@ namespace TEAMModelOS.Controllers
         /// <returns></returns>
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [ProducesDefaultResponseType]
         [HttpPost("add-comment")]
         [HttpPost("add-comment")]
+        [AuthToken(Roles = "teacher,admin")]
         public async Task<IActionResult> AddComment(JsonElement request)
         public async Task<IActionResult> AddComment(JsonElement request)
         {
         {
-            //var id = "Comment-" + request.TEAMModelId.Replace("#", "");
-            //ResponseBuilder builder = ResponseBuilder.custom();
-            //List<Comment> comments = await _azureCosmos.FindByDict<Comment>(new Dictionary<string, object> { { "code", request.TEAMModelId }, { "id", id } });
-            //Comment comment = new Comment();
-            //if (comments.IsEmpty())
-            //{
-            //    comment.id = id;
-            //    comment.code = request.TEAMModelId;
-            //    comment.comment.Add(request.comment);
-            //    comments.Add(comment);
-            //}
-            //else {
-            //    comment = comments[0];
-            //    comment.comment.Add(request.comment);
-            //}
-            //builder.Data(await _azureCosmos.SaveOrUpdate(comment));
-            //return builder.build();
-            //return Ok(await _azureCosmos.SaveOrUpdate(comment));
-            return Ok();
+            var (userid, name, _, school) = HttpContext.GetAuthTokenInfo();
+            if (!request.TryGetProperty("type", out JsonElement _type)) return BadRequest();
+            if (!request.TryGetProperty("comment", out JsonElement _comment)) return BadRequest();
+            Comment comment = null;
+            var client = _azureCosmos.GetCosmosClient();
+            try
+            {
+                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Comment>(userid, new PartitionKey("Comment"));
+                if (comment.boxes.IsNotEmpty())
+                {
+                    var bx = comment.boxes.Find(x => x.type.Equals($"{_type}"));
+                    if (bx != null)
+                    {
+                        bx.comment.Add($"{_comment}");
+                    }
+                    else
+                    {
+                        comment.boxes.Add(new Box { type = $"{_type}", comment = new List<string> { $"{_comment}" } });
+                    }
+                }
+                else {
+                    comment.boxes = new List<Box>() { new Box { type = $"{_type}", comment = new List<string> { $"{_comment}" } } };
+                }
+                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<Comment>(comment,userid, new PartitionKey("Comment"));
+            }
+            catch (CosmosException ex) {
+                comment = new Comment
+                {
+                    id = userid,
+                    code = "Comment",
+                    pk = "Comment",
+                    ttl = -1,
+                    boxes = new List<Box>() { new Box { type=$"{_type}", comment= new List<string> { $"{_comment}"} } },
+                };
+                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Comment>(comment, new PartitionKey("Comment"));
+            }
+            return Ok(new { comment });
         }
         }
 
 
         /// <summary>
         /// <summary>
@@ -77,32 +96,28 @@ namespace TEAMModelOS.Controllers
         /// <returns></returns>
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [ProducesDefaultResponseType]
         [HttpPost("find-comment")]
         [HttpPost("find-comment")]
-        public async Task<IActionResult> findComment(JsonElement requert)
+        [AuthToken(Roles = "teacher,admin")]
+        public async Task<IActionResult> FindComment()
         {
         {
-            //var client = _azureCosmos.GetCosmosClient();
-            //if (!requert.TryGetProperty("school_code", out JsonElement school_code)) return BadRequest();
-            ////var (id, name, picture, _) = HttpContext.GetAuthTokenInfo();
-            //if (!requert.TryGetProperty("id_token", out JsonElement id_token)) return BadRequest();
-            //var jwt = new JwtSecurityToken(id_token.GetString());
-            //if (!jwt.Payload.Iss.Equals("account.teammodel", StringComparison.Ordinal)) return BadRequest();
-            //var id = jwt.Payload.Sub;
-
-            //List<object> comments = new List<object>();
-            //var query = $"select c.id,c.comment from c";
-            //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Comment-{id}") }))
-            //{
-            //    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())
-            //        {
-            //            comments.Add(obj.ToObject<object>());
-            //        }
-            //    }
-            //}
-
-            return Ok();
+            var (userid, name, _, school) = HttpContext.GetAuthTokenInfo();
+            Comment comment = null;
+            var client = _azureCosmos.GetCosmosClient();
+            try
+            {
+                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<Comment>(userid, new PartitionKey("Comment"));
+            }
+            catch (CosmosException ex) {
+                comment = new Comment
+                {
+                    id = userid,
+                    code = "Comment",
+                    pk = "Comment",
+                    ttl = -1,
+                    boxes = new List<Box>(),
+                };
+                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Comment>(comment, new PartitionKey("Comment"));
+            }
+            return Ok(new { comment });
         }
         }
         /// <summary>
         /// <summary>
         /// 更新保存教师评语罐头,如果评语列表为空则删除
         /// 更新保存教师评语罐头,如果评语列表为空则删除
@@ -111,27 +126,19 @@ namespace TEAMModelOS.Controllers
         /// <returns></returns>
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [ProducesDefaultResponseType]
         [HttpPost("upsert-comment")]
         [HttpPost("upsert-comment")]
-        public async Task<IActionResult> UpsertComment(Comment request)
+        [AuthToken(Roles = "teacher,admin")]
+        public async Task<IActionResult> UpsertComment(Comment comment)
         {
         {
-            // request.TryAdd("PartitionKey", request.lang);
-            //ResponseBuilder builder = ResponseBuilder.custom();
-            //Comment comment = null;
-            //if (request.comment.Count > 0)
-            //{
-            //    if (string.IsNullOrEmpty(request.id)) {
-            //        request.id  = "Comment-" + request.code.Replace("#", "");
-            //    }
-            //    comment = await _azureCosmos.SaveOrUpdate<Comment>(request);
-            //}
-            //else {
-            //    if (!string.IsNullOrEmpty(request.id))
-            //    {
-            //        IdPk idPk = await _azureCosmos.DeleteAsync<Comment>(request.id, request.code);
-            //    }
-
-            //}
-            ////return builder.Data(comment).build();
-            return Ok();
+            var (userid, name, _, school) = HttpContext.GetAuthTokenInfo();
+            try {
+                var client = _azureCosmos.GetCosmosClient();
+                comment.pk = "Comment";
+                comment.id = userid;
+                comment.code = "Comment";
+                comment.ttl = -1;
+                comment = await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<Comment>(comment, new PartitionKey(comment.code));
+            } catch (CosmosException ex ) { }
+            return Ok(new { comment });
         }
         }
 
 
         //批注
         //批注