Browse Source

首页统计接口

CrazyIter_Bin 3 years ago
parent
commit
e341b11041

+ 2 - 1
TEAMModelOS.SDK/Models/Cosmos/Common/MQActivity.cs

@@ -3,7 +3,7 @@ using System.Collections.Generic;
 using System.Text;
 using TEAMModelOS.SDK.Models;
 
-namespace TEAMModelFunction
+namespace TEAMModelOS.SDK.Models
 {
     public class MQActivity :CosmosEntity
     {
@@ -12,6 +12,7 @@ namespace TEAMModelFunction
        // public string code { get; set; }
         public string owner { get; set; }
         public List<string> classes { get; set; }
+        public List<string> stuLists { get; set; }
         //如果已经完成则不写入???
         public string progress { get; set; }
         public string scope { get; set; }

+ 2 - 2
TEAMModelOS.SDK/Models/Service/StuListService.cs

@@ -20,7 +20,7 @@ namespace TEAMModelFunction
             
             try
             {
-                var query = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c  where  c.pk='{type}' " +
+                var query = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c  where  c.pk='{type}' " +
                     $" and (( array_contains(c.classes,'{stuListChange.listid}')) or ( array_contains(c.teachclasses,'{stuListChange.listid}')))";
                     //$"and A1 in('{stuListChange.listid}') ";
                 List<MQActivity> datas = new List<MQActivity>();
@@ -38,7 +38,7 @@ namespace TEAMModelFunction
                         teachers.Add(item);
                     }
                     foreach (var techer in teachers) {
-                        var queryTech = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c " +
+                        var queryTech = $"SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c " +
                             $" where c.school='{stuListChange.school}'   and   c.pk='{type}'" +
                             $" and (( array_contains(c.classes,'{stuListChange.listid}')) or ( array_contains(c.teachclasses,'{stuListChange.listid}')))";
                       //  $" and A1 in('{stuListChange.listid}') ";

+ 111 - 106
TEAMModelOS/Controllers/Teacher/CommentController.cs

@@ -19,6 +19,7 @@ using System.Text;
 using Azure.Messaging.ServiceBus;
 using Microsoft.Extensions.Configuration;
 using System.Linq;
+using TEAMModelOS.Filter;
 
 namespace TEAMModelOS.Controllers
 {
@@ -252,110 +253,103 @@ namespace TEAMModelOS.Controllers
 
         [ProducesDefaultResponseType]
         [HttpPost("tch-activity-count")]
-        //[AuthToken(Roles = "teacher")]
+        [AuthToken(Roles = "admin,teacher")]
         public async Task<IActionResult> TchActivityCount(JsonElement requert) {
             var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
-            if (string.IsNullOrWhiteSpace(school))
+            var client = _azureCosmos.GetCosmosClient();
+            HashSet<string> classes = new HashSet<string>();
+            HashSet<string> stulist = new HashSet<string>();
+            //获取学校的名单:
+            List<Schedule> schedules = new List<Schedule>();
+            string scheduleSql = $"SELECT   c.schedule  FROM c join scdl in c.schedule where c.pk='Course' and scdl.teacherId='{id}'";
+            await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<Course>(scheduleSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
             {
-                if (requert.TryGetProperty("school", out JsonElement schoolcode))
-                {
-                    if (!schoolcode.ValueKind.Equals(JsonValueKind.Undefined) && !schoolcode.ValueKind.Equals(JsonValueKind.Null) && schoolcode.ValueKind.Equals(JsonValueKind.String))
-                    {
-                        school = schoolcode.GetString();
-                    }
-                }
+                schedules.AddRange(item.schedule);
             }
-            if (string.IsNullOrWhiteSpace(id))
-            {
-                if (requert.TryGetProperty("userid", out JsonElement userid))
+            schedules.ForEach(x => {
+                if (!string.IsNullOrEmpty(x.classId)) {
+                    classes.Add(x.classId);
+                }
+                if (!string.IsNullOrEmpty(x.stulist))
                 {
-                    if (!userid.ValueKind.Equals(JsonValueKind.Undefined) && !userid.ValueKind.Equals(JsonValueKind.Null) && userid.ValueKind.Equals(JsonValueKind.String))
-                    {
-                        id = userid.GetString();
-                    }
+                    stulist.Add(x.stulist);
                 }
-            }
-            var pksql = "";
-            string type = null;
-            if (requert.TryGetProperty("pk", out JsonElement pk))
-            {
-                if (!pk.ValueKind.Equals(JsonValueKind.Undefined) && !pk.ValueKind.Equals(JsonValueKind.Null) && pk.ValueKind.Equals(JsonValueKind.String))
+            });
+            List<string> types = new List<string> { "Exam", "Survey" ,"Vote"};
+            List<MQActivity> datas = new List<MQActivity>();
+            List<KeyValuePair<string, int>> count = new List<KeyValuePair<string, int>>();
+            foreach (var type in types) {
+                string pksql = $"  c.pk='{type}' ";
+                string joinSqlClasses = "";
+                string andSqlClasses = "";
+                if (classes.ToList().IsNotEmpty())
                 {
-                    pksql = $"  c.pk='{pk}' ";
-                    type = $"{pk}";
+                    joinSqlClasses = " join A1 in c.classes ";
+                    List<string> sqlList = new List<string>();
+                    classes.ToList().ForEach(x => { sqlList.Add($" '{x}' "); });
+                    string sql = string.Join(" , ", sqlList);
+                    andSqlClasses = $"   A1 in ({sql}) ";
                 }
-            }
-            if (string.IsNullOrEmpty(type)) { return BadRequest("type is required!"); }
-            
-            //默认不指定返回大小
-            int? topcout = null;
-            if (requert.TryGetProperty("count", out JsonElement jcount))
-            {
-                if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
+                string joinSqlStulist = "";
+                string andSqlStulist = "";
+                if (stulist.ToList().IsNotEmpty())
                 {
-                    topcout = data;
+                    joinSqlStulist = " join A1 in c.stuLists ";
+                    List<string> sqlList = new List<string>();
+                    stulist.ToList().ForEach(x => { sqlList.Add($" '{x}' "); });
+                    string sql = string.Join(" , ", sqlList);
+                    andSqlStulist = $"   A1 in ({sql}) ";
                 }
-            }
-         
-            
-            //班级
-            string joinSqlClasses = "";
-            string andSqlClasses = "";
-            List<string> classes = null;
-            if (requert.TryGetProperty("classes", out JsonElement jclasses))
-            {
-                if (jclasses.ValueKind is JsonValueKind.Array)
+                string classesSql = "";
+                if (!string.IsNullOrWhiteSpace(joinSqlClasses))
                 {
-                    classes = jclasses.ToObject<List<string>>();
-                    if (classes.IsNotEmpty())
+                    classesSql = $" and    {andSqlClasses }  ";
+                }
+                int acount = 0;
+                if (!string.IsNullOrWhiteSpace(school) && classes.ToList().IsNotEmpty()) {
+                    string querySchool = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c {joinSqlClasses}   where   {pksql}    {classesSql}";
+                    //查询数据归属学校的
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(querySchool, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{school}") }))
                     {
-                        joinSqlClasses = " join A1 in c.classes ";
-                        List<string> sqlList = new List<string>();
-                        classes.ForEach(x => { sqlList.Add($" '{x}' "); });
-                        string sql = string.Join(" , ", sqlList);
-                        andSqlClasses = $"   A1 in ({sql}) ";
+                        if (item.progress.Equals("going", StringComparison.OrdinalIgnoreCase)) {
+                            datas.Add(item);
+                        }
+                        acount += 1;
                     }
                 }
-            }
-            string classesSql = "";
-            if (!string.IsNullOrWhiteSpace(joinSqlClasses))
-            {
-                classesSql = $" and    {andSqlClasses }  ";
-            }
-            List<JsonElement> datas = new List<JsonElement>();
-            var client = _azureCosmos.GetCosmosClient();
-            //班主任 ,任课教师只需要查询两种校园活动 和班级活动 ,  不查询私人教室创建的活动。  
-            if (!string.IsNullOrWhiteSpace(school) && classes.IsNotEmpty())
-            {
-                string querySchool = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c {joinSqlClasses}   where   {pksql}    {classesSql}";
-                //查询数据归属学校的
-                await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(querySchool,  requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{school}") }))
+                string stuListsSql = "";
+                if (!string.IsNullOrWhiteSpace(joinSqlStulist))
                 {
-                    using var json = await JsonDocument.ParseAsync(item.ContentStream);
-                    if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                    stuListsSql = $" and    {andSqlStulist }  ";
+                }
+               
+                if (!string.IsNullOrWhiteSpace(school) && stulist.ToList().IsNotEmpty())
+                {
+                    string querySchool = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c {joinSqlStulist}   where   {pksql}    {stuListsSql}";
+                    //查询数据归属学校的
+                    await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(querySchool, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{school}") }))
                     {
-                        foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                        if (item.progress.Equals("going", StringComparison.OrdinalIgnoreCase))
                         {
-                            datas.Add(obj.ToObject<JsonElement>());
+                            datas.Add(item);
                         }
-                        
+                        acount += 1;
                     }
                 }
-            }
-            string queryTeacher = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime  FROM c   where   {pksql}  ";
-            //查询数据归属学校的
-            await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryTeacher,  requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{id}") }))
-            {
-                using var json = await JsonDocument.ParseAsync(item.ContentStream);
-                if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                string queryTeacher = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime  FROM c   where   {pksql}  ";
+                //查询数据归属个人的
+                await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<MQActivity>(queryTeacher, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{id}") }))
                 {
-                    foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                    acount += 1;
+                    if (item.progress.Equals("going", StringComparison.OrdinalIgnoreCase))
                     {
-                        datas.Add(obj.ToObject<JsonElement>());
+                        datas.Add(item);
                     }
                 }
+                KeyValuePair<string,int> valuePair = new KeyValuePair<string, int>(type, acount);
+                count.Add(valuePair);
             }
-            return Ok();
+            return Ok(new { totalCount=count ,goingDatas=datas});
         }
 
 
@@ -462,6 +456,9 @@ namespace TEAMModelOS.Controllers
             //班级
             string joinSqlClasses = "";
             string andSqlClasses = "";
+
+            string joinSqlStulist = "";
+            string andSqlStulist = "";
             List<string> classes = null;
             if (requert.TryGetProperty("classes", out JsonElement jclasses))
             {
@@ -469,49 +466,57 @@ namespace TEAMModelOS.Controllers
                 {
                     classes = jclasses.ToObject<List<string>>();
                     if (classes.IsNotEmpty())
-                    {
+                    { 
+                        //行政班
                         joinSqlClasses = " join A1 in c.classes ";
-                        List<string> sqlList = new List<string>();
-                        classes.ForEach(x => { sqlList.Add($" '{x}' "); });
-                        string sql = string.Join(" , ", sqlList);
-                        andSqlClasses = $"   A1 in ({sql}) ";
+                        List<string> sqlListc = new List<string>();
+                        classes.ForEach(x => { sqlListc.Add($" '{x}' "); });
+                        string sqlc = string.Join(" , ", sqlListc);
+                        andSqlClasses = $"   A1 in ({sqlc}) ";
+                        //教学班
+                        joinSqlStulist = " join A1 in c.stuLists ";
+                        List<string> sqlListl = new List<string>();
+                        classes.ForEach(x => { sqlListl.Add($" '{x}' "); });
+                        string sqll = string.Join(" , ", sqlListl);
+                        andSqlStulist = $"   A1 in ({sqll}) ";
                     }
                 }
             }
+
+          
             string classesSql = "";
             if (!string.IsNullOrWhiteSpace(joinSqlClasses))
             {
                 classesSql = $" and    {andSqlClasses }  ";
             }
-
-            //科目
-            //string joinSqlSubjects = "";
-            //string andSqlSubjects = "";
-            //if (requert.TryGetProperty("subjects", out JsonElement jsubjects))
-            //{
-            //    if (jsubjects.ValueKind is JsonValueKind.Array)
-            //    {
-            //        List<string> subjects = jsubjects.ToObject<List<string>>();
-            //        if (subjects.IsNotEmpty())
-            //        {
-            //            joinSqlSubjects = " join A2 in c.subjects ";
-            //            List<string> sqlList = new List<string>();
-            //            subjects.ForEach(x => { sqlList.Add($" '{x}' "); });
-            //            string sql = string.Join(" , ", sqlList);
-            //            andSqlSubjects = $" and A2 in ({sql}) ";
-            //        }
-            //    }
-            //}
-
+            
             List<JsonElement> datas = new List<JsonElement>();
             var client = _azureCosmos.GetCosmosClient();
             //班主任 ,任课教师只需要查询两种校园活动 和班级活动 ,  不查询私人教室创建的活动。  
             if (!string.IsNullOrWhiteSpace(school) && classes.IsNotEmpty())
             {
                 //string querySchool = $" SELECT distinct  value c   FROM c {joinSqlClasses} {joinSqlSubjects}  where {stimesql}  {etimesql}  {progresssql}  {typesql}  {andSqlSubjects}  {tgSql}";
-                string querySchool = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c {joinSqlClasses}   where   {pksql}    {classesSql}";
+                string querySchoolclss = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c {joinSqlClasses}   where   {pksql}    {classesSql}";
                 //查询数据归属学校的
-                await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(querySchool, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{school}") }))
+                await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(querySchoolclss, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{school}") }))
+                {
+                    using var json = await JsonDocument.ParseAsync(item.ContentStream);
+                    if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
+                    {
+                        foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
+                        {
+                            datas.Add(obj.ToObject<JsonElement>());
+                        }
+                        //如果需要分页则跳出
+                        if (iscontinuation)
+                        {
+                            continuationTokenSchool = item.GetContinuationToken();
+                            break;
+                        }
+                    }
+                }
+                string querySchoollist = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime   FROM c {joinSqlStulist}   where   {pksql}    {andSqlStulist}";
+                await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(querySchoollist, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{school}") }))
                 {
                     using var json = await JsonDocument.ParseAsync(item.ContentStream);
                     if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)