Browse Source

1、增加定时任务功能,投票活动,作业活动目前可以在设定开始时间结束时间之后,自动改变活动状态了

李思淳 5 years ago
parent
commit
81ca86e38e

+ 12 - 0
TEAMModelGrpc/Services/BlobSASService.cs

@@ -20,6 +20,12 @@ namespace TEAMModelGrpc.Services
             _azureBlobDBRepository = azureBlobDBRepository;
         }
 
+        /// <summary>
+        /// 获取bolb共享访问权限
+        /// </summary>
+        /// <param name="empty"></param>
+        /// <param name="context"></param>
+        /// <returns></returns>
         public async Task<BlobSASDto> GetContainerSasUri(Empty empty, ServerCallContext context) {
             (string,string, string) aaa = _azureBlobDBRepository.GetContainerSasUri();
             BlobSASDto blobSASDto = new BlobSASDto();
@@ -29,6 +35,12 @@ namespace TEAMModelGrpc.Services
             return blobSASDto;
         }
 
+        /// <summary>
+        /// 获取blob共享访问权限 (只读)
+        /// </summary>
+        /// <param name="blob"></param>
+        /// <param name="context"></param>
+        /// <returns></returns>
         public async Task<BlobSASDto> GetContainerSASRead(BlobSASDto blob, ServerCallContext context)
         {
             (string, string) a = BlobUrlString(blob.Url);

+ 46 - 0
TEAMModelOS.Service/Services/Learn/Implements/TimerWorkService.cs

@@ -0,0 +1,46 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Text;
+using System.Threading.Tasks;
+using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
+using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
+using TEAMModelOS.Service.Services.Learn.Interfaces;
+
+namespace TEAMModelOS.Service.Services.Learn.Implements
+{
+    public class TimerWorkService: ITimerWorkService
+    {
+        private readonly IAzureCosmosDBV3Repository _cosmos;
+
+        public TimerWorkService(IAzureCosmosDBV3Repository cosmos)
+        {
+            _cosmos = cosmos;
+        }
+
+        public void TimerWork<T>(long startTime, int status , Dictionary<string, object> dict) where T : ID
+        {
+            System.Timers.Timer aTimer = new System.Timers.Timer();
+            // Create a timer with a two second interval.
+            long time = Math.Abs(startTime - new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds());
+            aTimer = new System.Timers.Timer(time);
+            // Hook up the Elapsed event for the timer. 
+            aTimer.Elapsed += async (sender, e) => await OnTimedEventAsync<T>(_cosmos,status, dict);
+            aTimer.AutoReset = false;
+            aTimer.Enabled = true;
+        }
+
+        public static async Task OnTimedEventAsync<T>(IAzureCosmosDBV3Repository _cosmos, int status, Dictionary<string, object> dict) where T : ID
+        {
+            List<T> homeWorks = await _cosmos.FindByDict<T>(dict);
+            if (homeWorks.IsNotEmpty())
+            {
+                PropertyInfo propertyInfo = homeWorks[0].GetType().GetProperty("state");
+                for (int i = 0; i < homeWorks.Count; i++)
+                    propertyInfo.SetValue(homeWorks[i], status);
+                await _cosmos.SaveOrUpdateAll<T>(homeWorks);
+
+            }
+        }
+    }
+}

+ 13 - 0
TEAMModelOS.Service/Services/Learn/Interfaces/ITimerWorkService.cs

@@ -0,0 +1,13 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+using TEAMModelOS.SDK.Context.Configuration;
+using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
+
+namespace TEAMModelOS.Service.Services.Learn.Interfaces
+{
+    public interface ITimerWorkService : IBusinessService
+    {
+        public void TimerWork<T>(long startTime, int status,Dictionary<string, object> dict ) where T : ID;
+    }
+}

+ 40 - 3
TEAMModelOS/Controllers/Learn/HomeWorkController.cs

@@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Mvc;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Threading.Tasks;
 using TEAMModelOS.Models;
 using TEAMModelOS.SDK.Context.Exception;
@@ -13,6 +14,8 @@ using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
 using TEAMModelOS.Service.Models.Core;
 using TEAMModelOS.Service.Models.Courses;
 using TEAMModelOS.Service.Models.Learn;
+using TEAMModelOS.Service.Services.Learn;
+using TEAMModelOS.Service.Services.Learn.Interfaces;
 
 namespace TEAMModelOS.Controllers.Learn
 {
@@ -24,12 +27,15 @@ namespace TEAMModelOS.Controllers.Learn
     public class HomeWorkController : ControllerBase
     {
         private readonly IAzureCosmosDBV3Repository _cosmos;
+        private readonly ITimerWorkService _timerWorkService;
 
-        public HomeWorkController(IAzureCosmosDBV3Repository cosmos)
+        public HomeWorkController(IAzureCosmosDBV3Repository cosmos, ITimerWorkService timerWorkService)
         {
             _cosmos = cosmos;
+            _timerWorkService = timerWorkService;
         }
 
+
         /// <summary>
         /// 撤消作业
         /// </summary>
@@ -95,6 +101,12 @@ namespace TEAMModelOS.Controllers.Learn
                 request.@params.homeWork.startTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
                 request.@params.homeWork.state = 200;
             }
+            else if (request.@params.homeWork.publishModel.Equals("1"))
+            {
+                //TimerWork<HomeWork>(request.@params.homeWork.startTime,new Dictionary<string, object> { { "id", request.@params.homeWork.id } });
+                //设定开始时间
+                _timerWorkService.TimerWork<HomeWork>(request.@params.homeWork.startTime,200, new Dictionary<string, object> { { "id", request.@params.homeWork.id } });
+            }
 
             if (request.@params.homeWork.state == 0) {
 
@@ -104,7 +116,8 @@ namespace TEAMModelOS.Controllers.Learn
 
                 HomeWork homeWork = await _cosmos.SaveOrUpdate<HomeWork>(request.@params.homeWork);
 
-
+            //设定结束时间
+            _timerWorkService.TimerWork<HomeWork>(request.@params.homeWork.endTime, 300, new Dictionary<string, object> { { "id", request.@params.homeWork.id } });
             //清除作业
             if (request.@params.reset)
             {
@@ -154,6 +167,30 @@ namespace TEAMModelOS.Controllers.Learn
             return builder.Data(homeWork).build();
         }
 
+        private void TimerWork<T>(long startTime,Dictionary<string, object> dict) where T : ID
+        {
+            System.Timers.Timer aTimer = new System.Timers.Timer();
+            // Create a timer with a two second interval.
+
+            long time = Math.Abs(startTime - new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds());
+            aTimer = new System.Timers.Timer(time);
+            // Hook up the Elapsed event for the timer. 
+            aTimer.Elapsed += async (sender, e) => await OnTimedEventAsync<T>(_cosmos, dict);
+            aTimer.AutoReset = false;
+            aTimer.Enabled = true;
+        }
+
+        private static async Task OnTimedEventAsync<T>(IAzureCosmosDBV3Repository _cosmos,Dictionary<string, object> dict)where T:ID
+        {
+            List<T> homeWorks = await _cosmos.FindByDict<T>(dict);
+            if (homeWorks.IsNotEmpty()) {
+                PropertyInfo propertyInfo = homeWorks[0].GetType().GetProperty("state");
+                for (int i = 0; i < homeWorks.Count; i++)
+                    propertyInfo.SetValue(homeWorks[i], 200);
+                await _cosmos.SaveOrUpdateAll<T>(homeWorks);
+
+            }
+        }
 
         /// <summary>
         /// 新增或修改学生作业关联表
@@ -364,7 +401,7 @@ namespace TEAMModelOS.Controllers.Learn
                         {
                             commentid = Guid.NewGuid().ToString(),
                             comment = request.@params.comment,
-                            createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(),
+                            createTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds(),
                             fromId = request.@params.fromId,
                             score = request.@params.score
                         };

+ 17 - 1
TEAMModelOS/Controllers/Learn/VoteController.cs

@@ -15,6 +15,7 @@ using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
 using TEAMModelOS.Service.Models.Core;
 using TEAMModelOS.Service.Models.Learn;
+using TEAMModelOS.Service.Services.Learn.Interfaces;
 
 namespace TEAMModelOS.Controllers.Learn
 {
@@ -27,12 +28,15 @@ namespace TEAMModelOS.Controllers.Learn
     {
 
         private readonly IAzureCosmosDBV3Repository _cosmos;
+        private readonly ITimerWorkService _timerWorkService;
 
-        public VoteController(IAzureCosmosDBV3Repository cosmos)
+        public VoteController(IAzureCosmosDBV3Repository cosmos, ITimerWorkService timerWorkService)
         {
             _cosmos = cosmos;
+            _timerWorkService = timerWorkService;
         }
 
+
         /// <summary>
         /// 新增 或 修改投票活动
         /// </summary>
@@ -52,8 +56,20 @@ namespace TEAMModelOS.Controllers.Learn
             {
                 request.@params.vote.startTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
                 request.@params.vote.state = 200;
+            } else if (request.@params.vote.publishModel.Equals("1")) { 
+            
+                _timerWorkService.TimerWork<Vote>(request.@params.vote.startTime,200, new Dictionary<string, object> { { "id", request.@params.vote.id } });
+
             }
+
+
+
             Vote homeWork = await _cosmos.SaveOrUpdate<Vote>(request.@params.vote);
+
+            //设定结束时间
+            _timerWorkService.TimerWork<Vote>(request.@params.vote.endTime, 300, new Dictionary<string, object> { { "id", request.@params.vote.id } });
+
+            //清除作业
             if (!request.@params.reset)
             {
                 //根据作业发布对象查找到每一个具体学生生成关联关系表 HomeWorkStudent