CrazyIter_Bin 3 лет назад
Родитель
Сommit
efd151e625

+ 29 - 0
TEAMModelFunction/MonitorServicesBus.cs

@@ -1029,5 +1029,34 @@ namespace TEAMModelFunction
                 await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-更新课堂记录出错\n{ex.Message}\n{ex.StackTrace}\n{data}\n{code}\n", GroupNames.成都开发測試群組);
             }
         }
+
+        /// <summary>
+        /// 完善课程变更,StuListChange,  originCode是学校编码 则表示名单是学校自定义名单,如果是tmdid则表示醍摩豆的私有名单,scope=school,private。
+        /// </summary>
+        /// <data msg>
+        /// CourseChange
+        ///// </data>
+        /// <param name="msg"></param>
+        /// <returns></returns>
+        [FunctionName("KnowledgeChange")]
+        public async Task KnowledgeChangeFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "knowledge-change", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
+            var jsonMsg = JsonDocument.Parse(msg).RootElement;
+            List<OldNew> old_new = null;
+            string school = null ;
+            if (jsonMsg.TryGetProperty("school", out JsonElement _school)) {
+                school = _school.GetString();
+            }
+            if (jsonMsg.TryGetProperty("old_new", out JsonElement _old_new))
+            {
+                old_new = _old_new.ToObject<List<OldNew>>();
+            }
+
+            if (old_new.IsNotEmpty() && !string.IsNullOrWhiteSpace(school)) {
+                string sql = $"select value(c) from c  join b in c. where ";
+                foreach (var on in old_new) { 
+                 
+                }
+            }
+        }
     }
 }

+ 55 - 0
TEAMModelOS.SDK/Helper/Common/ValidatorHelper/ValidatorHelper.cs

@@ -0,0 +1,55 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Text;
+
+namespace TEAMModelOS.SDK
+{
+    public static class ValidatorHelper
+    {
+        public static ValidResult IsValid(this Object obj) {
+            ValidResult result = new ValidResult();
+            try {
+                var context = new ValidationContext(obj, null, null);
+                var results = new List<ValidationResult>();
+                if (Validator.TryValidateObject(obj, context, results, true))
+                {
+                    result.isVaild = true;
+                }
+                else
+                {
+                    List<(string msg,  string name)> error = new List<(string msg, string name)>();
+                    foreach (var validationResult in results)
+                    {
+                        if (validationResult.MemberNames != null && validationResult.MemberNames.Count()>0) {
+                            validationResult.MemberNames.ToList().ForEach(x => {
+                                error.Add((validationResult.ErrorMessage, x));
+                            });
+                        }
+                    }
+                    result.errors = new Dictionary<string, IEnumerable<string>>();
+                    error.GroupBy(x => x.name).ToList().ForEach(z => {
+                        result.errors.Add(z.Key, z.ToList().Select(x => x.msg));
+                    });
+                }
+            } catch (Exception ex) {
+                result.isVaild = false;
+                result.errors = new Dictionary<string, IEnumerable<string>>();
+                result.errors.Add("InternalError", new List<string> { ex.Message });
+            }
+            return result;
+        }
+
+        
+    }
+    public class ValidResult
+    {
+        public string traceId { get; set; } = Guid.NewGuid().ToString();
+        public string type { get; set; } = "https://tools.ietf.org/html/rfc7231#section-6.5.1";
+        public string title { get; set; } = "One or more validation errors occurred.";
+        public int status { get; set; } = 400;
+        public bool isVaild { get; set; }
+        public Dictionary<string, IEnumerable<string>> errors { get; set; }
+    }
+}

+ 7 - 1
TEAMModelOS.SDK/Models/Cosmos/School/Knowledge.cs

@@ -39,7 +39,13 @@ namespace TEAMModelOS.SDK.Models
         public List<Block> blocks { get; set; } = new List<Block>();
 
     }
-
+    public class OldNew
+    {
+        [Required(ErrorMessage = "_old 必须设置")]
+        public string _old { get; set; }
+        [Required(ErrorMessage = "_new 必须设置")]
+        public string _new { get; set; }
+    }
     public class Block { 
         public string name { get; set; }
         public List<string> points { get; set; }

+ 29 - 1
TEAMModelOS/Controllers/Knowledge/KnowledgesController.cs

@@ -18,6 +18,9 @@ using Microsoft.Extensions.Options;
 using TEAMModelOS.Filter;
 using HTEXLib.COMM.Helpers;
 using Microsoft.AspNetCore.Authorization;
+using TEAMModelOS.SDK;
+using static TEAMModelOS.SDK.ValidatorHelper;
+using System.ComponentModel.DataAnnotations;
 
 namespace TEAMModelOS.Controllers
 {
@@ -41,6 +44,8 @@ namespace TEAMModelOS.Controllers
             _option = option?.Value;
             _azureRedis = azureRedis;
         }
+       
+
         /**
          * 
          {
@@ -81,7 +86,17 @@ namespace TEAMModelOS.Controllers
         [HttpPost("upsert")]
         [Authorize(Roles = "IES")]
         [AuthToken(Roles = "admin", Permissions = "knowledge-upd")]
-        public async Task<IActionResult> Upsert(Knowledge knowledge) {
+        public async Task<IActionResult> Upsert(JsonElement  json) {
+
+            Knowledge knowledge = json.GetProperty("knowledge").ToObject<Knowledge>();
+            List<OldNew> old_new = null;
+            if (json.TryGetProperty("old_new",out JsonElement _old_new )) {
+                old_new = _old_new.ToObject<List<OldNew>>();
+            }
+            ValidResult validResult = knowledge.IsValid();
+            if (!validResult.isVaild) {
+                return BadRequest(validResult);
+            }
             var client = _azureCosmos.GetCosmosClient();
             knowledge.code = $"Knowledge-{knowledge.owner}-{knowledge.subjectId}";
             StringBuilder sql = new StringBuilder($"select value(c) from c where c.periodId = '{knowledge.periodId}'");
@@ -103,6 +118,19 @@ namespace TEAMModelOS.Controllers
             var count = new { pcount = knowledge.points != null ? knowledge.points.Count : 0, bcount = knowledge.blocks != null ? knowledge.blocks.Count : 0 };
             //处理知识点,知识块计数问题
             await _azureRedis.GetRedisClient(8).HashSetAsync($"Knowledge:Count:{knowledge.owner}-{knowledge.subjectId}", knowledge.periodId, count.ToJsonString());
+            if (old_new.IsNotEmpty() && old!= null && knowledge!=null) {
+                var _old =  old_new.Select(x => x._old).ToList();
+                var notinold=  _old.Except(old.points);
+                if (notinold!= null && notinold.Count()>0) {
+                    return BadRequest($"{notinold.ToJsonString()} 不存在原来的知识点中");
+                }
+                var _new = old_new.Select(x => x._new).ToList();
+                var notinnew  = _old.Except(knowledge.points);
+                if (notinnew != null && notinnew.Count() > 0)
+                {
+                    return BadRequest($"{notinnew.ToJsonString()} 不存在新的知识点中");
+                }
+            }
             return Ok(knowledge);
         }