Browse Source

处理问卷调查

CrazyIter_Bin 4 years ago
parent
commit
2d6bed7429

+ 1 - 1
TEAMModelFunction/MonitorCosmosDB.cs

@@ -67,7 +67,7 @@ namespace TEAMModelFunction
                                 TriggerVote.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school, _azureRedis);
                                 break;
                             case "Survey":
-                                TriggerSurvey.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school);
+                                TriggerSurvey.Trigger(_azureCosmos, _serviceBus, _azureStorage, _dingDing, client, input, code, stime, etime, school, _azureRedis);
                                 break;
 
                         }

+ 11 - 4
TEAMModelFunction/TriggerSurvey.cs

@@ -17,7 +17,7 @@ namespace TEAMModelFunction
    public class TriggerSurvey
     {
         public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
-               CosmosClient client, Document input, string code, long stime, long etime, string school)
+               CosmosClient client, Document input, string code, long stime, long etime, string school,AzureRedisFactory _azureRedis)
         {
             Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
             List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", survey.progress } });
@@ -53,7 +53,7 @@ namespace TEAMModelFunction
                     break;
                 case "going":
                     ActivityData data;
-                    if (survey.scope == "school" || survey.scope == "teacher")
+                    if (survey.scope == "school")
                     {
                         data = new ActivityData
                         {
@@ -105,7 +105,7 @@ namespace TEAMModelFunction
                     else
                     {
                         long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
-                        ChangeRecord changeRecord = new ChangeRecord
+                        ChangeRecord changeRecord                                                                                                                                                                                                                                                   = new ChangeRecord
                         {
                             RowKey = input.Id,
                             PartitionKey = "going",
@@ -116,7 +116,14 @@ namespace TEAMModelFunction
                     }
                     break;
                 case "finish":
-
+                    var records =await  _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}_{survey.code}");
+                    List<dynamic> recs = new List<dynamic>();
+                    foreach (var rcd in records) {
+                        //var key =int.Parse(rcd.Name.ToString());
+                        var value = rcd.Value.ToString().ToObject<JsonElement>();
+                        recs.Add(new { index = rcd.Name.ToString(), count = value });
+                    }
+                    await  _azureStorage.UploadFileByContainer(survey.owner, recs.ToJsonString(), "vote", $"{survey.id}/record.json");
                     break;
             }
         }

+ 3 - 3
TEAMModelFunction/TriggerVote.cs

@@ -59,7 +59,7 @@ namespace TEAMModelFunction
                         break;
                     case "going":
                         ActivityData data;
-                        if (vote.scope == "school" || vote.scope == "teacher")
+                        if (vote.scope == "school" )
                         {
                             data = new ActivityData
                             {
@@ -150,7 +150,7 @@ namespace TEAMModelFunction
                         tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/index.json"));
                         //处理投票者的记录
                         await Task.WhenAll(tasks);
-                        if (vote.scope == "school" || vote.scope == "teacher")
+                        if (vote.scope == "school")
                         {
                             await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<Vote>(vote,vote.id, new Azure.Cosmos.PartitionKey(vote.code));
                         }
@@ -159,7 +159,7 @@ namespace TEAMModelFunction
                             await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
                         }
                         //更新结束状态
-                        if (vote.scope == "school" || vote.scope == "teacher")
+                        if (vote.scope == "school")
                         {
                             data = new ActivityData
                             {

+ 1 - 67
TEAMModelOS.SDK/Models/Cosmos/Common/Survey.cs

@@ -98,7 +98,7 @@ namespace TEAMModelOS.SDK.Models
         /// <summary>
         /// 问卷选项
         /// </summary>
-        public List<OptionSurvey> options { get; set; }
+        public List<CodeValue> options { get; set; }
         /// <summary>
         /// 判断judge  多选multiple 单选single
         /// </summary>
@@ -108,70 +108,4 @@ namespace TEAMModelOS.SDK.Models
         /// </summary>
         public bool required { get; set; }
     }
-
-    public class OptionSurvey{
-        /// <summary>
-        /// 选项编码
-        /// </summary>
-        public string code { get; set; }
-        /// <summary>
-        /// 选项文本
-        /// </summary>
-        public string value { get; set; }
-        /// <summary>
-        /// 选项描述
-        /// </summary>
-        //public string desc { get; set; }
-        /// <summary>
-        /// 选择数
-        /// </summary>
-        public int? count { get; set; }
-        /// <summary>
-        /// 其他答案数量
-        /// </summary>
-        public int? other { get; set; }
-    }
-
-
-
-
-
-    public class QuestionResult
-    {
-        public double finish { get; set; }
-        public double finishRate { get; set; }
-    }
-    public class Result { 
-        public double count { get; set; }
-        public double rate { get; set; }
-    }
-    public class Classes { 
-        public string code { get; set; }
-        public string id { get; set; }
-        public string name { get; set; }
-        public string scope { get; set; }
-        public Result result { get; set; }
-        public List<StudentInfo> students { get; set; }
-        public List<AnswerRate> answers { get; set; }
-    }
-    public class AnswerRate {
-        public string qid { get; set; }
-        public double answerRate { get; set; }
-        public List<CodeValue> option { get; set; }
-    }
-    public class StudentInfo {
-        public string id { get; set; }
-        public string name { get; set; }
-        public long finishTime { get; set; }
-        public List<AnswerInfo> answers { get; set; }
-        public ResultInfo result { get; set; }
-    }
-    public class ResultInfo
-    {
-        public double answerRate { get; set; }
-    }
-    public class AnswerInfo { 
-        public string qid { get; set; }
-        public string answer { get; set; }
-    }
 }

+ 4 - 2
TEAMModelOS/Controllers/Common/SurveyController.cs

@@ -38,7 +38,8 @@ namespace TEAMModelOS.Controllers
         private readonly AzureServiceBusFactory _serviceBus;
         private readonly DingDing _dingDing;
         private readonly Option _option;
-        public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis)
+        private readonly AzureStorageFactory _azureStorage;
+        public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
         {
             _snowflakeId= snowflakeId;
             _serviceBus = serviceBus;
@@ -46,6 +47,7 @@ namespace TEAMModelOS.Controllers
             _dingDing = dingDing;
             _option = option?.Value;
             _azureRedis = azureRedis;
+            _azureStorage = azureStorage;
         }
 
 
@@ -322,7 +324,7 @@ namespace TEAMModelOS.Controllers
         public async Task<IActionResult> Answer(JsonElement request)
         {
             var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
-            int msgid = await ActivityStudentService.Answer(request, _azureCosmos, _azureRedis, userid);
+            int msgid = await ActivityStudentService.Answer(request, _azureCosmos, _azureRedis, userid, _azureStorage);
             return Ok(new { msgid });
         }
     }

+ 51 - 2
TEAMModelOS/Services/Common/ActivityStudentService.cs

@@ -430,7 +430,7 @@ namespace TEAMModelOS.Services.Common
             return (datas, continuationTokenSchool,continuationTokenTeacher);
         }
 
-        public static async Task<int> Answer(JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid)
+        public static async Task<int> Answer(JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid, AzureStorageFactory _azureStorage)
         {
 
             DateTimeOffset now = DateTimeOffset.UtcNow;
@@ -469,7 +469,56 @@ namespace TEAMModelOS.Services.Common
                             var recs = record.ToObject<List<List<string>>>();
                             if (recs.IsNotEmpty() && recs.Count == survey.ans.Count)
                             {
-
+                               //处理问卷调查表的每一题选项数
+                                for (int index = 0; index < recs.Count; index++) {
+                                    Dictionary<string, int> dict = new Dictionary<string, int>();
+                                    if (recs[index].IsNotEmpty()) {
+                                        recs[index].ForEach(x => {
+                                            if (survey.ans[index].Contains(x))
+                                            {
+                                                if (dict.ContainsKey(x))
+                                                {
+                                                    dict[x] = dict[x] + 1;
+                                                }
+                                                else {
+                                                    dict[x] = 1;
+                                                }
+                                            }
+                                            else {
+                                                if (dict.ContainsKey("other"))
+                                                {
+                                                    dict["other"] = dict["other"] + 1;
+                                                }
+                                                else
+                                                {
+                                                    dict["other"] = 1;
+                                                }
+                                            }
+                                        });
+                                    }
+                                    var value= azureRedis.GetRedisClient(8).HashGet($"Survey:Record:{survey.id}_{survey.code}",index);
+                                    if (value != default && !value.IsNullOrEmpty)
+                                    {
+                                        Dictionary<string, int> dt = value.ToString().ToObject<Dictionary<string, int>>();
+                                        foreach (var kp in dict)
+                                        {
+                                            if (dt.ContainsKey(kp.Key))
+                                            {
+                                                dt[kp.Key] = dt[kp.Key] + kp.Value;
+                                            }
+                                            else
+                                            {
+                                                dt.Add(kp.Key, kp.Value);
+                                            }
+                                        }
+                                        await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}_{survey.code}", index, dt.ToJsonString());
+                                    }
+                                    else {
+                                       await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}_{survey.code}", index, dict.ToJsonString());
+                                    }
+                                }
+                                //保存当前提交人的记录
+                               await _azureStorage.UploadFileByContainer(survey.owner, record.ToJsonString(), "survey", $"{survey.name}/record/{userid}.json");
                             }
                             else {
                                 //提交的作答不符合问卷的答案长度。