Browse Source

update 课例自动删除。试卷增加mode

CrazyIter_Bin 3 năm trước cách đây
mục cha
commit
fc64d32671

+ 15 - 12
TEAMModelOS.FunctionV4/ServiceBus/ActiveTaskTopic.cs

@@ -25,6 +25,7 @@ using TEAMModelOS.SDK.Services;
 using Azure.Storage.Blobs.Models;
 using System.IO;
 using Azure;
+using static TEAMModelOS.SDK.Models.Service.LessonService;
 
 namespace TEAMModelOS.FunctionV4.ServiceBus
 {
@@ -993,7 +994,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                                     }
                                     // await _dingDing.SendBotMsg($"{_option.Location},课堂id:{_lessonId} 更新完成", GroupNames.醍摩豆服務運維群組);
 
-                                    
+                                    LessonService.DoAutoDeleteSchoolLessonRecord(lessonRecord, scope, client, school, tmdid,   teacher, _notificationService, _serviceBus, _azureStorage, _configuration);
                                     long? size = await _azureStorage.GetBlobContainerClient(blobname).GetBlobsSize($"records/{_lessonId}");
                                     Bloblog bloblog = new Bloblog
                                     {
@@ -1247,7 +1248,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                                             if (ids.Count >= limit)
                                             {
                                                 // 1-时间戳,7-时间戳
-                                                Dictionary<int, long> result = new Dictionary<int, long>();
+                                                Dictionary<int, ExpireTag> result = new Dictionary<int, ExpireTag>();
                                                 //暂定7天 
                                                 var now = DateTimeOffset.UtcNow;
                                                 //剩余3天的通知
@@ -1255,7 +1256,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                                                 //result.Add(3, day3);
                                                 //剩余1天的通知
                                                 var day1 = now.AddDays(Constant.private_lesson_expire - (Constant.private_lesson_expire-1)).ToUnixTimeMilliseconds();
-                                                result.Add(1, day1);
+                                                result.Add(1, new ExpireTag { expire = day1, tag = "notification" });
                                                 //到期通知
 
                                                 //不到五点上传的课例,七天之后直接删除。
@@ -1274,7 +1275,8 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                                                 }
 
                                                 lessonRecord.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
-                                                result.Add(Constant.private_lesson_expire, lessonRecord.expire);
+                                                //result.Add(Constant.private_lesson_expire, lessonRecord.expire);
+                                                result.Add(Constant.private_lesson_expire, new ExpireTag { expire = lessonRecord.expire, tag = "delete" });
                                                 string biz = "expire";
                                                 Notification notification = new Notification
                                                 {
@@ -1295,10 +1297,10 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                                                         stime = lessonRecord.startTime,
                                                         expire = lessonRecord.expire,
                                                         status = 1,
-                                                        day = Constant.private_lesson_expire,
+                                                        //day = Constant.private_lesson_expire,
                                                         time = now
                                                     }.ToJsonString(),
-                                                    expires = DateTimeOffset.UtcNow.AddDays(Constant.private_lesson_expire).ToUnixTimeSeconds()
+                                                    expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
                                                 };
                                                 var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
                                                 var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
@@ -1327,10 +1329,11 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                                                             tmdid = tmdid,
                                                             tmdname = teacher.name,
                                                             name = lessonRecord.name,
-                                                            startTime = lessonRecord.startTime
+                                                            startTime = lessonRecord.startTime,
+                                                            tag = item.Value.tag
                                                         }.ToJsonString());
                                                         message.ApplicationProperties.Add("name", "LessonRecordExpire");
-                                                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value));
+                                                        long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value.expire));
                                                         ChangeRecord changeRecord = new ChangeRecord
                                                         {
                                                             RowKey = lessonRecord.id,
@@ -1382,7 +1385,6 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
         [Function("LessonRecordExpire")]
         public async Task LessonRecordExpireFunc([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "lesson-record-expire", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
         {
-
             try
             {
                 var jsonMsg = JsonDocument.Parse(msg).RootElement;
@@ -1396,9 +1398,10 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                 jsonMsg.TryGetProperty("expire", out JsonElement expire);
                 jsonMsg.TryGetProperty("scope", out JsonElement scope);
                 jsonMsg.TryGetProperty("school", out JsonElement _school);
+                jsonMsg.TryGetProperty("tag", out JsonElement _tag);
                 var client = _azureCosmos.GetCosmosClient();
                 //处理到期删除
-                if ($"{progress}".Equals($"{Constant.private_lesson_expire}"))
+                if (_tag.ValueKind.Equals(JsonValueKind.String) && $"{_tag}".Equals("delete") )
                 {
                     string lessonId = $"{id}";
                     string tbname;
@@ -1453,7 +1456,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
                         stime = long.Parse($"{startTime}"),
                         expire = long.Parse($"{expire}"),
                         status = 1,
-                        day = Constant.private_lesson_expire,
+                        //day = Constant.private_lesson_expire,
                         time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
                     }.ToJsonString(),
                     expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
@@ -1466,7 +1469,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
             }
             catch (Exception ex)
             {
-                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,Study()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
+                await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,LessonRecordExpire()\n{ex.Message}\n{ex.StackTrace}\n{msg}", GroupNames.醍摩豆服務運維群組);
             }
 
         }

+ 1 - 0
TEAMModelOS.SDK/Models/Cosmos/School/Paper.cs

@@ -62,6 +62,7 @@ namespace TEAMModelOS.SDK.Models
         public int multipleRule { get; set; }
         public string sheet { get; set; }
         public string sheetNo { get; set; }
+        public string mode { get; set; }
         //记录试卷大小
         public long? size { get; set; } = 0;
 

+ 69 - 39
TEAMModelOS.SDK/Models/Service/LessonService.cs

@@ -17,9 +17,9 @@ namespace TEAMModelOS.SDK.Models.Service
     public class LessonService
     {
 
-        public static async void Do(LessonRecord lessonRecord,string  scope ,CosmosClient client,string school,string tmdid,
+        public static async void DoAutoDeleteSchoolLessonRecord(LessonRecord lessonRecord,string  scope ,CosmosClient client,string school,string tmdid,
             Teacher teacher, NotificationService _notificationService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration) {
-            if (lessonRecord.scope.Equals(scope))
+            if (lessonRecord.scope.Equals("school"))
             {
                 SchoolSetting setting = null;
                 Azure.Response schoolSetting = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(school, new PartitionKey("SchoolSetting"));
@@ -45,13 +45,13 @@ namespace TEAMModelOS.SDK.Models.Service
                     setting = new SchoolSetting() { lessonSetting = new LessonSetting { openAutoClean = 0, expireDays = Constant.school_lesson_expire } };
                 }
                 int school_lesson_expire =0;
-                bool ok = true;
+                bool save = true;
                 List<string> msg = new List<string>();
                 if (setting.lessonSetting.openAutoClean == 1)
                 {
                     if (setting.lessonSetting.conds.IsEmpty())
                     {
-                        ok = false;
+                        save = false;
                     }
                     else {
                         school_lesson_expire = setting.lessonSetting.expireDays;
@@ -65,77 +65,77 @@ namespace TEAMModelOS.SDK.Models.Service
                                         case "attendRate":
                                             if (!(lessonRecord.attendRate >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
                                             }
                                             break;
                                         case "groupCount":
                                             if (!(lessonRecord.groupCount >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "totalPoint":
                                             if (!(lessonRecord.totalPoint >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
                                             }
                                             break;
                                         case "collateTaskCount":
                                             if (!(lessonRecord.collateTaskCount >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "collateCount":
                                             if (!(lessonRecord.collateCount >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "pushCount":
                                             if (!(lessonRecord.pushCount >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "totalInteractPoint":
                                             if (!(lessonRecord.totalInteractPoint >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
                                             }
                                             break;
                                         case "interactionCount":
                                             if (!(lessonRecord.interactionCount >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "clientInteractionCount":
                                             if (!(lessonRecord.clientInteractionCount >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "examQuizCount":
                                             if (!(lessonRecord.examQuizCount >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "examPointRate":
                                             if (!(lessonRecord.examPointRate >= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
                                             }
                                             break;
@@ -147,77 +147,77 @@ namespace TEAMModelOS.SDK.Models.Service
                                         case "attendRate":
                                             if (!(lessonRecord.attendRate <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
                                             }
                                             break;
                                         case "groupCount":
                                             if (!(lessonRecord.groupCount <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "totalPoint":
                                             if (!(lessonRecord.totalPoint <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
                                             }
                                             break;
                                         case "collateTaskCount":
                                             if (!(lessonRecord.collateTaskCount <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "collateCount":
                                             if (!(lessonRecord.collateCount <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "pushCount":
                                             if (!(lessonRecord.pushCount <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "totalInteractPoint":
                                             if (!(lessonRecord.totalInteractPoint <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
                                             }
                                             break;
                                         case "interactionCount":
                                             if (!(lessonRecord.interactionCount <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "clientInteractionCount":
                                             if (!(lessonRecord.clientInteractionCount <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "examQuizCount":
                                             if (!(lessonRecord.examQuizCount <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
                                             }
                                             break;
                                         case "examPointRate":
                                             if (!(lessonRecord.examPointRate <= item.val))
                                             {
-                                                ok = false;
+                                                save = false;
                                                 msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
                                             }
                                             break;
@@ -229,13 +229,16 @@ namespace TEAMModelOS.SDK.Models.Service
                    
                 }
                 else {
-                    ok = true;
+                    save = false;
                     school_lesson_expire = Constant.school_lesson_expire;
+
+
                 }
 
-                if (ok && school_lesson_expire > 0) {
+                if (!save && school_lesson_expire > 0)
+                {
                     // 1-时间戳,7-时间戳
-                    Dictionary<int, long> result = new Dictionary<int, long>();
+                    Dictionary<int, ExpireTag> result = new Dictionary<int, ExpireTag>();
                     //暂定7天 
                     var now = DateTimeOffset.UtcNow;
                     //剩余3天的通知
@@ -243,24 +246,25 @@ namespace TEAMModelOS.SDK.Models.Service
                     //result.Add(3, day3);
                     //剩余1天的通知
                     var day1 = now.AddDays(school_lesson_expire - (school_lesson_expire - 1)).ToUnixTimeMilliseconds();
-                    result.Add(1, day1);
+                    result.Add(1, new ExpireTag { expire= day1 ,tag= "notification" });
                     //到期通知
                     //不到五点上传的课例,七天之后直接删除。
                     int addSecond = 0;
                     if (now.Hour > 5)
-                    { 
+                    {
                         // 到凌晨00点还差 (24 - now.Hour) *60 * 60 分钟,再加天数;
                         addSecond = school_lesson_expire * 86400 + (24 - now.Hour) * 3600;
                         //再加 00到05小时内的 随机秒数
                         Random rand = new Random();
-                        addSecond+= rand.Next(0 - 18000);
+                        addSecond += rand.Next(0 - 18000);
                     }
                     else
                     {
-                        addSecond = school_lesson_expire *24 * 60 * 60;
+                        addSecond = school_lesson_expire * 24 * 60 * 60;
                     }
                     lessonRecord.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
-                    result.Add(school_lesson_expire, lessonRecord.expire);
+                    result.Add(school_lesson_expire, new ExpireTag { expire = lessonRecord.expire, tag = "delete" });
+                   // result.Add(school_lesson_expire, lessonRecord.expire);
                     string biz = "expire";
                     Notification notification = new Notification
                     {
@@ -283,10 +287,10 @@ namespace TEAMModelOS.SDK.Models.Service
                             stime = lessonRecord.startTime,
                             expire = lessonRecord.expire,
                             status = 1,
-                            day = school_lesson_expire,
+                            //day = school_lesson_expire,
                             time = now
                         }.ToJsonString(),
-                        expires = DateTimeOffset.UtcNow.AddDays(school_lesson_expire).ToUnixTimeSeconds()
+                        expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
                     };
                     var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
                     var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
@@ -315,10 +319,11 @@ namespace TEAMModelOS.SDK.Models.Service
                                 tmdid = tmdid,
                                 tmdname = teacher.name,
                                 name = lessonRecord.name,
-                                startTime = lessonRecord.startTime
+                                startTime = lessonRecord.startTime,
+                                tag = item.Value.tag
                             }.ToJsonString());
                             message.ApplicationProperties.Add("name", "LessonRecordExpire");
-                            long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value));
+                            long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value.expire));
                             ChangeRecord changeRecord = new ChangeRecord
                             {
                                 RowKey = lessonRecord.id,
@@ -330,10 +335,35 @@ namespace TEAMModelOS.SDK.Models.Service
                         }
                     }
                 }
-              
+                else {
+
+                    if (lessonRecord.expire > 0)
+                    {
+                        var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
+                        List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
+                        foreach (var record in records)
+                        {
+                            try
+                            {
+                                await table.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
+                                await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
+                            }
+                            catch (Exception)
+                            {
+                                continue;
+                            }
+                        }
+                    }
+                    lessonRecord.expire = -1;
+                }
             }
         }
 
+
+        public record ExpireTag {
+            public long expire { get; set; }
+            public string tag { get; set; }
+        }
         /// <summary>
         /// 
         /// </summary>

+ 3 - 3
TEAMModelOS/Controllers/Both/PaperController.cs

@@ -98,7 +98,7 @@ namespace TEAMModelOS.Controllers
             var id = jwt.Payload.Sub;
             var client = _azureCosmos.GetCosmosClient();
             List<object> papers = new List<object>();
-            var query = $"select c.id,c.subjectCode,c.code,c.periodCode,c.name,c.itemCount,c.level,c.pointItem,c.pointScore,c.score,c.gradeCode,c.createTime,c.sheet,c.sheetNo,c.tags from c where c.id = {id}";
+            var query = $"select c.id,c.subjectCode,c.code,c.periodCode,c.name,c.itemCount,c.level,c.pointItem,c.pointScore,c.score,c.gradeCode,c.createTime,c.sheet,c.sheetNo,c.tags ,c.mode from c where c.id = {id}";
             await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{school_code}") }))
             {
                 using var json = await JsonDocument.ParseAsync(item.ContentStream);
@@ -157,7 +157,7 @@ namespace TEAMModelOS.Controllers
             
             if (scope.ToString().Equals("school"))
             {
-                sql.Append("select  c.id,c.code,c.name,c.blob,c.periodId,c.gradeIds,c.subjectId,c.subjectName,c.score,c.useCount,c.scope,c.scoring,c.createTime,c.sheet ,c.sheetNo, c.tags from c");
+                sql.Append("select  c.id,c.code,c.name,c.blob,c.periodId,c.gradeIds,c.subjectId,c.subjectName,c.score,c.useCount,c.scope,c.scoring,c.createTime,c.sheet ,c.mode ,c.sheetNo, c.tags from c");
                 AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
                 await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Paper>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{code}") }))
                 {
@@ -175,7 +175,7 @@ namespace TEAMModelOS.Controllers
             }
             if (scope.ToString().Equals("private"))
             {
-                sql.Append("select c.id,c.code,c.name,c.blob,c.subjectName,c.score,c.useCount,c.scope,c.scoring ,c.createTime ,c.sheet,c.sheetNo , c.tags from c");
+                sql.Append("select c.id,c.code,c.name,c.blob,c.subjectName,c.score,c.useCount,c.scope,c.scoring ,c.createTime ,c.sheet,c.sheetNo ,c.mode , c.tags from c");
                 AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
                 await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<Paper>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{code}") }))
                 {

+ 2 - 0
TEAMModelOS/Controllers/Both/SheetConfigController.cs

@@ -198,8 +198,10 @@ namespace TEAMModelOS.Controllers.Common
                         if (string.IsNullOrEmpty(sheet.no)) {
                             sheet.no = await SheetService.genSheetNo(client, _dingDing, _option, sheet.code, tbname, sheet.from);
                             paper.sheetNo = sheet.no;
+                            paper.mode = sheet.mode;
                         }
                         paper.sheetNo = sheet.no;
+                        paper.mode = sheet.mode;
                         paper = await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<Paper>(paper, $"{_paperId}", new PartitionKey($"{code}"));
 
                     }