Li 2 years ago
parent
commit
3dc022c05f
1 changed files with 36 additions and 21 deletions
  1. 36 21
      TEAMModelBI/Controllers/Census/PaperController.cs

+ 36 - 21
TEAMModelBI/Controllers/Census/PaperController.cs

@@ -6,6 +6,7 @@ using DocumentFormat.OpenXml.Drawing.Diagrams;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Http;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.AspNetCore.Mvc;
 using Microsoft.Extensions.Options;
 using Microsoft.Extensions.Options;
+using Microsoft.Extensions.Primitives;
 using System;
 using System;
 using System.Collections.Generic;
 using System.Collections.Generic;
 using System.Globalization;
 using System.Globalization;
@@ -57,37 +58,51 @@ namespace TEAMModelBI.Controllers.Census
         {
         {
             if (!jsonElement.TryGetProperty("code", out JsonElement code)) return BadRequest();
             if (!jsonElement.TryGetProperty("code", out JsonElement code)) return BadRequest();
             jsonElement.TryGetProperty("scope", out JsonElement _scope);
             jsonElement.TryGetProperty("scope", out JsonElement _scope);
-            Dictionary<string, object> dic = new();
-            if (jsonElement.TryGetProperty("periodId", out JsonElement periodId))
-                dic.Add("periodId", periodId);
-            if (jsonElement.TryGetProperty("subjectId", out JsonElement subjectId))
-                dic.Add("subjectId", subjectId);
-            if (jsonElement.TryGetProperty("gradeIds", out JsonElement gradeIds))
-                dic.Add("gradeIds", gradeIds);
-            if (jsonElement.TryGetProperty("isSort", out JsonElement isSort))
+
+            string scope = "school";
+            if (string.IsNullOrEmpty($"{_scope}"))
+                scope = $"{_scope}";
+            StringBuilder sql = new($"select value(c) from c where c.scope='{scope}'");
+
+            if (!jsonElement.TryGetProperty("periodId", out JsonElement periodId)) return BadRequest();
+            if (!jsonElement.TryGetProperty("subjectId", out JsonElement subjectId)) return BadRequest();
+            if (!jsonElement.TryGetProperty("gradeIds", out JsonElement gradeIds)) return BadRequest();
+            if (!jsonElement.TryGetProperty("isSort", out JsonElement isSort)) return BadRequest();
+            if (!string.IsNullOrEmpty($"{periodId}"))
+                sql.Append($" and c.periodId ='{periodId}' ");
+            if (!string.IsNullOrEmpty($"{subjectId}"))
             {
             {
-                if (!string.IsNullOrEmpty($"{isSort}"))
+                List<string> subIds = subjectId.ToObject<List<string>>();
+                if (subIds.Count > 1)
                 {
                 {
-                    if (bool.Parse($"{isSort}") == true)
-                        dic.Add("@DESC", "createTime");
-                    else
-                        dic.Add("@ASC", "createTime");
+                    sql.Append($" and c.subjectId in (");
+                    for (int i = 0; i < subIds.Count; i++)
+                    {
+                        if (i == (subIds.Count - 1))
+                            sql.Append($"'{subIds[i]}'");
+                        else
+                            sql.Append($"{subIds[i]},");
+                    }
                 }
                 }
-                else
-                    dic.Add("@ASC", "createTime");
+                else if(subIds.Count ==1)
+                {
+                    sql.Append($" and c.subjectId='{subjectId[0]}'");
+                }            
             }
             }
-            string scope = "school";
-            if (string.IsNullOrEmpty($"{_scope}"))
-                scope = $"{_scope}";
+            if (!string.IsNullOrEmpty($"{gradeIds}"))
+                sql.Append($" and ARRAY_CONTAINS(c.gradeIds,'{gradeIds}')");
+
+            if (bool.Parse($"{isSort}") == true)
+                sql.Append(" Order By c.createTime DESC");
+            else
+                sql.Append(" Order By c.createTime ASC");
             
             
             var cosmosClinet = _azureCosmos.GetCosmosClient();
             var cosmosClinet = _azureCosmos.GetCosmosClient();
-            StringBuilder sql = new("select value(c) from c ");
-            AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dic, sql);
 
 
             List<Paper> papers = new();
             List<Paper> papers = new();
             if (scope.ToString().Equals("school"))
             if (scope.ToString().Equals("school"))
             {
             {
-                await foreach (var item in cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Paper>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{code}") }))
+                await foreach (var item in cosmosClinet.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Paper>(queryText: sql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{code}") }))
                 {
                 {
                     papers.Add(item);
                     papers.Add(item);
                 }
                 }