|
@@ -17,6 +17,14 @@ using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
|
|
|
using TEAMModelOS.SDK.Extension;
|
|
|
using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
|
|
|
using TEAMModelOS.SDK.Helper.Common.StringHelper;
|
|
|
+using TEAMModelOS.Models;
|
|
|
+using Microsoft.Extensions.Options;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos;
|
|
|
+using Microsoft.AspNetCore.Authorization;
|
|
|
+using TEAMModelOS.Filter;
|
|
|
+using StackExchange.Redis;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
|
|
|
+using TEAMModelOS.Services.Common;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Learn
|
|
|
{
|
|
@@ -30,426 +38,268 @@ namespace TEAMModelOS.Controllers.Learn
|
|
|
[ApiController]
|
|
|
public class VoteController : ControllerBase
|
|
|
{
|
|
|
- private readonly SnowflakeId _snowflakeId;
|
|
|
+ private readonly AzureRedisFactory _azureRedis;
|
|
|
private readonly AzureCosmosFactory _azureCosmos;
|
|
|
+ private readonly SnowflakeId _snowflakeId;
|
|
|
private readonly AzureServiceBusFactory _serviceBus;
|
|
|
+ private readonly DingDing _dingDing;
|
|
|
+ private readonly Option _option;
|
|
|
|
|
|
- public VoteController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId)
|
|
|
+ public VoteController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
|
|
|
+ AzureRedisFactory azureRedis)
|
|
|
{
|
|
|
- _snowflakeId = snowflakeId;
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_serviceBus = serviceBus;
|
|
|
+ _snowflakeId = snowflakeId;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _option = option?.Value;
|
|
|
+ _azureRedis = azureRedis;
|
|
|
}
|
|
|
-
|
|
|
-
|
|
|
/// <summary>
|
|
|
/// 新增 或 修改投票活动
|
|
|
/// </summary>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("upsert")]
|
|
|
- public async Task<IActionResult> Upsert(VoteDto request)
|
|
|
+ [HttpPost("save")]
|
|
|
+ public async Task<IActionResult> Save(Vote request)
|
|
|
{
|
|
|
- /*ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- //新增
|
|
|
- if (string.IsNullOrEmpty(request.vote.id))
|
|
|
- {
|
|
|
- request.vote.id = _snowflakeId.NextId()+"";
|
|
|
- request.vote.status = 100;
|
|
|
- request.vote.code = typeof(Vote).Name + "-" + request.vote.code;
|
|
|
- }
|
|
|
- if (request.vote.publishModel.Equals("0"))
|
|
|
- {
|
|
|
- request.vote.startTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- request.vote.status = 200;
|
|
|
-
|
|
|
- } else if (request.vote.publishModel.Equals("1")) {
|
|
|
-
|
|
|
- string msgId = _snowflakeId.NextId() + "";
|
|
|
- long SequenceNumber = await _serviceBus.GetServiceBusClient().SendLeamMessage<Vote>(Constants.TopicName, request.vote.id, request.vote.code, request.vote.startTime, 200, msgId);
|
|
|
- request.vote.sequenceNumber = SequenceNumber;
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
- //新增ote
|
|
|
- //string code = request.vote.code;
|
|
|
- var client = _azureCosmos.GetCosmosClient();
|
|
|
- Vote vote = new Vote();
|
|
|
- request.vote.school = request.vote.code;
|
|
|
- request.vote.code = request.vote.pk + "-" + request.vote.code;
|
|
|
- request.vote.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- /*if (request.vote.publishModel.Equals("0"))
|
|
|
- {
|
|
|
- //request.vote.startTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
- request.vote.progress = "going";
|
|
|
- }
|
|
|
- else if (request.vote.publishModel.Equals("1"))
|
|
|
+ try
|
|
|
{
|
|
|
+ //新增Vote
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ request.code = request.pk + "-" + request.code;
|
|
|
+ request.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
|
|
|
- string msgId = _snowflakeId.NextId() + "";
|
|
|
- long SequenceNumber = await _serviceBus.GetServiceBusClient().SendLeamMessage<Survey>(Constants.TopicName, request.vote.id, request.vote.code, request.vote.startTime, "going", msgId);
|
|
|
- request.vote.sequenceNumber = SequenceNumber;
|
|
|
-
|
|
|
- }*/
|
|
|
- if (string.IsNullOrEmpty(request.vote.id))
|
|
|
- {
|
|
|
- request.vote.id = Guid.NewGuid().ToString();
|
|
|
- //request.survey.status = 100;
|
|
|
- request.vote.progress = "pending";
|
|
|
- /* long SequenceNumber = await _serviceBus.GetServiceBusClient().SendLeamMessage<Vote>(Constants.TopicName, request.vote.id, request.vote.code, request.vote.startTime);
|
|
|
- request.vote.sequenceNumber = SequenceNumber;*/
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request.vote, new PartitionKey($"{request.vote.code}"));
|
|
|
- /*if (request.vote.scope.Equals("school"))
|
|
|
+ if (string.IsNullOrEmpty(request.id))
|
|
|
{
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "School").CreateItemAsync(request.vote, new PartitionKey($"Vote-{code}"));
|
|
|
+ request.id = Guid.NewGuid().ToString();
|
|
|
+ request.progress = "pending";
|
|
|
+ request = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request, new PartitionKey($"{request.code}"));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(request.vote, new PartitionKey($"Vote-{code}"));
|
|
|
- }*/
|
|
|
+ Vote info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(request.id, new PartitionKey($"{request.code}"));
|
|
|
+ if (info.progress.Equals("going"))
|
|
|
+ {
|
|
|
+ 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 { request });
|
|
|
}
|
|
|
- else
|
|
|
+ catch (Exception e)
|
|
|
{
|
|
|
- Vote info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(request.vote.id, new PartitionKey($"{request.vote.code}"));
|
|
|
- if (info.progress.Equals("going"))
|
|
|
- {
|
|
|
- return Ok(new { v = "活动正在进行中" });
|
|
|
- }
|
|
|
- //request.vote.code = info.code;
|
|
|
- request.vote.progress = info.progress;
|
|
|
- /* await _serviceBus.GetServiceBusClient().cancelMessage(Constants.TopicName,info.sequenceNumber);
|
|
|
- long SequenceNumber = await _serviceBus.GetServiceBusClient().SendLeamMessage<Vote>(Constants.TopicName, request.vote.id, request.vote.code, request.vote.startTime);
|
|
|
- request.vote.sequenceNumber = SequenceNumber;*/
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(request.vote, info.id, new PartitionKey($"{info.code}"));
|
|
|
- /*if (request.vote.scope.Equals("school"))
|
|
|
- {
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(request.vote, request.vote.id, new PartitionKey($"{request.vote.code}"));
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(request.vote, request.vote.id, new PartitionKey($"{request.vote.code}"));
|
|
|
- }*/
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/save()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(e.StackTrace);
|
|
|
}
|
|
|
- //Vote homeWork = await _azureCosmos.SaveOrUpdate<Vote>(request.vote);
|
|
|
-
|
|
|
- //设定结束时间
|
|
|
- //_timerWorkService.TimerWork<Vote>(request.vote.endTime, 300, new Dictionary<string, object> { { "id", request.vote.id } });
|
|
|
- //设定结束时间
|
|
|
- //await _serviceBus.GetServiceBusClient().SendLeamMessage<Vote>(Constants.TopicName, request.vote.id, request.vote.code, request.vote.endTime);
|
|
|
- //清除作业
|
|
|
- /* if (!request.reset)
|
|
|
- {
|
|
|
- //根据作业发布对象查找到每一个具体学生生成关联关系表 HomeWorkStudent
|
|
|
-
|
|
|
- List<VoteRecord> voteRecords = await _azureCosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", request.vote.id } });
|
|
|
-
|
|
|
- if (voteRecords.IsNotEmpty())
|
|
|
- {
|
|
|
- await _azureCosmos.DeleteAll(voteRecords);
|
|
|
- }*/
|
|
|
- //List<Target> targets = request.@params.vote.target;
|
|
|
- //List<VoteRecord> votekStudents = new List<VoteRecord>();
|
|
|
- //foreach (Target target in targets)
|
|
|
- //{
|
|
|
- // //查询之前是否有 关联关系表 HomeWorkStudent 有则删除
|
|
|
-
|
|
|
- // List<ClassStudent> classroom = await _cosmos.FindByDict<ClassStudent>(new Dictionary<string, object> { { "id", target.classroomCode } });
|
|
|
- // if (classroom.IsNotEmpty() && classroom[0].code!=null)
|
|
|
- // {
|
|
|
- // foreach (ClassStudent student in classroom)
|
|
|
- // {
|
|
|
- // VoteRecord voteStudent = new VoteRecord();
|
|
|
- // voteStudent.id = request.@params.vote.id;
|
|
|
- // voteStudent.code = student.code;
|
|
|
- // voteStudent.classroom.code = target.classroomCode;
|
|
|
- // voteStudent.classroom.name = target.classroomName;
|
|
|
- // votekStudents.Add(voteStudent);
|
|
|
- // }
|
|
|
- // }
|
|
|
- //}
|
|
|
- //if (votekStudents.IsNotEmpty())
|
|
|
- //{
|
|
|
- // foreach (VoteRecord voteRecord in votekStudents)
|
|
|
- // {
|
|
|
- // List<Student> student = await _cosmos.FindByDict<Student>(new Dictionary<string, object> { { "studentId", voteRecord.code } });
|
|
|
- // if (student.IsNotEmpty())
|
|
|
- // {
|
|
|
- // voteRecord.name = student[0].name;
|
|
|
- // voteRecord.code = student[0].studentId;
|
|
|
- // }
|
|
|
- // }
|
|
|
- // await _cosmos.SaveOrUpdateAll(votekStudents);
|
|
|
- //}
|
|
|
- /* }*/
|
|
|
- //return builder.Data(homeWork).build();
|
|
|
- return Ok(new { vote });
|
|
|
}
|
|
|
-
|
|
|
/// <summary>
|
|
|
- /// 查询投票活动
|
|
|
+ /// 查询投票活动,用于列表,编辑,查看
|
|
|
/// </summary>
|
|
|
+ /// <data>
|
|
|
+ ///Vote-学校/教师编码 活动分区 !"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\"}}]"
|
|
|
+ /// </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();
|
|
|
- StringBuilder sql = new StringBuilder();
|
|
|
- sql.Append("select c.id,c.code,c.name,c.targetClassIds,c.options,c.description,c.status,c.startTime,c.selectMax,c.endTime,c.secret,c.progress from c ");
|
|
|
- /*if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
- if (!requert.TryGetProperty("@CURRPAGE", out JsonElement page)) return BadRequest();
|
|
|
- if (!requert.TryGetProperty("@PAGESIZE", out JsonElement size)) return BadRequest();
|
|
|
- if (!requert.TryGetProperty("@DESC", out JsonElement desc)) return BadRequest();*/
|
|
|
- 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);
|
|
|
- List<object> votes = new List<object>();
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{code}") }))
|
|
|
+ try
|
|
|
{
|
|
|
- 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())
|
|
|
+ //必须有学校或者教师编码
|
|
|
+ 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.TryGetInt64(out long data))
|
|
|
{
|
|
|
- votes.Add(obj.ToObject<object>());
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- /*if (scope.ToString().Equals("school"))
|
|
|
- {
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{code}") }))
|
|
|
+ stimestamp = data;
|
|
|
+ };
|
|
|
+ };
|
|
|
+ //默认当前时间
|
|
|
+ var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
+ if (!requert.TryGetProperty("etime", out JsonElement etime))
|
|
|
{
|
|
|
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
|
|
|
+ if (stime.TryGetInt64(out long data))
|
|
|
+ {
|
|
|
+ etimestamp = data;
|
|
|
+ };
|
|
|
|
|
|
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
|
|
|
+ };
|
|
|
+
|
|
|
+ string continuationToken = null;
|
|
|
+ //默认不指定返回大小
|
|
|
+ int? topcout=null;
|
|
|
+ if (!requert.TryGetProperty("count", out JsonElement jcount)) {
|
|
|
+ if(jcount.TryGetInt32(out int data))
|
|
|
{
|
|
|
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
|
|
|
- {
|
|
|
- votes.Add(obj.ToObject<object>());
|
|
|
- }
|
|
|
+ topcout = data;
|
|
|
+ };
|
|
|
+ };
|
|
|
+ //是否需要进行分页查询,默认不分页
|
|
|
+ bool iscontinuation = false;
|
|
|
+ //如果指定了返回大小
|
|
|
+ if (!requert.TryGetProperty("continuationToken", out JsonElement continuation))
|
|
|
+ {
|
|
|
+ //指定了cancellationToken 表示需要进行分页
|
|
|
+ if (!continuation.ValueKind.Equals(JsonValueKind.Null) && !continuation.ValueKind.Equals(JsonValueKind.String))
|
|
|
+ {
|
|
|
+ continuationToken = continuation.GetString();
|
|
|
+ iscontinuation = true;
|
|
|
}
|
|
|
- }
|
|
|
- }
|
|
|
- else
|
|
|
- {
|
|
|
- await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{code}") }))
|
|
|
+ };
|
|
|
+ List<object> votes = 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} ";
|
|
|
+ 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())
|
|
|
{
|
|
|
- votes.Add(obj.ToObject<object>());
|
|
|
+ votes.Add(obj.ToObject<JsonElement>());
|
|
|
+ }
|
|
|
+ //如果需要分页则跳出
|
|
|
+ if (iscontinuation) {
|
|
|
+ continuationToken = item.GetContinuationToken();
|
|
|
+ break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
- }*/
|
|
|
-
|
|
|
-
|
|
|
- return Ok(new { votes, votes.Count });
|
|
|
- /*ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- List<Vote> data = new List<Vote>();
|
|
|
- if (StringHelper.getKeyCount(request) > 0)
|
|
|
- {
|
|
|
- data = await _azureCosmos.FindByDict<Vote>(request);
|
|
|
+ return Ok(new { votes, continuationToken });
|
|
|
}
|
|
|
- else
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
|
|
|
-
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.StackTrace);
|
|
|
}
|
|
|
- return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();*/
|
|
|
}
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 删除投票活动
|
|
|
+ ///<summary>
|
|
|
+ /// 查询投票活动,用于创建者列表,编辑,查看,投票人员查看
|
|
|
/// </summary>
|
|
|
+ /// <data>
|
|
|
+ /// ! "id":"3c075347-75ef-4bcb-ae03-68678d02d5ef",
|
|
|
+ /// ! "code":"Vote-hbcn"/"code":"Vote-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();
|
|
|
+ Vote vote = null;
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Vote>(queryText: $"select value(c) from c where c.id = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
|
|
|
+ {
|
|
|
+ vote = item;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (vote != null)
|
|
|
{
|
|
|
- var response = await client.GetContainer("TEAMModelOS", "School").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
|
|
|
- return Ok(new { code = response.Status });
|
|
|
+
|
|
|
+ return Ok(vote);
|
|
|
}
|
|
|
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 (Exception e)
|
|
|
+ catch (Exception ex)
|
|
|
{
|
|
|
- return BadRequest();
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.StackTrace);
|
|
|
}
|
|
|
- //ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- /* List<IdPk> idPks = await _azureCosmos.DeleteAll<Vote>(request);
|
|
|
- if (idPks.IsNotEmpty())
|
|
|
- {
|
|
|
- foreach (IdPk idPk in idPks)
|
|
|
- {
|
|
|
- List<VoteRecord> votes = await _azureCosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", idPk.id } });
|
|
|
- await _azureCosmos.DeleteAll(votes);
|
|
|
- }
|
|
|
- //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 voteId);
|
|
|
- ////ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- //List<Vote> votes = await _azureCosmos.FindByDict<Vote>(new Dictionary<string, object> { { "id", voteId } });
|
|
|
-
|
|
|
- //foreach (Vote vote in votes)
|
|
|
- //{
|
|
|
- // vote.status = 100;
|
|
|
- // vote.progress = "pending";
|
|
|
- //}
|
|
|
- //List<Vote> vote1 = await _azureCosmos.UpdateAll<Vote>(votes);
|
|
|
-
|
|
|
- ////查询之前是否有 关联关系表 HomeWorkStudent 有则删除
|
|
|
- //List<VoteRecord> voteStudents = await _azureCosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", voteId } });
|
|
|
- //if (voteStudents.IsNotEmpty())
|
|
|
- //{
|
|
|
- // await _azureCosmos.DeleteAll(voteStudents);
|
|
|
- //}
|
|
|
- ////return builder.Data(vote1).build();
|
|
|
- //return Ok(vote1);
|
|
|
- return Ok();
|
|
|
}
|
|
|
|
|
|
- // TODO 代码优化
|
|
|
-
|
|
|
/// <summary>
|
|
|
- /// 查询 投票 学生关联
|
|
|
+ /// 删除投票活动 TODO 使用ttl删除,并处理相关事务逻辑
|
|
|
/// </summary>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("find-record")]
|
|
|
- public async Task<IActionResult> FindRecord(JsonElement request)
|
|
|
+ [HttpPost("delete")]
|
|
|
+ [AuthToken(Roles = "admin,teacher")]
|
|
|
+ public async Task<IActionResult> Delete(JsonElement request)
|
|
|
{
|
|
|
- //ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- //List<VoteRecord> data = new List<VoteRecord>();
|
|
|
- //List<Options> options = new List<Options>();
|
|
|
-
|
|
|
- //if (StringHelper.getKeyCount(request) > 0)
|
|
|
- //{
|
|
|
- // data = await _azureCosmos.FindByDict<VoteRecord>(request);
|
|
|
- // if (data.IsNotEmpty()) {
|
|
|
- // List< Vote > votes = await _azureCosmos.FindByDict<Vote>(new Dictionary<string, object> { { "id", data[0].id } });
|
|
|
- // List<OptionsVote> options2 = votes[0].options;
|
|
|
- // foreach (IGrouping<string, VoteRecord> voteStudents in data.GroupBy(x => x.option))
|
|
|
- // {
|
|
|
- // Options options1 = new Options();
|
|
|
- // options1.optionKey = voteStudents.Key;
|
|
|
- // options2.ForEach(x => { if (x.code == voteStudents.Key) options1.optionValue = x.value; });
|
|
|
- // if(options1.optionValue == null) options1.optionValue = "Null";
|
|
|
- // foreach (VoteRecord voteStudent in voteStudents)
|
|
|
- // {
|
|
|
- // options1.students.Add(voteStudent);
|
|
|
- // }
|
|
|
- // options.Add(options1);
|
|
|
- // }
|
|
|
- // }
|
|
|
- //}
|
|
|
- //else
|
|
|
- //{
|
|
|
- // //return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
|
|
|
- // return Ok("参数异常!");
|
|
|
- //}
|
|
|
- ////return builder.Data(options).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
|
|
|
- //return Ok(new { options, data.Count });
|
|
|
- return Ok();
|
|
|
+ try
|
|
|
+ {
|
|
|
+ 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();
|
|
|
+
|
|
|
+ Vote vote =await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}") );
|
|
|
+ bool flag = false;
|
|
|
+ //必须是本人或者这个学校的管理者才能删除
|
|
|
+ if (vote.creatorId == userid)
|
|
|
+ {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ if (vote.scope == "school"&& vote.code.Replace("Vote-", "").Equals(school)) {
|
|
|
+ flag = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (flag)
|
|
|
+ {
|
|
|
+ //使用ttl删除,并处理相关事务逻辑
|
|
|
+ vote.ttl = 1;
|
|
|
+ vote = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, vote.id, new PartitionKey($"{vote.code}"));
|
|
|
+ return Ok(new { flag });
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ return Ok(new { flag });
|
|
|
+ }
|
|
|
+ //var response = await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
|
|
|
+ // return Ok(new { code = response.Status });
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ return BadRequest();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
- // TODO 代码优化
|
|
|
-
|
|
|
/// <summary>
|
|
|
- /// 学生投票
|
|
|
+ /// 投票
|
|
|
/// </summary>
|
|
|
- /// <param name="request"></param>
|
|
|
- /// <returns></returns>
|
|
|
+ /// <redis>
|
|
|
+ /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
|
|
|
+ /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
|
|
|
+ /// </redis>
|
|
|
+ /// <param name="request">
|
|
|
+ /// !"id":"aaaa"
|
|
|
+ /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
|
|
|
+ /// !"option":["A","B","B"]/["1","2","3"]
|
|
|
+ /// </param>
|
|
|
+ /// <returns>
|
|
|
+ /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
|
|
|
+ /// </returns>
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("upsert-record")]
|
|
|
- public async Task<IActionResult> UpsertRecord(List<VoteRecord> request)
|
|
|
+ [HttpPost("decide")]
|
|
|
+ [AuthToken(Roles = "teacher,student")]
|
|
|
+ public async Task<IActionResult> UpsertRecord(JsonElement request)
|
|
|
{
|
|
|
- //ResponseBuilder builder = ResponseBuilder.custom();
|
|
|
- //await _azureCosmos.SaveOrUpdateAll<VoteRecord>(request);
|
|
|
- return Ok(request);
|
|
|
- //return builder.Data(request).build();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- public class Options
|
|
|
- {
|
|
|
-
|
|
|
- public Options()
|
|
|
- {
|
|
|
- students = new List<VoteRecord>();
|
|
|
- }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 选项Key
|
|
|
- /// </summary>
|
|
|
- public string optionKey { get; set; }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 选项value
|
|
|
- /// </summary>
|
|
|
- public string optionValue { get; set; }
|
|
|
-
|
|
|
- /// <summary>
|
|
|
- /// 选项人
|
|
|
- /// </summary>
|
|
|
- public List<VoteRecord> students { get; set; }
|
|
|
+ var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
|
|
|
+ int msgid = await ActivityService.Decide(request, _azureCosmos, _azureRedis, userid);
|
|
|
+ return Ok(new { msgid });
|
|
|
}
|
|
|
|
|
|
}
|