|
@@ -15,6 +15,9 @@ using TEAMModelOS.SDK.DI;
|
|
|
using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
|
|
|
using TEAMModelOS.SDK.Extension;
|
|
|
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using TEAMModelOS.Filter;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers
|
|
|
{
|
|
@@ -31,14 +34,18 @@ namespace TEAMModelOS.Controllers
|
|
|
private readonly AzureCosmosFactory _azureCosmos;
|
|
|
private readonly SnowflakeId _snowflakeId;
|
|
|
private readonly AzureServiceBusFactory _serviceBus;
|
|
|
- public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId)
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
+ private readonly Option _option;
|
|
|
+ public SurveyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option)
|
|
|
{
|
|
|
_snowflakeId= snowflakeId;
|
|
|
- // _timerWorkService = timerWorkService;
|
|
|
_serviceBus = serviceBus;
|
|
|
- _azureCosmos = azureCosmos;
|
|
|
+ _azureCosmos = azureCosmos;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _option = option?.Value;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
/// <summary>
|
|
|
/// 新增 或 修改投票活动
|
|
|
/// </summary>
|
|
@@ -46,449 +53,225 @@ namespace TEAMModelOS.Controllers
|
|
|
/// <returns></returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
[HttpPost("upsert")]
|
|
|
- public async Task<IActionResult> Upsert(SurveyDto request)
|
|
|
- {
|
|
|
- //ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- //新增
|
|
|
- //string code = request.survey.code;
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
- Survey survey;
|
|
|
- //request.survey.school = request.survey.code;
|
|
|
- request.survey.code = "Survey-" + request.survey.code;
|
|
|
- request.survey.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- /*if (request.survey.publishModel.Equals("0"))
|
|
|
- {
|
|
|
- //request.survey.startTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- request.survey.progress = "going";
|
|
|
- }
|
|
|
- else if (request.survey.publishModel.Equals("1"))
|
|
|
- {
|
|
|
-
|
|
|
- string msgId = _snowflakeId.NextId() + "";
|
|
|
- long SequenceNumber = await _serviceBus.GetServiceBusClient().SendLeamMessage<Survey>(Constants.TopicName, request.survey.id, request.survey.code, request.survey.startTime, "going", msgId);
|
|
|
- request.survey.sequenceNumber = SequenceNumber;
|
|
|
-
|
|
|
- }*/
|
|
|
- if (string.IsNullOrEmpty(request.survey.id))
|
|
|
- {
|
|
|
- request.survey.id = Guid.NewGuid().ToString();
|
|
|
- //request.survey.status = 100;
|
|
|
- request.survey.progress = "pending";
|
|
|
- //await _serviceBus.GetServiceBusClient().cancelMessage(Constants.TopicName, info.sequenceNumber);
|
|
|
- /* long SequenceNumber = await _serviceBus.GetServiceBusClient().SendLeamMessage<Survey>(Constants.TopicName, request.survey.id, request.survey.code, request.survey.startTime);
|
|
|
- request.survey.sequenceNumber = SequenceNumber;*/
|
|
|
- survey = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request.survey, new PartitionKey($"{request.survey.code}"));
|
|
|
- /*if (request.survey.scope.Equals("school"))
|
|
|
- {
|
|
|
- survey = await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(request.survey, new PartitionKey($"Survey-{code}"));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- survey = await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(request.survey, new PartitionKey($"Survey-{code}"));
|
|
|
- }*/
|
|
|
- //await _serviceBus.GetServiceBusClient().SendLeamMessage<Survey>(Constants.TopicName, request.id, request.code, request.startTime);
|
|
|
-
|
|
|
- }
|
|
|
- else {
|
|
|
- Survey info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(request.survey.id, new PartitionKey($"{request.survey.code}"));
|
|
|
- if (info.progress.Equals("going"))
|
|
|
- {
|
|
|
- return Ok(new { v = "活动正在进行中" });
|
|
|
- }
|
|
|
- //request.survey.code = info.code;
|
|
|
- request.survey.progress = info.progress;
|
|
|
- /*try {
|
|
|
- await _serviceBus.GetServiceBusClient().cancelMessage(Constants.TopicName, info.sequenceNumber);
|
|
|
- long SequenceNumber = await _serviceBus.GetServiceBusClient().SendLeamMessage<Survey>(Constants.TopicName, request.survey.id, request.survey.code, request.survey.startTime);
|
|
|
- request.survey.sequenceNumber = SequenceNumber;
|
|
|
- } catch (Exception e) {
|
|
|
- return BadRequest();
|
|
|
- //await _dingDing.SendBotMsg($"ServiceBusㄛExamBus()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
- } */
|
|
|
-
|
|
|
- survey = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(request.survey, info.id, new PartitionKey($"{info.code}"));
|
|
|
- //request.survey.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- /*if (request.survey.scope.Equals("school"))
|
|
|
- {
|
|
|
- survey = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(request.survey, request.survey.id, new PartitionKey($"{request.survey.code}"));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- survey = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(request.survey, request.survey.id, new PartitionKey($"{request.survey.code}"));
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
- }
|
|
|
- //Survey homeWork = await _azureCosmos.SaveOrUpdate<Survey>(request.survey);
|
|
|
-
|
|
|
- //设定结束时间
|
|
|
- //await _serviceBus.GetServiceBusClient().SendLeamMessage<Survey>(Constants.TopicName, request.survey.id, request.survey.code, request.survey.endTime);
|
|
|
-
|
|
|
- //清除作业
|
|
|
- /*if (!request.reset)
|
|
|
- {
|
|
|
- //查询之前是否有 关联关系表 HomeWorkStudent 有则删除
|
|
|
- List<SurveyRecord> surveyRecords = await _azureCosmos.FindByDict<SurveyRecord>(new Dictionary<string, object> { { "id", request.survey.id } });
|
|
|
- if (surveyRecords.IsNotEmpty())
|
|
|
- {
|
|
|
- await _azureCosmos.DeleteAll(surveyRecords);
|
|
|
- }
|
|
|
-
|
|
|
- ////根据作业发布对象查找到每一个具体学生生成关联关系表 HomeWorkStudent
|
|
|
- //List<Target> targets = request.@params.survey.target;
|
|
|
- //List<SurveyRecord> surveykStudents = new List<SurveyRecord>();
|
|
|
- //foreach (Target target in targets)
|
|
|
- //{
|
|
|
-
|
|
|
- // List<ClassStudent> classroom = await azureCosmosDBV3Repository.FindByDict<ClassStudent>(new Dictionary<string, object> { { "id", target.classroomCode } });
|
|
|
- // if (classroom.IsNotEmpty() && classroom[0].code != null)
|
|
|
- // {
|
|
|
- // foreach (ClassStudent student in classroom)
|
|
|
- // {
|
|
|
- // SurveyRecord surveyStudent = new SurveyRecord();
|
|
|
- // surveyStudent.id = request.@params.survey.id;
|
|
|
- // surveyStudent.code = student.code;
|
|
|
- // surveyStudent.classroom.code = target.classroomCode;
|
|
|
- // surveyStudent.classroom.name = target.classroomName;
|
|
|
- // surveykStudents.Add(surveyStudent);
|
|
|
- // }
|
|
|
- // }
|
|
|
- //}
|
|
|
- //if (surveykStudents.IsNotEmpty())
|
|
|
- //{
|
|
|
- // foreach (SurveyRecord surveyRecord in surveykStudents)
|
|
|
- // {
|
|
|
- // List<Student> student = await azureCosmosDBV3Repository.FindById<Student>(surveyRecord.code);
|
|
|
- // if (student.IsNotEmpty())
|
|
|
- // {
|
|
|
- // surveyRecord.name = student[0].name;
|
|
|
- // surveyRecord.code = student[0].TEAMModelId;
|
|
|
- // }
|
|
|
- // }
|
|
|
- // await azureCosmosDBV3Repository.SaveOrUpdateAll(surveykStudents);
|
|
|
- //}
|
|
|
- }*/
|
|
|
- //return builder.Data(homeWork).build();
|
|
|
- return Ok(survey);
|
|
|
- }
|
|
|
- /// <summary>
|
|
|
- /// 查询投票活动
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- [HttpPost("find-summary")]
|
|
|
- public async Task<IActionResult> FindSummary(JsonElement requert)
|
|
|
- {
|
|
|
-
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
- if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
- if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
|
|
|
- //var (id, name, picture, _) = HttpContext.GetAuthTokenInfo();
|
|
|
- List<object> surveys = new List<object>();
|
|
|
- StringBuilder sql = new StringBuilder();
|
|
|
- sql.Append("select c.id,c.code,c.name,c.startTime,c.endTime,c.description,c.progress from c ");
|
|
|
- Dictionary<string, object> dict = new Dictionary<string, object>();
|
|
|
- var emobj = requert.EnumerateObject();
|
|
|
- while (emobj.MoveNext())
|
|
|
- {
|
|
|
- dict[emobj.Current.Name] = emobj.Current.Value;
|
|
|
- }
|
|
|
- //处理code
|
|
|
- if (dict.TryGetValue("code", out object _))
|
|
|
- {
|
|
|
- dict.Remove("code");
|
|
|
- }
|
|
|
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{code}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
-
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ public async Task<IActionResult> Upsert(Survey request) {
|
|
|
+ try {
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ request.code = request.pk + "-" + request.code;
|
|
|
+ long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ request.createTime = now;
|
|
|
+ if (string.IsNullOrEmpty(request.id))
|
|
|
{
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ request.id = Guid.NewGuid().ToString();
|
|
|
+ if (request.startTime < now)
|
|
|
{
|
|
|
- surveys.Add(obj.ToObject<object>());
|
|
|
+ request.progress = "pending";
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- /*if (scope.ToString().Equals("school"))
|
|
|
- {
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{code}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
-
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ else
|
|
|
{
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- surveys.Add(obj.ToObject<object>());
|
|
|
- }
|
|
|
+ request.progress = "going";
|
|
|
}
|
|
|
+ request = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request, new PartitionKey($"{request.code}"));
|
|
|
}
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{code}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
-
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ else {
|
|
|
+ Survey info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(request.id, new PartitionKey($"{request.code}"));
|
|
|
+ if (info.progress.Equals("going"))
|
|
|
{
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- surveys.Add(obj.ToObject<object>());
|
|
|
- }
|
|
|
+ return Ok(new { v = "活动正在进行中" });
|
|
|
}
|
|
|
+ request.progress = info.progress;
|
|
|
+ request = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(request, info.id, new PartitionKey($"{info.code}"));
|
|
|
}
|
|
|
- }*/
|
|
|
-
|
|
|
- return Ok(new { surveys, surveys.Count });
|
|
|
- /*ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- Dictionary<string, object> dict = new Dictionary<string, object>();
|
|
|
- var emobj = request.EnumerateObject();
|
|
|
- while (emobj.MoveNext())
|
|
|
- {
|
|
|
- dict[emobj.Current.Name] = emobj.Current.Value;
|
|
|
- }
|
|
|
- List<Survey> data = new List<Survey>();
|
|
|
- List<string> props = new List<string> { "id", "code", "name", "type", "status", "startTime" };
|
|
|
- if (dict.Keys.Count > 0)
|
|
|
- {
|
|
|
-
|
|
|
- data = await _azureCosmos.FindByDict<Survey>(dict, props);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
|
|
|
-
|
|
|
+ return Ok(new { survey = request});
|
|
|
+ } catch (Exception ex) {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/save()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.StackTrace);
|
|
|
}
|
|
|
- return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count },{ "props", props } }).build();*/
|
|
|
+
|
|
|
}
|
|
|
/// <summary>
|
|
|
- /// 查询投票活动
|
|
|
+ /// 查询问卷调查,用于列表,编辑,查看
|
|
|
/// </summary>
|
|
|
+ /// <data>
|
|
|
+ ///Survey-学校/教师编码 活动分区 !"code":"hbcn"/1606285227
|
|
|
+ ///时间筛选范围开始时间 默认30天之前 ?"stime":1608274766154
|
|
|
+ ///时间筛选范围结束时间 默认当前时间 ?"etime":1608274766666
|
|
|
+ ///每页大小 ?"count":10/null/Undefined
|
|
|
+ ///分页Token ?"continuationToken":Undefined/null/"[{\"token\":\"+RID:~omxMAP3ipcSEEwAAAAAAAA==#RT:2#TRC:20#ISV:2#IEO:65551#QCF:1#FPC:AYQTAAAAAAAAiRMAAAAAAAA=\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}]"
|
|
|
+ /// 当前状态 ?"progress":Undefined/null/"" 表示两种状态都要查询/ "going"/"finish" 表示查询进行中/ 或者已完成 学生端只能查询正在进行或已经结束 going 已发布|finish 已结束
|
|
|
+ /// </data>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
[HttpPost("find")]
|
|
|
public async Task<IActionResult> Find(JsonElement requert)
|
|
|
{
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
- if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
- if (!requert.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
|
|
|
- //var (id, name, picture, _) = HttpContext.GetAuthTokenInfo();
|
|
|
- List<object> surveys = new List<object>();
|
|
|
- StringBuilder sql = new StringBuilder();
|
|
|
- sql.Append("select c.id,c.code,c.name,c.questions,c.classes,c.startTime,c.endTime,c.description,c.progress,c.targetClassIds from c ");
|
|
|
- Dictionary<string, object> dict = new Dictionary<string, object>();
|
|
|
- var emobj = requert.EnumerateObject();
|
|
|
- while (emobj.MoveNext())
|
|
|
- {
|
|
|
- dict[emobj.Current.Name] = emobj.Current.Value;
|
|
|
- }
|
|
|
- //处理code
|
|
|
- if (dict.TryGetValue("code", out object _))
|
|
|
- {
|
|
|
- dict.Remove("code");
|
|
|
- }
|
|
|
- AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{code}") }))
|
|
|
- {
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ try {
|
|
|
+ //必须有学校或者教师编码
|
|
|
+ if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
+ //开始时间,默认最近三十天
|
|
|
+ var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
|
|
|
+ if (requert.TryGetProperty("stime", out JsonElement stime))
|
|
|
+ {
|
|
|
+ if (!stime.ValueKind.Equals(JsonValueKind.Undefined) && stime.TryGetInt64(out long data))
|
|
|
+ {
|
|
|
+ stimestamp = data;
|
|
|
+ };
|
|
|
+ };
|
|
|
+ //默认当前时间
|
|
|
+ var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ if (requert.TryGetProperty("etime", out JsonElement etime))
|
|
|
+ {
|
|
|
+ if (!etime.ValueKind.Equals(JsonValueKind.Undefined) && etime.TryGetInt64(out long data))
|
|
|
+ {
|
|
|
+ etimestamp = data;
|
|
|
+ };
|
|
|
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ };
|
|
|
+ var progresssql = "";
|
|
|
+ if (!requert.TryGetProperty("progress", out JsonElement progress))
|
|
|
{
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+
|
|
|
+ if (!progress.ValueKind.Equals(JsonValueKind.Undefined) && !progress.ValueKind.Equals(JsonValueKind.Null) && progress.ValueKind.Equals(JsonValueKind.String))
|
|
|
{
|
|
|
- surveys.Add(obj.ToObject<object>());
|
|
|
+ progresssql = $" and c.progress='{progresssql}' ";
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
- /*if (scope.ToString().Equals("school"))
|
|
|
- {
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{code}") }))
|
|
|
+ string continuationToken = null;
|
|
|
+ //默认不指定返回大小
|
|
|
+ int? topcout = null;
|
|
|
+ if (requert.TryGetProperty("count", out JsonElement jcount))
|
|
|
{
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
-
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && jcount.TryGetInt32(out int data))
|
|
|
{
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- surveys.Add(obj.ToObject<object>());
|
|
|
- }
|
|
|
+ topcout = data;
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- else {
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Survey-{code}") }))
|
|
|
+ };
|
|
|
+ //是否需要进行分页查询,默认不分页
|
|
|
+ bool iscontinuation = false;
|
|
|
+ //如果指定了返回大小
|
|
|
+ if (requert.TryGetProperty("continuationToken", out JsonElement continuation))
|
|
|
+ {
|
|
|
+ //指定了cancellationToken 表示需要进行分页
|
|
|
+ if (!continuation.ValueKind.Equals(JsonValueKind.Null) && !continuation.ValueKind.Equals(JsonValueKind.Undefined))
|
|
|
+ {
|
|
|
+ continuationToken = continuation.GetString();
|
|
|
+ iscontinuation = true;
|
|
|
+ }
|
|
|
+ };
|
|
|
+ List<object> surveys = new List<object>();
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ var query = $"select c.id,c.name,c.code,c.startTime,c.endTime,c.progress from c where c.createTime >= {stimestamp} and c.createTime <= {etimestamp} {progresssql } ";
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query,
|
|
|
+ requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Vote-{code}") }))
|
|
|
{
|
|
|
using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
-
|
|
|
if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
{
|
|
|
foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
{
|
|
|
- surveys.Add(obj.ToObject<object>());
|
|
|
+ surveys.Add(obj.ToObject<JsonElement>());
|
|
|
+ }
|
|
|
+ //如果需要分页则跳出
|
|
|
+ if (iscontinuation)
|
|
|
+ {
|
|
|
+ continuationToken = item.GetContinuationToken();
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
- return Ok(new { surveys, surveys.Count });
|
|
|
- /*ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- Dictionary<string, object> dict = new Dictionary<string, object>();
|
|
|
- var emobj = request.EnumerateObject();
|
|
|
- while (emobj.MoveNext())
|
|
|
- {
|
|
|
- dict[emobj.Current.Name] = emobj.Current.Value;
|
|
|
- }
|
|
|
- List<Survey> data = new List<Survey>();
|
|
|
- if (dict.Keys.Count > 0)
|
|
|
- {
|
|
|
- data = await _azureCosmos.FindByDict<Survey>(dict);
|
|
|
+ return Ok(new { surveys, continuationToken });
|
|
|
+ } catch (Exception ex) {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/find()\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.StackTrace);
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
|
|
|
-
|
|
|
- }
|
|
|
- return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();*/
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 删除投票活动
|
|
|
+ ///<summary>
|
|
|
+ /// 查询问卷调查,用于创建者列表,编辑,查看,作答人员查看
|
|
|
/// </summary>
|
|
|
+ /// <data>
|
|
|
+ /// ! "id":"3c075347-75ef-4bcb-ae03-68678d02d5ef",
|
|
|
+ /// ! "code":"Survey-hbcn"/"code":"Survey-1606285227"
|
|
|
+ /// </data>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("delete")]
|
|
|
- public async Task<IActionResult> Delete(JsonElement request)
|
|
|
+ [HttpPost("find-id")]
|
|
|
+ public async Task<IActionResult> FindById(JsonElement requert)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
- if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
- if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
- if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
|
|
|
var client = _azureCosmos.GetCosmosClient();
|
|
|
- var response = await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
|
|
|
- return Ok(new { code = response.Status });
|
|
|
- /*if (scope.ToString().Equals("school"))
|
|
|
+ //活动id
|
|
|
+ if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
+ //活动分区
|
|
|
+ if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
+ Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.GetString(), new PartitionKey($"{code}"));
|
|
|
+ if (survey != null)
|
|
|
{
|
|
|
- var response = await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
|
|
|
- return Ok(new { code = response.Status });
|
|
|
+
|
|
|
+ return Ok(new { survey });
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- var response = await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
|
|
|
- return Ok(new { code = response.Status });
|
|
|
- }*/
|
|
|
-
|
|
|
+ return BadRequest("id,code不存在!");
|
|
|
+ }
|
|
|
}
|
|
|
- catch
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- return BadRequest();
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/survey/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.StackTrace);
|
|
|
}
|
|
|
- /* ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- List<IdPk> idPks = await _azureCosmos.DeleteAll<Survey>(request);
|
|
|
- if (idPks.IsNotEmpty())
|
|
|
- {
|
|
|
- foreach (IdPk idPk in idPks)
|
|
|
- {
|
|
|
- List<SurveyRecord> surveys = await _azureCosmos.FindByDict<SurveyRecord>(new Dictionary<string, object> { { "id", idPk.id } });
|
|
|
- await _azureCosmos.DeleteAll(surveys);
|
|
|
- }
|
|
|
- //builder.Data(idPks);
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- return Ok("删除失败!");
|
|
|
- //return builder.Error(ResponseCode.FAILED, "删除失败!").build();
|
|
|
- }
|
|
|
- return Ok(idPks);*/
|
|
|
- //return builder.build();
|
|
|
- }
|
|
|
-
|
|
|
- // TODO 代码优化
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 撤消问卷
|
|
|
- /// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
- [ProducesDefaultResponseType]
|
|
|
- [HttpPost("cancel")]
|
|
|
- public async Task<IActionResult> Cancel(JsonElement request)
|
|
|
- {
|
|
|
- //request.TryGetProperty("id", out JsonElement surveyId);
|
|
|
- //ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- //List<Survey> surveys = await _azureCosmos.FindByDict<Survey>(new Dictionary<string, object> { { "id", surveyId } });
|
|
|
-
|
|
|
- //foreach (Survey survey in surveys)
|
|
|
- //{
|
|
|
- // survey.status = 100;
|
|
|
- // survey.progress = "pending";
|
|
|
- //}
|
|
|
- //List<Survey> survey1 = await _azureCosmos.UpdateAll<Survey>(surveys);
|
|
|
|
|
|
- ////查询之前是否有 关联关系表 有则删除
|
|
|
- //List<SurveyRecord> surveyStudents = await _azureCosmos.FindByDict<SurveyRecord>(new Dictionary<string, object> { { "id", surveyId } });
|
|
|
- //if (surveyStudents.IsNotEmpty())
|
|
|
- //{
|
|
|
- // await _azureCosmos.DeleteAll(surveyStudents);
|
|
|
- //}
|
|
|
- return Ok();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 查询学生问卷调查记录
|
|
|
+ /// 删除问卷调查 TODO 使用ttl删除,并处理相关事务逻辑
|
|
|
/// </summary>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("find-record")]
|
|
|
- public async Task<IActionResult> FindRecord(JsonElement requert)
|
|
|
+ [HttpPost("delete")]
|
|
|
+ [AuthToken(Roles = "admin,teacher")]
|
|
|
+ public async Task<IActionResult> Delete(JsonElement request)
|
|
|
{
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
- if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
- //var (id, name, picture, _) = HttpContext.GetAuthTokenInfo();
|
|
|
-
|
|
|
- List<object> surveys = new List<object>();
|
|
|
- var query = $"select c.id,c.code,c.name,c.type,c.status,c.startTime from c where {id}";
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"SurveyRecord-{id}") }))
|
|
|
+ try
|
|
|
{
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
|
|
|
+ if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
+ if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ Survey survey = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Survey>(id.GetString(), new PartitionKey($"{code}"));
|
|
|
+ bool flag = false;
|
|
|
+ //必须是本人或者这个学校的管理者才能删除
|
|
|
+ if (survey.creatorId == userid)
|
|
|
{
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ if (survey.scope == "school" && survey.owner.Equals(school))
|
|
|
{
|
|
|
- surveys.Add(obj.ToObject<object>());
|
|
|
+ flag = true;
|
|
|
}
|
|
|
}
|
|
|
+ if (flag)
|
|
|
+ {
|
|
|
+ //使用ttl删除,并处理相关事务逻辑
|
|
|
+ survey.ttl = 1;
|
|
|
+ survey = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(survey, survey.id, new PartitionKey($"{survey.code}"));
|
|
|
+ return Ok(new { flag });
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return Ok(new { flag });
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- return Ok(new { surveys, surveys.Count });
|
|
|
- /*ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- Dictionary<string, object> dict = new Dictionary<string, object>();
|
|
|
- var emobj = request.EnumerateObject();
|
|
|
- while (emobj.MoveNext())
|
|
|
- {
|
|
|
- dict[emobj.Current.Name] = emobj.Current.Value;
|
|
|
- }
|
|
|
- List<SurveyRecord> data = new List<SurveyRecord>();
|
|
|
-
|
|
|
- if (dict.Keys.Count > 0)
|
|
|
- {
|
|
|
- data = await _azureCosmos.FindByDict<SurveyRecord>(dict);
|
|
|
- }
|
|
|
- else
|
|
|
+ catch (Exception e)
|
|
|
{
|
|
|
- return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
|
|
|
+ return BadRequest(e.StackTrace);
|
|
|
}
|
|
|
- return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();*/
|
|
|
- }
|
|
|
-
|
|
|
+ }
|
|
|
}
|
|
|
}
|