SheetConfigController.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IdentityModel.Tokens.Jwt;
  7. using System.Linq;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.Models;
  14. using Microsoft.Extensions.Options;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using Azure;
  17. using Microsoft.AspNetCore.Authorization;
  18. using TEAMModelOS.SDK.Services;
  19. namespace TEAMModelOS.Controllers.Common
  20. {
  21. [ProducesResponseType(StatusCodes.Status200OK)]
  22. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  23. [Route("sheet-config")]
  24. [ApiController]
  25. public class SheetConfigController : ControllerBase
  26. {
  27. private readonly AzureRedisFactory _azureRedis;
  28. private readonly AzureCosmosFactory _azureCosmos;
  29. private readonly SnowflakeId _snowflakeId;
  30. private readonly AzureServiceBusFactory _serviceBus;
  31. private readonly DingDing _dingDing;
  32. private readonly Option _option;
  33. private readonly AzureStorageFactory _azureStorage;
  34. public SheetConfigController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
  35. AzureRedisFactory azureRedis, AzureStorageFactory azureStorage)
  36. {
  37. _azureCosmos = azureCosmos;
  38. _serviceBus = serviceBus;
  39. _snowflakeId = snowflakeId;
  40. _dingDing = dingDing;
  41. _option = option?.Value;
  42. _azureRedis = azureRedis;
  43. _azureStorage = azureStorage;
  44. }
  45. /// <summary>
  46. ///查询答题卡
  47. /// </summary>
  48. /// <param name="request"></param>
  49. /// <returns></returns>
  50. [ProducesDefaultResponseType]
  51. [HttpPost("find")]
  52. public async Task<IActionResult> Find(JsonElement request) {
  53. try
  54. {
  55. var client = _azureCosmos.GetCosmosClient();
  56. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  57. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  58. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  59. if ($"{scope}" .Equals("school"))
  60. {
  61. SheetConfig config = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<SheetConfig>($"{id}", new PartitionKey($"SheetConfig-{code}"));
  62. return Ok(new { config = config, status = 200 });
  63. }
  64. else
  65. {
  66. SheetConfig config = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<SheetConfig>($"{id}", new PartitionKey($"SheetConfig-{code}"));
  67. return Ok(new { config = config,status=200 });
  68. }
  69. }
  70. catch (CosmosException e)
  71. {
  72. return Ok(new { status = e.Response.Status });
  73. }
  74. catch (Exception ex) {
  75. await _dingDing.SendBotMsg($"OS,{_option.Location},common/SheetConfig/upsert()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  76. return BadRequest(ex.StackTrace);
  77. }
  78. }
  79. /**
  80. @条件1 当examId有值,则paperId 和 owner必定有值,sheet代表一个评测下的某一个试卷的答题卡数据,且答题卡的scope对应为评测的scope,且school和creatorId 与评测保持一致,且sheet.id从前端传递来的无效,
  81. 因为id需要根据当前评测examId的试卷绑定的答题卡覆写或者新建
  82. 如果检测到examId下对应的试卷绑定答题卡sheet为空则新生成一个答题卡id,并自动绑定。
  83. @条件2 当examId没有值,且paperId有值,sheet代表一个试卷库的某一个试卷的答题卡数据,且答题卡的scope对应为试卷库paperId的scope,且school和creatorId 与评测保持一致,且sheet.id从前端传递来的无效,
  84. 因为id需要根据试卷绑定的答题卡覆写或者新建
  85. 如果检测到paperId的试卷绑定的答题卡sheett为空则新生成一个答题卡id,并自动绑定。如果检测aperId的试卷已经绑定答题卡,则只需要更新实时的答题卡数据。
  86. @条件3 当examId和paperId都没有值,sheet代表与评测,试卷库无关的答题卡数据,且答题卡的scope ,school和creatorId 根据当前创建者需要保存数据的规则有关。
  87. 且sheet.id,如果有值则是更新答题卡内容数据。
  88. 如果sheet.id,没有值则是新建保存答题卡数据。
  89. @条件4 其他情况的参数传递不予以考虑,直接返回400参数错误。
  90. **/
  91. /// <summary>
  92. /// 新增 或 修改答题卡
  93. /// </summary>
  94. /// <param name="request"></param>
  95. /// <returns></returns>
  96. [ProducesDefaultResponseType]
  97. [HttpPost("upsert")]
  98. [Authorize(Roles = "IES")]
  99. public async Task<IActionResult> Upsert(JsonElement request)
  100. {
  101. try
  102. {
  103. if (!request.TryGetProperty("sheet", out JsonElement _sheet)) { return BadRequest(); }
  104. request.TryGetProperty("examId", out JsonElement _examId);
  105. request.TryGetProperty("paperId", out JsonElement _paperId);
  106. request.TryGetProperty("owner", out JsonElement _owner);
  107. var client = _azureCosmos.GetCosmosClient();
  108. SheetConfig sheet = _sheet.ToObject<SheetConfig>();
  109. if (sheet == null) {
  110. return BadRequest("sheet params is null!");
  111. }
  112. if (!sheet.code.StartsWith("SheetConfig-")) {
  113. sheet.code = $"SheetConfig-{sheet.code}";
  114. }
  115. sheet.pk = $"SheetConfig";
  116. sheet.ttl = -1;
  117. string tbname = sheet.scope.Equals("school", StringComparison.OrdinalIgnoreCase)? "School": "Teacher";
  118. // @条件1 代表需要更新评测的某一个试卷的答题卡
  119. if (!_paperId.ValueKind.Equals(JsonValueKind.Null) && !_examId.ValueKind.Equals(JsonValueKind.Null) && !string.IsNullOrEmpty($"{ _examId}") && !string.IsNullOrEmpty($"{ _paperId}"))
  120. {
  121. sheet.from = "exam";
  122. string code = "";
  123. if (!string.IsNullOrEmpty($"{_owner}") && $"{_owner}".Equals("school", StringComparison.OrdinalIgnoreCase)) {
  124. code = $"Exam-{sheet.school}";
  125. sheet.scope = "school";
  126. }
  127. else {
  128. code = $"Exam-{sheet.creatorId}";
  129. sheet.scope = "private";
  130. }
  131. try
  132. {
  133. ExamInfo exam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ExamInfo>($"{_examId}", new PartitionKey($"{code}"));
  134. var ps = exam.papers.Where(p => p.id == $"{_paperId}").FirstOrDefault();
  135. if (ps != null)
  136. {
  137. if (string.IsNullOrEmpty(ps.sheet) || !Guid.TryParse(ps.sheet, out Guid guid))
  138. {
  139. sheet.id = Guid.NewGuid().ToString();
  140. ps.sheet = sheet.id;
  141. }
  142. else {
  143. sheet.id = ps.sheet;
  144. }
  145. if (string.IsNullOrEmpty(sheet.no)) {
  146. sheet.no = await SheetService.genSheetNo(client, _dingDing, _option, sheet.code, tbname, sheet.from);
  147. }
  148. ps.sheetNo = sheet.no;
  149. exam = await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync<ExamInfo>(exam, $"{_examId}", new PartitionKey($"{code}"));
  150. }
  151. else
  152. {
  153. return BadRequest($"paperId:{_paperId} is not exist!");
  154. }
  155. }
  156. catch (Exception ex)
  157. {
  158. return BadRequest($"{ex.Message},examId:{_examId} is not exist!");
  159. }
  160. }
  161. // @条件2 代表需要更新或保存试卷库的某一个试卷的答题卡
  162. else if ((string.IsNullOrEmpty($"{ _examId}") || !_examId.ValueKind.Equals(JsonValueKind.Null))
  163. && !_paperId.ValueKind.Equals(JsonValueKind.Null) && !string.IsNullOrEmpty($"{ _paperId}"))
  164. {
  165. sheet.from = "paper";
  166. string code = sheet.scope.Equals("school", StringComparison.OrdinalIgnoreCase) ? $"Paper-{sheet.school}": $"Paper-{sheet.creatorId}";
  167. try
  168. {
  169. Paper paper = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<Paper>($"{_paperId}", new PartitionKey($"{code}"));
  170. if (string.IsNullOrEmpty(paper.sheet)||!Guid.TryParse(paper.sheet,out Guid guid))
  171. {
  172. sheet.id = Guid.NewGuid().ToString();
  173. paper.sheet = sheet.id;
  174. }
  175. else
  176. {
  177. sheet.id = paper.sheet;
  178. }
  179. if (string.IsNullOrEmpty(sheet.no)) {
  180. sheet.no = await SheetService.genSheetNo(client, _dingDing, _option, sheet.code, tbname, sheet.from);
  181. paper.sheetNo = sheet.no;
  182. }
  183. paper.sheetNo = sheet.no;
  184. paper = await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<Paper>(paper, $"{_paperId}", new PartitionKey($"{code}"));
  185. }
  186. catch (Exception ex)
  187. {
  188. return BadRequest($"{ex.Message},paperId:{_paperId} is not exist!");
  189. }
  190. }
  191. //@条件3 代表需要直接产生一个答题卡
  192. else if (string.IsNullOrEmpty($"{ _examId}") && string.IsNullOrEmpty($"{ _paperId}"))
  193. {
  194. sheet.from = "sheet";
  195. if (string.IsNullOrEmpty(sheet.id)) {
  196. sheet.id = Guid.NewGuid().ToString();
  197. }
  198. if (string.IsNullOrEmpty(sheet.no)) {
  199. sheet.no = await SheetService.genSheetNo(client, _dingDing, _option, sheet.code, tbname, sheet.from);
  200. }
  201. }
  202. else {
  203. //@条件4
  204. return BadRequest("params is error!");
  205. }
  206. if (string.IsNullOrEmpty(sheet.no))
  207. {
  208. sheet.no = await SheetService.genSheetNo(client, _dingDing, _option, sheet.code, tbname,sheet.from);
  209. }
  210. sheet = await client.GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(sheet, new PartitionKey($"{sheet.code}"));
  211. return Ok(new { config = sheet, status=200 });
  212. }
  213. catch (CosmosException ex)
  214. {
  215. return Ok(new { status = ex.Status });
  216. }
  217. catch (Exception e)
  218. {
  219. await _dingDing.SendBotMsg($"OS,{_option.Location},common/SheetConfig/upsert()\n{e.Message}{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  220. return BadRequest(e.Message);
  221. }
  222. }
  223. /// <summary>
  224. ///删除答题卡
  225. /// </summary>
  226. /// <param name="request"></param>
  227. /// <returns></returns>
  228. [ProducesDefaultResponseType]
  229. [HttpPost("delete")]
  230. [Authorize(Roles = "IES")]
  231. public async Task<IActionResult> Delete(JsonElement request)
  232. {
  233. try
  234. {
  235. var client = _azureCosmos.GetCosmosClient();
  236. if (!request.TryGetProperty("ids", out JsonElement ids)) return BadRequest();
  237. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  238. if (!request.TryGetProperty("scope", out JsonElement scope)) return BadRequest();
  239. List<string> idslist = ids.ToObject<List<string>>();
  240. if ($"{scope}" .Equals("school"))
  241. {
  242. await client.GetContainer(Constant.TEAMModelOS, "School").DeleteItemsStreamAsync(idslist, $"SheetConfig-{code}");
  243. }
  244. else
  245. {
  246. await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemsStreamAsync(idslist, $"SheetConfig-{code}");
  247. }
  248. return Ok(new { status = 404 });
  249. }
  250. catch (CosmosException ex) {
  251. return Ok(new { status = ex.Status });
  252. }
  253. catch (Exception e)
  254. {
  255. await _dingDing.SendBotMsg($"OS,{_option.Location},common/SheetConfig/delete()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  256. return BadRequest(e.StackTrace);
  257. }
  258. }
  259. }
  260. }