CrazyIter_Bin 4 år sedan
förälder
incheckning
c44fbe8d48

+ 40 - 0
TEAMModelFunction/ClassChangeServiceBus.cs

@@ -0,0 +1,40 @@
+using Microsoft.Azure.WebJobs;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.DI;
+
+namespace TEAMModelFunction
+{
+    public class ClassChangeServiceBus
+    {
+        private readonly AzureCosmosFactory _azureCosmos;
+        private readonly DingDing _dingDing;
+        delegate void DoActivityTarget(string ids,string opt,string no,string source);
+        public ClassChangeServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing)
+        {
+            _azureCosmos = azureCosmos;
+            _dingDing = dingDing;
+        }
+        /// <summary>
+        /// 完善学生名单变更影响的活动
+        /// </summary>
+        /// <data msg>
+        /// "ids":["s111","t111"]//学生
+        /// "opt":"join/leave",//状态
+        /// "no":"CLASS001"//教室编号
+        /// "source":1/2  //学生名单数据来源 1是不同学校的学生账号,2是扫码加入的醍摩豆ID
+        /// </data>
+        /// <param name="msg"></param>
+        /// <returns></returns>
+        [FunctionName("ImproveActivity")]
+        public async Task ImproveActivity([ServiceBusTrigger("active-task", "classchange", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {
+            try {
+                var json = JsonDocument.Parse(msg);
+
+            } catch (Exception e) { }
+        }
+    }
+}

+ 3 - 3
TEAMModelFunction/MonitorCosmosDB.cs

@@ -60,13 +60,13 @@ namespace TEAMModelFunction
                         switch (pk)
                         {
                             case "Exam":
-                                ExamTrigger.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client,input,code,stime,etime,school);
+                                TriggerExam.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client,input,code,stime,etime,school);
                                 break;
                             case "Vote":
-                                VoteTrigger.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
+                                TriggerVote.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
                                 break;
                             case "Survey":
-                                SurveyTrigger.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
+                                TriggerSurvey.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
                                 break;
 
                         }

+ 1 - 1
TEAMModelFunction/ExamTrigger.cs

@@ -12,7 +12,7 @@ using TEAMModelOS.SDK.Models;
 
 namespace TEAMModelFunction
 {
-    public class ExamTrigger
+    public class TriggerExam
     {
         public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing, 
             CosmosClient client, Document input ,string code,long stime,long etime, string school)

+ 38 - 2
TEAMModelFunction/SurveyTrigger.cs

@@ -9,10 +9,11 @@ using System.Threading.Tasks;
 using TEAMModelOS.SDK.DI;
 using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
+using TEAMModelOS.SDK.Models.Cosmos;
 
 namespace TEAMModelFunction
 {
-   public class SurveyTrigger
+   public class TriggerSurvey
     {
         public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
                CosmosClient client, Document input, string code, long stime, long etime, string school)
@@ -50,6 +51,42 @@ namespace TEAMModelFunction
                     }
                     break;
                 case "going":
+                    var tcode = code.Replace("Survey-", "");
+                    ActivityData data;
+                    if (survey.scope == "school" || survey.scope == "teacher")
+                    {
+                        data = new ActivityData
+                        {
+                            id = survey.id,
+                            code = $"Activity-{tcode}",
+                            type = "survey",
+                            name = survey.name,
+                            startTime = survey.startTime,
+                            endTime = survey.endTime,
+                            scode = survey.code,
+                            scope = survey.scope,
+                            classes = survey.classes,
+                            tmdids = survey.tmdids
+                        };
+                        await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
+                    }
+                    else if (survey.scope == "private")
+                    {
+                        data = new ActivityData
+                        {
+                            id = survey.id,
+                            code = $"Activity-Common",
+                            type = "survey",
+                            name = survey.name,
+                            startTime = survey.startTime,
+                            endTime = survey.endTime,
+                            scode = survey.code,
+                            scope = survey.scope,
+                            classes = survey.classes,
+                            // tmdids = survey.tmdids
+                        };
+                        await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
+                    }
                     var messageSurveyEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
                     messageSurveyEnd.ApplicationProperties.Add("name", "Survey");
                     if (changeRecords.Count > 0)
@@ -70,7 +107,6 @@ namespace TEAMModelFunction
                             msgId = messageSurveyEnd.MessageId
                         };
                         await _azureStorage.Save<ChangeRecord>(changeRecord);
-                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
                     }
                     break;
             }

+ 116 - 0
TEAMModelFunction/TriggerVote.cs

@@ -0,0 +1,116 @@
+using Azure.Cosmos;
+using Azure.Messaging.ServiceBus;
+using Microsoft.Azure.Documents;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
+using TEAMModelOS.SDK.Models;
+using TEAMModelOS.SDK.Models.Cosmos;
+
+namespace TEAMModelFunction
+{
+    public static class TriggerVote
+    {
+
+        public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
+            CosmosClient client, Document input, string code, long stime, long etime, string school)
+        {
+            Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
+            List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
+            //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
+            switch (vote.progress)
+            {
+                case "pending":
+                    var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
+                    messageVote.ApplicationProperties.Add("name", "Vote");
+                    if (voteRecords.Count > 0)
+                    {
+                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
+                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
+                        voteRecords[0].sequenceNumber = start;
+                        await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
+                        //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
+                    }
+                    else
+                    {
+                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
+                        ChangeRecord changeRecord = new ChangeRecord
+                        {
+                            RowKey = input.Id,
+                            PartitionKey = "pending",
+                            sequenceNumber = start,
+                            msgId = messageVote.MessageId
+                        };
+                        await _azureStorage.Save<ChangeRecord>(changeRecord);
+                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
+                    }
+                    break;
+                case "going":
+                    var tcode = code.Replace("Vote-", "");
+                    ActivityData data ; 
+                    if (vote.scope == "school" || vote.scope == "teacher")
+                    {
+                        data = new ActivityData
+                        {
+                            id = vote.id,
+                            code = $"Activity-{tcode}",
+                            type = "vote",
+                            name = vote.name,
+                            startTime = vote.startTime,
+                            endTime = vote.endTime,
+                            scode = vote.code,
+                            scope = vote.scope,
+                            classes = vote.classes,
+                            tmdids = vote.tmdids
+                        };
+                        await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
+                    }
+                    else if(vote.scope== "private")
+                    {
+                        data = new ActivityData
+                        {
+                            id = vote.id,
+                            code = $"Activity-Common",
+                            type = "vote",
+                            name = vote.name,
+                            startTime = vote.startTime,
+                            endTime = vote.endTime,
+                            scode = vote.code,
+                            scope = vote.scope,
+                            classes = vote.classes,
+                           // tmdids = vote.tmdids
+                        };
+                        await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
+                    }
+                    var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
+                    messageVoteEnd.ApplicationProperties.Add("name", "Vote");
+                    if (voteRecords.Count > 0)
+                    {
+                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
+                        voteRecords[0].sequenceNumber = end;
+                        await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
+                    }
+                    else
+                    {
+                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
+                        ChangeRecord changeRecord = new ChangeRecord
+                        {
+                            RowKey = input.Id,
+                            PartitionKey = "going",
+                            sequenceNumber = end,
+                            msgId = messageVoteEnd.MessageId
+                        };
+                        await _azureStorage.Save<ChangeRecord>(changeRecord);
+                    }
+                    break;
+                case "finish":
+                    break;
+            }
+        }
+    }
+}

+ 0 - 206
TEAMModelFunction/VoteTrigger.cs

@@ -1,206 +0,0 @@
-using Azure.Cosmos;
-using Azure.Messaging.ServiceBus;
-using Microsoft.Azure.Documents;
-using System;
-using System.Collections.Generic;
-using System.Text;
-using System.Text.Json;
-using System.Threading.Tasks;
-using TEAMModelOS.SDK.DI;
-using TEAMModelOS.SDK.Extension;
-using TEAMModelOS.SDK.Models;
-using TEAMModelOS.SDK.Models.Cosmos;
-
-namespace TEAMModelFunction
-{
-    public static class VoteTrigger
-    {
-
-        public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
-            CosmosClient client, Document input, string code, long stime, long etime, string school)
-        {
-            Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
-            List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
-            //ChangeRecord voteRecord = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ChangeRecord>(input.Id, new Azure.Cosmos.PartitionKey($"{vote.progress}"));
-            switch (vote.progress)
-            {
-                case "pending":
-                    var messageVote = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
-                    messageVote.ApplicationProperties.Add("name", "Vote");
-                    if (voteRecords.Count > 0)
-                    {
-                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
-                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
-                        voteRecords[0].sequenceNumber = start;
-                        await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
-                        //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
-                    }
-                    else
-                    {
-                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVote, DateTimeOffset.FromUnixTimeMilliseconds(stime));
-                        ChangeRecord changeRecord = new ChangeRecord
-                        {
-                            RowKey = input.Id,
-                            PartitionKey = "pending",
-                            sequenceNumber = start,
-                            msgId = messageVote.MessageId
-                        };
-                        await _azureStorage.Save<ChangeRecord>(changeRecord);
-                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
-                    }
-                    break;
-                case "going":
-                     var tcode = code.Replace("Vote-", "");
-                    //处理教师可投票信息初始化
-                    if (vote.teachers != null && vote.scope == "school")
-                    {
-                        List<CommonData> teacherDatas = new List<CommonData>();
-                        vote.teachers.ForEach(x => {
-                            teacherDatas.Add(new CommonData
-                            {
-                                id = vote.id,
-                                code = $"Common-{x}",
-                                type = "vote",
-                                name = vote.name,
-                                startTime = vote.startTime,
-                                endTime = vote.endTime,
-                                scode = vote.code,
-                               // spcode = tcode
-                            });
-                        });
-                        List<Task<ItemResponse<CommonData>>> tasks = new List<Task<ItemResponse<CommonData>>>();
-                        foreach (var data in teacherDatas) {
-                           tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
-                        }
-                      var response= await Task.WhenAll(tasks);
-                    }
-                    //处理学校教室或私人教师可投票信息初始化
-                    if (vote.classes != null) {
-
-                        List<Task<ItemResponse<CommonData>>> tasks = new List<Task<ItemResponse<CommonData>>>();
-                        //学校教室
-                        if (vote.scope == "school")
-                        {
-                            List<CommonData> classDatas = new List<CommonData>();
-                            foreach (var clazz in vote.classes) {
-                                var response = await client.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(clazz, new Azure.Cosmos.PartitionKey($"Class-{tcode}"));
-                                if (response.Status == 200) {
-                                    using var json = await JsonDocument.ParseAsync(response.ContentStream);
-                                    Class clss = json.ToObject<Class>();
-                                    if (clss.students != null && clss.students.Count > 0) {
-                                        clss.students.ForEach(x => {
-                                            classDatas.Add(new CommonData
-                                            {
-                                                id = vote.id,
-                                                code = $"Common-{x.id}",
-                                                type = "vote",
-                                                name = vote.name,
-                                                startTime = vote.startTime,
-                                                endTime = vote.endTime,
-                                                scode = vote.code,
-                                               // spcode = x.scode
-                                            }); 
-                                        });
-                                    }
-                                }
-                            }
-                            foreach (var data in classDatas)
-                            {
-                                tasks.Add(client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
-                            }
-
-                        }
-                        //私人教室
-                        else if (vote.scope == "private") {
-                         
-                            foreach (var clazz in vote.classes)
-                            {
-                                var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(clazz, new Azure.Cosmos.PartitionKey($"Class-{tcode}"));
-                                if (response.Status == 200)
-                                {
-                                    using var json = await JsonDocument.ParseAsync(response.ContentStream);
-                                    Class clss = json.ToObject<Class>();
-                                    //处理私人教室的学生数据来源问题
-                                    //从不同学校的学生账号
-                                    if (clss.source == 1) {
-                                        List<CommonData> classDatas = new List<CommonData>();
-                                        if (clss.students != null && clss.students.Count > 0)
-                                        {
-                                            clss.students.ForEach(x => {
-                                                classDatas.Add(new CommonData
-                                                {
-                                                    id = vote.id,
-                                                    code = $"Common-{x.id}",
-                                                    type = "vote",
-                                                    name = vote.name,
-                                                    startTime = vote.startTime,
-                                                    endTime = vote.endTime,
-                                                    scode = vote.code,
-                                                   // spcode = x.scode
-                                                }); 
-                                            });
-                                            foreach (var data in classDatas)
-                                            {
-                                                tasks.Add(client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
-                                            }
-                                        }
-                                    }
-                                    //从扫码加入的醍摩豆ID
-                                    if (clss.source == 2) {
-                                        List<CommonData> classDatas = new List<CommonData>();
-                                        if (clss.students != null && clss.students.Count > 0)
-                                        {
-                                            clss.students.ForEach(x => {
-                                                classDatas.Add(new CommonData
-                                                {
-                                                    id = vote.id,
-                                                    code = $"Common-{x.id}",
-                                                    type = "vote",
-                                                    name = vote.name,
-                                                    startTime = vote.startTime,
-                                                    endTime = vote.endTime,
-                                                    scode = vote.code,
-                                                    //spcode = x.scode
-                                                }) ; 
-                                            });
-                                            foreach (var data in classDatas)
-                                            {
-                                                tasks.Add(client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync<CommonData>(data, new Azure.Cosmos.PartitionKey(data.code)));
-                                            }
-                                        }
-                                    }
-                                }
-                            }
-                        }
-                        var task =  await Task.WhenAll(tasks);
-                    }
-                    var messageVoteEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
-                    messageVoteEnd.ApplicationProperties.Add("name", "Vote");
-                    if (voteRecords.Count > 0)
-                    {
-                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
-                        await _serviceBus.GetServiceBusClient().cancelMessage("active-task", voteRecords[0].sequenceNumber);
-                        voteRecords[0].sequenceNumber = end;
-                        await _azureStorage.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
-                        //await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(voteRecord, voteRecord.id, new Azure.Cosmos.PartitionKey($"{voteRecord.code}"));
-                    }
-                    else
-                    {
-                        long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
-                        ChangeRecord changeRecord = new ChangeRecord
-                        {
-                            RowKey = input.Id,
-                            PartitionKey = "going",
-                            sequenceNumber = end,
-                            msgId = messageVoteEnd.MessageId
-                        };
-                        await _azureStorage.Save<ChangeRecord>(changeRecord);
-                        //await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(changeRecord, new Azure.Cosmos.PartitionKey($"{changeRecord.code}"));
-                    }
-                    break;
-                case "finish":
-                    break;
-            }
-        }
-    }
-}

+ 70 - 0
TEAMModelOS.SDK/Models/Cosmos/Common/ActivityData.cs

@@ -0,0 +1,70 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Models.Cosmos
+{
+
+    /// <summary>
+    /// 
+    /// [
+    //      Activity|-----问卷调查
+    //        |-----投票活动
+    //        |-----评测活动
+    //        |-----作业活动
+    //        |-----学习活动
+    //]
+    //[
+    //    {
+    //        "type": "vote",//"vote"/"exam"/"homework"/"learn"/"survey"
+    //        "name": "第一届紫藤小学人气教师投票",
+    //        "startTime": 1608274766154,
+    //        "endTime": 1608912000000,
+    //        "scode": "Vote-hbcn",
+    //        "id": "aaaaaaaaaaaaaaaaaaa",
+    //        "code": "Activity-hbcn",
+    //        "pk": "Activity",
+    //        "scope":"private/school/teacher",
+    //        "classes":["S-C-00001","S-C-00002","S-C-00003","S-C-00004"],
+    //        "tmdids":["TMDID0001","TMDID0002","TMDID0003","TMDID0004",]//只有问卷调查和投票活动设置,且分区键必须为学校的
+    //},
+    //    {
+    //        "type": "vote",//"vote"/"exam"/"homework"/"learn"/"survey"
+    //        "name": "第一届醍摩豆杯人气教师投票",
+    //        "startTime": 1608274766154,
+    //        "endTime": 1608912000000,
+    //        "scode": "Vote-TMDID0001",
+    //        "id": "bbbbbbbbbbbbbbbbbbbbb",
+    //        "code": "Activity-Common",//教师个人发布的活动统一使用Common,用班级子查询
+    //        "pk": "Activity",
+    //        "scope":"private/school/teacher",
+    //        "classes":["P-C-00004","S-C-00001"],//如果是醍摩豆ID则直接搜寻加入的私人教师编码,如果是校内学生则需要多搜寻一次。
+    //        "tmdids":[]
+    //    }
+    //]
+    /// </summary>
+    public class ActivityData : CosmosEntity
+    {
+        public ActivityData() {
+            pk = "Activity";
+        }
+        /// <summary>
+        /// 业务类型  vote投票 survey问卷 exam评测 learn学习活动 homework作业活动
+        /// </summary>
+        public string type { get; set; }
+        public string name { get; set; }
+        public long  startTime { get; set; }
+        public long endTime { get; set; }
+        /// <summary>
+        /// 活动的分区键 Vote-hbcn/Vote-1606294378
+        /// </summary>
+        public string scode { get; set; }
+        /// <summary>
+        /// private/school/teacher
+        /// </summary>
+        public string scope { get; set; }
+        public List<string> classes { get; set; }
+        public List<string> tmdids { get; set; }
+
+    }
+}

+ 0 - 29
TEAMModelOS.SDK/Models/Cosmos/Common/CommonData.cs

@@ -1,29 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-
-namespace TEAMModelOS.SDK.Models.Cosmos
-{
-    public class CommonData : CosmosEntity
-    {
-        public CommonData() {
-            pk = "Common";
-        }
-        /// <summary>
-        /// 业务类型  vote投票 survey问卷 exam评测 learn学习活动 homework作业活动
-        /// </summary>
-        public string type { get; set; }
-        public string name { get; set; }
-        public long  startTime { get; set; }
-        public long endTime { get; set; }
-        /// <summary>
-        /// 活动的分区键 Vote-hbcn/Vote-1606294378
-        /// </summary>
-        public string scode { get; set; }
-        /// <summary>
-        /// 学校分区键或私人教室的分区键 hbcn/1606294378
-        /// </summary>
-       // public string spcode { get; set; }
-
-    }
-}

+ 1 - 1
TEAMModelOS.SDK/Models/Cosmos/Common/Vote.cs

@@ -55,7 +55,7 @@ namespace TEAMModelOS.SDK.Models
         /// <summary>
         /// 参与投票的教师醍摩豆id
         /// </summary>
-        public List<string> teachers { get; set; }
+        public List<string> tmdids { get; set; }
         /// <summary>
         ///  scope 为school时 是学校的班级  为private 时是私人班级
         /// </summary>

+ 3 - 3
TEAMModelOS.SDK/Models/Cosmos/School/Survey.cs

@@ -16,7 +16,7 @@ namespace TEAMModelOS.SDK.Models
         public Survey() {
             pk = "Survey";
             questions = new List<Questions>();
-            classes = new List<Classes>();
+            classes = new List<string>();
         }
        
         public string name { get; set; }   //测试问卷名称', // 问卷名称
@@ -51,8 +51,8 @@ namespace TEAMModelOS.SDK.Models
         /// </summary>
         public long endTime { get; set; }
         public long createTime { get; set; } // 问卷发布时间
-        public List<Classes> classes { get; set; }
-
+        public List<string> classes { get; set; }
+        public List<string> tmdids { get; set; }
         //public long sequenceNumber { get; set; }
 
         public string url { get; set; }

+ 21 - 8
TEAMModelOS/Controllers/Common/VoteController.cs

@@ -200,7 +200,7 @@ namespace TEAMModelOS.Controllers.Learn
         [Authorize(Roles = "teacher")]
         public async Task<IActionResult> FindTch(JsonElement requert)
         {
-            (List<CommonData> datas, string continuationToken) = await FindByRole("Teacher", requert);
+            (List<ActivityData> datas, string continuationToken) = await FindByRole("Teacher", requert);
             return Ok(new { datas, continuationToken });
         }
 
@@ -223,11 +223,11 @@ namespace TEAMModelOS.Controllers.Learn
         [Authorize(Roles = "student")]
         public async Task<IActionResult> FindStu(JsonElement requert)
         {
-            (List<CommonData> datas, string continuationToken)  =await  FindByRole("Student", requert);
+            (List<ActivityData> datas, string continuationToken)  =await  FindByRole("Student", requert);
             return Ok(new { datas, continuationToken });
         }
 
-        public async Task<(List<CommonData> datas, string continuationToken)> FindByRole(string containerId, JsonElement requert) {
+        public async Task<(List<ActivityData> datas, string continuationToken)> FindByRole(string containerId, JsonElement requert) {
             //开始时间,默认最近三十天
             var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
             if (!requert.TryGetProperty("stime", out JsonElement stime))
@@ -276,7 +276,7 @@ namespace TEAMModelOS.Controllers.Learn
                     iscontinuation = true;
                 }
             };
-            List<CommonData> datas = new List<CommonData>();
+            List<ActivityData> datas = new List<ActivityData>();
             var query = $"select value(c) from c where c.createTime >= {stimestamp} and c.createTime <= {etimestamp} ";
             var client = _azureCosmos.GetCosmosClient();
             var (id, _, _, _) = HttpContext.GetAuthTokenInfo();
@@ -287,7 +287,7 @@ namespace TEAMModelOS.Controllers.Learn
                 {
                     foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
                     {
-                        datas.Add(obj.ToObject<CommonData>());
+                        datas.Add(obj.ToObject<ActivityData>());
                     }
                     //如果需要分页则跳出
                     if (iscontinuation)
@@ -369,15 +369,28 @@ namespace TEAMModelOS.Controllers.Learn
                 return BadRequest();
             }
         }
+
         /// <summary>
-        /// 学生投票
+        /// 投票
         /// </summary>
+        /// <data>
+        /// !"id":"aaaa"
+        /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
+        /// !"option":"A/B/C..1/2/3..."
+        /// </data>
+        /// <redis>
+        /// 投票活动选项计数器 数据集合 key:"Vote:Count:AAA",value:"A",score:1
+        /// </redis>
         /// <param name="request"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
-        [HttpPost("upsert-record")]
-        public async Task<IActionResult> UpsertRecord(List<VoteRecord> request)
+        [HttpPost("decide")]
+        public async Task<IActionResult> UpsertRecord(JsonElement request)
         {
+            //1.再次检查投票
+
+            //2.处理投票
+            
             return Ok(request);
         }
     }

+ 4 - 2
TEAMModelOS/Controllers/School/StudentCommonController.cs

@@ -30,10 +30,11 @@ namespace TEAMModelOS.Controllers
         /// 查询投票活动,用于教师投票的列表
         /// </summary>
         /// <data>
-        ///Common-学生/教师编码 对象分区       !"code":"habook01"/"1606285227" 
+        ///加入的班级信息                      ?classes:[{"classid":"S-C-00001","scope":"school"},{"classid":"P-C-00004","scope":"private"}]
         ///活动类型                            ?"type":"vote"/"exam"/"homework"/"learn"/"survey"" // vote投票 survey问卷 exam评测 learn学习活动 homework作业活动
         ///时间筛选范围开始时间 默认30天之前   ?"stime":1608274766154  
         ///时间筛选范围结束时间 默认当前时间   ?"etime":1608274766666 
+        ///是否展示列表的 Tips                 ? true/false
         ///每页大小     ?"count":10/null/Undefined  
         ///分页Token    ?"continuationToken":Undefined/null/"[{\"token\":\"+RID:~omxMAP3ipcSEEwAAAAAAAA==#RT:2#TRC:20#ISV:2#IEO:65551#QCF:1#FPC:AYQTAAAAAAAAiRMAAAAAAAA=\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}]"
         /// </data>
@@ -45,7 +46,8 @@ namespace TEAMModelOS.Controllers
         public async Task<IActionResult> FindTch(JsonElement requert)
         {
             var (id, _, _, _) = HttpContext.GetAuthTokenInfo();
-            (List<CommonData> datas, string continuationToken) = await ActivityService.FindByRole("Student", requert, id, _azureCosmos);
+
+            (List<ActivityData> datas, string continuationToken) = await ActivityService.FindByRole("School", requert, id, _azureCosmos);
             return Ok(new { datas, continuationToken });
         }
     }

+ 3 - 2
TEAMModelOS/Controllers/Teacher/TeacherCommonController.cs

@@ -32,10 +32,11 @@ namespace TEAMModelOS.Controllers
         /// 查询投票活动,用于教师投票的列表
         /// </summary>
         /// <data>
-        ///Common-学生/教师编码 对象分区       !"code":"habook01"/"1606285227" 
+        ///加入的班级信息                      ?classes:[{"classid":"S-C-00001","scope":"school"},{"classid":"P-C-00004","scope":"private"}]
         ///活动类型                            ?"type":"vote"/"exam"/"homework"/"learn"/"survey"" // vote投票 survey问卷 exam评测 learn学习活动 homework作业活动
         ///时间筛选范围开始时间 默认30天之前   ?"stime":1608274766154  
         ///时间筛选范围结束时间 默认当前时间   ?"etime":1608274766666 
+        ///是否展示列表的 Tips                 ? "tips":true/false
         ///每页大小     ?"count":10/null/Undefined  
         ///分页Token    ?"continuationToken":Undefined/null/"[{\"token\":\"+RID:~omxMAP3ipcSEEwAAAAAAAA==#RT:2#TRC:20#ISV:2#IEO:65551#QCF:1#FPC:AYQTAAAAAAAAiRMAAAAAAAA=\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}]"
         /// </data>
@@ -47,7 +48,7 @@ namespace TEAMModelOS.Controllers
         public async Task<IActionResult> FindTch(JsonElement requert)
         {
             var (id, _, _, _) = HttpContext.GetAuthTokenInfo();
-            (List<CommonData> datas, string continuationToken) = await ActivityService.FindByRole("Teacher", requert, id, _azureCosmos);
+            (List<ActivityData> datas, string continuationToken) = await ActivityService.FindByRole("Teacher", requert, id, _azureCosmos);
             return Ok(new { datas, continuationToken });
         }
     }

+ 126 - 21
TEAMModelOS/Services/Common/ActivityService.cs

@@ -7,12 +7,22 @@ using TEAMModelOS.SDK.Models.Cosmos;
 using TEAMModelOS.SDK.Extension;
 using Azure.Cosmos;
 using TEAMModelOS.SDK.DI;
+using HTEXLib.COMM.Helpers;
+using System.Text;
+using TEAMModelOS.SDK.Models;
 
 namespace TEAMModelOS.Services.Common
 {
     public static class ActivityService
     {
-        public static async Task<(List<CommonData> datas, string continuationToken)> FindByRole(string containerId, JsonElement requert,string id, AzureCosmosFactory _azureCosmos)
+
+        /// <summary>
+        /// 活动委托
+        /// </summary>
+        /// <param name="data"></param>
+        /// <returns></returns>
+        delegate dynamic DoActivityTips(ActivityData data, AzureCosmosFactory _azureCosmos,string id);
+        public static async Task<(List<ActivityData> datas, string continuationToken)> FindByRole(string containerId, JsonElement requert, string id, AzureCosmosFactory _azureCosmos)
         {
             //开始时间,默认最近三十天
             var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
@@ -21,25 +31,25 @@ namespace TEAMModelOS.Services.Common
                 if (stime.TryGetInt64(out long data))
                 {
                     stimestamp = data;
-                };
-            };
+                } 
+            } 
             if (!requert.TryGetProperty("type", out JsonElement type))
             {
                 if (stime.TryGetInt64(out long data))
                 {
                     stimestamp = data;
-                };
-            };
-            //默认当前时间
+                } 
+            } 
+            //默认当前时间,  未开始的不能查询
             var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
-            if (!requert.TryGetProperty("etime", out JsonElement etime))
-            {
-                if (etime.TryGetInt64(out long data))
-                {
-                    etimestamp = data;
-                };
+            //if (!requert.TryGetProperty("etime", out JsonElement etime))
+            //{
+            //    if (etime.TryGetInt64(out long data))
+            //    {
+            //        etimestamp = data;
+            //    };
 
-            };
+            //};
             string continuationToken = null;
             //默认不指定返回大小
             int? topcout = null;
@@ -48,24 +58,45 @@ namespace TEAMModelOS.Services.Common
                 if (jcount.TryGetInt32(out int data))
                 {
                     topcout = data;
-                };
-            };
+                } 
+            } 
             //是否需要进行分页查询,默认不分页
             bool iscontinuation = false;
             //如果指定了返回大小
             if (!requert.TryGetProperty("continuationToken", out JsonElement continuation))
             {
                 //指定了cancellationToken 表示需要进行分页
-                if (!continuation.ValueKind.Equals(JsonValueKind.Null) && !continuation.ValueKind.Equals(JsonValueKind.String))
+                if (!continuation.ValueKind.Equals(JsonValueKind.Null) && continuation.ValueKind.Equals(JsonValueKind.String))
                 {
                     continuationToken = continuation.GetString();
                     iscontinuation = true;
                 }
-            };
-            List<CommonData> datas = new List<CommonData>();
-            var query = $"select value(c) from c where c.createTime >= {stimestamp} and c.createTime <= {etimestamp} ";
+            } 
+            //班级
+            List<string> classes=null;
+            if (!requert.TryGetProperty("classes", out JsonElement jclasses))
+            {
+                if (jclasses.ValueKind is JsonValueKind.Array ) {
+                    classes = jclasses.ToObject<List<string>>();
+                }
+            } 
+            string query = null;
+            if (classes.IsNotEmpty() )
+            {
+                StringBuilder insql =new StringBuilder();
+                classes.ForEach( x => { insql.Append($"'{x}',"); });
+                string sql = insql.ToString();
+                sql = sql.ToString().Substring(0, sql.Length - 2);
+                query = $" SELECT distinct  value c   FROM c   JOIN A0 IN c.classes join A1 in c.tmdids   where c.startTime >= {stimestamp} and c.startTime <= {etimestamp} and  c.pk='Activity'  and   (A0 in('{sql}') or A1 in ('{id}')) ";
+            }
+            else {
+                query = $" SELECT distinct  value c   FROM c    join A1 in c.tmdids   where c.startTime >= {stimestamp} and c.startTime <= {etimestamp}  and c.pk='Activity'  and   A1 ='{id}' ";
+            }
+            
+            List<ActivityData> datas = new List<ActivityData>();
+            
+            
             var client = _azureCosmos.GetCosmosClient();
-           
             await foreach (var item in client.GetContainer("TEAMModelOS", containerId).GetItemQueryStreamIterator(query, continuationToken: continuationToken, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Common-{id}") }))
             {
                 using var json = await JsonDocument.ParseAsync(item.ContentStream);
@@ -73,7 +104,7 @@ namespace TEAMModelOS.Services.Common
                 {
                     foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
                     {
-                        datas.Add(obj.ToObject<CommonData>());
+                        datas.Add(obj.ToObject<ActivityData>());
                     }
                     //如果需要分页则跳出
                     if (iscontinuation)
@@ -83,7 +114,81 @@ namespace TEAMModelOS.Services.Common
                     }
                 }
             }
+            bool tips = false;
+            if (!requert.TryGetProperty("tips", out JsonElement jtips))
+            {
+                //指定了cancellationToken 表示需要进行分页
+                if (!jtips.ValueKind.Equals(JsonValueKind.Null) && !jtips.ValueKind.Equals(JsonValueKind.True))
+                {
+                    tips = jtips.GetBoolean();
+                }
+            }
+            DoActivityTips activityTips;
+            dynamic res = default;
+            foreach (var data in datas)
+            {
+                switch (data.type)
+                {
+                        //投票
+                    case "vote":
+                        activityTips = DoVoteTips;
+                        res = activityTips(data,_azureCosmos,  id );
+                        break;
+                        //问卷
+                    case "survey":
+                        activityTips = DoSurveyTips;
+                        res = activityTips(data, _azureCosmos,id);
+                        break;
+                        //评测
+                    case "exam":
+                        activityTips = DoExamTips;
+                        res = activityTips(data, _azureCosmos, id);
+                        break;
+                        //学习活动
+                    case "learn":
+                        activityTips = DoLearnTips;
+                        res = activityTips(data, _azureCosmos, id);
+                        break;
+                        //作业活动
+                    case "homework":
+                        activityTips = DoHomeworkTips;
+                        res = activityTips(data, _azureCosmos, id);
+                        break;
+                    default: break;
+                }
+            }
             return (datas, continuationToken);
         }
+        private async static Task<dynamic> DoVoteTips(ActivityData commonData , AzureCosmosFactory _azureCosmos,string id )
+        {
+            Vote vote=null;
+            var client = _azureCosmos.GetCosmosClient();
+            RedisHelper.HGet($"Vote:{id}","");
+            await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Vote>($"select value c from c where c.id='{commonData.id}'", 
+                requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(commonData.scode) }))
+            {
+                vote = item;
+                break;
+            }
+            
+            return null;
+        }
+        private static dynamic DoHomeworkTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
+        {
+            return null;
+        }
+        private static dynamic DoLearnTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
+        {
+            return null;
+        }
+        private static dynamic DoExamTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
+        {
+            return null;
+        }
+        private static dynamic DoSurveyTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
+        {
+            return null;
+        }
+       
     }
 }