Selaa lähdekoodia

问卷投票完善。

CrazyIter_Bin 4 vuotta sitten
vanhempi
commit
10a152811f

+ 13 - 8
TEAMModelFunction/TriggerSurvey.cs

@@ -29,16 +29,20 @@ namespace TEAMModelFunction
             if (survey != null) {
             if (survey != null) {
                 var adid = survey.id;
                 var adid = survey.id;
                 var adcode = $"Activity-{survey.owner}";
                 var adcode = $"Activity-{survey.owner}";
-                if (survey.scope == "school")
-                {
-                    data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
-                }
-                else if (survey.scope == "private")
-                {
-                    data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
-                }
+                try {
+                    if (survey.scope == "school")
+                    {
+                        data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
+                    }
+                    else if (survey.scope == "private")
+                    {
+                        data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
+                    }
+                } catch { data = null; }
+                await _dingDing.SendBotMsg($"问卷调查【{survey.name}-{survey.id}---{survey.ttl}】正在操作", GroupNames.成都开发測試群組);
                 if (survey.ttl >= 1)
                 if (survey.ttl >= 1)
                 {
                 {
+                  
                     _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
                     _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
                     _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
                     _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
                     if (data != null)
                     if (data != null)
@@ -53,6 +57,7 @@ namespace TEAMModelFunction
                             data = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
                             data = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
                         }
                         }
                     }
                     }
+                    await _dingDing.SendBotMsg($"问卷调查【{survey.name}-{survey.id}】被删除", GroupNames.成都开发測試群組);
                     return;
                     return;
                 }
                 }
                 else {
                 else {

+ 13 - 8
TEAMModelFunction/TriggerVote.cs

@@ -28,13 +28,17 @@ namespace TEAMModelFunction
             if (vote != null) {
             if (vote != null) {
                 var adid = vote.id;
                 var adid = vote.id;
                 var adcode =   $"Activity-{vote.owner}";
                 var adcode =   $"Activity-{vote.owner}";
-                if (vote.scope == "school")
-                {
-                    data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
-                }
-                else if (vote.scope == "private")
-                {
-                    data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
+                try {
+                    if (vote.scope == "school")
+                    {
+                        data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
+                    }
+                    else if (vote.scope == "private")
+                    {
+                        data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
+                    }
+                } catch {
+                    data = null;
                 }
                 }
                 if (vote.ttl >= 1)
                 if (vote.ttl >= 1)
                 {
                 {
@@ -51,7 +55,8 @@ namespace TEAMModelFunction
                         {
                         {
                             data = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data,adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
                             data = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data,adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
                         }
                         }
-                    } 
+                    }
+                    await _dingDing.SendBotMsg($"投票活动【{vote.name}-{vote.id}】被删除", GroupNames.醍摩豆服務運維群組);
                     return;
                     return;
                 }
                 }
                 else
                 else

+ 123 - 114
TEAMModelOS/Controllers/Common/SurveyController.cs

@@ -24,6 +24,7 @@ using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
 using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
 using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
 using System.Net.Http;
 using System.Net.Http;
 using TEAMModelOS.SDK.Models.Cosmos;
 using TEAMModelOS.SDK.Models.Cosmos;
+using Azure.Messaging.ServiceBus;
 
 
 namespace TEAMModelOS.Controllers
 namespace TEAMModelOS.Controllers
 {
 {
@@ -296,8 +297,8 @@ namespace TEAMModelOS.Controllers
                 if (flag)
                 if (flag)
                 {
                 {
                     //使用ttl删除,并处理相关事务逻辑
                     //使用ttl删除,并处理相关事务逻辑
-                    survey.ttl = 1;
-                    survey = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, survey.id, new PartitionKey($"{survey.code}"));
+                    survey.ttl = 2;
+                    survey = await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(survey, new PartitionKey($"{survey.code}"));
                     return Ok(new { flag });
                     return Ok(new { flag });
                 }
                 }
                 else
                 else
@@ -477,137 +478,145 @@ namespace TEAMModelOS.Controllers
                 //活动分区
                 //活动分区
                 if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
                 if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
                 Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.GetString(), new PartitionKey($"{code}"));
                 Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.GetString(), new PartitionKey($"{code}"));
+                survey.ttl = 1;
+                List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", $"{id}" }, { "PartitionKey", survey.progress } });
+                ActivityData data = null;
                 if (survey != null)
                 if (survey != null)
                 {
                 {
-                    ActivityData data;
-
-                    if (survey.progress == "finish") {
-                        var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
-                        List<dynamic> recs = new List<dynamic>();
-                        foreach (var rcd in records)
+                    var adid = survey.id;
+                    var adcode = $"Activity-{survey.owner}";
+                    try {
+                        if (survey.scope == "school")
                         {
                         {
-                            var value = rcd.Value.ToString().ToObject<JsonElement>();
-                            recs.Add(new { index = rcd.Name.ToString(), ans = value });
+                            data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
                         }
                         }
-                        var cods = new { records = recs };
-                        //问卷整体情况
-                        await _azureStorage.UploadFileByContainer(survey.owner, cods.ToJsonString(), "survey", $"{survey.id}/record.json");
-                        //结算每道题的答题情况
-
-                        var ContainerClient = _azureStorage.GetBlobContainerClient(survey.owner);
-                        var route = ContainerClient.Uri.ToString();
-                        List<BlobItem> items = await ContainerClient.List($"survey/{survey.id}/urecord");
-                        List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
-                        //获取
-
-                        foreach (BlobItem item in items)
+                        else if (survey.scope == "private")
                         {
                         {
-                            BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(survey.owner, $"{item.Name}");
-                            var url = $"{route}/{item.Name}?{blobAuth.sas}";
-                            var response = await _clientFactory.CreateClient().GetAsync(new Uri(url));
-                            var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
-                            var Record = json.RootElement.ToObject<SurveyRecord>();
-                            surveyRecords.Add(Record);
+                            data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
                         }
                         }
-                        List<Task<string>> tasks = new List<Task<string>>();
-                        for (int index = 0; index < survey.ans.Count; index++)
+                    } catch {
+                        data = null;
+                    }
+                    if (survey.ttl >= 1)
+                    {
+                        _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
+                        _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
+                        if (data != null)
                         {
                         {
-                            string url = $"{survey.id}/qrecord/{index}.json";
-                            QuestionRecord question = new QuestionRecord() { index = index };
-                            foreach (SurveyRecord record in surveyRecords)
+                            data.ttl = 1;
+                            if (survey.scope == "school")
                             {
                             {
-                                if (record.ans.Count == survey.ans.Count)
+                                data = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
+                            }
+                            else if (survey.scope == "private")
+                            {
+                                data = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
+                            }
+                        }
+                        await _dingDing.SendBotMsg($"问卷调查【{survey.name}-{survey.id}】被删除", GroupNames.醍摩豆服務運維群組);
+                        return Ok();
+                    }
+                    else
+                    {
+                        switch (survey.progress)
+                        {
+
+                            case "finish":
+                                var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
+                                List<dynamic> recs = new List<dynamic>();
+                                foreach (var rcd in records)
+                                {
+                                    var value = rcd.Value.ToString().ToObject<JsonElement>();
+                                    recs.Add(new { index = rcd.Name.ToString(), ans = value });
+                                }
+                                var cods = new { records = recs };
+                                //问卷整体情况
+                                await _azureStorage.UploadFileByContainer(survey.owner, cods.ToJsonString(), "survey", $"{survey.id}/record.json");
+                                //结算每道题的答题情况
+
+                                var ContainerClient = _azureStorage.GetBlobContainerClient(survey.owner);
+                                var route = ContainerClient.Uri.ToString();
+                                List<BlobItem> items = await ContainerClient.List($"survey/{survey.id}/urecord");
+                                List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
+                                //获取
+
+                                foreach (BlobItem item in items)
+                                {
+                                    BlobAuth blobAuth = _azureStorage.GetBlobSasUriRead(survey.owner, $"{item.Name}");
+                                    var url = $"{route}/{item.Name}?{blobAuth.sas}";
+                                    var response = await _clientFactory.CreateClient().GetAsync(new Uri(url));
+                                    var json = await JsonDocument.ParseAsync(await response.Content.ReadAsStreamAsync());
+                                    var Record = json.RootElement.ToObject<SurveyRecord>();
+                                    surveyRecords.Add(Record);
+                                }
+                                List<Task<string>> tasks = new List<Task<string>>();
+                                for (int index = 0; index < survey.ans.Count; index++)
                                 {
                                 {
-                                    foreach (var an in record.ans[index])
+                                    string url = $"{survey.id}/qrecord/{index}.json";
+                                    QuestionRecord question = new QuestionRecord() { index = index };
+                                    foreach (SurveyRecord record in surveyRecords)
                                     {
                                     {
-                                        //
-                                        if (question.opt.ContainsKey(an))
+                                        if (record.ans.Count == survey.ans.Count)
                                         {
                                         {
-                                            if (question.opt[an] != null)
-                                            {
-                                                question.opt[an].Add(record.userid);
-                                            }
-                                            else
-                                            {
-                                                question.opt[an] = new HashSet<string>() { record.userid };
-                                            }
-                                        }
-                                        else
-                                        {
-                                            if (survey.ans[index].Contains(an))
-                                            {
-                                                //如果是客观题code
-                                                question.opt.Add(an, new HashSet<string> { record.userid });
-                                            }
-                                            else
+                                            foreach (var an in record.ans[index])
                                             {
                                             {
-                                                //如果不是客观code
-                                                question.other[record.userid] = an;
+                                                //
+                                                if (question.opt.ContainsKey(an))
+                                                {
+                                                    if (question.opt[an] != null)
+                                                    {
+                                                        question.opt[an].Add(record.userid);
+                                                    }
+                                                    else
+                                                    {
+                                                        question.opt[an] = new HashSet<string>() { record.userid };
+                                                    }
+                                                }
+                                                else
+                                                {
+                                                    if (survey.ans[index].Contains(an))
+                                                    {
+                                                        //如果是客观题code
+                                                        question.opt.Add(an, new HashSet<string> { record.userid });
+                                                    }
+                                                    else
+                                                    {
+                                                        //如果不是客观code
+                                                        question.other[record.userid] = an;
+                                                    }
+                                                }
                                             }
                                             }
                                         }
                                         }
                                     }
                                     }
+                                    tasks.Add(_azureStorage.UploadFileByContainer(survey.owner, question.ToJsonString(), "survey", url, false));
                                 }
                                 }
-                            }
-                            tasks.Add(_azureStorage.UploadFileByContainer(survey.owner, question.ToJsonString(), "survey", url, false));
-                        }
-                        await Task.WhenAll(tasks);
-
-                        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")
-                        {
-                            data = new ActivityData
-                            {
-                                id = survey.id,
-                                code = $"Activity-{survey.owner}",
-                                type = "survey",
-                                name = survey.name,
-                                startTime = survey.startTime,
-                                endTime = survey.endTime,
-                                scode = survey.code,
-                                scope = survey.scope,
-                                progress = "finish",
-                                classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
-                                tmdids = survey.tmdids.IsNotEmpty() ? survey.tmdids : new List<string> { "" },
-                                owner = survey.owner,
-                                subjects = new List<string> { "" }
-
-                            };
-                            await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
-                        }
-                        else if (survey.scope == "private")
-                        {
-                            //更新结束状态
-                            data = new ActivityData
-                            {
-                                id = survey.id,
-                                code = $"Activity-Common",
-                                type = "survey",
-                                name = survey.name,
-                                startTime = survey.startTime,
-                                endTime = survey.endTime,
-                                scode = survey.code,
-                                scope = survey.scope,
-                                progress = "finish",
-                                classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
-                                owner = survey.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));
+                                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));
+                                }
+                                else
+                                {
+                                    //异动,且已经有结算记录则不必再继续。
+                                    break;
+                                }
+                                await Task.WhenAll(tasks);
+                                //更新结束状态
+                                data.progress = "finish";
+                                if (survey.scope == "school")
+                                {
+                                    await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
+                                }
+                                else if (survey.scope == "private")
+                                {
+                                    await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
+                                }
+                                break;
                         }
                         }
                     }
                     }
-                    return Ok(new { survey });
-                }
-                else
-                {
-                    return BadRequest("id,code不存在!");
+
                 }
                 }
+                return Ok();
             }
             }
             catch (Exception ex)
             catch (Exception ex)
             {
             {

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

@@ -298,8 +298,8 @@ namespace TEAMModelOS.Controllers.Learn
                 if (flag)
                 if (flag)
                 { 
                 { 
                     //使用ttl删除,并处理相关事务逻辑
                     //使用ttl删除,并处理相关事务逻辑
-                    vote.ttl = 1;
-                    vote = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, vote.id, new PartitionKey($"{vote.code}"));
+                    vote.ttl = 10;
+                    vote = await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(vote,  new PartitionKey($"{vote.code}"));
                     return Ok(new { flag });
                     return Ok(new { flag });
                 }
                 }
                 else {
                 else {