|
@@ -17,6 +17,8 @@ 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;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Learn
|
|
|
{
|
|
@@ -30,15 +32,19 @@ namespace TEAMModelOS.Controllers.Learn
|
|
|
[ApiController]
|
|
|
public class VoteController : ControllerBase
|
|
|
{
|
|
|
+ private readonly AzureCosmosFactory _azureCosmos;
|
|
|
private readonly SnowflakeId _snowflakeId;
|
|
|
- private readonly AzureCosmosFactory _azureCosmos;
|
|
|
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)
|
|
|
{
|
|
|
- _snowflakeId = snowflakeId;
|
|
|
_azureCosmos = azureCosmos;
|
|
|
_serviceBus = serviceBus;
|
|
|
+ _snowflakeId = snowflakeId;
|
|
|
+ _dingDing = dingDing;
|
|
|
+ _option = option?.Value;
|
|
|
}
|
|
|
|
|
|
|
|
@@ -48,153 +54,120 @@ namespace TEAMModelOS.Controllers.Learn
|
|
|
/// <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"))
|
|
|
- {
|
|
|
-
|
|
|
- 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;
|
|
|
+ try {
|
|
|
+ //新增Vote
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ request.code = request.pk + "-" + request.code;
|
|
|
+ request.createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
|
|
|
|
- }*/
|
|
|
- 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 });
|
|
|
+ } catch (Exception e) {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/save()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(e.StackTrace);
|
|
|
}
|
|
|
- else
|
|
|
- {
|
|
|
- Vote info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(request.vote.id, new PartitionKey($"{request.vote.code}"));
|
|
|
- if (info.progress.Equals("going"))
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 查询投票活动,用于列表,编辑,查看
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("find")]
|
|
|
+ public async Task<IActionResult> Find(JsonElement requert) {
|
|
|
+ try {
|
|
|
+ if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
+ List<object> votes = new List<object>();
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ var query = $"select c.id,c.name,c.code,c.startTime,c.progress, from c ";
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Vote-{code}") }))
|
|
|
{
|
|
|
- return Ok(new { v = "活动正在进行中" });
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Ok(votes);
|
|
|
+ } catch (Exception ex) {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.StackTrace);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// 查询投票活动,用于列表,编辑,查看
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("find-id")]
|
|
|
+ public async Task<IActionResult> FindById(JsonElement requert) {
|
|
|
+ try {
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
+ 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;
|
|
|
}
|
|
|
- //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"))
|
|
|
+ if (vote != null)
|
|
|
{
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "School").ReplaceItemAsync(request.vote, request.vote.id, new PartitionKey($"{request.vote.code}"));
|
|
|
+ return Ok(vote);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
- vote = await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(request.vote, request.vote.id, new PartitionKey($"{request.vote.code}"));
|
|
|
- }*/
|
|
|
+ return BadRequest("id,code不存在!");
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
|
|
|
+ return BadRequest(ex.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 } });
|
|
|
+
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 查询投票活动,用于学生可以投票的列表
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="request"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ [ProducesDefaultResponseType]
|
|
|
+ [HttpPost("find-stu")]
|
|
|
+ public async Task<IActionResult> FindStu(JsonElement requert) {
|
|
|
+ var(id,name,_,school) = HttpContext.GetAuthTokenInfo();
|
|
|
|
|
|
- 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 });
|
|
|
+ return Ok();
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 查询投票活动
|
|
|
+ /// 查询投票活动,用于教师投票的列表
|
|
|
/// </summary>
|
|
|
/// <param name="request"></param>
|
|
|
/// <returns></returns>
|
|
|
-
|
|
|
[ProducesDefaultResponseType]
|
|
|
- [HttpPost("find")]
|
|
|
- public async Task<IActionResult> Find(JsonElement requert)
|
|
|
+ [HttpPost("find-tch")]
|
|
|
+ public async Task<IActionResult> FindTch(JsonElement requert)
|
|
|
{
|
|
|
|
|
|
var client = _azureCosmos.GetCosmosClient();
|