Browse Source

增加时间,及完成状态。

CrazyIter_Bin 3 years ago
parent
commit
64ce64fa48

+ 0 - 1
TEAMModelOS.SDK/Models/Cosmos/Common/StuActivity.cs

@@ -63,6 +63,5 @@ namespace TEAMModelOS.SDK.Models.Cosmos.Common
         /// 任务完成状态,-1 未参与,0,未完成, 已完成
         /// </summary>
         public int taskStatus { get; set; } = -1;
-
     }
 }

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

@@ -427,10 +427,10 @@ namespace TEAMModelOS.Controllers
         [AuthToken(Roles = "teacher,student")]
         public async Task<IActionResult> Answer(JsonElement request)
         {
-            //  var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
-            if (request.TryGetProperty("userid", out JsonElement userid)) {
-                int msgid = await ActivityStudentService.Answer(request, _azureCosmos, _azureRedis, $"{userid}", _azureStorage);
-                return Ok(new { msgid });
+            var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
+            if (request.TryGetProperty("userid", out JsonElement _userid)) {
+                (int msgid ,int taskStatus) = await ActivityStudentService.Answer(request, _azureCosmos, _azureRedis, $"{userid}",school, _azureStorage);
+                return Ok(new { msgid , taskStatus });
             }
             else { return Ok(new { msgid = 0 }); }
         }

+ 6 - 6
TEAMModelOS/Controllers/Common/VoteController.cs

@@ -493,9 +493,9 @@ namespace TEAMModelOS.Controllers.Learn
         [AuthToken(Roles = "teacher,student")]
         public async Task<IActionResult> Decide(JsonElement request)
         {
-            var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
-            int msgid = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis ,_azureStorage, userid);
-            return Ok(new { msgid });
+            var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
+            (int msgid,int taskStatus) = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis ,_azureStorage, userid, school);
+            return Ok(new { msgid, taskStatus });
         }
         /// <summary>
         /// 投票
@@ -519,10 +519,10 @@ namespace TEAMModelOS.Controllers.Learn
         {
             //var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
             //活动id
-            if (request.TryGetProperty("userid", out JsonElement userid))
+            if (request.TryGetProperty("userid", out JsonElement userid)&& request.TryGetProperty("school", out JsonElement _school))
             {
-                int msgid = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis, _azureStorage, $"{userid}");
-                return Ok(new { msgid });
+                (int msgid, int taskStatus) = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis, _azureStorage, $"{userid}",$"{_school}");
+                return Ok(new { msgid ,taskStatus });
             }
             else { return Ok(new { msgid = 0 }); }
         }

+ 135 - 36
TEAMModelOS/Services/Common/ActivityStudentService.cs

@@ -19,20 +19,19 @@ namespace TEAMModelOS.Services.Common
 {
     public static class ActivityStudentService
     {
-
-       
-        public static async Task<int> Decide(JsonElement request,AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis , AzureStorageFactory _azureStorage, string userid ) {
+        public static async Task<(int msgid,int taskStatus)> Decide(JsonElement request,AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis , AzureStorageFactory _azureStorage, string userid ,string school) {
             Vote vote = null;
             DateTimeOffset now = DateTimeOffset.UtcNow;
             long curr = now.ToUnixTimeMilliseconds();
             byte msgid = 0;//0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
+            int taskStatus = 0;
             //活动id
             if (!request.TryGetProperty("id", out JsonElement id)) {
-                return msgid;
+                return (msgid, -1);
             }
             //活动分区
             if (!request.TryGetProperty("code", out JsonElement code)) {
-                return msgid;
+                return (msgid, taskStatus); 
             }
             Dictionary<string, int> option = new Dictionary<string, int>();
             if (request.TryGetProperty("option", out JsonElement joption))
@@ -41,12 +40,12 @@ namespace TEAMModelOS.Services.Common
                 if (option.IsEmpty())
                 {
                     msgid = 6;
-                    return msgid; 
+                    return (msgid, -1); 
                 }
             }
             else
             {
-                return msgid;
+                return (msgid, -1); 
             }
          
             try
@@ -60,52 +59,82 @@ namespace TEAMModelOS.Services.Common
                     vote = item;
                     break;
                 }
+
                 if (vote != null)
                 {
+
                     //判断投票时间是否在起止时间内
                     if (curr >= vote.startTime && curr <= vote.endTime)
                     {
+                        string endField = null;
+
                         string Field = "";
+                        var endDtae = DateTimeOffset.FromUnixTimeMilliseconds(vote.endTime);
                         RedisValue value;
-                        switch (vote.times) {
+                        switch (vote.times)
+                        {
                             case "once":
                                 // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
                                 Field = $"{userid}-once";
                                 HashEntry[] values = _azureRedis.GetRedisClient(8).HashGetAll($"Vote:Record:{vote.id}");
-                                if (values != null  && values.Length > 0)
+                                if (values != null && values.Length > 0)
                                 {
                                     value = new RedisValue();
-                                    foreach (var val in values) {
-                                        if (val.Name.ToString() == Field) {
+                                    foreach (var val in values)
+                                    {
+                                        if (val.Name.ToString() == Field)
+                                        {
                                             value = val.Value;
                                             break;
                                         }
                                     }
-                                    msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid,vote.times, "once");
+                                    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, vote.times, "once");
                                 }
+                                if (msgid == 1) { taskStatus = 1; }
                                 break;
                             case "day": //周期内每天
                                 Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
+                                endField = $"{userid}-day-{endDtae.ToString("yyyyMMdd")}";
+                                if (Field.Equals(endField))
+                                {
+                                    taskStatus = 1;
+                                }
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
-                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis,userid, vote.times, now.ToString("yyyyMMdd"));
+                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyyMMdd"));
                                 break;
                             case "week": //自然周
                                 int week = GetWeek(now);
+                                int endweek = GetWeek(endDtae);
                                 Field = $"{userid}-week-{now.ToString("yyyy")}{week}";
+                                endField = $"{userid}-week-{endDtae.ToString("yyyy")}{endweek}";
+                                if (Field.Equals(endField))
+                                {
+                                    taskStatus = 1;
+                                }
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
-                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times,$"{now.ToString("yyyy")}{week}"); 
+                                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")}";
+                                endField = $"{userid}-month-{endDtae.ToString("yyyyMM")}";
+                                if (Field.Equals(endField))
+                                {
+                                    taskStatus = 1;
+                                }
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
-                                msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyyMM")); 
+                                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")}";
+                                endField = $"{userid}-year-{endDtae.ToString("yyyy")}";
+                                if (Field.Equals(endField))
+                                {
+                                    taskStatus = 1;
+                                }
                                 value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
                                 msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyy"));
                                 break;
@@ -115,6 +144,36 @@ namespace TEAMModelOS.Services.Common
                     {
                         msgid = 2;
                     }
+                    try
+                    {
+                        if (!string.IsNullOrEmpty(school))
+                        {
+                            StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReadItemAsync<StuActivity>(vote.id, new PartitionKey($"Activity-{school}-{userid}"));
+                            activity.taskStatus = taskStatus;
+                            await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReplaceItemAsync<StuActivity>(activity, vote.id, new PartitionKey($"Activity-{school}-{userid}"));
+                        }
+                        else
+                        {
+                            StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuActivity>(vote.id, new PartitionKey($"Activity-{userid}"));
+                            activity.taskStatus = taskStatus;
+                            await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuActivity>(activity, vote.id, new PartitionKey($"Activity-{userid}"));
+                        }
+
+                    }
+                    catch (CosmosException ex)
+                    {
+                        if (ex.Status == 404)
+                        {
+                            StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuActivity>(vote.id, new PartitionKey($"Activity-{userid}"));
+                            activity.taskStatus = taskStatus;
+                            await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuActivity>(activity, vote.id, new PartitionKey($"Activity-{userid}"));
+                        }
+
+                    }
+                }
+                else
+                {
+                    return (msgid, -1);
                 }
             }
             catch (Exception e)
@@ -162,7 +221,8 @@ namespace TEAMModelOS.Services.Common
                 string url = $"/vote/{vote.id}/record.json";
                 tasks.Add(_azureStorage.UploadFileByContainer(blobcntr, new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
             }
-            return msgid;
+             
+            return (msgid,taskStatus);
         }
 
         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)
@@ -401,21 +461,22 @@ namespace TEAMModelOS.Services.Common
         }
 
         
-        public static async Task<int> Answer(JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid, AzureStorageFactory _azureStorage)
+        public static async Task<(int msgid,int taskStatus)> Answer(JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid,string school, AzureStorageFactory _azureStorage)
         {
 
             DateTimeOffset now = DateTimeOffset.UtcNow;
             long curr = now.ToUnixTimeMilliseconds();
-            byte msgid = 0;//
+            int msgid = 0;//
+            int taskStatus = 0;
             //活动id
             if (!request.TryGetProperty("id", out JsonElement id))
             {
-                return msgid;
+                return (msgid ,-1);
             }
             //活动分区
             if (!request.TryGetProperty("code", out JsonElement code))
             {
-                return msgid;
+                return (msgid, -1);
             }
 
             try
@@ -435,28 +496,33 @@ namespace TEAMModelOS.Services.Common
                     //判断投票时间是否在起止时间内
                     if (curr >= survey.startTime && curr <= survey.endTime)
                     {
-                        if (request.TryGetProperty("record", out JsonElement _record)) 
+                        if (request.TryGetProperty("record", out JsonElement _record))
                         {
                             var recs = _record.ToObject<List<List<string>>>();
                             if (recs.IsNotEmpty() && recs.Count == survey.answers.Count)
                             {
                                 //处理问卷调查表的每一题选项数
-                               // List<Task<string>> tasks = new List<Task<string>>();
-                                for (int index = 0; index < recs.Count; index++) {
+                                // List<Task<string>> tasks = new List<Task<string>>();
+                                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 (recs[index].IsNotEmpty())
+                                    {
+                                        recs[index].ForEach(x =>
+                                        {
                                             if (survey.answers[index].Contains(x))
                                             {
                                                 if (dict.ContainsKey(x))
                                                 {
                                                     dict[x] = dict[x] + 1;
                                                 }
-                                                else {
+                                                else
+                                                {
                                                     dict[x] = 1;
                                                 }
                                             }
-                                            else {
+                                            else
+                                            {
                                                 if (dict.ContainsKey("other"))
                                                 {
                                                     dict["other"] = dict["other"] + 1;
@@ -470,7 +536,7 @@ namespace TEAMModelOS.Services.Common
                                             }
                                         });
                                     }
-                                    var value= azureRedis.GetRedisClient(8).HashGet($"Survey:Record:{survey.id}",index);
+                                    var value = azureRedis.GetRedisClient(8).HashGet($"Survey:Record:{survey.id}", index);
                                     if (value != default && !value.IsNullOrEmpty)
                                     {
                                         Dictionary<string, int> dt = value.ToString().ToObject<Dictionary<string, int>>();
@@ -487,8 +553,9 @@ namespace TEAMModelOS.Services.Common
                                         }
                                         await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dt.ToJsonString());
                                     }
-                                    else {
-                                       await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dict.ToJsonString());
+                                    else
+                                    {
+                                        await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dict.ToJsonString());
                                     }
                                 }
 
@@ -497,15 +564,15 @@ namespace TEAMModelOS.Services.Common
                                 //保存当前提交人的记录
 
                                 string blobcntr = null;
-                                if (survey.scope .Equals("school"))
+                                if (survey.scope.Equals("school"))
                                 {
                                     blobcntr = survey.school;
                                 }
-                                else  
+                                else
                                 {
                                     blobcntr = survey.creatorId;
                                 }
-                                await _azureStorage.UploadFileByContainer(blobcntr, new SurveyRecord { ans= recs, userid=userid, time = curr }.ToJsonString(), "survey", $"{survey.id}/urecord/{userid}.json");
+                                await _azureStorage.UploadFileByContainer(blobcntr, new SurveyRecord { ans = recs, userid = userid, time = curr }.ToJsonString(), "survey", $"{survey.id}/urecord/{userid}.json");
                                 ///bgn 20210805 huanghb 实时结算
                                 await azureRedis.GetRedisClient(8).SetAddAsync($"Survey:Submit:{survey.id}", userid);
 
@@ -582,10 +649,11 @@ namespace TEAMModelOS.Services.Common
                                 //问卷整体情况
                                 await _azureStorage.UploadFileByContainer(blobcntr, cods.ToJsonString(), "survey", $"{survey.id}/record.json");
                                 ///end 20210805 huanghb 实时结算
-
+                                taskStatus = 1;
                                 msgid = 1;
                             }
-                            else {
+                            else
+                            {
                                 //提交的作答不符合问卷的答案长度。
                                 msgid = 3;
                             }
@@ -595,13 +663,44 @@ namespace TEAMModelOS.Services.Common
                     {
                         msgid = 2;
                     }
+                    try
+                    {
+                        if (!string.IsNullOrEmpty(school))
+                        {
+                            StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReadItemAsync<StuActivity>(survey.id, new PartitionKey($"Activity-{school}-{userid}"));
+                            activity.taskStatus = taskStatus;
+                            await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "School").ReplaceItemAsync<StuActivity>(activity, survey.id, new PartitionKey($"Activity-{school}-{userid}"));
+
+                        }
+                        else
+                        {
+                            StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuActivity>(survey.id, new PartitionKey($"Activity-{userid}"));
+                            activity.taskStatus = taskStatus;
+                            await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuActivity>(activity, survey.id, new PartitionKey($"Activity-{userid}"));
+                        }
+
+                    }
+                    catch (CosmosException ex)
+                    {
+                        if (ex.Status == 404)
+                        {
+                            StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<StuActivity>(survey.id, new PartitionKey($"Activity-{userid}"));
+                            activity.taskStatus = taskStatus;
+                            await _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<StuActivity>(activity, survey.id, new PartitionKey($"Activity-{userid}"));
+                        }
+                    }
+                }
+                else {
+                    return (msgid, -1);
                 }
+
             }
             catch (Exception e)
             {
                 throw new Exception(e.StackTrace);
             }
-            return msgid;
+
+            return (msgid, taskStatus); 
         }
         public class RdsRecord {
             public Dictionary<string, int> srecord { get; set; } = new Dictionary<string, int>();