123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Grpc.Extension.Common;
- using Microsoft.AspNetCore.Http;
- using Microsoft.AspNetCore.Mvc;
- using Microsoft.Azure.Cosmos.Linq;
- using Microsoft.Azure.Documents.SystemFunctions;
- using TEAMModelOS.Models;
- using TEAMModelOS.SDK.Context.Exception;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK;
- using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
- using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
- using TEAMModelOS.Service.Models;
- using TEAMModelOS.Service.Services.Learn.Interfaces;
- using TEAMModelOS.SDK.Context.Constant.Common;
- namespace TEAMModelOS.Controllers.Learn
- {
- /// <summary>
- /// 投票活动
- /// </summary>
- [Route("api/[controller]")]
- [ApiController]
- public class VoteController : BaseController
- {
- private readonly IAzureCosmosDBV3Repository _cosmos;
-
- private readonly IServiceBusService _serviceBus;
- public VoteController(IAzureCosmosDBV3Repository cosmos, IServiceBusService serviceBus)
- {
- _cosmos = cosmos;
- _serviceBus = serviceBus;
- }
- /// <summary>
- /// 新增 或 修改投票活动
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("upsert")]
- public async Task<BaseResponse> Upsert(JosnRequest<VoteDto> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- //新增
- if (string.IsNullOrEmpty(request.@params.vote.id))
- {
- request.@params.vote.id = Guid.NewGuid().ToString();
- request.@params.vote.status = 100;
- }
- if (request.@params.vote.publishModel.Equals("0"))
- {
- request.@params.vote.startTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
- request.@params.vote.status = 200;
-
- } else if (request.@params.vote.publishModel.Equals("1")) {
- string msgId = Guid.NewGuid().ToString();
- long SequenceNumber = await _serviceBus.SendMessage<Vote>(Constants.TopicName, request.@params.vote.id, request.@params.vote.code, request.@params.vote.startTime, 200, msgId);
- request.@params.vote.sequenceNumber = SequenceNumber;
- }
- Vote homeWork = await _cosmos.SaveOrUpdate<Vote>(request.@params.vote);
- //设定结束时间
- //_timerWorkService.TimerWork<Vote>(request.@params.vote.endTime, 300, new Dictionary<string, object> { { "id", request.@params.vote.id } });
- //设定结束时间
- string msgEndId = Guid.NewGuid().ToString();
- await _serviceBus.SendMessage<Vote>(Constants.TopicName, request.@params.vote.id, request.@params.vote.code, request.@params.vote.endTime, 300, msgEndId);
- //清除作业
- if (!request.@params.reset)
- {
- //根据作业发布对象查找到每一个具体学生生成关联关系表 HomeWorkStudent
- List<VoteRecord> voteRecords = await _cosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", request.@params.vote.id } });
- if (voteRecords.IsNotEmpty())
- {
- await _cosmos.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();
- }
- /// <summary>
- /// 查询投票活动
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("find")]
- public async Task<BaseResponse> Find(JosnRequest<Dictionary<string, object>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- List<Vote> data = new List<Vote>();
- if (request.@params.Keys.Count > 0)
- {
- data = await _cosmos.FindByDict<Vote>(request.@params);
- }
- else
- {
- return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
- }
- return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
- }
- /// <summary>
- /// 删除投票活动
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("delete")]
- public async Task<BaseResponse> Delete(JosnRequest<List<IdPk>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- List<IdPk> idPks = await _cosmos.DeleteAll<Vote>(request.@params);
- if (idPks.IsNotEmpty())
- {
- foreach (IdPk idPk in idPks)
- {
- List<VoteRecord> votes = await _cosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", idPk.id } });
- await _cosmos.DeleteAll(votes);
- }
- builder.Data(idPks);
- }
- else
- {
- return builder.Error(ResponseCode.FAILED, "删除失败!").build();
- }
- return builder.build();
- }
- /// <summary>
- /// 撤消投票
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("cancel")]
- public async Task<BaseResponse> Cancel(JosnRequest<string> voteId)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- List<Vote> votes = await _cosmos.FindByDict<Vote>(new Dictionary<string, object> { { "id", voteId.@params } });
- foreach (Vote vote in votes)
- {
- vote.status = 100;
- }
- List<Vote> vote1 = await _cosmos.UpdateAll<Vote>(votes);
- //查询之前是否有 关联关系表 HomeWorkStudent 有则删除
- List<VoteRecord> voteStudents = await _cosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", voteId.@params } });
- if (voteStudents.IsNotEmpty())
- {
- await _cosmos.DeleteAll(voteStudents);
- }
- return builder.Data(vote1).build();
- }
- /// <summary>
- /// 查询 投票 学生关联
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("findRecord")]
- public async Task<BaseResponse> FindRecord(JosnRequest<Dictionary<string, object>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- List<VoteRecord> data = new List<VoteRecord>();
- List<Options> options = new List<Options>();
- if (request.@params.Keys.Count > 0)
- {
- data = await _cosmos.FindByDict<VoteRecord>(request.@params);
- if (data.IsNotEmpty()) {
- List< Vote > votes = await _cosmos.FindByDict<Vote>(new Dictionary<string, object> { { "id", data[0].id } });
- List<Vote.Option> options2 = votes[0].option;
- 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 builder.Data(options).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
- }
- /// <summary>
- /// 学生投票
- /// </summary>
- /// <param name="request"></param>
- /// <returns></returns>
- [HttpPost("upsertRecord")]
- public async Task<BaseResponse> UpsertRecord(JosnRequest<List<VoteRecord>> request)
- {
- ResponseBuilder builder = ResponseBuilder.custom();
- await _cosmos.SaveOrUpdateAll<VoteRecord>(request.@params);
- return builder.Data(request.@params).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; }
- }
- }
- }
|