VoteController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using Grpc.Extension.Common;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using Microsoft.Azure.Cosmos.Linq;
  9. using Microsoft.Azure.Documents.SystemFunctions;
  10. using TEAMModelOS.Models;
  11. using TEAMModelOS.SDK.Context.Exception;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK;
  14. using TEAMModelOS.SDK.Helper.Common.CollectionHelper;
  15. using TEAMModelOS.SDK.Module.AzureCosmosDBV3;
  16. using TEAMModelOS.Service.Models;
  17. using TEAMModelOS.Service.Services.Learn.Interfaces;
  18. using TEAMModelOS.SDK.Context.Constant.Common;
  19. namespace TEAMModelOS.Controllers.Learn
  20. {
  21. /// <summary>
  22. /// 投票活动
  23. /// </summary>
  24. [Route("api/[controller]")]
  25. [ApiController]
  26. public class VoteController : BaseController
  27. {
  28. private readonly IAzureCosmosDBV3Repository _cosmos;
  29. private readonly IServiceBusService _serviceBus;
  30. public VoteController(IAzureCosmosDBV3Repository cosmos, IServiceBusService serviceBus)
  31. {
  32. _cosmos = cosmos;
  33. _serviceBus = serviceBus;
  34. }
  35. /// <summary>
  36. /// 新增 或 修改投票活动
  37. /// </summary>
  38. /// <param name="request"></param>
  39. /// <returns></returns>
  40. [HttpPost("upsert")]
  41. public async Task<BaseResponse> Upsert(JosnRequest<VoteDto> request)
  42. {
  43. ResponseBuilder builder = ResponseBuilder.custom();
  44. //新增
  45. if (string.IsNullOrEmpty(request.@params.vote.id))
  46. {
  47. request.@params.vote.id = Guid.NewGuid().ToString();
  48. request.@params.vote.status = 100;
  49. }
  50. if (request.@params.vote.publishModel.Equals("0"))
  51. {
  52. request.@params.vote.startTime = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds();
  53. request.@params.vote.status = 200;
  54. } else if (request.@params.vote.publishModel.Equals("1")) {
  55. string msgId = Guid.NewGuid().ToString();
  56. long SequenceNumber = await _serviceBus.SendMessage<Vote>(Constants.TopicName, request.@params.vote.id, request.@params.vote.code, request.@params.vote.startTime, 200, msgId);
  57. request.@params.vote.sequenceNumber = SequenceNumber;
  58. }
  59. Vote homeWork = await _cosmos.SaveOrUpdate<Vote>(request.@params.vote);
  60. //设定结束时间
  61. //_timerWorkService.TimerWork<Vote>(request.@params.vote.endTime, 300, new Dictionary<string, object> { { "id", request.@params.vote.id } });
  62. //设定结束时间
  63. string msgEndId = Guid.NewGuid().ToString();
  64. await _serviceBus.SendMessage<Vote>(Constants.TopicName, request.@params.vote.id, request.@params.vote.code, request.@params.vote.endTime, 300, msgEndId);
  65. //清除作业
  66. if (!request.@params.reset)
  67. {
  68. //根据作业发布对象查找到每一个具体学生生成关联关系表 HomeWorkStudent
  69. List<VoteRecord> voteRecords = await _cosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", request.@params.vote.id } });
  70. if (voteRecords.IsNotEmpty())
  71. {
  72. await _cosmos.DeleteAll(voteRecords);
  73. }
  74. //List<Target> targets = request.@params.vote.target;
  75. //List<VoteRecord> votekStudents = new List<VoteRecord>();
  76. //foreach (Target target in targets)
  77. //{
  78. // //查询之前是否有 关联关系表 HomeWorkStudent 有则删除
  79. // List<ClassStudent> classroom = await _cosmos.FindByDict<ClassStudent>(new Dictionary<string, object> { { "id", target.classroomCode } });
  80. // if (classroom.IsNotEmpty() && classroom[0].code!=null)
  81. // {
  82. // foreach (ClassStudent student in classroom)
  83. // {
  84. // VoteRecord voteStudent = new VoteRecord();
  85. // voteStudent.id = request.@params.vote.id;
  86. // voteStudent.code = student.code;
  87. // voteStudent.classroom.code = target.classroomCode;
  88. // voteStudent.classroom.name = target.classroomName;
  89. // votekStudents.Add(voteStudent);
  90. // }
  91. // }
  92. //}
  93. //if (votekStudents.IsNotEmpty())
  94. //{
  95. // foreach (VoteRecord voteRecord in votekStudents)
  96. // {
  97. // List<Student> student = await _cosmos.FindByDict<Student>(new Dictionary<string, object> { { "studentId", voteRecord.code } });
  98. // if (student.IsNotEmpty())
  99. // {
  100. // voteRecord.name = student[0].name;
  101. // voteRecord.code = student[0].studentId;
  102. // }
  103. // }
  104. // await _cosmos.SaveOrUpdateAll(votekStudents);
  105. //}
  106. }
  107. return builder.Data(homeWork).build();
  108. }
  109. /// <summary>
  110. /// 查询投票活动
  111. /// </summary>
  112. /// <param name="request"></param>
  113. /// <returns></returns>
  114. [HttpPost("find")]
  115. public async Task<BaseResponse> Find(JosnRequest<Dictionary<string, object>> request)
  116. {
  117. ResponseBuilder builder = ResponseBuilder.custom();
  118. List<Vote> data = new List<Vote>();
  119. if (request.@params.Keys.Count > 0)
  120. {
  121. data = await _cosmos.FindByDict<Vote>(request.@params);
  122. }
  123. else
  124. {
  125. return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
  126. }
  127. return builder.Data(data).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
  128. }
  129. /// <summary>
  130. /// 删除投票活动
  131. /// </summary>
  132. /// <param name="request"></param>
  133. /// <returns></returns>
  134. [HttpPost("delete")]
  135. public async Task<BaseResponse> Delete(JosnRequest<List<IdPk>> request)
  136. {
  137. ResponseBuilder builder = ResponseBuilder.custom();
  138. List<IdPk> idPks = await _cosmos.DeleteAll<Vote>(request.@params);
  139. if (idPks.IsNotEmpty())
  140. {
  141. foreach (IdPk idPk in idPks)
  142. {
  143. List<VoteRecord> votes = await _cosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", idPk.id } });
  144. await _cosmos.DeleteAll(votes);
  145. }
  146. builder.Data(idPks);
  147. }
  148. else
  149. {
  150. return builder.Error(ResponseCode.FAILED, "删除失败!").build();
  151. }
  152. return builder.build();
  153. }
  154. /// <summary>
  155. /// 撤消投票
  156. /// </summary>
  157. /// <param name="request"></param>
  158. /// <returns></returns>
  159. [HttpPost("cancel")]
  160. public async Task<BaseResponse> Cancel(JosnRequest<string> voteId)
  161. {
  162. ResponseBuilder builder = ResponseBuilder.custom();
  163. List<Vote> votes = await _cosmos.FindByDict<Vote>(new Dictionary<string, object> { { "id", voteId.@params } });
  164. foreach (Vote vote in votes)
  165. {
  166. vote.status = 100;
  167. }
  168. List<Vote> vote1 = await _cosmos.UpdateAll<Vote>(votes);
  169. //查询之前是否有 关联关系表 HomeWorkStudent 有则删除
  170. List<VoteRecord> voteStudents = await _cosmos.FindByDict<VoteRecord>(new Dictionary<string, object> { { "id", voteId.@params } });
  171. if (voteStudents.IsNotEmpty())
  172. {
  173. await _cosmos.DeleteAll(voteStudents);
  174. }
  175. return builder.Data(vote1).build();
  176. }
  177. /// <summary>
  178. /// 查询 投票 学生关联
  179. /// </summary>
  180. /// <param name="request"></param>
  181. /// <returns></returns>
  182. [HttpPost("findRecord")]
  183. public async Task<BaseResponse> FindRecord(JosnRequest<Dictionary<string, object>> request)
  184. {
  185. ResponseBuilder builder = ResponseBuilder.custom();
  186. List<VoteRecord> data = new List<VoteRecord>();
  187. List<Options> options = new List<Options>();
  188. if (request.@params.Keys.Count > 0)
  189. {
  190. data = await _cosmos.FindByDict<VoteRecord>(request.@params);
  191. if (data.IsNotEmpty()) {
  192. List< Vote > votes = await _cosmos.FindByDict<Vote>(new Dictionary<string, object> { { "id", data[0].id } });
  193. List<Vote.Option> options2 = votes[0].option;
  194. foreach (IGrouping<string, VoteRecord> voteStudents in data.GroupBy(x => x.option))
  195. {
  196. Options options1 = new Options();
  197. options1.optionKey = voteStudents.Key;
  198. options2.ForEach(x => { if (x.code == voteStudents.Key) options1.optionValue = x.value; });
  199. if(options1.optionValue == null) options1.optionValue = "Null";
  200. foreach (VoteRecord voteStudent in voteStudents)
  201. {
  202. options1.students.Add(voteStudent);
  203. }
  204. options.Add(options1);
  205. }
  206. }
  207. }
  208. else
  209. {
  210. return builder.Error(ResponseCode.PARAMS_ERROR, "参数异常!").build();
  211. }
  212. return builder.Data(options).Extend(new Dictionary<string, object> { { "count", data.Count } }).build();
  213. }
  214. /// <summary>
  215. /// 学生投票
  216. /// </summary>
  217. /// <param name="request"></param>
  218. /// <returns></returns>
  219. [HttpPost("upsertRecord")]
  220. public async Task<BaseResponse> UpsertRecord(JosnRequest<List<VoteRecord>> request)
  221. {
  222. ResponseBuilder builder = ResponseBuilder.custom();
  223. await _cosmos.SaveOrUpdateAll<VoteRecord>(request.@params);
  224. return builder.Data(request.@params).build();
  225. }
  226. public class Options {
  227. public Options() {
  228. students = new List<VoteRecord>();
  229. }
  230. /// <summary>
  231. /// 选项Key
  232. /// </summary>
  233. public string optionKey { get; set; }
  234. /// <summary>
  235. /// 选项value
  236. /// </summary>
  237. public string optionValue { get; set; }
  238. /// <summary>
  239. /// 选项人
  240. /// </summary>
  241. public List<VoteRecord> students { get; set; }
  242. }
  243. }
  244. }