|
@@ -19,6 +19,11 @@ using TEAMModelOS.Models;
|
|
|
using Microsoft.Extensions.Options;
|
|
|
using TEAMModelOS.Filter;
|
|
|
using TEAMModelOS.Services.Common;
|
|
|
+using Azure.Storage.Blobs.Models;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
|
|
|
+using TEAMModelOS.SDK.Module.AzureBlob.Configuration;
|
|
|
+using System.Net.Http;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers
|
|
|
{
|
|
@@ -39,7 +44,9 @@ namespace TEAMModelOS.Controllers
|
|
|
private readonly DingDing _dingDing;
|
|
|
private readonly Option _option;
|
|
|
private readonly AzureStorageFactory _azureStorage;
|
|
|
- public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option, AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
|
|
|
+ private readonly IHttpClientFactory _clientFactory;
|
|
|
+ public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
|
|
|
+ AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IHttpClientFactory clientFactory)
|
|
|
{
|
|
|
_snowflakeId= snowflakeId;
|
|
|
_serviceBus = serviceBus;
|
|
@@ -48,6 +55,7 @@ namespace TEAMModelOS.Controllers
|
|
|
_option = option?.Value;
|
|
|
_azureRedis = azureRedis;
|
|
|
_azureStorage = azureStorage;
|
|
|
+ _clientFactory = clientFactory;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -340,7 +348,6 @@ namespace TEAMModelOS.Controllers
|
|
|
/// <param name="request">
|
|
|
/// !"id":"aaaa"
|
|
|
/// !"code":"Survey-hbcn"/"code":"Survey-1606285227"
|
|
|
- /// !"record":[["A","B"],["A"],["D"],[],["建议提升服务质量"]]
|
|
|
/// </param>
|
|
|
/// <returns>
|
|
|
/// msgid=0投票失败,1提交成功,2不在时间范围内,3不在发布范围内,6未设置投票项
|
|
@@ -360,9 +367,15 @@ namespace TEAMModelOS.Controllers
|
|
|
{
|
|
|
return BadRequest();
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
- else { return Ok(new { msgid = 0 }); }
|
|
|
+ List<string> userids = new List<string>();
|
|
|
+ var values= await _azureRedis.GetRedisClient(8).SetMembersAsync($"Survey:Submit:{id}");
|
|
|
+ if (values != default && values.Length>0) {
|
|
|
+ foreach(var val in values) {
|
|
|
+ userids.Add(val);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Ok(new { userids = userids });
|
|
|
+
|
|
|
}
|
|
|
|
|
|
|
|
@@ -402,5 +415,172 @@ namespace TEAMModelOS.Controllers
|
|
|
}
|
|
|
return Ok(new {records = res});
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ /// <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();
|
|
|
+ Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.GetString(), new PartitionKey($"{code}"));
|
|
|
+ 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 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++)
|
|
|
+ {
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ foreach (var an in record.ans[index])
|
|
|
+ {
|
|
|
+ //
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ await Task.WhenAll(tasks);
|
|
|
+ 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));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Ok(new { survey });
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ public class QuestionRecord
|
|
|
+ {
|
|
|
+ public int index { get; set; }
|
|
|
+ public Dictionary<string, HashSet<string>> opt { get; set; } = new Dictionary<string, HashSet<string>>();
|
|
|
+ public Dictionary<string, string> other { get; set; } = new Dictionary<string, string>();
|
|
|
}
|
|
|
}
|