|
@@ -44,9 +44,10 @@ namespace TEAMModelOS.Controllers.Learn
|
|
|
private readonly AzureServiceBusFactory _serviceBus;
|
|
|
private readonly DingDing _dingDing;
|
|
|
private readonly Option _option;
|
|
|
+ private readonly AzureStorageFactory _azureStorage;
|
|
|
|
|
|
public VoteController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
|
|
|
- AzureRedisFactory azureRedis)
|
|
|
+ AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
|
|
|
{
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_serviceBus = serviceBus;
|
|
@@ -54,6 +55,7 @@ namespace TEAMModelOS.Controllers.Learn
|
|
|
_dingDing = dingDing;
|
|
|
_option = option?.Value;
|
|
|
_azureRedis = azureRedis;
|
|
|
+ _azureStorage = azureStorage;
|
|
|
}
|
|
|
/// <summary>
|
|
|
/// 新增 或 修改投票活动
|
|
@@ -344,12 +346,12 @@ namespace TEAMModelOS.Controllers.Learn
|
|
|
var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{id}");
|
|
|
//获取投票活动的选项及投票数
|
|
|
var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{id}");
|
|
|
- List<OptionVote> options = new List<OptionVote>();
|
|
|
+ List<dynamic> options = new List<dynamic>();
|
|
|
if (counts != null && counts.Length > 0)
|
|
|
{
|
|
|
foreach (var count in counts)
|
|
|
{
|
|
|
- options.Add(new OptionVote { code = count.Element.ToString(), count = (int)count.Score });
|
|
|
+ options.Add(new { code = count.Element.ToString(), count = (int)count.Score });
|
|
|
}
|
|
|
}
|
|
|
List<JsonElement> res = new List<JsonElement>();
|
|
@@ -447,5 +449,130 @@ namespace TEAMModelOS.Controllers.Learn
|
|
|
}
|
|
|
else { return Ok(new { msgid = 0 }); }
|
|
|
}
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 问卷记录 当活动没结算且没有BlobUrl时则调用此接口
|
|
|
+ /// </summary>
|
|
|
+ /// <redis>
|
|
|
+ /// {"C":2,"A":2,"other":2}
|
|
|
+ /// </redis>
|
|
|
+ /// <param name="request">
|
|
|
+ /// !"id":"aaaa"
|
|
|
+ /// !"code":"Survey-hbcn"/"code":"Survey-1606285227"
|
|
|
+ /// </param>
|
|
|
+ /// <returns>
|
|
|
+ /// </returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("settlement")]
|
|
|
+ //[AuthToken(Roles = "teacher,student")]
|
|
|
+ public async Task<IActionResult> Settlement(JsonElement request)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ //活动id
|
|
|
+ if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
+ //活动分区
|
|
|
+ if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
+ Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}"));
|
|
|
+ if (vote != null)
|
|
|
+ {
|
|
|
+ ActivityData data;
|
|
|
+
|
|
|
+ if (vote.progress == "finish")
|
|
|
+ {
|
|
|
+ //获取投票活动的所有投票记录
|
|
|
+ var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
|
|
|
+ //获取投票活动的选项及投票数
|
|
|
+ var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
|
|
|
+ List<dynamic> countcds = new List<dynamic>();
|
|
|
+ if (counts != null && counts.Length > 0)
|
|
|
+ {
|
|
|
+ foreach (var count in counts)
|
|
|
+ {
|
|
|
+ countcds.Add(new { code= count.Element.ToString() , count = (int)count.Score });
|
|
|
+ }
|
|
|
+ }
|
|
|
+ List<Task<string>> tasks = new List<Task<string>>();
|
|
|
+ List<VoteRecord> recordsBlob = new List<VoteRecord>();
|
|
|
+ foreach (var rcd in records)
|
|
|
+ {
|
|
|
+ var value = rcd.Value.ToString().ToObject<VoteRecord>();
|
|
|
+ recordsBlob.Add(value);
|
|
|
+ }
|
|
|
+ //分组每个人的
|
|
|
+ var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
|
|
|
+ foreach (var g in gp)
|
|
|
+ {
|
|
|
+ tasks.Add(_azureStorage.UploadFileByContainer(vote.owner, g.list.ToJsonString(), "vote", $"{vote.id}/urecord/{g.key}.json"));
|
|
|
+ }
|
|
|
+ //处理活动方的记录,
|
|
|
+ string url = $"/vote/{vote.id}/record.json";
|
|
|
+ vote.recordUrl = url;
|
|
|
+ tasks.Add(_azureStorage.UploadFileByContainer(vote.owner,new{ options = countcds, records = recordsBlob } .ToJsonString(), "vote", $"{vote.id}/record.json"));
|
|
|
+ //处理投票者的记录
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
+ //
|
|
|
+ if (string.IsNullOrEmpty(vote.recordUrl))
|
|
|
+ {
|
|
|
+ await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync<Vote>(vote, vote.id, new Azure.Cosmos.PartitionKey(vote.code));
|
|
|
+ }
|
|
|
+ //更新结束状态
|
|
|
+ if (vote.scope == "school")
|
|
|
+ {
|
|
|
+ data = new ActivityData
|
|
|
+ {
|
|
|
+ id = vote.id,
|
|
|
+ code = $"Activity-{vote.owner}",
|
|
|
+ type = "vote",
|
|
|
+ name = vote.name,
|
|
|
+ startTime = vote.startTime,
|
|
|
+ endTime = vote.endTime,
|
|
|
+ scode = vote.code,
|
|
|
+ scope = vote.scope,
|
|
|
+ progress = "finish",
|
|
|
+ classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
|
|
|
+ tmdids = vote.tmdids.IsNotEmpty() ? vote.tmdids : new List<string> { "" },
|
|
|
+ owner = vote.owner,
|
|
|
+ subjects = new List<string> { "" }
|
|
|
+
|
|
|
+ };
|
|
|
+ await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<ActivityData>(data, data.id, new Azure.Cosmos.PartitionKey(data.code));
|
|
|
+ }
|
|
|
+ else if (vote.scope == "private")
|
|
|
+ {
|
|
|
+ //更新结束状态
|
|
|
+ data = new ActivityData
|
|
|
+ {
|
|
|
+ id = vote.id,
|
|
|
+ code = $"Activity-Common",
|
|
|
+ type = "vote",
|
|
|
+ name = vote.name,
|
|
|
+ startTime = vote.startTime,
|
|
|
+ endTime = vote.endTime,
|
|
|
+ scode = vote.code,
|
|
|
+ scope = vote.scope,
|
|
|
+ progress = "finish",
|
|
|
+ classes = vote.classes.IsNotEmpty() ? vote.classes : new List<string> { "" },
|
|
|
+ owner = vote.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));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Ok(new { vote });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return BadRequest("id,code不存在!");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.StackTrace);
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|