ArtController.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. using Azure.Cosmos;
  2. using DinkToPdf.Contracts;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text.Json;
  13. using System.Threading.Tasks;
  14. using TEAMModelOS.Filter;
  15. using TEAMModelOS.Models;
  16. using TEAMModelOS.SDK;
  17. using TEAMModelOS.SDK.DI;
  18. using TEAMModelOS.SDK.Extension;
  19. using TEAMModelOS.SDK.Models;
  20. using TEAMModelOS.SDK.Models.Cosmos.Common;
  21. namespace TEAMModelOS.Controllers.Common
  22. {
  23. [ProducesResponseType(StatusCodes.Status200OK)]
  24. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  25. [Route("common/art")]
  26. [ApiController]
  27. public class ArtController : ControllerBase
  28. {
  29. private readonly AzureCosmosFactory _azureCosmos;
  30. private readonly SnowflakeId _snowflakeId;
  31. private readonly AzureServiceBusFactory _serviceBus;
  32. private readonly DingDing _dingDing;
  33. private readonly Option _option;
  34. private readonly AzureStorageFactory _azureStorage;
  35. private readonly AzureRedisFactory _azureRedis;
  36. private readonly IConverter _converter;
  37. public IConfiguration _configuration { get; set; }
  38. private readonly CoreAPIHttpService _coreAPIHttpService;
  39. public ArtController(IConverter converter, CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
  40. IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
  41. {
  42. _coreAPIHttpService = coreAPIHttpService;
  43. _azureCosmos = azureCosmos;
  44. _serviceBus = serviceBus;
  45. _snowflakeId = snowflakeId;
  46. _dingDing = dingDing;
  47. _option = option?.Value;
  48. _azureStorage = azureStorage;
  49. _azureRedis = azureRedis;
  50. _configuration = configuration;
  51. _converter = converter;
  52. }
  53. /// <summary>
  54. /// 保存艺术评价信息
  55. /// </summary>
  56. /// <param name="request"></param>
  57. /// <returns></returns>
  58. [ProducesDefaultResponseType]
  59. [AuthToken(Roles = "teacher,admin")]
  60. [HttpPost("save")]
  61. [Authorize(Roles = "IES")]
  62. public async Task<IActionResult> Save(JsonElement request)
  63. {
  64. try
  65. {
  66. if (!request.TryGetProperty("art", out JsonElement art)) return BadRequest();
  67. var client = _azureCosmos.GetCosmosClient();
  68. ArtEvaluation ae = art.ToObject<ArtEvaluation>();
  69. string code = ae.school;
  70. ae.publish = 0;
  71. ae.ttl = -1;
  72. ae.progress = "going";
  73. if (!ae.owner.Equals("area"))
  74. {
  75. ae.owner = "school";
  76. ae.code = "Art-" + code;
  77. ae.scope = "school";
  78. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  79. ae.createTime = now;
  80. }
  81. if (string.IsNullOrEmpty(ae.id))
  82. {
  83. ae.id = Guid.NewGuid().ToString();
  84. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(ae, new PartitionKey($"{ae.code}"));
  85. }
  86. else
  87. {
  88. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(ae, ae.id, new PartitionKey($"{ae.code}"));
  89. }
  90. return Ok(new { ae });
  91. }
  92. catch (Exception ex)
  93. {
  94. await _dingDing.SendBotMsg($"OS,{_option.Location},art/save()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  95. return Ok(new { code = 500, msg = ex.Message });
  96. }
  97. }
  98. [ProducesDefaultResponseType]
  99. [AuthToken(Roles = "teacher,admin")]
  100. [HttpPost("delete")]
  101. [Authorize(Roles = "IES")]
  102. public async Task<IActionResult> Delete(JsonElement request)
  103. {
  104. try
  105. {
  106. //object userScope = null;
  107. //object _standard = null;
  108. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  109. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  110. /* string standard = null;
  111. HttpContext?.Items?.TryGetValue("Scope", out userScope);
  112. if (userScope != null && $"{userScope}".Equals(Constant.ScopeTeacher))
  113. {
  114. HttpContext?.Items?.TryGetValue("Standard", out _standard);
  115. standard = _standard != null && string.IsNullOrEmpty($"{userScope}") ? _standard.ToString() : null;
  116. }*/
  117. var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  118. var client = _azureCosmos.GetCosmosClient();
  119. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Art-{code}"));
  120. if (sresponse.Status == 200)
  121. {
  122. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  123. ArtEvaluation art = json.ToObject<ArtEvaluation>();
  124. //必须是本人或者这个学校的管理者才能删除
  125. bool flag = false;
  126. if (art.creatorId == userid)
  127. {
  128. flag = true;
  129. }
  130. else
  131. {
  132. if (art.scope == "school" && art.school.Equals(school))
  133. {
  134. flag = true;
  135. }
  136. }
  137. /*try
  138. {
  139. (List<RMember> members, List<RGroupList> groups) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, null, art.school, null);
  140. await StatisticsService.DoChange(new TeacherTrainChange
  141. { standard = standard, tmdids = members.Select(x => x.id)?.ToList(), school = art.school, update = new HashSet<string> { StatisticsService.OfflineRecord }, statistics = 0 }, _azureCosmos);
  142. }
  143. catch (Exception ex)
  144. {
  145. await _dingDing.SendBotMsg($"OS,{_option.Location},art/delete()ex\n{ex.Message}\n{ex.StackTrace},\n", GroupNames.醍摩豆服務運維群組);
  146. }*/
  147. if (flag)
  148. {
  149. art.status = 404;
  150. foreach (var info in art.settings) {
  151. /* if (info.TryGetProperty("examId", out JsonElement eId)) {
  152. }*/
  153. foreach (var acs in info.task) {
  154. if (!string.IsNullOrEmpty(acs.acId)) {
  155. if (acs.type.Equals("exam"))
  156. {
  157. Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(acs.acId, new PartitionKey($"Exam-{code}"));
  158. if (response.Status == 200)
  159. {
  160. ExamInfo data = JsonDocument.Parse(response.Content).RootElement.Deserialize<ExamInfo>();
  161. data.status = 404;
  162. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Exam-{code}"));
  163. }
  164. }
  165. if (acs.type.Equals("survey"))
  166. {
  167. Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(acs.acId, new PartitionKey($"Survey-{code}"));
  168. if (response.Status == 200)
  169. {
  170. Survey data = JsonDocument.Parse(response.Content).RootElement.Deserialize<Survey>();
  171. data.status = 404;
  172. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Survey-{code}"));
  173. }
  174. }
  175. if (acs.type.Equals("homework"))
  176. {
  177. Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(acs.acId, new PartitionKey($"Homework-{code}"));
  178. if (response.Status == 200)
  179. {
  180. Homework data = JsonDocument.Parse(response.Content).RootElement.Deserialize<Homework>();
  181. data.status = 404;
  182. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Homework-{code}"));
  183. }
  184. }
  185. }
  186. }
  187. }
  188. /*if (!string.IsNullOrEmpty(art.examId))
  189. {
  190. Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.examId, new PartitionKey($"Exam-{code}"));
  191. if (response.Status == 200)
  192. {
  193. ExamLite data = JsonDocument.Parse(response.Content).RootElement.Deserialize<ExamLite>();
  194. data.status = 404;
  195. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Exam-{code}"));
  196. }
  197. }
  198. if (!string.IsNullOrEmpty(art.surveyId))
  199. {
  200. Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.surveyId, new PartitionKey($"Survey-{code}"));
  201. if (response.Status == 200)
  202. {
  203. Survey data = JsonDocument.Parse(response.Content).RootElement.Deserialize<Survey>();
  204. data.status = 404;
  205. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Survey-{code}"));
  206. }
  207. }
  208. if (!string.IsNullOrEmpty(art.workId))
  209. {
  210. Azure.Response response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(art.workId, new PartitionKey($"Homework-{code}"));
  211. if (response.Status == 200)
  212. {
  213. Homework data = JsonDocument.Parse(response.Content).RootElement.Deserialize<Homework>();
  214. data.status = 404;
  215. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(data, data.id, new PartitionKey($"Homework-{code}"));
  216. }
  217. }*/
  218. await client.GetContainer(Constant.TEAMModelOS, "Common").ReplaceItemAsync(art, art.id, new PartitionKey($"{art.code}"));
  219. }
  220. }
  221. return Ok(new { id });
  222. }
  223. catch (Exception e)
  224. {
  225. await _dingDing.SendBotMsg($"OS,{_option.Location},art/delete()\n{e.Message}\n{e.StackTrace},\n", GroupNames.醍摩豆服務運維群組);
  226. return Ok(new { code = 500 });
  227. }
  228. }
  229. /// <param name="request"></param>
  230. /// <returns></returns>
  231. [ProducesDefaultResponseType]
  232. [Authorize(Roles = "IES")]
  233. [AuthToken(Roles = "teacher,admin")]
  234. [HttpPost("find")]
  235. public async Task<IActionResult> Find(JsonElement request)
  236. {
  237. try
  238. {
  239. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  240. var client = _azureCosmos.GetCosmosClient();
  241. var query = $"select c.id,c.img,c.name,c.type,c.startTime,c.endTime,c.presenter,c.topic,c.address,c.owner,c.progress from c where (c.status<>404 or IS_DEFINED(c.status) = false )";
  242. string continuationToken = string.Empty;
  243. string token = default;
  244. //是否需要进行分页查询,默认不分页
  245. bool iscontinuation = false;
  246. if (request.TryGetProperty("token", out JsonElement token_1))
  247. {
  248. token = token_1.GetString();
  249. iscontinuation = true;
  250. };
  251. //默认不指定返回大小
  252. int? topcout = null;
  253. if (request.TryGetProperty("count", out JsonElement jcount))
  254. {
  255. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  256. {
  257. topcout = data;
  258. }
  259. }
  260. List<object> arts = new();
  261. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Art-{code}") }))
  262. {
  263. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  264. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  265. {
  266. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  267. {
  268. arts.Add(obj.ToObject<object>());
  269. }
  270. }
  271. if (iscontinuation)
  272. {
  273. continuationToken = item.GetContinuationToken();
  274. break;
  275. }
  276. }
  277. return Ok(new { arts });
  278. }
  279. catch (Exception e)
  280. {
  281. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  282. return Ok(new { code = 500});
  283. }
  284. }
  285. [ProducesDefaultResponseType]
  286. [Authorize(Roles = "IES")]
  287. [AuthToken(Roles = "teacher,admin,student")]
  288. [HttpPost("find-summary")]
  289. public async Task<IActionResult> FindSummary(JsonElement request)
  290. {
  291. try
  292. {
  293. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  294. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  295. var client = _azureCosmos.GetCosmosClient();
  296. ArtEvaluation art = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<ArtEvaluation>(id.GetString(), new PartitionKey($"Art-{code}"));
  297. if (art != null)
  298. {
  299. return Ok(new { art, code = 200 });
  300. }
  301. else
  302. {
  303. return Ok(new { art, code = 404 });
  304. }
  305. }
  306. catch (CosmosException ex) when (ex.Status == 404)
  307. {
  308. return Ok(new { code = 404 });
  309. }
  310. catch (Exception e)
  311. {
  312. await _dingDing.SendBotMsg($"OS,{_option.Location},art/FindSummary()\n{e.Message}\n{e.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  313. return Ok(new { code = 500 });
  314. }
  315. }
  316. /// <param name="request"></param>
  317. /// <returns></returns>
  318. [ProducesDefaultResponseType]
  319. [Authorize(Roles = "IES")]
  320. [AuthToken(Roles = "teacher,admin")]
  321. [HttpPost("find-by-teacher")]
  322. public async Task<IActionResult> FindByTeacher(JsonElement request)
  323. {
  324. try
  325. {
  326. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  327. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  328. var client = _azureCosmos.GetCosmosClient();
  329. var query = $"select c.id,c.img,c.name,c.startTime,c.type,c.endTime,c.presenter,c.topic,c.address,c.owner from c join A0 in c.teachers where A0.id = '{tId}'";
  330. List<object> arts = new();
  331. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Art-{code}") }))
  332. {
  333. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  334. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  335. {
  336. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  337. {
  338. arts.Add(obj.ToObject<object>());
  339. }
  340. }
  341. }
  342. return Ok(new { arts });
  343. }
  344. catch (Exception e)
  345. {
  346. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-by-teacher()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  347. return Ok(new { code = 500});
  348. }
  349. }
  350. [ProducesDefaultResponseType]
  351. [Authorize(Roles = "IES")]
  352. [AuthToken(Roles = "teacher,admin")]
  353. [HttpPost("find-summary-by-teacher")]
  354. public async Task<IActionResult> FindSummaryByTeacher(JsonElement request)
  355. {
  356. try
  357. {
  358. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  359. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  360. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  361. var client = _azureCosmos.GetCosmosClient();
  362. List<object> arts = new();
  363. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: $"select value(c) from c join A0 in c.teachers where A0.id = '{tId}' and c.id = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Art-{code}") }))
  364. {
  365. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  366. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  367. {
  368. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  369. {
  370. arts.Add(obj.ToObject<object>());
  371. }
  372. }
  373. }
  374. return Ok(new { arts });
  375. }
  376. catch (Exception e)
  377. {
  378. await _dingDing.SendBotMsg($"OS,{_option.Location},art/find-summary-by-teacher()\n{e.Message}\n{e.StackTrace}\n{e.StackTrace}", GroupNames.醍摩豆服務運維群組);
  379. return Ok(new { code = 500 });
  380. }
  381. }
  382. }
  383. }