WorkController.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Authorization;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.Configuration;
  6. using Microsoft.Extensions.Options;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Text.Json;
  12. using System.Threading.Tasks;
  13. using TEAMModelOS.Models;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models;
  17. namespace TEAMModelOS.Controllers.Common
  18. {
  19. [ProducesResponseType(StatusCodes.Status200OK)]
  20. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  21. [Route("common/work")]
  22. [ApiController]
  23. public class WorkController : ControllerBase
  24. {
  25. private readonly AzureCosmosFactory _azureCosmos;
  26. private readonly SnowflakeId _snowflakeId;
  27. private readonly AzureServiceBusFactory _serviceBus;
  28. private readonly DingDing _dingDing;
  29. private readonly Option _option;
  30. private readonly AzureStorageFactory _azureStorage;
  31. private readonly AzureRedisFactory _azureRedis;
  32. public IConfiguration _configuration { get; set; }
  33. public WorkController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
  34. IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
  35. {
  36. _azureCosmos = azureCosmos;
  37. _serviceBus = serviceBus;
  38. _snowflakeId = snowflakeId;
  39. _dingDing = dingDing;
  40. _option = option?.Value;
  41. _azureStorage = azureStorage;
  42. _azureRedis = azureRedis;
  43. _configuration = configuration;
  44. }
  45. /// <summary>
  46. /// 保存评测信息
  47. /// </summary>
  48. /// <param name="request"></param>
  49. /// <returns></returns>
  50. [ProducesDefaultResponseType]
  51. //[AuthToken(Roles = "teacher,admin")]
  52. [HttpPost("save")]
  53. [Authorize(Roles = "IES")]
  54. public async Task<IActionResult> Save(JsonElement request)
  55. {
  56. try
  57. {
  58. var client = _azureCosmos.GetCosmosClient();
  59. Homework work = request.ToObject<Homework>();
  60. work.owner = "school";
  61. try
  62. {
  63. work.ttl = -1;
  64. work.code = "Homework-" + work.school;
  65. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  66. work.createTime = now;
  67. if (work.startTime > now)
  68. {
  69. work.progress = "pending";
  70. }
  71. else
  72. {
  73. work.progress = "going";
  74. }
  75. if (string.IsNullOrEmpty(work.id))
  76. {
  77. work.id = Guid.NewGuid().ToString();
  78. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(work, new PartitionKey($"{work.code}"));
  79. }
  80. else
  81. {
  82. await client.GetContainer("TEAMModelOS", "Common").UpsertItemAsync(work, new PartitionKey($"{work.code}"));
  83. }
  84. }
  85. catch (Exception e)
  86. {
  87. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-saveMore\n{e.Message}{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  88. }
  89. return Ok(new { work.id, code = (int)HttpStatusCode.OK });
  90. }
  91. catch (Exception ex)
  92. {
  93. await _dingDing.SendBotMsg($"OS,{_option.Location},Homework/save()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  94. return BadRequest();
  95. }
  96. }
  97. /// <summary>
  98. /// 上传作答
  99. /// </summary>
  100. /// <param name="request"></param>
  101. /// <returns></returns>
  102. [ProducesDefaultResponseType]
  103. [Authorize(Roles = "IES")]
  104. //[AuthToken(Roles = "teacher,admin")]
  105. [HttpPost("record-in")]
  106. public async Task<IActionResult> RecordIn(JsonElement request)
  107. {
  108. try
  109. {
  110. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  111. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  112. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  113. if (!request.TryGetProperty("ans", out JsonElement ans)) return BadRequest();
  114. var client = _azureCosmos.GetCosmosClient();
  115. //List<List<string>> answer = ans.ToObject<List<List<string>>>();
  116. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  117. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Homework-{code}"));
  118. if (response.Status == (int)HttpStatusCode.OK)
  119. {
  120. var json = await JsonDocument.ParseAsync(response.ContentStream);
  121. Homework work = json.ToObject<Homework>();
  122. //List<List<string>> standard = work.items.answers;
  123. bool flag = work.teachers.Exists(s => s.id.Equals(tId.GetString()));
  124. List<string> ids = work.teachers.Select(e => e.id).ToList();
  125. int index = ids.IndexOf(tId.GetString());
  126. if (flag)
  127. {
  128. foreach (var record in work.teachers)
  129. {
  130. if (record.id.Equals(tId.GetString()))
  131. {
  132. record.hw = ans.GetString();
  133. record.hwTime = now;
  134. }
  135. }
  136. }
  137. else {
  138. Submits submits = new();
  139. submits.id = tId.GetString();
  140. submits.hw = ans.GetString();
  141. submits.hwTime = now;
  142. work.teachers.Add(submits);
  143. }
  144. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(work, work.id, new PartitionKey($"{work.code}"));
  145. }
  146. else
  147. {
  148. return Ok(new { code = HttpStatusCode.NotFound });
  149. }
  150. return Ok(new { code = HttpStatusCode.OK });
  151. }
  152. catch (Exception ex)
  153. {
  154. await _dingDing.SendBotMsg($"OS,{_option.Location},Homework/record-in()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  155. return BadRequest();
  156. }
  157. }
  158. [ProducesDefaultResponseType]
  159. [Authorize(Roles = "IES")]
  160. //[AuthToken(Roles = "teacher")]
  161. [HttpPost("delete")]
  162. public async Task<IActionResult> Delete(JsonElement request)
  163. {
  164. try
  165. {
  166. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  167. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  168. var client = _azureCosmos.GetCosmosClient();
  169. var response = await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Homework-{code}"));
  170. return Ok(new { id, code = response.Status });
  171. }
  172. catch (Exception e)
  173. {
  174. await _dingDing.SendBotMsg($"OS,{_option.Location},Homework/delete()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  175. return BadRequest();
  176. }
  177. }
  178. /// <param name="request"></param>
  179. /// <returns></returns>
  180. [ProducesDefaultResponseType]
  181. [Authorize(Roles = "IES")]
  182. //[AuthToken(Roles = "teacher")]
  183. [HttpPost("find")]
  184. public async Task<IActionResult> Find(JsonElement requert)
  185. {
  186. try
  187. {
  188. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  189. var client = _azureCosmos.GetCosmosClient();
  190. var query = $"select c.id,c.name,c.createTime,c.startTime,c.endTime,c.allowSupply,c.allowComment from c where (c.status<>404 or IS_DEFINED(c.status) = false ) ";
  191. string continuationToken = string.Empty;
  192. string token = default;
  193. //是否需要进行分页查询,默认不分页
  194. bool iscontinuation = false;
  195. if (requert.TryGetProperty("token", out JsonElement token_1))
  196. {
  197. token = token_1.GetString();
  198. iscontinuation = true;
  199. };
  200. //默认不指定返回大小
  201. int? topcout = null;
  202. if (requert.TryGetProperty("count", out JsonElement jcount))
  203. {
  204. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  205. {
  206. topcout = data;
  207. }
  208. }
  209. List<object> works = new();
  210. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Homework-{code}") }))
  211. {
  212. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  213. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  214. {
  215. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  216. {
  217. works.Add(obj.ToObject<object>());
  218. }
  219. }
  220. if (iscontinuation)
  221. {
  222. continuationToken = item.GetContinuationToken();
  223. break;
  224. }
  225. }
  226. return Ok(new { works });
  227. }
  228. catch (Exception e)
  229. {
  230. await _dingDing.SendBotMsg($"OS,{_option.Location},Homework/find()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  231. return BadRequest();
  232. }
  233. }
  234. [ProducesDefaultResponseType]
  235. [Authorize(Roles = "IES")]
  236. //[AuthToken(Roles = "teacher")]
  237. [HttpPost("find-summary")]
  238. public async Task<IActionResult> FindSummary(JsonElement requert)
  239. {
  240. try
  241. {
  242. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  243. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  244. var client = _azureCosmos.GetCosmosClient();
  245. List<Homework> works = new();
  246. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Homework>(queryText: $"select value(c) from c where (c.status<>404 or IS_DEFINED(c.status) = false ) and c.id = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Homework-{code}") }))
  247. {
  248. works.Add(item);
  249. }
  250. return Ok(new { works });
  251. }
  252. catch (Exception e)
  253. {
  254. await _dingDing.SendBotMsg($"OS,{_option.Location},Homework/FindSummary()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  255. return BadRequest();
  256. }
  257. }
  258. /// <param name="request"></param>
  259. /// <returns></returns>
  260. [ProducesDefaultResponseType]
  261. [Authorize(Roles = "IES")]
  262. //[AuthToken(Roles = "teacher")]
  263. [HttpPost("find-by-teacher")]
  264. public async Task<IActionResult> FindByTeacher(JsonElement requert)
  265. {
  266. try
  267. {
  268. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  269. if (!requert.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  270. var client = _azureCosmos.GetCosmosClient();
  271. var query = $"select c.id,c.name,c.createTime,A0.time from c join A0 in c.teachers where (c.status<>404 or IS_DEFINED(c.status) = false ) and A0.id = '{tId}'";
  272. List<object> works = new();
  273. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Homework-{code}") }))
  274. {
  275. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  276. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  277. {
  278. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  279. {
  280. works.Add(obj.ToObject<object>());
  281. }
  282. }
  283. }
  284. return Ok(new { works });
  285. }
  286. catch (Exception e)
  287. {
  288. await _dingDing.SendBotMsg($"OS,{_option.Location},Homework/find-by-teacher()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  289. return BadRequest();
  290. }
  291. }
  292. [ProducesDefaultResponseType]
  293. [Authorize(Roles = "IES")]
  294. //[AuthToken(Roles = "teacher")]
  295. [HttpPost("find-summary-by-teacher")]
  296. public async Task<IActionResult> FindSummaryByTeacher(JsonElement requert)
  297. {
  298. try
  299. {
  300. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  301. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  302. if (!requert.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  303. var client = _azureCosmos.GetCosmosClient();
  304. List<object> works = new();
  305. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c join A0 in c.teachers where (c.status<>404 or IS_DEFINED(c.status) = false ) and A0.id = '{tId}' and c.id = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Homework-{code}") }))
  306. {
  307. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  308. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  309. {
  310. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  311. {
  312. works.Add(obj.ToObject<object>());
  313. }
  314. }
  315. }
  316. return Ok(new { works });
  317. }
  318. catch (Exception e)
  319. {
  320. await _dingDing.SendBotMsg($"OS,{_option.Location},Homework/find-summary-by-teacher()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  321. return BadRequest();
  322. }
  323. }
  324. }
  325. }