Forráskód Böngészése

优化查询未加区学校的分页

Li 3 éve
szülő
commit
85211ea170

+ 23 - 4
TEAMModelBI/Controllers/BISchool/AreaRelevantController.cs

@@ -104,7 +104,7 @@ namespace TEAMModelBI.Controllers.BISchool
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
-        [AuthToken(Roles = "admin,assist")]
+        [AuthToken(Roles = "admin,,rdc,assist")]
         [HttpPost("set-areashiftschool")]
         public async Task<IActionResult> SetAreaShiftSchool(JsonElement jsonElement)
         {
@@ -127,9 +127,28 @@ namespace TEAMModelBI.Controllers.BISchool
 
                 if (tempSchool.manyAreas.Count > 0)
                 {
-                    var temp = tempSchool.manyAreas.Find(ma => ma.areaId == $"{areaId}");
-                    if (temp != null)
-                        tempSchool.manyAreas.Remove(new ManyArea { areaId = $"{areaId}", standard = $"{standard}" });
+                    if (bool.Parse($"{isDefault}") == true) 
+                    {
+                        tempSchool.areaId = "";
+                        tempSchool.standard = "";
+                    }
+
+                    if (tempSchool.manyAreas != null)
+                    {
+                        if (!string.IsNullOrEmpty($"{areaId}"))
+                        {
+                            var temp = tempSchool.manyAreas.Find(ma => ma.areaId == $"{areaId}");
+                            if (temp != null)
+                                tempSchool.manyAreas.Remove(temp);
+                        }
+
+                        if (!string.IsNullOrEmpty($"{standard}"))
+                        {
+                            var temp = tempSchool.manyAreas.Find(ma => ma.standard == $"{standard}");
+                            if (temp != null)
+                                tempSchool.manyAreas.Remove(temp);
+                        }
+                    }
                 }
                 School school = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReplaceItemAsync<School>(tempSchool, tempSchool.id, new PartitionKey("Base"));
 

+ 47 - 10
TEAMModelBI/Controllers/BISchool/SchoolController.cs

@@ -63,13 +63,31 @@ namespace TEAMModelBI.Controllers.BISchool
             {
                 jsonElement.TryGetProperty("areaId", out JsonElement areaId);
                 var cosmosClient = _azureCosmos.GetCosmosClient();
-                string sqltxt = "SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.areaId,c.standard,c.manyAreas FROM c WHERE c.standard=null or c.areaId=null";
-                if (!string.IsNullOrEmpty($"{areaId}"))
-                    sqltxt = $"SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.areaId,c.standard,c.manyAreas FROM c WHERE c.areaId !='{areaId}' or ARRAY_CONTAINS(c.manyAreas,'{areaId}',false) or ARRAY_LENGTH(c.manyAreas) = 0 ";
-                //sqltxt = $"SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.manyAreas FROM c join m in c.manyAreas WHERE c.areaId!='{areaId}' or m.areaId!='{areaId}'";
+                //默认不指定返回大小
+                int? pageSize = null;
+                string continuationToken = string.Empty;
+                string pageToken = default;
+                if (jsonElement.TryGetProperty("pageSize", out JsonElement jsonPageSize))
+                {
+                    if (!jsonPageSize.ValueKind.Equals(JsonValueKind.Undefined) && !jsonPageSize.ValueKind.Equals(JsonValueKind.Null) && jsonPageSize.TryGetInt32(out int tempPageSize))
+                    {
+                        pageSize = tempPageSize;
+                    }
+                }
+                //是否需要进行分页查询,默认不分页
+                bool iscontinuation = false;
+                if (pageSize != null && pageSize.Value > 0)
+                {
+                    iscontinuation = true;
+                }
+                if (jsonElement.TryGetProperty("contToken", out JsonElement ContToken))
+                {
+                    pageToken = ContToken.GetString();
+                }
 
-                List<NotAreaSchool> notAreaSchools = new List<NotAreaSchool>();
-                await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+                string sqltxt = "SELECT c.id,c.name,c.schoolCode,c.province,c.city,c.dist,c.picture,c.period,c.areaId,c.standard,c.manyAreas FROM c WHERE c.areaId !='{areaId}'";
+                List<NotAreaSchool> tempNotAreaSchools = new();
+                await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: sqltxt, continuationToken: pageToken, requestOptions: new QueryRequestOptions() { MaxItemCount = pageSize, PartitionKey = new PartitionKey("Base") }))
                 {
                     using var json = await JsonDocument.ParseAsync(item.ContentStream);
                     if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
@@ -95,14 +113,33 @@ namespace TEAMModelBI.Controllers.BISchool
                                 notAreaSchool.manyAreas = obj.GetProperty("manyAreas").ToObject<List<ManyArea>>();
                             }
                             catch { }
-                            notAreaSchools.Add(notAreaSchool);
+                            tempNotAreaSchools.Add(notAreaSchool);
+                        }
+
+                        if (iscontinuation)
+                        {
+                            continuationToken = item.GetContinuationToken();
+                            break;
                         }
                     }
                 }
+                List<NotAreaSchool> notAreaSchools = new List<NotAreaSchool>();
+                tempNotAreaSchools.ForEach(nas =>
+                {
+                    if (nas.manyAreas == null)
+                        notAreaSchools.Add(nas);
+                    else
+                    {
+                        var not = nas.manyAreas.Find(f => f.areaId == $"{areaId}");
+                        if (not == null)
+                            notAreaSchools.Add(nas);
+                    }
+                });
+
                 //if (!string.IsNullOrEmpty($"{areaId}"))                    
                 //    notAreaSchools = notAreaSchools.Select(na => new NotAreaSchool { id = na.id, name = na.name, schoolCode = na.schoolCode, picture = na.picture, period = na.period, province = na.province, city = na.city = na.city, dist = na.dist,  manyAreas =  new List<ManyArea> { na.manyAreas.Find(m => m.areaId != $"{areaId}") } }).ToList();
 
-                return Ok(new { state = 200, notAreaSchools });
+                return Ok(new { state = 200, continuationToken,cont= notAreaSchools.Count, notAreaSchools });
             }
             catch (Exception ex)
             {
@@ -117,7 +154,7 @@ namespace TEAMModelBI.Controllers.BISchool
         /// <param name="jsonElement"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
-        [AuthToken(Roles = "admin,assist")]
+        [AuthToken(Roles = "admin,rdc,assist")]
         [HttpPost("set-schooljoinarea")]
         public async Task<IActionResult> SetSchoolJoinArea(JsonElement jsonElement)
         {
@@ -262,7 +299,7 @@ namespace TEAMModelBI.Controllers.BISchool
         /// <param name="school"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
-        [AuthToken(Roles = "admin,assist")]
+        [AuthToken(Roles = "admin,rdc,assist")]
         [HttpPost("upd-school")]
         public async Task<IActionResult> UpdSchool(School school)
         {