CrazyIter_Bin 4 éve
szülő
commit
8cea06ec5f

+ 73 - 18
TEAMModelFunction/TriggerSurvey.cs

@@ -120,37 +120,44 @@ namespace TEAMModelFunction
                     }
                     break;
                 case "finish":
-                    var records =await  _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
+                    var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
                     List<dynamic> recs = new List<dynamic>();
-                    foreach (var rcd in records) {
+                    foreach (var rcd in records)
+                    {
                         var value = rcd.Value.ToString().ToObject<JsonElement>();
                         recs.Add(new { index = rcd.Name.ToString(), ans = value });
                     }
                     var cods = new { records = recs };
                     //问卷整体情况
-                    await  _azureStorage.UploadFileByContainer(survey.owner, cods.ToJsonString(), "survey", $"{survey.id}/record.json");
+                    await _azureStorage.UploadFileByContainer(survey.owner, cods.ToJsonString(), "survey", $"{survey.id}/record.json");
                     //结算每道题的答题情况
-                   
-                    var ContainerClient =   _azureStorage.GetBlobContainerClient(survey.owner);
+
+                    var ContainerClient = _azureStorage.GetBlobContainerClient(survey.owner);
                     var route = ContainerClient.Uri.ToString();
                     List<BlobItem> items = await ContainerClient.List($"survey/{survey.id}/urecord");
                     List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
                     //获取
-                    BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(survey.owner, $"survey/{survey.id}/record.json");
-                    foreach (BlobItem item in items) {
-                        var url = $"{route}/{item.Name}";
-                        var response = await _clientFactory.CreateClient().GetAsync(new Uri(url + blobAuth.sas));
+
+                    foreach (BlobItem item in items)
+                    {
+                        BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(survey.owner, $"{item.Name}");
+                        var url = $"{route}/{item.Name}?{blobAuth.sas}";
+                        var response = await _clientFactory.CreateClient().GetAsync(new Uri(url));
                         var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
                         var Record = json.RootElement.ToObject<SurveyRecord>();
                         surveyRecords.Add(Record);
                     }
                     List<Task<string>> tasks = new List<Task<string>>();
-                    for (int index = 0; index < survey.ans.Count; index++) {
+                    for (int index = 0; index < survey.ans.Count; index++)
+                    {
                         string url = $"{survey.id}/qrecord/{index}.json";
-                        QuestionRecord question = new QuestionRecord() { index=index};
-                        foreach (SurveyRecord record in surveyRecords) {
-                            if (record.ans.Count == survey.ans.Count) {
-                                foreach (var an in record.ans[index]) {
+                        QuestionRecord question = new QuestionRecord() { index = index };
+                        foreach (SurveyRecord record in surveyRecords)
+                        {
+                            if (record.ans.Count == survey.ans.Count)
+                            {
+                                foreach (var an in record.ans[index])
+                                {
                                     //
                                     if (question.opt.ContainsKey(an))
                                     {
@@ -158,17 +165,20 @@ namespace TEAMModelFunction
                                         {
                                             question.opt[an].Add(record.userid);
                                         }
-                                        else {
-                                            question.opt[an] = new HashSet<string>() { record.userid } ;
+                                        else
+                                        {
+                                            question.opt[an] = new HashSet<string>() { record.userid };
                                         }
                                     }
-                                    else {
+                                    else
+                                    {
                                         if (survey.ans[index].Contains(an))
                                         {
                                             //如果是客观题code
                                             question.opt.Add(an, new HashSet<string> { record.userid });
                                         }
-                                        else {
+                                        else
+                                        {
                                             //如果不是客观code
                                             question.other[record.userid] = an;
                                         }
@@ -179,6 +189,51 @@ namespace TEAMModelFunction
                         tasks.Add(_azureStorage.UploadFileByContainer(survey.owner, question.ToJsonString(), "survey", url, false));
                     }
                     await Task.WhenAll(tasks);
+                    survey.recordUrl = $"/survey/{survey.id}/record.json";
+                    await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Survey>(survey, survey.id, new Azure.Cosmos.PartitionKey(survey.code));
+                    //更新结束状态
+                    if (survey.scope == "school")
+                    {
+                        data = new ActivityData
+                        {
+                            id = survey.id,
+                            code = $"Activity-{survey.owner}",
+                            type = "survey",
+                            name = survey.name,
+                            startTime = survey.startTime,
+                            endTime = survey.endTime,
+                            scode = survey.code,
+                            scope = survey.scope,
+                            progress = "finish",
+                            classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
+                            tmdids = survey.tmdids.IsNotEmpty() ? survey.tmdids : new List<string> { "" },
+                            owner = survey.owner,
+                            subjects = new List<string> { "" }
+
+                        };
+                        await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
+                    }
+                    else if (survey.scope == "private")
+                    {
+                        //更新结束状态
+                        data = new ActivityData
+                        {
+                            id = survey.id,
+                            code = $"Activity-Common",
+                            type = "survey",
+                            name = survey.name,
+                            startTime = survey.startTime,
+                            endTime = survey.endTime,
+                            scode = survey.code,
+                            scope = survey.scope,
+                            progress = "finish",
+                            classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
+                            owner = survey.owner,
+                            tmdids = new List<string> { "" },
+                            subjects = new List<string> { "" }
+                        };
+                        await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
+                    }
                     break;
             }
         }

+ 1 - 8
TEAMModelFunction/TriggerVote.cs

@@ -155,14 +155,7 @@ namespace TEAMModelFunction
                         tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/record.json"));
                         //处理投票者的记录
                         await Task.WhenAll(tasks);
-                        if (vote.scope == "school")
-                        {
-                            await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Vote>(vote,vote.id, new Azure.Cosmos.PartitionKey(vote.code));
-                        }
-                        else if (vote.scope == "private")
-                        {
-                            await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
-                        }
+                        await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
                         //更新结束状态
                         if (vote.scope == "school")
                         {

+ 176 - 1
TEAMModelOS/Controllers/Common/SurveyController.cs

@@ -19,6 +19,11 @@ using TEAMModelOS.Models;
 using Microsoft.Extensions.Options;
 using TEAMModelOS.Filter;
 using TEAMModelOS.Services.Common;
+using Azure.Storage.Blobs.Models;
+using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
+using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
+using System.Net.Http;
+using TEAMModelOS.SDK.Models.Cosmos;
 
 namespace TEAMModelOS.Controllers
 {
@@ -39,7 +44,9 @@ namespace TEAMModelOS.Controllers
         private readonly DingDing _dingDing;
         private readonly Option _option;
         private readonly AzureStorageFactory _azureStorage;
-        public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
+        private readonly IHttpClientFactory _clientFactory;
+        public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, 
+            AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IHttpClientFactory clientFactory)
         {
             _snowflakeId= snowflakeId;
             _serviceBus = serviceBus;
@@ -48,6 +55,7 @@ namespace TEAMModelOS.Controllers
             _option = option?.Value;
             _azureRedis = azureRedis;
             _azureStorage = azureStorage;
+            _clientFactory = clientFactory;
         }
 
 
@@ -407,5 +415,172 @@ namespace TEAMModelOS.Controllers
             }
             return Ok(new {records = res});
         }
+
+
+        /// <summary>
+        /// 问卷记录 当活动没结算且没有BlobUrl时则调用此接口
+        /// </summary>
+        /// <redis>
+        /// {"C":2,"A":2,"other":2}
+        /// </redis>
+        /// <param name="request">
+        /// !"id":"aaaa"
+        /// !"code":"Survey-hbcn"/"code":"Survey-1606285227"
+        /// </param>
+        /// <returns>
+        /// </returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("settlement")]
+        //[AuthToken(Roles = "teacher,student")]
+        public async Task<IActionResult> Settlement(JsonElement request)
+        {
+            try
+            {
+                var client = _azureCosmos.GetCosmosClient();
+                //活动id
+                if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
+                //活动分区
+                if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
+                Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.GetString(), new PartitionKey($"{code}"));
+                if (survey != null)
+                {
+                    ActivityData data;
+
+                    if (survey.progress == "finish") {
+                        var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
+                        List<dynamic> recs = new List<dynamic>();
+                        foreach (var rcd in records)
+                        {
+                            var value = rcd.Value.ToString().ToObject<JsonElement>();
+                            recs.Add(new { index = rcd.Name.ToString(), ans = value });
+                        }
+                        var cods = new { records = recs };
+                        //问卷整体情况
+                        await _azureStorage.UploadFileByContainer(survey.owner, cods.ToJsonString(), "survey", $"{survey.id}/record.json");
+                        //结算每道题的答题情况
+
+                        var ContainerClient = _azureStorage.GetBlobContainerClient(survey.owner);
+                        var route = ContainerClient.Uri.ToString();
+                        List<BlobItem> items = await ContainerClient.List($"survey/{survey.id}/urecord");
+                        List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
+                        //获取
+                        
+                        foreach (BlobItem item in items)
+                        {
+                            BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(survey.owner, $"{item.Name}");
+                            var url = $"{route}/{item.Name}?{blobAuth.sas}";
+                            var response = await _clientFactory.CreateClient().GetAsync(new Uri(url));
+                            var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
+                            var Record = json.RootElement.ToObject<SurveyRecord>();
+                            surveyRecords.Add(Record);
+                        }
+                        List<Task<string>> tasks = new List<Task<string>>();
+                        for (int index = 0; index < survey.ans.Count; index++)
+                        {
+                            string url = $"{survey.id}/qrecord/{index}.json";
+                            QuestionRecord question = new QuestionRecord() { index = index };
+                            foreach (SurveyRecord record in surveyRecords)
+                            {
+                                if (record.ans.Count == survey.ans.Count)
+                                {
+                                    foreach (var an in record.ans[index])
+                                    {
+                                        //
+                                        if (question.opt.ContainsKey(an))
+                                        {
+                                            if (question.opt[an] != null)
+                                            {
+                                                question.opt[an].Add(record.userid);
+                                            }
+                                            else
+                                            {
+                                                question.opt[an] = new HashSet<string>() { record.userid };
+                                            }
+                                        }
+                                        else
+                                        {
+                                            if (survey.ans[index].Contains(an))
+                                            {
+                                                //如果是客观题code
+                                                question.opt.Add(an, new HashSet<string> { record.userid });
+                                            }
+                                            else
+                                            {
+                                                //如果不是客观code
+                                                question.other[record.userid] = an;
+                                            }
+                                        }
+                                    }
+                                }
+                            }
+                            tasks.Add(_azureStorage.UploadFileByContainer(survey.owner, question.ToJsonString(), "survey", url, false));
+                        }
+                        await Task.WhenAll(tasks);
+                        survey.recordUrl = $"/survey/{survey.id}/record.json";
+                        await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Survey>(survey, survey.id, new Azure.Cosmos.PartitionKey(survey.code));
+                        //更新结束状态
+                        if (survey.scope == "school")
+                        {
+                            data = new ActivityData
+                            {
+                                id = survey.id,
+                                code = $"Activity-{survey.owner}",
+                                type = "survey",
+                                name = survey.name,
+                                startTime = survey.startTime,
+                                endTime = survey.endTime,
+                                scode = survey.code,
+                                scope = survey.scope,
+                                progress = "finish",
+                                classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
+                                tmdids = survey.tmdids.IsNotEmpty() ? survey.tmdids : new List<string> { "" },
+                                owner = survey.owner,
+                                subjects = new List<string> { "" }
+
+                            };
+                            await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
+                        }
+                        else if (survey.scope == "private")
+                        {
+                            //更新结束状态
+                            data = new ActivityData
+                            {
+                                id = survey.id,
+                                code = $"Activity-Common",
+                                type = "survey",
+                                name = survey.name,
+                                startTime = survey.startTime,
+                                endTime = survey.endTime,
+                                scode = survey.code,
+                                scope = survey.scope,
+                                progress = "finish",
+                                classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
+                                owner = survey.owner,
+                                tmdids = new List<string> { "" },
+                                subjects = new List<string> { "" }
+                            };
+                            await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
+                        }
+                    }
+                    return Ok(new { survey });
+                }
+                else
+                {
+                    return BadRequest("id,code不存在!");
+                }
+            }
+            catch (Exception ex)
+            {
+                await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
+                return BadRequest(ex.StackTrace);
+            }
+        }
+
+    }
+    public class QuestionRecord
+    {
+        public int index { get; set; }
+        public Dictionary<string, HashSet<string>> opt { get; set; } = new Dictionary<string, HashSet<string>>();
+        public Dictionary<string, string> other { get; set; } = new Dictionary<string, string>();
     }
 }