CrazyIter_Bin 4 years ago
parent
commit
a1c62d372e

+ 16 - 0
TEAMModelOS.SDK/Models/Cosmos/Common/Inner/SurveyRecord.cs

@@ -0,0 +1,16 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace TEAMModelOS.SDK.Models.Cosmos.Common.Inner
+{
+    public  class SurveyRecord
+    {
+        /// <summary>
+        /// 问卷列表
+        /// </summary>
+        public List<List<string>> ans { get; set; }
+        public long time { get; set; }
+        public string userid { get; set; }
+    }
+}

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

@@ -66,8 +66,12 @@ namespace TEAMModelOS.SDK.Models
         public long updateTime { get; set; }
         //将问题放入Blob  hbcn/survey/问卷调查id.json  存放内容 Question的数组
         public string blobUrl { get; set; }
+        // public List<Question> questions { get; set; }
+        /// <summary>
+        /// 学生作答记录/ 状态为finish时进行结算
+        /// </summary>
+        public string recordUrl { get; set; }
         
-       // public List<Question> questions { get; set; }
 
     }
 

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

@@ -18,6 +18,7 @@ using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
 using TEAMModelOS.Models;
 using Microsoft.Extensions.Options;
 using TEAMModelOS.Filter;
+using TEAMModelOS.Services.Common;
 
 namespace TEAMModelOS.Controllers
 {
@@ -31,18 +32,20 @@ namespace TEAMModelOS.Controllers
     [ApiController]
     public class SurveyController : ControllerBase
     {
+        private readonly AzureRedisFactory _azureRedis;
         private readonly AzureCosmosFactory _azureCosmos;
         private readonly SnowflakeId _snowflakeId;
         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)
+        public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis)
         {
             _snowflakeId= snowflakeId;
             _serviceBus = serviceBus;
             _azureCosmos = azureCosmos;
             _dingDing = dingDing;
             _option = option?.Value;
+            _azureRedis = azureRedis;
         }
 
 
@@ -273,5 +276,30 @@ namespace TEAMModelOS.Controllers
                 return BadRequest(e.StackTrace);
             }
         }
+
+        /// <summary>
+        /// 问卷答案提交
+        /// </summary>
+        /// <redis>
+        /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY  key:"Vote:Count:AAA",value:"A",score:1
+        /// 投票活动 投票记录  使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
+        /// </redis>
+        /// <param name="request">
+        /// !"id":"aaaa"
+        /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
+        /// !"record":[["A","B"],["A"],["D"],[],["建议提升服务质量"]]
+        /// </param>
+        /// <returns>
+        /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
+        /// </returns>
+        [ProducesDefaultResponseType]
+        [HttpPost("answer")]
+        [AuthToken(Roles = "teacher,student")]
+        public async Task<IActionResult> Answer(JsonElement request)
+        {
+            var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
+            int msgid = await ActivityStudentService.Answer(request, _azureCosmos, _azureRedis, userid);
+            return Ok(new { msgid });
+        }
     }
 }

+ 0 - 16
TEAMModelOS/Models/Dto/SurveyDto.cs

@@ -1,16 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
-using TEAMModelOS.Models;
-using TEAMModelOS.SDK.Models;
-
-namespace TEAMModelOS.Models.Dto
-{
-    public class SurveyDto
-    {
-        public Survey survey { get; set; }
-
-         public bool reset { get; set; }
-    }
-}

+ 0 - 14
TEAMModelOS/Models/Dto/VoteDto.cs

@@ -1,14 +0,0 @@
-using System;
-using System.Collections.Generic;
-using System.Linq;
-using System.Threading.Tasks;
-using TEAMModelOS.SDK.Models;
-
-namespace TEAMModelOS.Models.Dto
-{
-    public class VoteDto
-    {
-        public Vote vote { get; set; }
-        public bool reset { get; set; }
-    }
-}

+ 54 - 0
TEAMModelOS/Services/Common/ActivityStudentService.cs

@@ -429,6 +429,60 @@ namespace TEAMModelOS.Services.Common
             }
             return (datas, continuationTokenSchool,continuationTokenTeacher);
         }
+
+        public static async Task<int> Answer(JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid)
+        {
+
+            DateTimeOffset now = DateTimeOffset.UtcNow;
+            long curr = now.ToUnixTimeMilliseconds();
+            byte msgid = 0;//
+            //活动id
+            if (!request.TryGetProperty("id", out JsonElement id))
+            {
+                return msgid;
+            }
+            //活动分区
+            if (!request.TryGetProperty("code", out JsonElement code))
+            {
+                return msgid;
+            }
+
+            try
+            {
+                //1.再次检查投票
+                var client = _azureCosmos.GetCosmosClient();
+                Survey survey = null;
+                ///TODO 检查是否在投票范围内,包括在tmdids 及班级  但是需要处理认证金钥中的班级问题
+                await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Survey>(queryText: $"select c.id,c.code , c.progress,c.times,c.voteNum,c.startTime,c.endTime from c where c.id = '{id}'",
+                    requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
+                {
+                    survey = item;
+                    break;
+                }
+                if (survey != null)
+                {
+                    //判断投票时间是否在起止时间内
+                    if (curr >= survey.startTime && curr <= survey.endTime)
+                    {
+                        if (request.TryGetProperty("record", out JsonElement record)) 
+                        { 
+
+                        }
+                        
+                    }
+                    else
+                    {
+                        msgid = 2;
+                    }
+                }
+            }
+            catch (Exception e)
+            {
+                throw new Exception(e.StackTrace);
+            }
+            return msgid;
+        }
+
         private async static Task<dynamic> DoVoteTips(ActivityData commonData , AzureCosmosFactory _azureCosmos,string userid,AzureRedisFactory _azureRedis)
         {
             Vote vote=null;