|
@@ -21,235 +21,209 @@ namespace TEAMModelFunction
|
|
|
{
|
|
|
public class TriggerSurvey
|
|
|
{
|
|
|
- public static async void Trigger(AzureCosmosFactory _azureCosmos, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
|
|
|
- CosmosClient client, Document input, string code, long stime, long etime, string school,AzureRedisFactory _azureRedis, System.Net.Http.IHttpClientFactory _clientFactory)
|
|
|
+ public static async void Trigger( AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
|
|
|
+ CosmosClient client, Document input, TriggerData tdata,AzureRedisFactory _azureRedis)
|
|
|
{
|
|
|
- Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(input.Id, new Azure.Cosmos.PartitionKey($"{code}"));
|
|
|
+ if ((tdata.status != null && tdata.status.Value == 404) || tdata.ttl > 0)
|
|
|
+ {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ var adid = tdata.id;
|
|
|
+ var adcode = $"Activity-{tdata.owner}";
|
|
|
+ ActivityData data = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (tdata.scope == "school")
|
|
|
+ {
|
|
|
+ data = await client.GetContainer("TEAMModelOS", "School").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
|
|
|
+ }
|
|
|
+ else if (tdata.scope == "private")
|
|
|
+ {
|
|
|
+ data = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ActivityData>(adid, new Azure.Cosmos.PartitionKey($"{adcode}"));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch { data = null; }
|
|
|
+ Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(input.Id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
|
|
|
List<ChangeRecord> changeRecords = await _azureStorage.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", input.Id }, { "PartitionKey", survey.progress } });
|
|
|
- ActivityData data = null ;
|
|
|
if (survey != null) {
|
|
|
- var adid = survey.id;
|
|
|
- var adcode = $"Activity-{survey.owner}";
|
|
|
- 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}-ttl={survey.ttl}】正在操作", GroupNames.成都开发測試群組);
|
|
|
- if (survey.ttl >= 1)
|
|
|
+ switch (survey.progress)
|
|
|
{
|
|
|
-
|
|
|
- _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
|
|
|
- _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
|
|
|
- if (data != null)
|
|
|
- {
|
|
|
-
|
|
|
- if (survey.scope == "school")
|
|
|
+ case "pending":
|
|
|
+ var messageSurvey = new ServiceBusMessage(new { id = input.Id, progress = "going", code = tdata.code }.ToJsonString());
|
|
|
+ messageSurvey.ApplicationProperties.Add("name", "Survey");
|
|
|
+ if (changeRecords.Count > 0)
|
|
|
{
|
|
|
- data = await client.GetContainer("TEAMModelOS", "School").DeleteItemAsync<ActivityData>( adid, new Azure.Cosmos.PartitionKey($"{data.code}"));
|
|
|
+ await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
|
|
|
+ long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
|
|
|
+ changeRecords[0].sequenceNumber = start;
|
|
|
+ await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
|
|
|
}
|
|
|
- else if (survey.scope == "private")
|
|
|
+ else
|
|
|
{
|
|
|
- data = await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemAsync<ActivityData>( adid, new Azure.Cosmos.PartitionKey($"{data.code}"));
|
|
|
- }
|
|
|
- }
|
|
|
- await _dingDing.SendBotMsg($"问卷调查【{survey.name}-{survey.id}】被删除", GroupNames.成都开发測試群組);
|
|
|
- return;
|
|
|
- }
|
|
|
- else {
|
|
|
- switch (survey.progress)
|
|
|
- {
|
|
|
- case "pending":
|
|
|
- var messageSurvey = new ServiceBusMessage(new { id = input.Id, progress = "going", code = code }.ToJsonString());
|
|
|
- messageSurvey.ApplicationProperties.Add("name", "Survey");
|
|
|
- if (changeRecords.Count > 0)
|
|
|
- {
|
|
|
- await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
|
|
|
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(stime));
|
|
|
- changeRecords[0].sequenceNumber = start;
|
|
|
- await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
|
|
|
- }
|
|
|
- else
|
|
|
+ long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(tdata.stime));
|
|
|
+ ChangeRecord changeRecord = new ChangeRecord
|
|
|
{
|
|
|
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurvey, DateTimeOffset.FromUnixTimeMilliseconds(stime));
|
|
|
- ChangeRecord changeRecord = new ChangeRecord
|
|
|
- {
|
|
|
- RowKey = input.Id,
|
|
|
- PartitionKey = "pending",
|
|
|
- sequenceNumber = start,
|
|
|
- msgId = messageSurvey.MessageId
|
|
|
- };
|
|
|
- await _azureStorage.Save<ChangeRecord>(changeRecord);
|
|
|
- }
|
|
|
- break;
|
|
|
- case "going":
|
|
|
-
|
|
|
- if (survey.scope == "school")
|
|
|
+ RowKey = input.Id,
|
|
|
+ PartitionKey = "pending",
|
|
|
+ sequenceNumber = start,
|
|
|
+ msgId = messageSurvey.MessageId
|
|
|
+ };
|
|
|
+ await _azureStorage.Save<ChangeRecord>(changeRecord);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "going":
|
|
|
+ if (survey.scope == "school")
|
|
|
+ {
|
|
|
+ data = new ActivityData
|
|
|
{
|
|
|
- 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,
|
|
|
- classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
|
|
|
- tmdids = survey.tmdids.IsNotEmpty() ? survey.tmdids : new List<string> { "" },
|
|
|
- progress = "going",
|
|
|
- owner = survey.owner,
|
|
|
- subjects = new List<string> { "" }
|
|
|
- };
|
|
|
- await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
|
|
|
- }
|
|
|
- else if (survey.scope == "private")
|
|
|
+ id = survey.id,
|
|
|
+ code = $"Activity-{survey.owner}",
|
|
|
+ type = "survey",
|
|
|
+ name = survey.name,
|
|
|
+ startTime = survey.startTime,
|
|
|
+ endTime = survey.endTime,
|
|
|
+ scode = survey.code,
|
|
|
+ scope = survey.scope,
|
|
|
+ classes = survey.classes.IsNotEmpty() ? survey.classes : new List<string> { "" },
|
|
|
+ tmdids = survey.tmdids.IsNotEmpty() ? survey.tmdids : new List<string> { "" },
|
|
|
+ progress = "going",
|
|
|
+ owner = survey.owner,
|
|
|
+ subjects = new List<string> { "" }
|
|
|
+ };
|
|
|
+ await client.GetContainer("TEAMModelOS", "School").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
|
|
|
+ }
|
|
|
+ else if (survey.scope == "private")
|
|
|
+ {
|
|
|
+ data = new ActivityData
|
|
|
{
|
|
|
- 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 = "going",
|
|
|
- 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").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
|
|
|
- }
|
|
|
- var messageSurveyEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = code }.ToJsonString());
|
|
|
- messageSurveyEnd.ApplicationProperties.Add("name", "Survey");
|
|
|
- if (changeRecords.Count > 0)
|
|
|
+ id = survey.id,
|
|
|
+ code = $"Activity-Common",
|
|
|
+ type = "survey",
|
|
|
+ name = survey.name,
|
|
|
+ startTime = survey.startTime,
|
|
|
+ endTime = survey.endTime,
|
|
|
+ scode = survey.code,
|
|
|
+ scope = survey.scope,
|
|
|
+ progress = "going",
|
|
|
+ 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").UpsertItemAsync<ActivityData>(data, new Azure.Cosmos.PartitionKey(data.code));
|
|
|
+ }
|
|
|
+ var messageSurveyEnd = new ServiceBusMessage(new { id = input.Id, progress = "finish", code = tdata.code }.ToJsonString());
|
|
|
+ messageSurveyEnd.ApplicationProperties.Add("name", "Survey");
|
|
|
+ if (changeRecords.Count > 0)
|
|
|
+ {
|
|
|
+ long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
|
|
|
+ await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
|
|
|
+ changeRecords[0].sequenceNumber = end;
|
|
|
+ await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.etime));
|
|
|
+ ChangeRecord changeRecord = new ChangeRecord
|
|
|
{
|
|
|
- long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
|
|
|
- await _serviceBus.GetServiceBusClient().cancelMessage("active-task", changeRecords[0].sequenceNumber);
|
|
|
- changeRecords[0].sequenceNumber = end;
|
|
|
- await _azureStorage.SaveOrUpdate<ChangeRecord>(changeRecords[0]);
|
|
|
- }
|
|
|
- else
|
|
|
+ RowKey = input.Id,
|
|
|
+ PartitionKey = "going",
|
|
|
+ sequenceNumber = end,
|
|
|
+ msgId = messageSurveyEnd.MessageId
|
|
|
+ };
|
|
|
+ await _azureStorage.Save<ChangeRecord>(changeRecord);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ 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);
|
|
|
+
|
|
|
+ //获取
|
|
|
+ try {
|
|
|
+ List<string> items = await ContainerClient.List($"survey/{survey.id}/urecord");
|
|
|
+ List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
|
|
|
+ foreach (string item in items)
|
|
|
{
|
|
|
- long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync("active-task", messageSurveyEnd, DateTimeOffset.FromUnixTimeMilliseconds(etime));
|
|
|
- ChangeRecord changeRecord = new ChangeRecord
|
|
|
- {
|
|
|
- RowKey = input.Id,
|
|
|
- PartitionKey = "going",
|
|
|
- sequenceNumber = end,
|
|
|
- msgId = messageSurveyEnd.MessageId
|
|
|
- };
|
|
|
- await _azureStorage.Save<ChangeRecord>(changeRecord);
|
|
|
+ var Download = await _azureStorage.GetBlobContainerClient(survey.owner).GetBlobClient(item).DownloadAsync();
|
|
|
+ var json = await JsonDocument.ParseAsync(Download.Value.Content);
|
|
|
+ var Record = json.RootElement.ToObject<SurveyRecord>();
|
|
|
+ surveyRecords.Add(Record);
|
|
|
}
|
|
|
- break;
|
|
|
- case "finish":
|
|
|
- var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
|
|
|
- List<dynamic> recs = new List<dynamic>();
|
|
|
- foreach (var rcd in records)
|
|
|
+ await _dingDing.SendBotMsg($"问卷调查问题结算数据{surveyRecords.ToJsonString()}", GroupNames.成都开发測試群組);
|
|
|
+ for (int index = 0; index < survey.ans.Count; index++)
|
|
|
{
|
|
|
- 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);
|
|
|
-
|
|
|
- //获取
|
|
|
- try {
|
|
|
- List<string> items = await ContainerClient.List($"survey/{survey.id}/urecord");
|
|
|
- List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
|
|
|
- foreach (string item in items)
|
|
|
- {
|
|
|
- var Download = await _azureStorage.GetBlobContainerClient(survey.owner).GetBlobClient(item).DownloadAsync();
|
|
|
- var json = await JsonDocument.ParseAsync(Download.Value.Content);
|
|
|
- var Record = json.RootElement.ToObject<SurveyRecord>();
|
|
|
- surveyRecords.Add(Record);
|
|
|
- }
|
|
|
- await _dingDing.SendBotMsg($"问卷调查问题结算数据{surveyRecords.ToJsonString()}", GroupNames.成都开发測試群組);
|
|
|
- for (int index = 0; index < survey.ans.Count; index++)
|
|
|
+ string url = $"{survey.id}/qrecord/{index}.json";
|
|
|
+ QuestionRecord question = new QuestionRecord() { index = index };
|
|
|
+ foreach (SurveyRecord record in surveyRecords)
|
|
|
{
|
|
|
- string url = $"{survey.id}/qrecord/{index}.json";
|
|
|
- QuestionRecord question = new QuestionRecord() { index = index };
|
|
|
- foreach (SurveyRecord record in surveyRecords)
|
|
|
+ if (record.ans.Count == survey.ans.Count)
|
|
|
{
|
|
|
- if (record.ans.Count == survey.ans.Count)
|
|
|
+ foreach (var an in record.ans[index])
|
|
|
{
|
|
|
- foreach (var an in record.ans[index])
|
|
|
+ //
|
|
|
+ if (question.opt.ContainsKey(an))
|
|
|
{
|
|
|
- //
|
|
|
- if (question.opt.ContainsKey(an))
|
|
|
+ if (question.opt[an] != null)
|
|
|
{
|
|
|
- if (question.opt[an] != null)
|
|
|
- {
|
|
|
- question.opt[an].Add(record.userid);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- question.opt[an] = new HashSet<string>() { record.userid };
|
|
|
- }
|
|
|
+ question.opt[an].Add(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;
|
|
|
- }
|
|
|
+ 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;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- await _azureStorage.UploadFileByContainer(survey.owner, question.ToJsonString(), "survey", url);
|
|
|
}
|
|
|
- } catch (Exception ex) {
|
|
|
- await _dingDing.SendBotMsg($"问卷调查问题结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
|
|
|
- }
|
|
|
- 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));
|
|
|
+ await _azureStorage.UploadFileByContainer(survey.owner, question.ToJsonString(), "survey", url);
|
|
|
}
|
|
|
- _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
|
|
|
- _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
|
|
|
- break;
|
|
|
- }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ await _dingDing.SendBotMsg($"问卷调查问题结算异常{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
|
|
|
+ }
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ // 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));
|
|
|
+ }
|
|
|
+ _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Record:{survey.id}");
|
|
|
+ _azureRedis.GetRedisClient(8).KeyDelete($"Survey:Submit:{survey.id}");
|
|
|
+ break;
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
-
|
|
|
}
|
|
|
}
|
|
|
/**
|