Bläddra i källkod

投票活动查询

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

+ 8 - 2
TEAMModelOS/Controllers/Common/VoteController.cs

@@ -69,7 +69,6 @@ namespace TEAMModelOS.Controllers.Learn
                 //新增Vote
                 var client = _azureCosmos.GetCosmosClient();
                 request.code = request.pk + "-" + request.code;
-                
                 long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
                 request.createTime = now;
                 if (string.IsNullOrEmpty(request.id))
@@ -82,7 +81,6 @@ namespace TEAMModelOS.Controllers.Learn
                     else { 
                         request.progress = "going"; 
                     }
-                        
                     request = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request, new PartitionKey($"{request.code}"));
                 }
                 else
@@ -92,6 +90,14 @@ namespace TEAMModelOS.Controllers.Learn
                     {
                         return Ok(new { v = "活动正在进行中" });
                     }
+                    if (request.startTime > now)
+                    {
+                        request.progress = "pending";
+                    }
+                    else
+                    {
+                        request.progress = "going";
+                    }
                     request.progress = info.progress;
                     request = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(request, info.id, new PartitionKey($"{info.code}"));
                 }

+ 1 - 1
TEAMModelOS/Controllers/School/StudentCommonController.cs

@@ -45,7 +45,7 @@ namespace TEAMModelOS.Controllers
         /// <returns></returns>
         [ProducesDefaultResponseType]
         [HttpPost("stu-find-activity")]
-        [AuthToken(Roles = "student")]
+       // [AuthToken(Roles = "student")]
         public async Task<IActionResult> FindStu(JsonElement request)
         {
             var (id, name, pic,school) = HttpContext.GetAuthTokenInfo();

+ 20 - 11
TEAMModelOS/Services/Common/ActivityStudentService.cs

@@ -76,15 +76,13 @@ namespace TEAMModelOS.Services.Common
                             case "once":
                                 // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
                                 Field = $"{userid}-once";
-                                RedisValue[] values = _azureRedis.GetRedisClient(8).HashValues($"Vote:Record:{vote.id}_{vote.code}");
+                                HashEntry[] values = _azureRedis.GetRedisClient(8).HashGetAll($"Vote:Record:{vote.id}_{vote.code}");
                                 if (values != null  && values.Length > 0)
                                 {
-                                    
                                     value = new RedisValue();
                                     foreach (var val in values) {
-                                        VoteRecord record = val.ToString().ToObject<VoteRecord>();
-                                        if (record.userid==userid) {
-                                            value = val;
+                                        if (val.Name.ToString() == Field) {
+                                            value = val.Value;
                                             break;
                                         }
                                     }
@@ -142,6 +140,8 @@ namespace TEAMModelOS.Services.Common
                     record.userid = userid;
                     //保存投票记录
                     bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}_{vote.code}", Field, record.ToJsonString());
+                    //单独保存每个人方便查询的记录
+                    bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}_{vote.code}:{userid}", Field, record.ToJsonString());
                     //当前投票分组计数存入活动的Redis
                     var group_opt= option.GroupBy(x => x);
                     foreach (var opt in group_opt) {
@@ -161,6 +161,8 @@ namespace TEAMModelOS.Services.Common
                     //保存投票记录
                     VoteRecord record = new VoteRecord { opt = option, time = curr, userid = userid };
                     bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}_{vote.code}", Field, record.ToJsonString());
+                    //单独保存每个人方便查询的记录
+                    bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}_{vote.code}:{userid}", Field, record.ToJsonString());
                     //当前投票分组计数存入活动的Redis
                     var group_opt = option.GroupBy(x => x);
                     foreach (var opt in group_opt)
@@ -212,11 +214,18 @@ namespace TEAMModelOS.Services.Common
         /// <returns></returns>
         public static async Task<(List<ActivityData> datas, string continuationTokenSchool,string continuationTokenTeacher)> FindAsStu( JsonElement requert, string id,string school, AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis)
         {
+            if (string.IsNullOrWhiteSpace(id)) {
+                id = requert.GetProperty("userid").GetString();
+            }
+            if (string.IsNullOrWhiteSpace(school))
+            {
+                school = requert.GetProperty("school").GetString();
+            }
             //开始时间,默认最近三十天
             var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
             if (!requert.TryGetProperty("stime", out JsonElement stime))
             {
-                if (stime.TryGetInt64(out long data))
+                if (!stime.ValueKind.Equals(JsonValueKind.Undefined) && !stime.ValueKind.Equals(JsonValueKind.Null) &&stime.TryGetInt64(out long data))
                 {
                     stimestamp = data;
                 } 
@@ -249,7 +258,7 @@ namespace TEAMModelOS.Services.Common
             int? topcout = null;
             if (!requert.TryGetProperty("count", out JsonElement jcount))
             {
-                if (jcount.TryGetInt32(out int data))
+                if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
                 {
                     topcout = data;
                 } 
@@ -283,7 +292,7 @@ namespace TEAMModelOS.Services.Common
             string joinSqlClasses = "";
             string andSqlClasses = "";
             List<string> classes=null;
-            if (!requert.TryGetProperty("classes", out JsonElement jclasses))
+            if ( requert.TryGetProperty("classes", out JsonElement jclasses))
             {
                 if (jclasses.ValueKind is JsonValueKind.Array ) {
                     classes = jclasses.ToObject<List<string>>();
@@ -307,7 +316,7 @@ namespace TEAMModelOS.Services.Common
             //科目
             string joinSqlSubjects = "";
             string andSqlSubjects = "";
-            if (!requert.TryGetProperty("subjects", out JsonElement jsubjects))
+            if ( requert.TryGetProperty("subjects", out JsonElement jsubjects))
             {
                 if (jsubjects.ValueKind is JsonValueKind.Array)
                 {
@@ -325,7 +334,7 @@ namespace TEAMModelOS.Services.Common
             List<ActivityData> datas = new List<ActivityData>();
             var client = _azureCosmos.GetCosmosClient();
             //查询数据归属学校的
-            await foreach (var item in client.GetContainer("TEAMModelOS","School").GetItemQueryStreamIterator(querySchool, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Activity-{id}") }))
+            await foreach (var item in client.GetContainer("TEAMModelOS","School").GetItemQueryStreamIterator(querySchool, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Activity-{school}") }))
             {
                 using var json = await JsonDocument.ParseAsync(item.ContentStream);
                 if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
@@ -365,7 +374,7 @@ namespace TEAMModelOS.Services.Common
             bool tips = false;
             if (!requert.TryGetProperty("tips", out JsonElement jtips))
             {
-                if (!jtips.ValueKind.Equals(JsonValueKind.Null) && !jtips.ValueKind.Equals(JsonValueKind.True))
+                if (!jtips.ValueKind.Equals(JsonValueKind.Undefined) && !jtips.ValueKind.Equals(JsonValueKind.Null) && !jtips.ValueKind.Equals(JsonValueKind.True))
                 {
                     tips = jtips.GetBoolean();
                 }