SurveyController.cs 10 KB

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