Selaa lähdekoodia

研修活动评测简易活动流程补充

zhouj1203@hotmail.com 3 vuotta sitten
vanhempi
commit
c3a369aba0

+ 48 - 0
TEAMModelFunction/MonitorServicesBus.cs

@@ -153,6 +153,54 @@ namespace TEAMModelFunction
                 await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Homework()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
             }
 
+        }
+        [FunctionName("Study")]
+        public async Task Study([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "study", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
+        {
+            try
+            {
+                var jsonMsg = JsonDocument.Parse(msg);
+                jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
+                jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
+                jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
+                var client = _azureCosmos.GetCosmosClient();
+                Study study = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Study>(id.ToString(), new PartitionKey($"{code}"));
+                study.progress = progress.ToString();
+                await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(study, id.ToString(), new PartitionKey($"{code}"));
+            }
+            catch (CosmosException)
+            {
+
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Study()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
+            }
+
+        }
+        [FunctionName("ExamLite")]
+        public async Task ExamLite([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "examlite", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
+        {
+            try
+            {
+                var jsonMsg = JsonDocument.Parse(msg);
+                jsonMsg.RootElement.TryGetProperty("id", out JsonElement id);
+                jsonMsg.RootElement.TryGetProperty("progress", out JsonElement progress);
+                jsonMsg.RootElement.TryGetProperty("code", out JsonElement code);
+                var client = _azureCosmos.GetCosmosClient();
+                ExamLite lite = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamLite>(id.ToString(), new PartitionKey($"{code}"));
+                lite.progress = progress.ToString();
+                await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(lite, id.ToString(), new PartitionKey($"{code}"));
+            }
+            catch (CosmosException)
+            {
+
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,ExamLite()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
+            }
+
         }
         [FunctionName("Blob")]
         public async Task Blob([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "blob", Connection = "Azure:ServiceBus:ConnectionString")] string msg) {

+ 81 - 28
TEAMModelFunction/TriggerExamLite.cs

@@ -1,4 +1,5 @@
 using Azure.Cosmos;
+using Azure.Messaging.ServiceBus;
 using HTEXLib.COMM.Helpers;
 using Microsoft.Azure.Documents;
 using System;
@@ -6,6 +7,7 @@ using System.Collections.Generic;
 using System.Text;
 using TEAMModelOS.SDK;
 using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Models.Cosmos.Common;
 
@@ -37,39 +39,90 @@ namespace TEAMModelFunction
                 }
                 await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修评测活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
                 ExamLite lite = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ExamLite>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
-                //List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
+                List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", lite.progress } });
                 if (lite != null)
                 {
-                    (List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, lite.tchLists, lite.school);
-                    List<StuActivity> tchActivities = new List<StuActivity>();
-
-                    if (tchList.IsNotEmpty())
+                    switch (lite.progress)
                     {
-                        tchList.ForEach(x =>
-                        {
-                            tchActivities.Add(new StuActivity
+                        case "pending":
+                            var messageWork = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
+                            messageWork.ApplicationProperties.Add("name", "ExamLite");
+                            if (changeRecords.Count > 0)
+                            {
+                                await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
+                                long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
+                                changeRecords[0].sequenceNumber = start;
+                                await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
+                            }
+                            else
+                            {
+                                long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
+                                ChangeRecord changeRecord = new ChangeRecord
+                                {
+                                    RowKey = input.Id,
+                                    PartitionKey = "pending",
+                                    sequenceNumber = start,
+                                    msgId = messageWork.MessageId
+                                };
+                                await _azureStorage.Save<ChangeRecord>(changeRecord);
+                            }
+                            break;
+                        case "going":
+                            (List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, lite.tchLists, lite.school);
+                            List<StuActivity> tchActivities = new List<StuActivity>();
+
+                            if (tchList.IsNotEmpty())
+                            {
+                                tchList.ForEach(x =>
+                                {
+                                    tchActivities.Add(new StuActivity
+                                    {
+                                        pk = "Activity",
+                                        id = lite.id,
+                                        code = $"Activity-{x.id}",
+                                        type = "ExamLite",
+                                        name = lite.name,
+                                        startTime = lite.startTime,
+                                        endTime = lite.endTime,
+                                        scode = lite.code,
+                                        scope = lite.scope,
+                                        school = lite.school,
+                                        creatorId = lite.creatorId,
+                                        subjects = new List<string> { "" },
+                                        blob = null,
+                                        owner = lite.owner,
+                                        createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
+                                        taskStatus = -1,
+                                        classIds = lite.tchLists
+                                    });
+                                });
+                            }
+                            await TriggerStuActivity.SaveStuActivity(client, _dingDing, null, null, tchActivities);
+                            var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
+                            messageWorkEnd.ApplicationProperties.Add("name", "ExamLite");
+                            if (changeRecords.Count > 0)
+                            {
+                                long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
+                                await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
+                                changeRecords[0].sequenceNumber = end;
+                                await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
+                            }
+                            else
                             {
-                                pk = "Activity",
-                                id = lite.id,
-                                code = $"Activity-{x.id}",
-                                type = "ExamLite",
-                                name = lite.name,
-                                startTime = lite.startTime,
-                                endTime = lite.endTime,
-                                scode = lite.code,
-                                scope = lite.scope,
-                                school = lite.school,
-                                creatorId = lite.creatorId,
-                                subjects = new List<string> { "" },
-                                blob = null,
-                                owner = lite.owner,
-                                createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
-                                taskStatus = -1,
-                                classIds = lite.tchLists
-                            });
-                        });
+                                long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
+                                ChangeRecord changeRecord = new ChangeRecord
+                                {
+                                    RowKey = input.Id,
+                                    PartitionKey = "going",
+                                    sequenceNumber = end,
+                                    msgId = messageWorkEnd.MessageId
+                                };
+                                await _azureStorage.Save<ChangeRecord>(changeRecord);
+                            }
+                            break;
+                        case "finish":
+                            break;
                     }
-                    await TriggerStuActivity.SaveStuActivity(client, _dingDing, null, null, tchActivities);
 
 
                 }

+ 81 - 29
TEAMModelFunction/TriggerStudy.cs

@@ -1,4 +1,5 @@
 using Azure.Cosmos;
+using Azure.Messaging.ServiceBus;
 using HTEXLib.COMM.Helpers;
 using Microsoft.Azure.Documents;
 using System;
@@ -6,6 +7,7 @@ using System.Collections.Generic;
 using System.Text;
 using TEAMModelOS.SDK;
 using TEAMModelOS.SDK.DI;
+using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Models.Cosmos.Common;
 
@@ -37,41 +39,91 @@ namespace TEAMModelFunction
                 }
                 await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}研修活动【{tdata.name}-{tdata.id}-ttl={tdata.ttl}】正在操作", GroupNames.成都开发測試群組);
                 Study study = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Study>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
-                //List<ChangeRecord> voteRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", vote.progress } });
+                List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", study.progress } });
                 if (study != null)
                 {
-                    (List<TmdInfo> tchList,_ ) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school);
-                    List<StuActivity> tchActivities = new List<StuActivity>();
 
-                    if (tchList.IsNotEmpty())
+                    switch (study.progress)
                     {
-                        tchList.ForEach(x =>
-                        {
-                            tchActivities.Add(new StuActivity
+                        case "pending":
+                            var messageWork = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
+                            messageWork.ApplicationProperties.Add("name", "Study");
+                            if (changeRecords.Count > 0)
                             {
-                                pk = "Activity",
-                                id = study.id,
-                                code = $"Activity-{x.id}",
-                                type = "Study",
-                                name = study.name,
-                                startTime = study.startTime,
-                                endTime = study.endTime,
-                                scode = study.code,
-                                scope = study.scope,
-                                school = study.school,
-                                creatorId = study.creatorId,
-                                subjects = new List<string> { "" },
-                                blob = null,
-                                owner = study.owner,
-                                createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
-                                taskStatus = -1,
-                                classIds = study.tchLists
-                            });
-                        });
-                    }
-                    await TriggerStuActivity.SaveStuActivity(client, _dingDing, null, null, tchActivities);
-
+                                await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
+                                long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
+                                changeRecords[0].sequenceNumber = start;
+                                await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
+                            }
+                            else
+                            {
+                                long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWork, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
+                                ChangeRecord changeRecord = new ChangeRecord
+                                {
+                                    RowKey = input.Id,
+                                    PartitionKey = "pending",
+                                    sequenceNumber = start,
+                                    msgId = messageWork.MessageId
+                                };
+                                await _azureStorage.Save<ChangeRecord>(changeRecord);
+                            }
+                            break;
+                        case "going":
+                            (List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school);
+                            List<StuActivity> tchActivities = new List<StuActivity>();
 
+                            if (tchList.IsNotEmpty())
+                            {
+                                tchList.ForEach(x =>
+                                {
+                                    tchActivities.Add(new StuActivity
+                                    {
+                                        pk = "Activity",
+                                        id = study.id,
+                                        code = $"Activity-{x.id}",
+                                        type = "Study",
+                                        name = study.name,
+                                        startTime = study.startTime,
+                                        endTime = study.endTime,
+                                        scode = study.code,
+                                        scope = study.scope,
+                                        school = study.school,
+                                        creatorId = study.creatorId,
+                                        subjects = new List<string> { "" },
+                                        blob = null,
+                                        owner = study.owner,
+                                        createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
+                                        taskStatus = -1,
+                                        classIds = study.tchLists
+                                    });
+                                });
+                            }
+                            await TriggerStuActivity.SaveStuActivity(client, _dingDing, null, null, tchActivities);
+                            var messageWorkEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
+                            messageWorkEnd.ApplicationProperties.Add("name", "Study");
+                            if (changeRecords.Count > 0)
+                            {
+                                long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
+                                await _serviceBus.GetServiceBusClient().cancelMessage(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), changeRecords[0].sequenceNumber);
+                                changeRecords[0].sequenceNumber = end;
+                                await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
+                            }
+                            else
+                            {
+                                long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageWorkEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
+                                ChangeRecord changeRecord = new ChangeRecord
+                                {
+                                    RowKey = input.Id,
+                                    PartitionKey = "going",
+                                    sequenceNumber = end,
+                                    msgId = messageWorkEnd.MessageId
+                                };
+                                await _azureStorage.Save<ChangeRecord>(changeRecord);
+                            }
+                            break;
+                        case "finish":
+                            break;
+                    }
                 }
 
             }

+ 1 - 0
TEAMModelOS.SDK/Models/Cosmos/Common/ExamLite.cs

@@ -75,6 +75,7 @@ namespace TEAMModelOS.SDK.Models
         public string scope { get; set; }
         //容器名称 container name
         //public string cn { get; set; }
+        public string progress { get; set; }
     }
     public class Record
     {