浏览代码

新增查询所有学校信息接口

Li 2 年之前
父节点
当前提交
56f977e25d
共有 1 个文件被更改,包括 157 次插入25 次删除
  1. 157 25
      TEAMModelBI/Controllers/BISchool/BatchSchoolController.cs

+ 157 - 25
TEAMModelBI/Controllers/BISchool/BatchSchoolController.cs

@@ -185,7 +185,7 @@ namespace TEAMModelBI.Controllers.BISchool
                             if (schoolStatu.Status != 200) tempStaus = false;
                             else createSchoolInfo.createCount = createSchoolInfo.createCount >= 3 ? createSchoolInfo.createCount = 3 : createSchoolInfo.createCount += 1;
                         } while (tempStaus);
-                        
+
                         if (createSchoolInfo.id != null)
                         {
                             string campusId = Guid.NewGuid().ToString();
@@ -220,7 +220,7 @@ namespace TEAMModelBI.Controllers.BISchool
                             upSc.Add(upSchool.id);
                             //创建学校
                             await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<School>(upSchool, new PartitionKey(upSchool.code));
-                            
+
                             var content = new StringContent(bischool.admin.ToArray().ToJsonString(), Encoding.UTF8, "application/json");
                             string json = await _coreAPIHttpService.GetUserInfos(content);
                             List<TmdUserinfo> tmdInfos = json.ToObject<List<TmdUserinfo>>();
@@ -235,7 +235,7 @@ namespace TEAMModelBI.Controllers.BISchool
                                     teacher = tchJson.ToObject<Teacher>();
                                     Teacher.TeacherSchool tchSc = null;
                                     //var tempTch = teacher.schools.Select(x => x.schoolId.Equals(upSchool.id)).ToString();
-                                    tchSc = teacher.schools.Find(x => x.schoolId.Equals(upSchool.id));                                
+                                    tchSc = teacher.schools.Find(x => x.schoolId.Equals(upSchool.id));
                                     if (tchSc == null)
                                     {
                                         //教师存在,在该教师信息中添加要管理的学校信息
@@ -295,7 +295,7 @@ namespace TEAMModelBI.Controllers.BISchool
                                         ttl = -1
                                     };
 
-                                    stringBuilder.Append($"权限:{string.Join(",", schoolTeacher.roles)}】"); 
+                                    stringBuilder.Append($"权限:{string.Join(",", schoolTeacher.roles)}】");
                                     noticeDD.Append($"{schoolTeacher.name}【{schoolTeacher.id}】");
                                     await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").CreateItemAsync<SchoolTeacher>(schoolTeacher, new PartitionKey(schoolTeacher.code));
                                 }
@@ -476,7 +476,7 @@ namespace TEAMModelBI.Controllers.BISchool
 
                 if (upSc.Count > 1)
                     await _dingDing.SendBotMsg($"BI,{_option.Location} \n 批量建校信息:{noticeDD}", GroupNames.成都开发測試群組);
-                else if(upSc.Count == 1)
+                else if (upSc.Count == 1)
                     await _dingDing.SendBotMsg($"BI,{_option.Location} \n 单个建校信息:{noticeDD}", GroupNames.成都开发測試群組);
 
                 ////v2通知
@@ -577,6 +577,8 @@ namespace TEAMModelBI.Controllers.BISchool
 
                 if ($"{order}".Equals("desc"))
                     stringBuilder.Append(" order by c.createTime desc");
+                else
+                    stringBuilder.Append(" order by c.createTime asc");
 
                 if (!string.IsNullOrEmpty($"{tmdId}"))
                 {
@@ -591,6 +593,8 @@ namespace TEAMModelBI.Controllers.BISchool
 
                         if ($"{order}".Equals("desc"))
                             sqlTxt.Append(" order by c.createTime desc");
+                        else 
+                            sqlTxt.Append(" order by c.createTime asc");
 
                         await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AssistSchool>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
                         {
@@ -598,11 +602,8 @@ namespace TEAMModelBI.Controllers.BISchool
                         }
                     }
                 }
-                else 
+                else
                 {
-                    if ($"{order}".Equals("desc"))
-                        stringBuilder.Append(" order by c.createTime desc");
-
                     scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
 
                     await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), continuationToken: pageToken, requestOptions: new QueryRequestOptions() { MaxItemCount = pageSize, PartitionKey = new PartitionKey("Base") }))
@@ -705,7 +706,7 @@ namespace TEAMModelBI.Controllers.BISchool
                     }
 
                     item.assists = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "assist");
-                    item.scAdmin = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "admin");                    
+                    item.scAdmin = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "admin");
                     item.lessonCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c ", $"LessonRecord-{item.id}");
                 }
 
@@ -742,6 +743,137 @@ namespace TEAMModelBI.Controllers.BISchool
             }
         }
 
+        /// <summary>
+        /// 所有信息
+        /// </summary>
+        /// <param name="jsonElement"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("get-allscinfo")]
+        public async Task<IActionResult> GetAllScsInfo(JsonElement jsonElement)
+        {
+            try
+            {
+                jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
+                jsonElement.TryGetProperty("role", out JsonElement role);
+                jsonElement.TryGetProperty("scId", out JsonElement scId);
+                jsonElement.TryGetProperty("name", out JsonElement name);
+                jsonElement.TryGetProperty("order", out JsonElement order);
+
+                jsonElement.TryGetProperty("province", out JsonElement province);
+                jsonElement.TryGetProperty("city", out JsonElement city);
+                jsonElement.TryGetProperty("dist", out JsonElement dist);
+
+                //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
+                var cosmosClient = _azureCosmos.GetCosmosClient();
+                ////分开部署,就不需要,一站多用时,取消注释
+                //if ($"{site}".Equals(BIConst.Global))
+                //    cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
+                int scCnt = 0;
+
+                List<AssistSchool> schoolAssists = new(); //返回学校列表集合
+                List<string> schoolIds = new();
+                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 ");
+                StringBuilder scCntSql = new($"select value(count(c.id)) from c");
+                
+                if (!string.IsNullOrEmpty($"{scId}") && string.IsNullOrEmpty($"{name}"))
+                {
+                    stringBuilder.Append($" where c.id='{scId}'");
+                    scCntSql.Append($" where c.id='{scId}'");
+                }
+                if (string.IsNullOrEmpty($"{scId}") && !string.IsNullOrEmpty($"{name}"))
+                {
+                    stringBuilder.Append($" where Contains(c.name,'{name}')");
+                    scCntSql.Append($" where Contains(c.name,'{name}')");
+                }
+                if (!string.IsNullOrEmpty($"{province}") && string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
+                {
+                    stringBuilder.Append($" where Contains(c.province,'{province}')");
+                    scCntSql.Append($" where  Contains(c.province,'{province}')");
+                }
+                if (!string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{city}") && string.IsNullOrEmpty($"{dist}"))
+                {
+                    stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
+                    scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}')");
+                }
+                if (!string.IsNullOrEmpty($"{dist}") && !string.IsNullOrEmpty($"{city}") && !string.IsNullOrEmpty($"{dist}"))
+                {
+                    stringBuilder.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
+                    scCntSql.Append($" where Contains(c.province,'{province}') and Contains(c.city,'{city}') and Contains(c.dist,'{dist}')");
+                }
+
+                if (!string.IsNullOrEmpty($"{tmdId}"))
+                {
+                    schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}", roles: !string.IsNullOrEmpty($"{role}") ? $"{role}" : "assist", isMany: true);
+                    string scsSql = BICommonWay.ManyScSql("c.id", schoolIds);
+                    scCntSql.Append($" where {scsSql}");
+
+                    scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
+                    foreach (var id in schoolIds)
+                    {
+                        StringBuilder sqlTxt = 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 where c.id='{id}'");
+
+                        if ($"{order}".Equals("desc"))
+                            sqlTxt.Append(" order by c.createTime desc");
+                        else
+                            sqlTxt.Append(" order by c.createTime asc");
+
+                        await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<AssistSchool>(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+                        {
+                            schoolAssists.Add(itemSchool);
+                        }
+                    }
+                }
+                else
+                {
+                    if ($"{order}".Equals("desc"))
+                        stringBuilder.Append(" order by c.createTime desc");
+                    else
+                        stringBuilder.Append(" order by c.createTime asc");
+
+                    scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", scCntSql.ToString(), "Base");
+
+                    await foreach (var itemSchool in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryText: stringBuilder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
+                    {
+                        using var json = await JsonDocument.ParseAsync(itemSchool.ContentStream);
+                        if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetInt16() > 0)
+                        {
+                            foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                            {
+                                schoolAssists.Add(obj.ToObject<AssistSchool>());
+                            }
+                        }
+                    }
+                }
+
+                foreach (var item in schoolAssists)
+                {
+                    var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(item.id, new PartitionKey("ProductSum"));
+                    if (response.Status == 200)
+                    {
+                        using var json = await JsonDocument.ParseAsync(response.ContentStream);
+                        if (json.RootElement.TryGetProperty("serial", out JsonElement serial) && !serial.ValueKind.Equals(JsonValueKind.Null))
+                            item.serial = serial.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
+                        if (json.RootElement.TryGetProperty("service", out JsonElement service) && !service.ValueKind.Equals(JsonValueKind.Null))
+                            item.service = service.ToObject<List<SchoolProductSumData>>().Select(x => x.prodCode).ToList();
+                        if (json.RootElement.TryGetProperty("hard", out JsonElement hard) && !hard.ValueKind.Equals(JsonValueKind.Null))
+                            item.hard = hard.ToObject<List<SchoolProductSumDataHard>>().Select(x => x.prodCode).ToList();
+                    }
+
+                    item.assists = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "assist");
+                    item.scAdmin = await CommonFind.FindSchoolRoles(cosmosClient, item.id, "admin");
+                    item.lessonCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"select value(count(c.id)) from c ", $"LessonRecord-{item.id}");
+                }
+
+                return Ok(new { state = 200, scCnt, schoolAssists });
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"BI,{_option.Location}  /batchschool/get-allscinfo()  \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
+                return BadRequest();
+            }
+        }
+
         /// <summary>
         /// 使用yieId 关键字 本地效率未测出
         /// </summary>
@@ -1036,7 +1168,7 @@ namespace TEAMModelBI.Controllers.BISchool
                                 //查询该教师是否存在该校
                                 SchoolTeacher schoolTeacher = null;
                                 var resScTch = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{itemTeacher}", new PartitionKey($"Teacher-{tempShool.id}"));
-                                if (resScTch.Status == 200) 
+                                if (resScTch.Status == 200)
                                 {
                                     using var tchJson = await JsonDocument.ParseAsync(resScTch.ContentStream);
                                     schoolTeacher = tchJson.ToObject<SchoolTeacher>();
@@ -1177,13 +1309,13 @@ namespace TEAMModelBI.Controllers.BISchool
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [HttpPost("get-checlkexist")]
-        public async Task<IActionResult> GetCheckExist(JsonElement jsonElement) 
+        public async Task<IActionResult> GetCheckExist(JsonElement jsonElement)
         {
             if (!jsonElement.TryGetProperty("scNames", out JsonElement _scNames)) return BadRequest();
             if (!jsonElement.TryGetProperty("accounts", out JsonElement _accounts)) return BadRequest();
             if (!jsonElement.TryGetProperty("areaIds", out JsonElement _areaIds)) return BadRequest();
             jsonElement.TryGetProperty("schools", out JsonElement schools);
-            
+
             var cosmosClient = _azureCosmos.GetCosmosClient();
             List<CreateSchoolInfo> cSchools = new();
             if (!string.IsNullOrEmpty($"{schools}"))
@@ -1224,10 +1356,10 @@ namespace TEAMModelBI.Controllers.BISchool
                     tmdInfos.ForEach(tmd =>
                     {
                         if (!string.IsNullOrEmpty(tmd.id))
-                            if (tmd.id.Equals($"{ac}")) 
+                            if (tmd.id.Equals($"{ac}"))
                                 noTmdInfo = tmd;
                         if (!string.IsNullOrEmpty(tmd.mobile))
-                            if (tmd.mobile.Equals($"{ac}")) 
+                            if (tmd.mobile.Equals($"{ac}"))
                                 noTmdInfo = tmd;
                         if (!string.IsNullOrEmpty(tmd.mail))
                             if (tmd.mail.Equals($"{ac}"))
@@ -1239,7 +1371,7 @@ namespace TEAMModelBI.Controllers.BISchool
                 });
             }
 
-            if (areaIds.Count > 0) 
+            if (areaIds.Count > 0)
             {
                 foreach (var item in areaIds)
                 {
@@ -1280,7 +1412,7 @@ namespace TEAMModelBI.Controllers.BISchool
             if (existScNames.Count > 0 || noAccounts.Count > 0 || noAreaIds.Count > 0 || synPro.Count > 0)
                 return Ok(new { state = RespondCode.Created, existScNames, noAccounts, noAreaIds, synPro, createScInfo });
             else
-                return Ok(new { state = RespondCode.Ok , createScInfo });
+                return Ok(new { state = RespondCode.Ok, createScInfo });
         }
 
         /// <summary>
@@ -1397,7 +1529,7 @@ namespace TEAMModelBI.Controllers.BISchool
 
             var cosmosClient = _azureCosmos.GetCosmosClient();
 
-            List<string> scIds= _scIds.ToObject<List<string>>();
+            List<string> scIds = _scIds.ToObject<List<string>>();
             List<string> manages = _manages.ToObject<List<string>>();
 
             List<ExistScManage> existScManages = new();
@@ -1441,7 +1573,7 @@ namespace TEAMModelBI.Controllers.BISchool
                                 };
 
                                 var haveTeacher = tempTeacher.schools.Find(x => x.schoolId.Equals($"{scId}"));
-                                if (haveTeacher == null) 
+                                if (haveTeacher == null)
                                 {
                                     School school = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemAsync<School>(scId, new PartitionKey("Base"));
                                     //教师存在,在该教师信息中添加要管理的学校信息
@@ -1466,7 +1598,7 @@ namespace TEAMModelBI.Controllers.BISchool
                                 School scInfo = tchJson.ToObject<School>();
                                 teacher.schools.Add(new() { schoolId = scInfo.id, name = scInfo.name, status = "join", time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), picture = scInfo.picture, areaId = scInfo.areaId });
                             }
-                            
+
                             teacher = await cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<Teacher>(teacher, new PartitionKey("Base"));
                             SchoolTeacher addSchoolTeacher = new()
                             {
@@ -1502,7 +1634,7 @@ namespace TEAMModelBI.Controllers.BISchool
         [ProducesDefaultResponseType]
         [AuthToken(Roles = "admin")]
         [HttpPost("batch-scmanage")]
-        public async Task<IActionResult> BatchScManage(JsonElement jsonElement) 
+        public async Task<IActionResult> BatchScManage(JsonElement jsonElement)
         {
             if (!jsonElement.TryGetProperty("scManages", out JsonElement _scManages)) return BadRequest();
             var cosmosClient = _azureCosmos.GetCosmosClient();
@@ -1683,11 +1815,11 @@ namespace TEAMModelBI.Controllers.BISchool
                             touch = schoolConfig.PresetExam[0].touch
                         },
                         periodType = x.pidType
-                        
+
                     });
                 });
             }
-            else 
+            else
             {
                 periods.Add(new Period
                 {
@@ -1837,7 +1969,7 @@ namespace TEAMModelBI.Controllers.BISchool
         /// </summary>
         public record ExistScManage
         {
-            public string scId{ get; set; }
+            public string scId { get; set; }
             public string tmdId { get; set; }
         }
 
@@ -1845,7 +1977,7 @@ namespace TEAMModelBI.Controllers.BISchool
         /// <summary>
         /// 管理员
         /// </summary>
-        public record ScManages 
+        public record ScManages
         {
             public string id { get; set; }
             public string name { get; set; }