CrazyIter_Bin 2 年之前
父節點
當前提交
017c794c6c

+ 96 - 10
TEAMModelOS.SDK/Models/Service/Common/SyllabusService.cs

@@ -1,7 +1,11 @@
 using Azure.Cosmos;
 using Azure.Cosmos;
 using DocumentFormat.OpenXml.Bibliography;
 using DocumentFormat.OpenXml.Bibliography;
+using DocumentFormat.OpenXml.ExtendedProperties;
+using DocumentFormat.OpenXml.Office2010.Excel;
 using DocumentFormat.OpenXml.Wordprocessing;
 using DocumentFormat.OpenXml.Wordprocessing;
 using HTEXLib.COMM.Helpers;
 using HTEXLib.COMM.Helpers;
+using Microsoft.Azure.Cosmos.Linq;
+using OpenXmlPowerTools;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Linq;
 using System.Linq;
@@ -24,34 +28,116 @@ namespace TEAMModelOS.SDK.Services
         /// <param name="pid"></param>
         /// <param name="pid"></param>
         /// <param name="newNodes"></param>
         /// <param name="newNodes"></param>
         /// <returns></returns>
         /// <returns></returns>
-        public async static Task<HashSet<Tnode>> ImportSyllabus(List<List<string>> nodes,string volumeId,string scope ,string code , AzureCosmosFactory azureCosmos) { 
-            List<Syllabus> syllabuses= new List<Syllabus>();
+        public async static Task<(List<List<IdCode>> idCodes, HashSet<Syllabus> syllabus)> ImportSyllabus(List<List<string>> nodes,string volumeId,string scope ,string code,string creatorId ,string creatorName, AzureCosmosFactory azureCosmos, bool ignoreCharacter = true) { 
+            HashSet<Syllabus> syllabuses= new HashSet<Syllabus>();
             string tbname = scope.Equals("school", StringComparison.OrdinalIgnoreCase) ? Constant.School : Constant.Teacher;
             string tbname = scope.Equals("school", StringComparison.OrdinalIgnoreCase) ? Constant.School : Constant.Teacher;
             await foreach (var item in azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c where c.volumeId='{volumeId}'",
             await foreach (var item in azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<Syllabus>(queryText: $"select value(c) from c where c.volumeId='{volumeId}'",
                         requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") })) {
                         requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Syllabus-{code}") })) {
                 syllabuses.Add(item);
                 syllabuses.Add(item);
             }
             }
-            
+            HashSet<Syllabus> chapters = new HashSet<Syllabus>();
+            long now= DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+            List<List<IdCode> > resNodes = new List<List<IdCode>>();
             foreach (var points in nodes) {
             foreach (var points in nodes) {
+                // points 作为 层级路径   ["汽车考照", "汽车法规","驾驶道德、交通安全常识及行车安全检查与维护"] 
                 int level = 0;
                 int level = 0;
+                // 根节点 作为章节进行当前 路径操作的主要对象
                 Syllabus chapter = null;
                 Syllabus chapter = null;
+                //用于记录  ["汽车考照", "汽车法规","驾驶道德、交通安全常识及行车安全检查与维护"]  标题的id 路径
+                List<(string title, string id)> nodesVal = new List<(string title, string id)>();
+                //上一次操作的 id 用于方便检索下一层级时作为pid  
+                string lastId =null;
                 foreach (var point in points) {
                 foreach (var point in points) {
-                    string pt = Regex.Replace(point, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ");
+                    string express = "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]";
+                    //是否开启特殊符号的正则过滤
+                    string pt = ignoreCharacter? Regex.Replace(point, express, ""):point;
                     if (level == 0)
                     if (level == 0)
                     {
                     {
-                        var sylbs = syllabuses.SelectMany(z => z.children).Where(c => c.pid.Equals(volumeId) && c.title.Equals(Regex.Replace(point, "[ \\[ \\] \\^ \\-|()【】/' {}_*×――(^)$%~!@#$…&%¥—+=<>《》!!???::•`·、。,;,.;\"‘’“”-]", " ")));
+                        var sylbs = syllabuses.SelectMany(z => z.children).Where(c => c.pid.Equals(volumeId) && pt.Equals(ignoreCharacter? Regex.Replace(c.title, express, ""): c.title));
                         if (sylbs.Any())
                         if (sylbs.Any())
                         {
                         {
-                            chapter = syllabuses.Find(z=>z.id.Equals(sylbs.First().id));
-                            if (chapter == null) { }
+                            //找到相关的章节
+                            chapter = syllabuses.Where(z=>z.id.Equals(sylbs.First().id)).FirstOrDefault();
+                            if (chapter == null )
+                            {
+                                foreach (var item in syllabuses)
+                                {
+                                    var it = item.children.Find(z => z.id.Equals(sylbs.First().id));
+                                    if (it != null)
+                                    {
+                                        item.id = sylbs.First().id;
+                                        chapter = item;
+                                        chapters.Add(chapter);
+                                        lastId = item.id;
+                                        nodesVal.Add((point, item.id));
+                                        break;
+                                    }
+                                }
+                            }
+                            else {
+                                chapters.Add(chapter);
+                                lastId = chapter.id;
+                                nodesVal.Add((point, sylbs.First().id));
+                            }
                         }
                         }
+                        else {
+                            //未找到相关的章节
+                            string id = id = Guid.NewGuid().ToString();
+                            lastId= id;
+                            chapter = new Syllabus {
+                                id = id,
+                                volumeId = volumeId,
+                                code = $"Syllabus-{code}",
+                                pk = "Syllabus",
+                                ttl = -1,
+                                scope= scope,
+                                children= new List<Tnode> { new Tnode {id = id ,pid=volumeId,  creatorId = creatorId,
+                                    creatorName = creatorName,title=pt,updateTime= now} }
+                            };
+                            chapters.Add(chapter);
+                            syllabuses.Add(chapter);
+                            nodesVal.Add((point, id));
+                        }
+                        //下钻一层
+                        level += 1;
                     }
                     }
-                    else { 
-                    
+                    else {
+                        if (!string.IsNullOrWhiteSpace(lastId))
+                        {
+                            var child =  chapter.children.Find(child => child.pid.Equals(lastId)  && pt.Equals(ignoreCharacter ? Regex.Replace(child.title, express, "") : child.title));
+                            if (child == null)
+                            {
+                                string id = id = Guid.NewGuid().ToString();
+                                child = new Tnode {
+                                    id = id,
+                                    pid = lastId,
+                                    creatorId = creatorId,
+                                    creatorName = creatorName,
+                                    title = pt,
+                                    updateTime = now
+                                };
+                                chapter.children.Add(child);
+                                lastId = child.id;
+                                nodesVal.Add((point, child.id));
+                            }
+                            else {
+                                lastId= child.id;
+                                nodesVal.Add((point, child.id));
+                            }
+                            level += 1;
+                        }
                     }
                     }
                 }
                 }
+                if (chapter != null) { 
+                    resNodes.Add(nodesVal.Select(z => new IdCode { id = z.id, code = z.title }).ToList());
+                }
+            }
+            foreach(var chapter in chapters)
+            {
+                await azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(chapter);
             }
             }
-            return null;
+          
+            return (resNodes,chapters);
         }
         }
 
 
 
 

+ 34 - 2
TEAMModelOS/Controllers/Both/SyllabusController.cs

@@ -42,7 +42,37 @@ namespace TEAMModelOS.Controllers
             _dingDing = dingDing;
             _dingDing = dingDing;
             _option = option?.Value;
             _option = option?.Value;
         }
         }
-
+        [ProducesDefaultResponseType]
+        [HttpPost("import-syllabus")]
+        #if !DEBUG
+          [Authorize(Roles = "IES")]
+        #endif
+        [AuthToken(Roles = "teacher,admin")]
+        public async Task<IActionResult> ImportSyllabus(JsonElement request) {
+            var (userid, name, _, school) = HttpContext.GetAuthTokenInfo();
+            List<List<string>> nodes = new List<List<string>>();
+            if (!request.TryGetProperty("nodes", out JsonElement _nodes)) {
+                return BadRequest();
+            }
+            else {
+                try {
+                    if (_nodes.ValueKind.Equals(JsonValueKind.Array))
+                    {
+                        nodes = _nodes.ToObject<List<List<string>>>();
+                    }
+                } catch { 
+                }
+            }
+            if (!request.TryGetProperty("volumeId", out JsonElement _volumeId)) return BadRequest();
+            if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
+            if (!request.TryGetProperty("code", out JsonElement _code)) return BadRequest();
+            bool ignoreCharacter = false;
+            if (request.TryGetProperty("ignoreCharacter", out JsonElement _ignoreCharacter)) {
+                ignoreCharacter = _ignoreCharacter.ValueKind.Equals(JsonValueKind.True)?true:false;
+            }
+            (List<List<IdCode>> idCodes, HashSet<Syllabus> syllabuses) data =  await  SyllabusService.ImportSyllabus(nodes, $"{_volumeId}", $"{_scope}", $"{_scope}", userid, name,_azureCosmos,ignoreCharacter);
+            return Ok(new {codes= data.idCodes, data.syllabuses });
+        }
         /// <summary>
         /// <summary>
         /// 检查资源链接是否被其他结构关联
         /// 检查资源链接是否被其他结构关联
         /// 
         /// 
@@ -255,7 +285,9 @@ namespace TEAMModelOS.Controllers
         /// <returns></returns>
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [ProducesDefaultResponseType]
         [HttpPost("find-id")]
         [HttpPost("find-id")]
-        [Authorize(Roles = "IES")]
+#if !DEBUG
+          [Authorize(Roles = "IES")]
+#endif
         [AuthToken(Roles = "teacher,admin,student")]
         [AuthToken(Roles = "teacher,admin,student")]
         public async Task<IActionResult> Find(JsonElement request)
         public async Task<IActionResult> Find(JsonElement request)
         {
         {