VoteController.cs 11 KB

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