Browse Source

处理作业活动

zhouj1203@hotmail.com 3 years ago
parent
commit
d9b6f56bee

+ 21 - 6
TEAMModelOS.SDK/Models/Service/HomeworkService.cs

@@ -1,15 +1,19 @@
 using Azure.Cosmos;
+using Azure.Messaging.ServiceBus;
+using Microsoft.Extensions.Configuration;
 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;
 
 namespace TEAMModelOS.SDK.Models.Service
 {
-	public static class HomeworkService
-	{
-        public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, Homework work)
+    public static class HomeworkService
+    {
+        public static async Task<string> saveMoreAsync(CosmosClient client, DingDing _dingDing, Homework work, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration)
         {
             try
             {
@@ -17,10 +21,21 @@ namespace TEAMModelOS.SDK.Models.Service
                 work.code = "Homework-" + work.school;
                 long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                 work.createTime = now;
+                var messageBlob = new ServiceBusMessage();
+                string blobcntr = null;
+                blobcntr = work.school;
+                work.size = await _azureStorage.GetBlobContainerClient(work.school).GetBlobsSize($"homework/{work.id}");
+                messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "insert", root = $"homework/{work.id}", name = $"{work.school}" }.ToJsonString());
 
+                messageBlob.ApplicationProperties.Add("name", "BlobRoot");
+                var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
+                await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
+                work.recordUrl = $"/homework/{work.id}/record.json";
+                var cods = new { records = new List<string>(), userids = new List<string>(), question = new List<QuestionRecord>() };
+                await _azureStorage.UploadFileByContainer(blobcntr, cods.ToJsonString(), "homework", $"{work.id}/record.json");
+                work.id = Guid.NewGuid().ToString();
                 if (string.IsNullOrEmpty(work.id))
-                {
-                    work.id = Guid.NewGuid().ToString();
+                {                   
                     if (work.startTime > now)
                     {
                         work.progress = "pending";
@@ -32,7 +47,7 @@ namespace TEAMModelOS.SDK.Models.Service
                     await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(work, new PartitionKey($"{work.code}"));
                 }
                 else
-                {
+                {                   
                     await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(work, new PartitionKey($"{work.code}"));
                 }
                 return work.id;

+ 8 - 3
TEAMModelOS/Controllers/Common/AreaController.cs

@@ -193,7 +193,7 @@ namespace TEAMModelOS.Controllers
         /// <param name="request"></param>
         /// <returns></returns>
         [ProducesDefaultResponseType]
-        //[AuthToken(Roles = "teacher,admin")]
+        [AuthToken(Roles = "teacher,admin")]
         [HttpPost("save-study")]
         public async Task<IActionResult> SaveStudy(JsonElement request)
         {
@@ -204,6 +204,7 @@ namespace TEAMModelOS.Controllers
                 if (!request.TryGetProperty("study", out JsonElement stu)) return BadRequest();
                 List<parameter> parameters = para.ToObject<List<parameter>>();
                 var client = _azureCosmos.GetCosmosClient();
+                var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
                 long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                 List<Study> studies = new();
                 Study areaStudy = stu.ToObject<Study>();
@@ -212,6 +213,7 @@ namespace TEAMModelOS.Controllers
                 areaStudy.scope = "school";
                 areaStudy.code = "Study-" + id.GetString();
                 areaStudy.createTime = now;
+                areaStudy.creatorId = userid;
                 if (areaStudy.startTime > now)
                 {
                     areaStudy.progress = "pending";
@@ -235,7 +237,7 @@ namespace TEAMModelOS.Controllers
                     study.ttl = -1;
                     study.code = "Study-" + pa.sId;
                     study.createTime = now;
-
+                    study.creatorId = userid;
                     if (study.startTime > now)
                     {
                         study.progress = "pending";
@@ -281,6 +283,7 @@ namespace TEAMModelOS.Controllers
                             trExam.school = pa.sId;
                             trExam.areaId = id.GetString();
                             trExam.tchLists = pa.gId;
+                            trExam.creatorId = userid;
                             trExam.targetType = "research";
                             trExam.blob = $"/{trExam.areaId}/exam/{study.pId}/index.json";
                             //处理发布对象
@@ -305,6 +308,7 @@ namespace TEAMModelOS.Controllers
                             trSurvey.areaId = id.GetString();
                             trSurvey.tchLists = pa.gId;
                             trSurvey.scope = "school";
+                            trSurvey.creatorId = userid;
                             trSurvey.targetType = "research";
                             trSurvey.blob = $"/{trSurvey.areaId}/survey/{study.pId}/index.json";
                             await getMoreSurvey(pa, trSurvey);
@@ -327,10 +331,11 @@ namespace TEAMModelOS.Controllers
                             homework.areaId = id.GetString();
                             homework.tchLists = pa.gId;
                             homework.scope = "school";
+                            homework.creatorId = userid;
                             homework.targetType = "research";
                             //homework.blob = $"/{homework.areaId}/survey/{study.pId}/index.json";
                             await getMoreWork(pa, homework);
-                            string workId = await HomeworkService.saveMoreAsync(client, _dingDing, homework);
+                            string workId = await HomeworkService.saveMoreAsync(client, _dingDing, homework,_serviceBus,_azureStorage,_configuration);
                             if (string.IsNullOrEmpty(workId))
                             {
                                 return Ok(new { code = (int)HttpStatusCode.BadRequest, msg = "作业活动异常" });