CrazyIter_Bin 1 年之前
父節點
當前提交
e28c77b243

+ 1 - 0
TEAMModelOS.SDK/Models/Cosmos/School/Debate.cs

@@ -10,6 +10,7 @@ namespace TEAMModelOS.SDK.Models
     /// </summary>
     public class Debate : CosmosEntity
     {
+        public string scope { get; set; }
         public string userType { get; set; }
         ///id设计  uuid
         /// code 设计  Debate-hbcn

+ 50 - 28
TEAMModelOS/Controllers/School/DebateController.cs

@@ -54,7 +54,8 @@ namespace TEAMModelOS.Controllers
             {
                 if (!request.TryGetProperty("debateId", out JsonElement _debateId)) { return BadRequest(); }
                 if (!request.TryGetProperty("debateCode", out JsonElement _debateCode)) { return BadRequest(); }
-                string debateCode = $"{_debateCode}".StartsWith("Debate-") ? $"{_debateCode}" : $"Debate-{_debateCode}";
+                if (!request.TryGetProperty("scope", out JsonElement _scope)) { return BadRequest(); }
+                string debateCode = $"{_scope}".Equals("school") ? $"{_debateCode}".StartsWith("Debate-") ? $"{_debateCode}" : $"Debate-{_debateCode}" : "Debate";
                 List<dynamic> debates = new List<dynamic>();
                 Debate debate = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReadItemAsync<Debate>($"{_debateId}", new PartitionKey($"{debateCode}"));
                 return Ok(new { debate = debate , replyCount = debate.replies!=null  ?debate.replies.Count:0});
@@ -85,7 +86,8 @@ namespace TEAMModelOS.Controllers
             {
                 if (!request.TryGetProperty("debateId", out JsonElement _debateId)) { return BadRequest(); }
                 if (!request.TryGetProperty("debateCode", out JsonElement _debateCode)) { return BadRequest(); }
-                string debateCode = $"{_debateCode}".StartsWith("Debate-") ? $"{_debateCode}" : $"Debate-{_debateCode}";
+                if (!request.TryGetProperty("scope", out JsonElement _scope)) { return BadRequest(); }
+                string debateCode =$"{_scope}".Equals("school")? $"{_debateCode}".StartsWith("Debate-") ? $"{_debateCode}" : $"Debate-{_debateCode}": "Debate";
                 await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync($"{_debateId}", new PartitionKey($"{debateCode}"));
                 return Ok(new { status=200});
             }
@@ -112,37 +114,43 @@ namespace TEAMModelOS.Controllers
         public async Task<IActionResult> Find(JsonElement request) {
             try
             {
+
                 List<dynamic> debates = new List<dynamic>();
                 request.TryGetProperty("source", out JsonElement _source);
                 request.TryGetProperty("tmdid", out JsonElement _tmdid);
                 request.TryGetProperty("userType", out JsonElement _userType);
                 request.TryGetProperty("keyWord", out JsonElement _keyWord);
                 request.TryGetProperty("comid", out JsonElement _comid);
-                if (request.TryGetProperty("code", out JsonElement code))
+                string debateCode = "Debate";
+                if (request.TryGetProperty("code", out JsonElement _code)  && !string.IsNullOrWhiteSpace($"{_code}"))
                 {
-                    StringBuilder stringBuilder = new StringBuilder($"select c.id,c.code,c.tmdid,c.userType,c.tmdname,c.title,c.comment,c.time,c.comid,c.likeCount,c.school,c.wordCount,c.timeoutReply,c.expire,c.source,c.pk, ARRAY_LENGTH( c.replies ) as replyCount from c where c.pk='Debate' ");
-                    if (!string.IsNullOrEmpty($"{_source}")) {
-                        stringBuilder.Append($" and c.source='{_source}'");
-                    }
-                    if (!string.IsNullOrEmpty($"{_tmdid}") && !string.IsNullOrWhiteSpace($"{_userType}"))
-                    {
-                        stringBuilder.Append($" and c.tmdid='{_tmdid}' and c.userType='{_userType}'");
-                    }
-                    if (!string.IsNullOrEmpty($"{_keyWord}"))
-                    {
-                        stringBuilder.Append($" and contains(c.title,'{_keyWord}') ");
-                    }
-                    if (!string.IsNullOrEmpty($"{_comid}"))
-                    {
-                        stringBuilder.Append($" and c.comid='{_comid}'");
-                    }
-                    stringBuilder.Append("order by c.time desc ");
-                    await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School")
-                            .GetItemQueryIterator<dynamic>(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Debate-{code}") }))
-                    {
-                        debates.Add(item);
-                    }
+                    debateCode=    $"{_code}".StartsWith("Debate-") ? $"{_code}" : $"Debate-{_code}";
+                }
+               
+               
+                StringBuilder stringBuilder = new StringBuilder($"select c.id,c.code,c.tmdid,c.userType,c.tmdname,c.title,c.comment,c.time,c.comid,c.likeCount,c.school,c.wordCount,c.timeoutReply,c.expire,c.source,c.pk, ARRAY_LENGTH( c.replies ) as replyCount from c where c.pk='Debate' ");
+                if (!string.IsNullOrEmpty($"{_source}")) {
+                    stringBuilder.Append($" and c.source='{_source}'");
+                }
+                if (!string.IsNullOrEmpty($"{_tmdid}") && !string.IsNullOrWhiteSpace($"{_userType}"))
+                {
+                    stringBuilder.Append($" and c.tmdid='{_tmdid}' and c.userType='{_userType}'");
+                }
+                if (!string.IsNullOrEmpty($"{_keyWord}"))
+                {
+                    stringBuilder.Append($" and contains(c.title,'{_keyWord}') ");
                 }
+                if (!string.IsNullOrEmpty($"{_comid}"))
+                {
+                    stringBuilder.Append($" and c.comid='{_comid}'");
+                }
+                stringBuilder.Append("order by c.time desc ");
+                await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School")
+                        .GetItemQueryIterator<dynamic>(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(debateCode) }))
+                {
+                    debates.Add(item);
+                }
+                
                 return Ok(debates);
             }
             catch (Exception ex)
@@ -191,10 +199,14 @@ namespace TEAMModelOS.Controllers
             if (!request.TryGetProperty("opt", out JsonElement _opt)) { return BadRequest(); }
             if (!request.TryGetProperty("debateId", out JsonElement _debateId)) { return BadRequest(); }
             if (!request.TryGetProperty("debateCode", out JsonElement _debateCode)) { return BadRequest(); }
-           
+            if (!request.TryGetProperty("scope", out JsonElement _scope )) { return BadRequest(); }
             DebateReply reply = null;
+          
             string debateCode = $"{_debateCode}".StartsWith("Debate-") ? $"{_debateCode}" : $"Debate-{_debateCode}";
-            if (!string.IsNullOrEmpty($"{_debateId}") && !string.IsNullOrEmpty($"{_debateCode}") && !string.IsNullOrEmpty($"{_opt}"))
+            if (_scope.Equals("private")) {
+                debateCode="Debate";
+            }
+            if (!string.IsNullOrEmpty($"{_debateId}") && !string.IsNullOrEmpty($"{debateCode}") && !string.IsNullOrEmpty($"{_opt}"))
             {
                 switch ($"{_opt}")
                 {
@@ -331,7 +343,6 @@ namespace TEAMModelOS.Controllers
         /// <param name="request"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
-        //[AuthToken(Roles = "teacher")]
         [HttpPost("upsert")]
         [AuthToken(Roles = "teacher,admin,student")]
 
@@ -339,6 +350,8 @@ namespace TEAMModelOS.Controllers
         [Authorize(Roles = "IES")]
 #endif
         public async Task<IActionResult> upsert(JsonElement request) {
+
+            var (tid, tname, _, tschool) = HttpContext.GetAuthTokenInfo();
             /// 评论/话题的统一id,用于被关联
             request.TryGetProperty("comid", out JsonElement _comid);
             /// 创建来源
@@ -364,6 +377,15 @@ namespace TEAMModelOS.Controllers
             else {
                 debate.code = debate.code.StartsWith("Debate-") ? debate.code : $"Debate-{debate.code}";
             }
+            ///个人的不建立分区键
+            if (debate.code.Contains(tid)) {
+                debate.scope="private";
+                debate.code="Debate";
+            }
+            if (!string.IsNullOrWhiteSpace(tschool)  &&   debate.code.Contains(tschool)) 
+            {
+                debate.scope="school";
+            }
             if (string.IsNullOrEmpty(debate.id)) {
                 debate.id = Guid.NewGuid().ToString();
                 debate.time= DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();

+ 18 - 15
TEAMModelOS/Controllers/XTest/FixDataController.cs

@@ -90,25 +90,28 @@ namespace TEAMModelOS.Controllers
         }
 
         [ProducesDefaultResponseType]
-        [HttpPost("fix-elegant")]
+        [HttpPost("fix-debate")]
         public async Task<IActionResult> FixElegant(JsonElement json)
         {
-            string sqlE = $"select value c from c where array_length(c.classes)>0 and  c.pk='Elegant' and contains(c.code,'Elegant-') and  ( c.periodId= null  or IS_DEFINED(c.periodId) = false  )";
-            var resultE = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<Elegant>(sqlE);
+            string sqlE = $"select value c from c where c.pk='Debate'  ";
+            var resultE = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<Debate>(sqlE);
             if (resultE .list.IsNotEmpty()) {
-                List<Class> classes = new List<Class>();
-                string classSQL = $"select value c from c where c.id in({string.Join(",",resultE.list.Where(v=>v.classes.Count>0).Select(x => x.classes.First()).Select(z =>$"'{z}'"))}) and  c.pk='Class' and contains(c.code,'Class-')";
-                var resultC = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).GetList<Class>(classSQL);
-                if (resultC.list.IsNotEmpty()) {
-                    classes.AddRange(resultC.list);
-                }
-                foreach (var item in resultE.list)
+                foreach (var item in resultE.list) 
                 {
-                    var classId = item.classes.First();
-                    var  claszz=  classes.Find(x => x.id.Equals(classId));
-                    if (claszz!= null) {
-                        item.periodId=claszz.periodId;
-                        await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(item, new PartitionKey(item.code));
+                    item.scope="school";
+                }
+                var group = resultE.list.Where(x => !string.IsNullOrWhiteSpace(x.school)).GroupBy(x => x.school).Select(x => new { key = x.Key, list = x.ToList() });
+                foreach (var gp in group) {
+                    List<Task<ItemResponse<Debate>>> tasks = new List<Task<ItemResponse<Debate>>>();
+                    try
+                    {
+                        foreach (var item in gp.list)
+                        {
+                           await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).UpsertItemAsync(item);
+                        }
+                    }
+                    catch (Exception ex) {
+                       await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
                     }
                 }
             }