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

优化查询学校信息,和学校相关的信息

Li 3 éve
szülő
commit
3d7d3c1054

+ 112 - 110
TEAMModelBI/Controllers/BISchool/BatchSchoolController.cs

@@ -387,102 +387,104 @@ namespace TEAMModelBI.Controllers.BISchool
         {
             try
             {
+                jsonElement.TryGetProperty("tmdId", out JsonElement assistId);
                 jsonElement.TryGetProperty("schoolCode", out JsonElement _schoolCode);
                 var cosmosClient = _azureCosmos.GetCosmosClient();
-                List<SchoolAssist> schoolAssists = new();
+                List<AssistSchool> schoolAssists = new();
                 //StringBuilder stringBuilder = new StringBuilder("select value(c) from c");
-                StringBuilder stringBuilder = new StringBuilder("select c.id,c.code,c.schoolCode,c.name,c.region,c.province,c.city,c.dist,c.size,c.address,c.picture,c.type,c.scale,c.areaId,c.standard from c");
+                StringBuilder stringBuilder = new("select c.id,c.code,c.schoolCode,c.name,c.region,c.province,c.city,c.dist,c.size,c.address,c.picture,c.type,c.scale,c.areaId,c.standard from c");
 
                 if (!string.IsNullOrEmpty($"{_schoolCode}"))
                 {
                     stringBuilder.Append($" where c.id='{_schoolCode}'");
                 }
 
-                //await foreach (var itemSchool in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
-                //{
-                //    SchoolAssist schoolAssist = new()
-                //    {
-                //        id = itemSchool.id,
-                //        code = itemSchool.code,
-                //        schoolCode = itemSchool.schoolCode,
-                //        name = itemSchool.name,
-                //        region = itemSchool.region,
-                //        province = itemSchool.province,
-                //        city = itemSchool.city,
-                //        dist = itemSchool.dist,
-                //        size = itemSchool.size,
-                //        address = itemSchool.address,
-                //        picture = itemSchool.picture,
-                //        type = itemSchool.type,
-                //        scale = itemSchool.scale,
-                //        areaId = itemSchool.areaId,
-                //        standard = itemSchool.standard
-                //    };
-
-                //    var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(itemSchool.id, new PartitionKey("ProductSum"));
-                //    if (response.Status == 200)
-                //    {
-                //        using var json = await JsonDocument.ParseAsync(response.ContentStream);
-                //        schoolAssist.serial = json.RootElement.GetProperty("serial").GetArrayLength();
-                //        schoolAssist.service = json.RootElement.GetProperty("service").GetArrayLength();
-                //        schoolAssist.hard = json.RootElement.GetProperty("hard").GetArrayLength();
-                //    }
-
-                //    schoolAssist.assists = await CommonFind.FindSchoolRoles(cosmosClient, itemSchool.id, "assist");
-
-                //    schoolAssists.Add(schoolAssist);
-                //}
-                //return Ok(new { state = 200, schoolAssists });
-
-                await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+                List<string> schools = new();
+                if (!string.IsNullOrEmpty($"{assistId}"))
+                    schools = await CommonFind.FindSchoolIds(cosmosClient, $"{assistId}");
+                if (schools.Count > 0)
+                {
+                    schoolAssists = await CommonFind.FindAssistSchools(cosmosClient, schools);
+                }
+                else
                 {
-                    using var jsonSchool = await JsonDocument.ParseAsync(itemSchool.ContentStream);
-                    foreach (var objSchool in jsonSchool.RootElement.GetProperty("Documents").EnumerateArray())
+                    await foreach (var itemSchool in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<AssistSchool>(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
                     {
-                        var schoolId = objSchool.GetProperty("id").GetString();
-                        SchoolAssist schoolAssist = new()
-                        {
-                            id = schoolId,
-                            code = objSchool.GetProperty("code").GetString(),
-                            schoolCode = objSchool.GetProperty("schoolCode").GetString(),
-                            name = objSchool.GetProperty("name").GetString(),
-                            region = objSchool.GetProperty("region").GetString(),
-                            province = objSchool.GetProperty("province").GetString(),
-                            city = objSchool.GetProperty("city").GetString(),
-                            dist = objSchool.GetProperty("dist").GetString(),
-                            size = objSchool.GetProperty("size").GetInt32(),
-                            address = objSchool.GetProperty("address").GetString(),
-                            picture = objSchool.GetProperty("picture").GetString(),
-                            type = objSchool.GetProperty("type").GetInt32(),
-                            //scale = objSchool.GetProperty("scale").GetInt32(),
-                            areaId = objSchool.GetProperty("areaId").GetString(),
-                            standard = objSchool.GetProperty("standard").GetString(),
-                        };
-
-                        try { schoolAssist.scale = objSchool.GetProperty("scale").GetInt32(); }
-                        catch { schoolAssist.scale = 0; }
+                        schoolAssists.Add(itemSchool);
+                    }
 
-                        //var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(schoolId, new PartitionKey("ProductSum"));
-                        //if (response.Status == 200)
-                        //{
-                        //    using var json = await JsonDocument.ParseAsync(response.ContentStream);
-                        //    schoolAssist.serial = json.RootElement.GetProperty("serial").GetArrayLength();
-                        //    schoolAssist.service = json.RootElement.GetProperty("service").GetArrayLength();
-                        //    schoolAssist.hard = json.RootElement.GetProperty("hard").GetArrayLength();
-                        //}
+                    schoolAssists.ForEach(async school =>
+                    {
+                        var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school.id, new PartitionKey("ProductSum"));
+                        if (response.Status == 200)
+                        {
+                            using var json = await JsonDocument.ParseAsync(response.ContentStream);
+                            school.serial = json.RootElement.GetProperty("serial").GetArrayLength();
+                            school.service = json.RootElement.GetProperty("service").GetArrayLength();
+                            school.hard = json.RootElement.GetProperty("hard").GetArrayLength();
+                        }
 
-                        //schoolAssist.assists = await CommonFind.FindSchoolRoles(cosmosClient, schoolId, "assist");
-                        schoolAssists.Add(schoolAssist);
-                    }
+                        school.assists = await CommonFind.FindSchoolRoles(cosmosClient, school.id, "assist");
+                    });
                 }
 
-                List<SchoolAssist> tempSchoolAssists = new();
-                await foreach (var temp in GetSchools(cosmosClient, schoolAssists)) 
-                {
-                    tempSchoolAssists.AddRange(temp);
-                }
+                return Ok(new { state = 200, schoolAssists });
 
-                return Ok(new { state = 200, schoolAssists = tempSchoolAssists });
+                //if (schools.Count > 0)
+                //{
+                //    schoolAssists = await CommonFind.FindAssistSchools(cosmosClient, schools);
+                //}
+                //else
+                //{
+                //    await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+                //    {
+                //        using var jsonSchool = await JsonDocument.ParseAsync(itemSchool.ContentStream);
+                //        foreach (var objSchool in jsonSchool.RootElement.GetProperty("Documents").EnumerateArray())
+                //        {
+                //            var schoolId = objSchool.GetProperty("id").GetString();
+                //            AssistSchool schoolAssist = new()
+                //            {
+                //                id = schoolId,
+                //                code = objSchool.GetProperty("code").GetString(),
+                //                schoolCode = objSchool.GetProperty("schoolCode").GetString(),
+                //                name = objSchool.GetProperty("name").GetString(),
+                //                region = objSchool.GetProperty("region").GetString(),
+                //                province = objSchool.GetProperty("province").GetString(),
+                //                city = objSchool.GetProperty("city").GetString(),
+                //                dist = objSchool.GetProperty("dist").GetString(),
+                //                size = objSchool.GetProperty("size").GetInt32(),
+                //                address = objSchool.GetProperty("address").GetString(),
+                //                picture = objSchool.GetProperty("picture").GetString(),
+                //                type = objSchool.GetProperty("type").GetInt32(),
+                //                //scale = objSchool.GetProperty("scale").GetInt32(),
+                //                areaId = objSchool.GetProperty("areaId").GetString(),
+                //                standard = objSchool.GetProperty("standard").GetString(),
+                //            };
+
+                //            try { schoolAssist.scale = objSchool.GetProperty("scale").GetInt32(); }
+                //            catch { schoolAssist.scale = 0; }
+
+                //            //var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(schoolId, new PartitionKey("ProductSum"));
+                //            //if (response.Status == 200)
+                //            //{
+                //            //    using var json = await JsonDocument.ParseAsync(response.ContentStream);
+                //            //    schoolAssist.serial = json.RootElement.GetProperty("serial").GetArrayLength();
+                //            //    schoolAssist.service = json.RootElement.GetProperty("service").GetArrayLength();
+                //            //    schoolAssist.hard = json.RootElement.GetProperty("hard").GetArrayLength();
+                //            //}
+
+                //            //schoolAssist.assists = await CommonFind.FindSchoolRoles(cosmosClient, schoolId, "assist");
+                //            schoolAssists.Add(schoolAssist);
+                //        }
+                //    }
+                //}
+                //List<AssistSchool> tempSchoolAssists = new();
+                //await foreach (var temp in GetSchools(cosmosClient, schoolAssists))
+                //{
+                //    tempSchoolAssists.AddRange(temp);
+                //}
+
+                //return Ok(new { state = 200, schoolAssists = tempSchoolAssists });
 
             }
             catch (Exception ex)
@@ -498,9 +500,9 @@ namespace TEAMModelBI.Controllers.BISchool
         /// <param name="cosmosClient"></param>
         /// <param name="schoolAssists"></param>
         /// <returns></returns>
-        private async IAsyncEnumerable<List<SchoolAssist>> GetSchools(CosmosClient cosmosClient,List<SchoolAssist> schoolAssists)
+        private async IAsyncEnumerable<List<AssistSchool>> GetSchools(CosmosClient cosmosClient,List<AssistSchool> schoolAssists)
         {
-            List<SchoolAssist> tempSchoolAssists = new();
+            List<AssistSchool> tempSchoolAssists = new();
             foreach (var temp in schoolAssists)
             {
                 var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(temp.id, new PartitionKey("ProductSum"));
@@ -846,7 +848,7 @@ namespace TEAMModelBI.Controllers.BISchool
         public async Task<IActionResult> GetSchools(JsonElement jsonElement) 
         {
             //List<School> schools = new List<School>();
-            List<SchoolAssist> schoolAssists = new List<SchoolAssist>();
+            List<AssistSchool> schoolAssists = new List<AssistSchool>();
             var cosmosClient = _azureCosmos.GetCosmosClient();
             if (!jsonElement.TryGetProperty("endPosition", out JsonElement endPosition)) return BadRequest();
             if (!jsonElement.TryGetProperty("pageSize", out JsonElement pageSize)) return BadRequest();
@@ -855,7 +857,7 @@ namespace TEAMModelBI.Controllers.BISchool
 
             await foreach (var itemSchool in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: sqltxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
             {
-                SchoolAssist schoolAssist = new SchoolAssist()
+                AssistSchool schoolAssist = new()
                 {
                     id = itemSchool.id,
                     code = itemSchool.code,
@@ -1011,32 +1013,32 @@ namespace TEAMModelBI.Controllers.BISchool
 
         #endregion
 
-        public class SchoolAssist
-        {
-            public string id { get; set; }
-            public string code { get; set; }
-            public string schoolCode { get; set; }
-            public string name { get; set; }
-            public string region { get; set; }
-            public string province { get; set; }
-            public string city { get; set; }
-            public string dist { get; set; }
-            public int size { get; set; }
-            public string address { get; set; }
-            public string picture { get; set; }
-            public int type { get; set; }
-            public int scale { get; set; }
-            public string standard { get; set; }
-            public string areaId { get; set; }
-            public List<SchoolTeacherRoles> assists { get; set; }
-            public int serial { get; set; } //软体
-            public int service { get; set; } //服务
-            public int hard { get; set; } //硬体
-            //public List<SchoolProductSumData> serial { get; set; } //软体
-            //public List<SchoolProductSumData> service { get; set; } //服务
-            //public List<SchoolProductSumDataHard> hard { get; set; } //硬体
-
-        }
+        //public class SchoolAssist
+        //{
+        //    public string id { get; set; }
+        //    public string code { get; set; }
+        //    public string schoolCode { get; set; }
+        //    public string name { get; set; }
+        //    public string region { get; set; }
+        //    public string province { get; set; }
+        //    public string city { get; set; }
+        //    public string dist { get; set; }
+        //    public int size { get; set; }
+        //    public string address { get; set; }
+        //    public string picture { get; set; }
+        //    public int type { get; set; }
+        //    public int scale { get; set; }
+        //    public string standard { get; set; }
+        //    public string areaId { get; set; }
+        //    public List<SchoolTeacherRoles> assists { get; set; }
+        //    public int serial { get; set; } //软体
+        //    public int service { get; set; } //服务
+        //    public int hard { get; set; } //硬体
+        //    //public List<SchoolProductSumData> serial { get; set; } //软体
+        //    //public List<SchoolProductSumData> service { get; set; } //服务
+        //    //public List<SchoolProductSumDataHard> hard { get; set; } //硬体
+
+        //}
 
 
         /// <summary>

+ 38 - 3
TEAMModelBI/Tool/CommonFind.cs

@@ -74,7 +74,7 @@ namespace TEAMModelBI.Tool
         /// <returns>返回学校角色列表</returns>
         public static async Task<List<SchoolTeacherRoles>> FindSchoolRoles(CosmosClient cosmosClient,string schoolId,string roles)
         {
-            List<SchoolTeacherRoles> strs = new List<SchoolTeacherRoles>();
+            List<SchoolTeacherRoles> strs = new();
             string managerSql = $"SELECT DISTINCT REPLACE(c.code, 'Teacher-', '') AS schoolId, c.id, c.name FROM c WHERE ARRAY_CONTAINS(c.roles, '{roles}', true) AND c.pk = 'Teacher' AND c.status = 'join' AND c.code = 'Teacher-{schoolId}'";
             await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: managerSql, requestOptions: new QueryRequestOptions() { }))
             {
@@ -102,7 +102,7 @@ namespace TEAMModelBI.Tool
         /// <returns>返回顾问相关的学校ID集合</returns>
         public static async Task<List<string>> FindSchoolIds(CosmosClient cosmosClient, string tmdId)
         {
-            List<string> schoolIds = new List<string>();
+            List<string> schoolIds = new();
             string schoolSql = $"SELECT DISTINCT REPLACE(c.code,'Teacher-','') AS schoolId,c.code,c.roles,c.id,c.name From c where ARRAY_CONTAINS(c.roles,'assist',true) AND c.pk = 'Teacher' AND c.status = 'join' AND c.id='{tmdId}'";
 
             await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: schoolSql, requestOptions: new QueryRequestOptions() { }))
@@ -126,7 +126,7 @@ namespace TEAMModelBI.Tool
         /// <returns>返回学校ID的集合</returns>
         public static async Task<List<string>> FindSchoolIds(CosmosClient cosmosClient, string sqlTxt, string code)
         {
-            List<string> schoolIds = new List<string>();
+            List<string> schoolIds = new();
 
             await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
             {
@@ -140,5 +140,40 @@ namespace TEAMModelBI.Tool
             return schoolIds;
         }
 
+        /// <summary>
+        /// 依据学校编号查询学校信息和学校相关的信息
+        /// </summary>
+        /// <param name="cosmosClient"></param>
+        /// <param name="schoos"></param>
+        /// <returns></returns>
+        public static async Task<List<AssistSchool>> FindAssistSchools(CosmosClient cosmosClient, List<string> schoos)
+        {
+            List<AssistSchool> assistSchools = new();
+            foreach (var id in schoos)
+            {
+                string sqlTxt = $"select c.id,c.code,c.schoolCode,c.name,c.region,c.province,c.city,c.dist,c.size,c.address,c.picture,c.type,c.scale,c.areaId,c.standard from c where c.id='{id}'";
+                await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AssistSchool>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+                {
+                    assistSchools.Add(itemSchool);
+                }
+            }
+
+            foreach (var school in assistSchools)
+            {
+                var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(school.id, new PartitionKey("ProductSum"));
+                if (response.Status == 200)
+                {
+                    using var json = await JsonDocument.ParseAsync(response.ContentStream);
+                    school.serial = json.RootElement.GetProperty("serial").GetArrayLength();
+                    school.service = json.RootElement.GetProperty("service").GetArrayLength();
+                    school.hard = json.RootElement.GetProperty("hard").GetArrayLength();
+                }
+
+                school.assists = await CommonFind.FindSchoolRoles(cosmosClient, school.id, "assist");
+            }
+            
+            return assistSchools;
+        }
+
     }
 }