Browse Source

调整自动删除上一条个人课例。
获取一条最新的 且没有被收藏,且没有被标记为过期的记录,且不是当前触发的id 。

CrazyIter_Bin 3 years ago
parent
commit
376d1c2d3f
1 changed files with 103 additions and 89 deletions
  1. 103 89
      TEAMModelOS.FunctionV4/ServiceBus/ActiveTaskTopic.cs

+ 103 - 89
TEAMModelOS.FunctionV4/ServiceBus/ActiveTaskTopic.cs

@@ -1557,6 +1557,7 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
 
                                                 ids.Add(item);
                                             }
+
                                             // 220601  收藏不限條數,但空間不足時,僅保留最後一次上傳的課例   ()
                                             //包含收藏的本人的个人课例
                                             //string favorite_count_sql = $"select value(c.id) from  c where c.type='LessonRecord' and c.owner='{tmdid}'   and c.scope='private' ";
@@ -1576,104 +1577,117 @@ namespace TEAMModelOS.FunctionV4.ServiceBus
 
                                             if (ids.Count >= limit)
                                             {
-                                                // 1-时间戳,7-时间戳
-                                                Dictionary<int, ExpireTag> result = new Dictionary<int, ExpireTag>();
-                                                //暂定7天 
-                                                var now = DateTimeOffset.UtcNow;
-                                                //剩余3天的通知
-                                                //var day3= now.AddDays(Constant.private_lesson_expire - 3).ToUnixTimeMilliseconds();
-                                                //result.Add(3, day3);
-                                                //剩余1天的通知
-                                                var day1 = now.AddDays(Constant.private_lesson_expire - (Constant.private_lesson_expire - 1)).ToUnixTimeMilliseconds();
-                                                result.Add(1, new ExpireTag { expire = day1, tag = "notification" });
-                                                //到期通知
-
-                                                //不到五点上传的课例,七天之后直接删除。
-                                                int addSecond = 0;
-                                                if (now.Hour > 5)
-                                                {
-                                                    // 到凌晨00点还差 (24 - now.Hour) *60 * 60 分钟,再加天数;
-                                                    addSecond = Constant.private_lesson_expire * 86400 + (24 - now.Hour) * 3600;
-                                                    //再加 00到05小时内的 随机秒数
-                                                    Random rand = new Random();
-                                                    int randInt = rand.Next(0, 18000);
-                                                    addSecond += randInt;
-                                                }
-                                                else
+                                                LessonRecord lessonRecordExpire = null;
+                                                //获取一条最新的 且没有被收藏,且没有被标记为过期的记录,且不是当前触发的id 。
+                                                string sql = $"SELECT top 1 value(c)  FROM c  where  ( c.expire<=0 or IS_DEFINED(c.expire) = false  ) and  c.tmdid='{tmdid}' " +
+                                                    $"and  c.favorite<=0  and c.id<>'{lessonRecord.id}' order by c.startTime desc ";
+                                                await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetItemQueryIterator<LessonRecord>(
+                                                    queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"{code}") }))
                                                 {
-                                                    addSecond = Constant.private_lesson_expire * 24 * 60 * 60;
+                                                    lessonRecordExpire = item;break;
                                                 }
-
-                                                lessonRecord.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
-                                                //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
-                                                {
-                                                    hubName = "hita",
-                                                    type = "msg",
-                                                    from = $"ies5:{Environment.GetEnvironmentVariable("Option:Location")}:private",
-                                                    to = new List<string> { tmdid },
-                                                    label = $"{biz}_lessonRecord",
-                                                    body = new
+                                                if (lessonRecordExpire != null) {
+                                                    // 1-时间戳,7-时间戳
+                                                    Dictionary<int, ExpireTag> result = new Dictionary<int, ExpireTag>();
+                                                    //暂定7天 
+                                                    var now = DateTimeOffset.UtcNow;
+                                                    //剩余3天的通知
+                                                    //var day3= now.AddDays(Constant.private_lesson_expire - 3).ToUnixTimeMilliseconds();
+                                                    //result.Add(3, day3);
+                                                    //剩余1天的通知
+                                                    var day1 = now.AddDays(Constant.private_lesson_expire - (Constant.private_lesson_expire - 1)).ToUnixTimeMilliseconds();
+                                                    result.Add(1, new ExpireTag { expire = day1, tag = "notification" });
+                                                    //到期通知
+
+                                                    //不到五点上传的课例,七天之后直接删除。
+                                                    int addSecond = 0;
+                                                    if (now.Hour > 5)
                                                     {
-                                                        location = $"{Environment.GetEnvironmentVariable("Option:Location")}",
-                                                        biz = biz,
-                                                        tmdid = teacher.id,
-                                                        tmdname = teacher.name,
-                                                        sid = lessonRecord.id,
-                                                        sname = lessonRecord.name,
-                                                        scope = scope,
-                                                        stime = lessonRecord.startTime,
-                                                        expire = lessonRecord.expire,
-                                                        status = 1,
-                                                        //day = Constant.private_lesson_expire,
-                                                        time = now
-                                                    }.ToJsonString(),
-                                                    expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
-                                                };
-                                                var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
-                                                var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
-                                                var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
-                                                var location = $"{Environment.GetEnvironmentVariable("Option:Location")}";
-                                                await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
-
-                                                var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
-                                                List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
-                                                if (records.Count <= 0)
-                                                {
-                                                    foreach (var item in result)
+                                                        // 到凌晨00点还差 (24 - now.Hour) *60 * 60 分钟,再加天数;
+                                                        addSecond = Constant.private_lesson_expire * 86400 + (24 - now.Hour) * 3600;
+                                                        //再加 00到05小时内的 随机秒数
+                                                        Random rand = new Random();
+                                                        int randInt = rand.Next(0, 18000);
+                                                        addSecond += randInt;
+                                                    }
+                                                    else
+                                                    {
+                                                        addSecond = Constant.private_lesson_expire * 24 * 60 * 60;
+                                                    }
+
+                                                    lessonRecordExpire.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
+                                                    //result.Add(Constant.private_lesson_expire, lessonRecordExpire.expire);
+                                                    result.Add(Constant.private_lesson_expire, new ExpireTag { expire = lessonRecordExpire.expire, tag = "delete" });
+                                                    string biz = "expire";
+                                                    Notification notification = new Notification
                                                     {
-                                                        string PartitionKey = string.Format("{0}{1}{2}", lessonRecord.code, "-", $"expire-{item.Key}");
-                                                        //课堂的id ,
-                                                        //课堂的通知时间类型progress, 默认就会发送一条,到期前一天发送一条,最后已到期发送一条。
-                                                        var message = new ServiceBusMessage(new
+                                                        hubName = "hita",
+                                                        type = "msg",
+                                                        from = $"ies5:{Environment.GetEnvironmentVariable("Option:Location")}:private",
+                                                        to = new List<string> { tmdid },
+                                                        label = $"{biz}_lessonRecord",
+                                                        body = new
                                                         {
-                                                            id = lessonRecord.id,
-                                                            progress = item.Key,
-                                                            code = lessonRecord.code,
-                                                            scope = lessonRecord.scope,
-                                                            school = lessonRecord.school,
-                                                            opt = "delete",
-                                                            expire = lessonRecord.expire,
-                                                            tmdid = tmdid,
+                                                            location = $"{Environment.GetEnvironmentVariable("Option:Location")}",
+                                                            biz = biz,
+                                                            tmdid = teacher.id,
                                                             tmdname = teacher.name,
-                                                            name = lessonRecord.name,
-                                                            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.expire));
-                                                        ChangeRecord changeRecord = new ChangeRecord
+                                                            sid = lessonRecordExpire.id,
+                                                            sname = lessonRecordExpire.name,
+                                                            scope = scope,
+                                                            stime = lessonRecordExpire.startTime,
+                                                            expire = lessonRecordExpire.expire,
+                                                            status = 1,
+                                                            //day = Constant.private_lesson_expire,
+                                                            time = now
+                                                        }.ToJsonString(),
+                                                        expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
+                                                    };
+                                                    var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
+                                                    var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
+                                                    var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
+                                                    var location = $"{Environment.GetEnvironmentVariable("Option:Location")}";
+                                                    await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
+
+                                                    var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
+                                                    List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecordExpire.id } });
+                                                    if (records.Count <= 0)
+                                                    {
+                                                        foreach (var item in result)
                                                         {
-                                                            RowKey = lessonRecord.id,
-                                                            PartitionKey = PartitionKey,
-                                                            sequenceNumber = start,
-                                                            msgId = message.MessageId
-                                                        };
-                                                        await table.Save<ChangeRecord>(changeRecord);
+                                                            string PartitionKey = string.Format("{0}{1}{2}", lessonRecordExpire.code, "-", $"expire-{item.Key}");
+                                                            //课堂的id ,
+                                                            //课堂的通知时间类型progress, 默认就会发送一条,到期前一天发送一条,最后已到期发送一条。
+                                                            var message = new ServiceBusMessage(new
+                                                            {
+                                                                id = lessonRecordExpire.id,
+                                                                progress = item.Key,
+                                                                code = lessonRecordExpire.code,
+                                                                scope = lessonRecordExpire.scope,
+                                                                school = lessonRecordExpire.school,
+                                                                opt = "delete",
+                                                                expire = lessonRecordExpire.expire,
+                                                                tmdid = tmdid,
+                                                                tmdname = teacher.name,
+                                                                name = lessonRecordExpire.name,
+                                                                startTime = lessonRecordExpire.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.expire));
+                                                            ChangeRecord changeRecord = new ChangeRecord
+                                                            {
+                                                                RowKey = lessonRecordExpire.id,
+                                                                PartitionKey = PartitionKey,
+                                                                sequenceNumber = start,
+                                                                msgId = message.MessageId
+                                                            };
+                                                            await table.Save<ChangeRecord>(changeRecord);
+                                                        }
                                                     }
+                                                    await client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReplaceItemAsync(lessonRecordExpire, lessonRecordExpire.id, new PartitionKey(lessonRecordExpire.code));
                                                 }
+                                               
                                             }
                                         }
                                         else