Bläddra i källkod

调整结算逻辑,避免死循环

CrazyIter_Bin 4 år sedan
förälder
incheckning
c02b326db0

+ 6 - 2
TEAMModelFunction/TriggerSurvey.cs

@@ -189,8 +189,12 @@ 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 (string.IsNullOrEmpty(survey.recordUrl)) {
+                        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")
                     {

+ 28 - 23
TEAMModelFunction/TriggerVote.cs

@@ -27,8 +27,8 @@ namespace TEAMModelFunction
             if (vote.ttl >= 1)
             {
                 //TODO  处理TTL删除业务
-                _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}_{vote.code}");
-                _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}_{vote.code}");
+                _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}");
+                _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
                 return;
             }
             else {
@@ -125,37 +125,42 @@ namespace TEAMModelFunction
                         break;
                     case "finish":
                         //获取投票活动的所有投票记录
-                        var records= await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}_{vote.code}");
+                        var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
                         //获取投票活动的选项及投票数
-                        var counts= _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}_{vote.code}");
-                        if (counts != null && counts.Length > 0) {
-                            foreach (var count in counts) {
-                                vote.options.ForEach(x => {
-                                    //重新赋值
-                                    if (x.code.Equals(count.Element.ToString())) {
-                                        x.count = (int)count.Score;
-                                    }
-                                });
+                        var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
+                        List<dynamic> countcds = new List<dynamic>();
+                        if (counts != null && counts.Length > 0)
+                        {
+                            foreach (var count in counts)
+                            {
+                                countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
                             }
                         }
                         List<Task<string>> tasks = new List<Task<string>>();
                         List<VoteRecord> recordsBlob = new List<VoteRecord>();
-                        foreach (var rcd in records) {
+                        foreach (var rcd in records)
+                        {
                             var value = rcd.Value.ToString().ToObject<VoteRecord>();
                             recordsBlob.Add(value);
                         }
                         //分组每个人的 
-                        var gp= recordsBlob.GroupBy(x => x.userid).Select(x=>new { key=x.Key,list=x.ToList()});
-                        foreach (var g in gp) { 
+                        var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
+                        foreach (var g in gp)
+                        {
                             tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
                         }
-                        //处理活动方的记录
-                        string url = $"vote/{vote.id}/record.json";
-                        vote.recordUrl = url;
-                        tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/record.json"));
+                        //处理活动方的记录, 
+                        string url = $"/vote/{vote.id}/record.json";
+                      
+                        tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
                         //处理投票者的记录
                         await Task.WhenAll(tasks);
-                        await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
+                        //
+                        if (string.IsNullOrEmpty(vote.recordUrl))
+                        {
+                            vote.recordUrl = url;
+                            await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
+                        }
                         //更新结束状态
                         if (vote.scope == "school")
                         {
@@ -174,9 +179,9 @@ namespace TEAMModelFunction
                                 tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
                                 owner = vote.owner,
                                 subjects = new List<string> { "" }
-                                
+
                             };
-                            await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
+                            await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
                         }
                         else if (vote.scope == "private")
                         {
@@ -197,7 +202,7 @@ namespace TEAMModelFunction
                                 tmdids = new List<string> { "" },
                                 subjects = new List<string> { "" }
                             };
-                            await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data,data.id, new Azure.Cosmos.PartitionKey(data.code));
+                            await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
                         }
                         break;
                 }

+ 0 - 5
TEAMModelOS.SDK/Models/Cosmos/Common/Vote.cs

@@ -116,11 +116,6 @@ namespace TEAMModelOS.SDK.Models
         /// 投票对象描述
         /// </summary>
         public string desc { get; set; }
-        /// <summary>
-        /// 得票数量
-        /// </summary>
-        public int? count { get; set; } = 0;
     }
-    
 }
 

+ 130 - 3
TEAMModelOS/Controllers/Common/VoteController.cs

@@ -44,9 +44,10 @@ namespace TEAMModelOS.Controllers.Learn
         private readonly AzureServiceBusFactory _serviceBus;
         private readonly DingDing _dingDing;
         private readonly Option _option;
+        private readonly AzureStorageFactory _azureStorage;
 
         public VoteController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
-            AzureRedisFactory azureRedis)
+            AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
         {
             _azureCosmos = azureCosmos;
             _serviceBus = serviceBus;
@@ -54,6 +55,7 @@ namespace TEAMModelOS.Controllers.Learn
             _dingDing = dingDing;
             _option = option?.Value;
             _azureRedis = azureRedis;
+            _azureStorage = azureStorage;
         }
         /// <summary>
         /// 新增 或 修改投票活动
@@ -344,12 +346,12 @@ namespace TEAMModelOS.Controllers.Learn
             var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{id}");
             //获取投票活动的选项及投票数
             var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{id}");
-            List<OptionVote> options = new List<OptionVote>();
+            List<dynamic> options = new List<dynamic>();
             if (counts != null && counts.Length > 0)
             {
                 foreach (var count in counts)
                 {
-                    options.Add(new OptionVote { code = count.Element.ToString(), count = (int)count.Score });
+                    options.Add(new   { code = count.Element.ToString(), count = (int)count.Score });
                 }
             }
             List<JsonElement> res = new List<JsonElement>();
@@ -447,5 +449,130 @@ namespace TEAMModelOS.Controllers.Learn
             }
             else { return Ok(new { msgid = 0 }); }
         }
+
+        /// <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();
+                Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}"));
+                if (vote != null)
+                {
+                    ActivityData data;
+
+                    if (vote.progress == "finish")
+                    {
+                        //获取投票活动的所有投票记录
+                        var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
+                        //获取投票活动的选项及投票数
+                        var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
+                        List<dynamic> countcds = new List<dynamic>();
+                        if (counts != null && counts.Length > 0)
+                        {
+                            foreach (var count in counts)
+                            {
+                                countcds.Add(new { code= count.Element.ToString() , count = (int)count.Score });
+                            }
+                        }
+                        List<Task<string>> tasks = new List<Task<string>>();
+                        List<VoteRecord> recordsBlob = new List<VoteRecord>();
+                        foreach (var rcd in records)
+                        {
+                            var value = rcd.Value.ToString().ToObject<VoteRecord>();
+                            recordsBlob.Add(value);
+                        }
+                        //分组每个人的 
+                        var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
+                        foreach (var g in gp)
+                        {
+                            tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
+                        }
+                        //处理活动方的记录, 
+                        string url = $"/vote/{vote.id}/record.json";
+                        vote.recordUrl = url;
+                        tasks.Add(_azureStorage.UploadFileByContainer(vote.owner,new{ options = countcds, records = recordsBlob }   .ToJsonString(), "vote", $"{vote.id}/record.json"));
+                        //处理投票者的记录
+                        await Task.WhenAll(tasks);
+                        //
+                        if (string.IsNullOrEmpty(vote.recordUrl))
+                        {
+                            await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
+                        }
+                        //更新结束状态
+                        if (vote.scope == "school")
+                        {
+                            data = new ActivityData
+                            {
+                                id = vote.id,
+                                code = $"Activity-{vote.owner}",
+                                type = "vote",
+                                name = vote.name,
+                                startTime = vote.startTime,
+                                endTime = vote.endTime,
+                                scode = vote.code,
+                                scope = vote.scope,
+                                progress = "finish",
+                                classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
+                                tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
+                                owner = vote.owner,
+                                subjects = new List<string> { "" }
+
+                            };
+                            await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
+                        }
+                        else if (vote.scope == "private")
+                        {
+                            //更新结束状态
+                            data = new ActivityData
+                            {
+                                id = vote.id,
+                                code = $"Activity-Common",
+                                type = "vote",
+                                name = vote.name,
+                                startTime = vote.startTime,
+                                endTime = vote.endTime,
+                                scode = vote.code,
+                                scope = vote.scope,
+                                progress = "finish",
+                                classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
+                                owner = vote.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 { vote });
+                }
+                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);
+            }
+        }
     }
 }

+ 1 - 0
TEAMModelOS/TEAMModelOS.csproj

@@ -16,6 +16,7 @@
     <Folder Include="JwtRsaFile\" />
     <Folder Include="logfile\" />
     <Folder Include="Services\Evaluation\" />
+    <Folder Include="wwwroot\" />
   </ItemGroup>  
   <ItemGroup>
     <ProjectReference Include="..\TEAMModelOS.SDK\TEAMModelOS.SDK.csproj" />