Browse Source

结算投票结果

CrazyIter_Bin 4 years ago
parent
commit
23911806b2

+ 12 - 7
TEAMModelFunction/TriggerVote.cs

@@ -3,6 +3,7 @@ using Azure.Messaging.ServiceBus;
 using Microsoft.Azure.Documents;
 using System;
 using System.Collections.Generic;
+using System.Linq;
 using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
@@ -11,6 +12,7 @@ using TEAMModelOS.SDK.Extension;
 using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 using TEAMModelOS.SDK.Models;
 using TEAMModelOS.SDK.Models.Cosmos;
+using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
 
 namespace TEAMModelFunction
 {
@@ -137,17 +139,20 @@ namespace TEAMModelFunction
                             }
                         }
                         List<Task<string>> tasks = new List<Task<string>>();
-                        List<dynamic> recordsBlob = new List<dynamic>();
+                        List<VoteRecord> recordsBlob = new List<VoteRecord>();
                         foreach (var rcd in records) {
-                            var key = rcd.Name.ToString().Split("-")[0];
-                            var value = rcd.Value.ToString().ToObject<JsonElement>();
-                            recordsBlob.Add(new { key,value});
-                            tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, value.ToJsonString(), "vote", $"{vote.id}/{key}.json"));
+                            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}/index.json";
+                        string url = $"vote/{vote.id}/record.json";
                         vote.recordUrl = url;
-                        tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/index.json"));
+                        tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, recordsBlob.ToJsonString(), "vote", $"{vote.id}/record.json"));
                         //处理投票者的记录
                         await Task.WhenAll(tasks);
                         if (vote.scope == "school")

+ 2 - 0
TEAMModelOS.SDK/Models/Cosmos/Common/Inner/VoteRecord.cs

@@ -9,5 +9,7 @@ namespace TEAMModelOS.SDK.Models.Cosmos.Common.Inner
         public Dictionary<string, int> opt { get; set; } = new Dictionary<string, int>();
         public long time { get; set; }
         public string userid { get; set; }
+        public string times { get; set; }
+        public string endpoint { get; set; }
     }
 }

+ 35 - 25
TEAMModelOS/Controllers/Common/VoteController.cs

@@ -331,17 +331,6 @@ namespace TEAMModelOS.Controllers.Learn
         //[AuthToken(Roles = "teacher,student")]
         public async Task<IActionResult> Record(JsonElement request)
         {
-            string uid = null;
-            if (request.TryGetProperty("userid", out JsonElement userid)) {
-                if (userid.ValueKind.Equals(JsonValueKind.String)) {
-                    uid = userid.GetString();
-                }
-            }
-            //if (string.IsNullOrWhiteSpace(uid)) {
-            //    var (auid, _, _, _) = HttpContext.GetAuthTokenInfo();
-            //    uid = auid;
-            //}
-           
             if (!request.TryGetProperty("id", out JsonElement id))
             {
                 return BadRequest();
@@ -366,23 +355,44 @@ namespace TEAMModelOS.Controllers.Learn
             List<JsonElement> res = new List<JsonElement>();
             foreach (var rcd in records)
             {
-                var key = rcd.Name.ToString().Split("-")[0];
-                if (!string.IsNullOrWhiteSpace(uid))
-                {
-                    if (uid.Equals(key))
-                    {
-                        var value = rcd.Value.ToString().ToObject<JsonElement>();
-                        res.Add(value);
-                    }
-                }
-                else {
-                    var value = rcd.Value.ToString().ToObject<JsonElement>();
-                    res.Add(value);
-                }
+                var value = rcd.Value.ToString().ToObject<JsonElement>();
+                res.Add(value);
             }
-            return Ok(new { options, records= res,now =DateTimeOffset.UtcNow });
+            return Ok(new { options, records= res});
         }
 
+        /// <summary>
+        /// 个人已投票记录查询
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("decided")]
+        [AuthToken(Roles = "teacher,student")]
+        public async Task<IActionResult> Decided(JsonElement request)
+        {
+            var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
+            if (!request.TryGetProperty("id", out JsonElement id))
+            {
+                return BadRequest();
+            }
+            //活动分区
+            if (!request.TryGetProperty("code", out JsonElement code))
+            {
+                return BadRequest();
+            }
+            //获取投票活动的所有投票记录
+            var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{id}:{userid}");
+            List<JsonElement> res = new List<JsonElement>();
+            foreach (var rcd in records)
+            {
+                var value = rcd.Value.ToString().ToObject<JsonElement>();
+                res.Add(value);
+            }
+            return Ok(new { records= res });
+        }
+
+
         /// <summary>
         /// 投票
         /// </summary>

+ 12 - 9
TEAMModelOS/Services/Common/ActivityStudentService.cs

@@ -86,32 +86,33 @@ namespace TEAMModelOS.Services.Common
                                             break;
                                         }
                                     }
-                                    msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid);
+                                    msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid,vote.times, "once");
                                 }
                                 else
                                 {
-                                    msgid = await VoteIng(vote, new RedisValue(), msgid, option, Field, curr, _azureRedis, userid);
+                                    msgid = await VoteIng(vote, new RedisValue(), msgid, option, Field, curr, _azureRedis, userid, vote.times, "once");
                                 }
                                 break;
                             case "day": //周期内每天
                                 Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
-                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis,userid);
+                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis,userid, vote.times, now.ToString("yyyyMMdd"));
                                 break;
                             case "week": //自然周
-                                Field = $"{userid}-week-{now.ToString("yyyy")}{GetWeek(now)}";
+                                int week = GetWeek(now);
+                                Field = $"{userid}-week-{now.ToString("yyyy")}{week}";
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
-                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid); 
+                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times,$"{now.ToString("yyyy")}{week}"); 
                                 break;
                             case "month":  //月份
                                 Field = $"{userid}-month-{now.ToString("yyyyMM")}";
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
-                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid); 
+                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyyMM")); 
                                 break;
                             case "year"://年份
                                 Field = $"{userid}-year-{now.ToString("yyyy")}";
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
-                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid);
+                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyy"));
                                 break;
                         }
                     }
@@ -127,7 +128,7 @@ namespace TEAMModelOS.Services.Common
             }
             return msgid;
         }
-        public static async Task<byte> VoteIng(Vote vote, RedisValue value, byte msgid, Dictionary<string, int> option, string Field, long curr, AzureRedisFactory _azureRedis,string userid)
+        public static async Task<byte> VoteIng(Vote vote, RedisValue value, byte msgid, Dictionary<string, int> option, string Field, long curr, AzureRedisFactory _azureRedis,string userid,string times,string endpoint)
         {
             if (!value.IsNullOrEmpty)
             {
@@ -156,6 +157,8 @@ namespace TEAMModelOS.Services.Common
                     }
                     record.time = curr;
                     record.userid = userid;
+                    record.times = times;
+                    record.endpoint = endpoint;
                     //保存投票记录
                     bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
                     //单独保存每个人方便查询的记录
@@ -177,7 +180,7 @@ namespace TEAMModelOS.Services.Common
                 if (option.Count <= vote.voteNum)
                 {
                     //保存投票记录
-                    VoteRecord record = new VoteRecord { opt = option, time = curr, userid = userid };
+                    VoteRecord record = new VoteRecord { opt = option, time = curr, userid = userid ,times=times,endpoint=endpoint};
                     bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
                     //单独保存每个人方便查询的记录
                     bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}:{userid}", Field, record.ToJsonString());