StudyController.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.Options;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Net;
  10. using System.Text.Json;
  11. using System.Threading.Tasks;
  12. using TEAMModelOS.Filter;
  13. using TEAMModelOS.Models;
  14. using TEAMModelOS.SDK;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.Extension;
  17. using TEAMModelOS.SDK.Models;
  18. using TEAMModelOS.SDK.Models.Cosmos.Teacher;
  19. namespace TEAMModelOS.Controllers.Common
  20. {
  21. [ProducesResponseType(StatusCodes.Status200OK)]
  22. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  23. //[Authorize(Roles = "IES5")
  24. [Route("common/study")]
  25. [ApiController]
  26. public class StudyController : ControllerBase
  27. {
  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. private readonly AzureRedisFactory _azureRedis;
  35. public IConfiguration _configuration { get; set; }
  36. public StudyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
  37. IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
  38. {
  39. _azureCosmos = azureCosmos;
  40. _serviceBus = serviceBus;
  41. _snowflakeId = snowflakeId;
  42. _dingDing = dingDing;
  43. _option = option?.Value;
  44. _azureStorage = azureStorage;
  45. _azureRedis = azureRedis;
  46. _configuration = configuration;
  47. }
  48. /// <summary>
  49. /// 保存研修信息
  50. /// </summary>
  51. /// <param name="request"></param>
  52. /// <returns></returns>
  53. [ProducesDefaultResponseType]
  54. [AuthToken(Roles = "teacher,admin")]
  55. [HttpPost("save")]
  56. public async Task<IActionResult> Save(JsonElement request)
  57. {
  58. try
  59. {
  60. var client = _azureCosmos.GetCosmosClient();
  61. Study study = request.ToObject<Study>();
  62. string code = study.school;
  63. study.ttl = -1;
  64. study.owner = "school";
  65. study.code = "Study-" + code;
  66. study.scope = "school";
  67. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  68. study.createTime = now;
  69. if (string.IsNullOrEmpty(study.id))
  70. {
  71. study.progress = "going";
  72. study.id = Guid.NewGuid().ToString();
  73. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(study, new PartitionKey($"{study.code}"));
  74. }
  75. else
  76. {
  77. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  78. }
  79. return Ok(new { study });
  80. }
  81. catch (Exception ex)
  82. {
  83. await _dingDing.SendBotMsg($"OS,{_option.Location},study/save()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  84. return BadRequest();
  85. }
  86. }
  87. /// <summary>
  88. /// 签到
  89. /// </summary>
  90. /// <param name="request"></param>
  91. /// <returns></returns>
  92. [ProducesDefaultResponseType]
  93. //[AuthToken(Roles = "teacher,admin")]
  94. [HttpPost("sign-in")]
  95. public async Task<IActionResult> Sign(JsonElement request)
  96. {
  97. await _dingDing.SendBotMsg($"OS,{_option.Location},study/sign-in()\n{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  98. try
  99. {
  100. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  101. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  102. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  103. var client = _azureCosmos.GetCosmosClient();
  104. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  105. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(id.ToString(), new PartitionKey($"StudyRecord-{tId}"));
  106. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  107. if (sresponse.Status == (int)HttpStatusCode.OK)
  108. {
  109. var sJson = await JsonDocument.ParseAsync(sresponse.ContentStream);
  110. Study study = sJson.ToObject<Study>();
  111. if (response.Status == (int)HttpStatusCode.OK)
  112. {
  113. var json = await JsonDocument.ParseAsync(response.ContentStream);
  114. StudyRecord record = json.ToObject<StudyRecord>();
  115. record.sign = study.startTime < now ? "2" : "1";
  116. record.signTime = now;
  117. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(record, record.id, new PartitionKey($"{record.code}"));
  118. }
  119. else
  120. {
  121. StudyRecord setting = new();
  122. setting.id = id.GetString();
  123. setting.tId = tId.GetString();
  124. setting.signTime = now;
  125. setting.code = "StudyRecord-" + tId.GetString();
  126. setting.sign = study.startTime < now ? "2" : "1";
  127. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(setting, new PartitionKey($"{setting.code}"));
  128. }
  129. }
  130. else
  131. {
  132. return Ok(new { code = HttpStatusCode.NotFound });
  133. }
  134. return Ok();
  135. }
  136. catch (Exception ex)
  137. {
  138. await _dingDing.SendBotMsg($"OS,{_option.Location},study/sign-in()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  139. return BadRequest();
  140. }
  141. }
  142. /*/// <summary>
  143. /// 上传作业
  144. /// </summary>
  145. /// <param name="request"></param>
  146. /// <returns></returns>
  147. [ProducesDefaultResponseType]
  148. [AuthToken(Roles = "teacher,admin")]
  149. [HttpPost("upload")]
  150. public async Task<IActionResult> Upload(JsonElement request)
  151. {
  152. try
  153. {
  154. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  155. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  156. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  157. if (!request.TryGetProperty("hw", out JsonElement hw)) return BadRequest();
  158. var client = _azureCosmos.GetCosmosClient();
  159. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  160. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  161. if (response.Status == (int)HttpStatusCode.OK)
  162. {
  163. var json = await JsonDocument.ParseAsync(response.ContentStream);
  164. Study study = json.ToObject<Study>();
  165. bool flag = study.teachers.Exists(s => s.id.Equals(tId.GetString()));
  166. if (flag)
  167. {
  168. foreach (Setting setting in study.teachers)
  169. {
  170. if (setting.id.Equals(tId.GetString()))
  171. {
  172. setting.hwTime = now;
  173. setting.hw = hw.GetString();
  174. }
  175. }
  176. }
  177. else {
  178. Setting setting = new Setting();
  179. setting.id = tId.GetString();
  180. setting.hwTime = now;
  181. setting.hw = hw.GetString();
  182. study.teachers.Add(setting);
  183. }
  184. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  185. }
  186. else
  187. {
  188. return Ok(new { code = HttpStatusCode.NotFound });
  189. }
  190. return Ok(new { code = HttpStatusCode.OK});
  191. }
  192. catch (Exception ex)
  193. {
  194. await _dingDing.SendBotMsg($"OS,{_option.Location},study/upload()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  195. return BadRequest();
  196. }
  197. }*/
  198. [ProducesDefaultResponseType]
  199. //[AuthToken(Roles = "teacher")]
  200. [HttpPost("delete")]
  201. public async Task<IActionResult> Delete(JsonElement request)
  202. {
  203. try
  204. {
  205. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  206. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  207. var client = _azureCosmos.GetCosmosClient();
  208. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  209. if (sresponse.Status == 200)
  210. {
  211. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  212. Study study = json.ToObject<Study>();
  213. if (!string.IsNullOrEmpty(study.examId))
  214. {
  215. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(study.examId, new PartitionKey($"ExamLite-{code}"));
  216. }
  217. if (!string.IsNullOrEmpty(study.surveyId))
  218. {
  219. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(study.surveyId, new PartitionKey($"Survey-{code}"));
  220. }
  221. if (!string.IsNullOrEmpty(study.workId))
  222. {
  223. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(study.workId, new PartitionKey($"Homework-{code}"));
  224. }
  225. }
  226. var response = await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  227. return Ok(new { id, code = response.Status });
  228. }
  229. catch (Exception e)
  230. {
  231. await _dingDing.SendBotMsg($"OS,{_option.Location},study/delete()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  232. return BadRequest();
  233. }
  234. }
  235. /// <param name="request"></param>
  236. /// <returns></returns>
  237. [ProducesDefaultResponseType]
  238. //[AuthToken(Roles = "teacher")]
  239. [HttpPost("find")]
  240. public async Task<IActionResult> Find(JsonElement requert)
  241. {
  242. try
  243. {
  244. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  245. var client = _azureCosmos.GetCosmosClient();
  246. var query = $"select c.id,c.img,c.name,c.type,c.startTime,c.endTime,c.presenter,c.topic,c.address,c.owner from c ";
  247. string continuationToken = string.Empty;
  248. string token = default;
  249. //是否需要进行分页查询,默认不分页
  250. bool iscontinuation = false;
  251. if (requert.TryGetProperty("token", out JsonElement token_1))
  252. {
  253. token = token_1.GetString();
  254. iscontinuation = true;
  255. };
  256. //默认不指定返回大小
  257. int? topcout = null;
  258. if (requert.TryGetProperty("count", out JsonElement jcount))
  259. {
  260. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  261. {
  262. topcout = data;
  263. }
  264. }
  265. List<object> studies = new();
  266. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Study-{code}") }))
  267. {
  268. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  269. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  270. {
  271. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  272. {
  273. studies.Add(obj.ToObject<object>());
  274. }
  275. }
  276. if (iscontinuation)
  277. {
  278. continuationToken = item.GetContinuationToken();
  279. break;
  280. }
  281. }
  282. return Ok(new { studies });
  283. }
  284. catch (Exception e)
  285. {
  286. await _dingDing.SendBotMsg($"OS,{_option.Location},study/find()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  287. return BadRequest();
  288. }
  289. }
  290. [ProducesDefaultResponseType]
  291. //[AuthToken(Roles = "teacher")]
  292. [HttpPost("find-summary")]
  293. public async Task<IActionResult> FindSummary(JsonElement requert)
  294. {
  295. try
  296. {
  297. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  298. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  299. var client = _azureCosmos.GetCosmosClient();
  300. Study study = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Study>(id.GetString(), new PartitionKey($"Study-{code}"));
  301. if (study != null)
  302. {
  303. (List<RMember> tchList, List<RGroupList> classInfo) = await GroupListService.GetStutmdidListids(client, _dingDing, study.tchLists, study.school);
  304. List<StudyRecord> records = new();
  305. foreach (var member in tchList)
  306. {
  307. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(id.ToString(), new PartitionKey($"StudyRecord-{member.id}"));
  308. if (response.Status == (int)HttpStatusCode.OK)
  309. {
  310. var json = await JsonDocument.ParseAsync(response.ContentStream);
  311. StudyRecord record = json.ToObject<StudyRecord>();
  312. records.Add(record);
  313. }
  314. }
  315. // (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
  316. //(List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school);
  317. return Ok(new { study, records, status = 200 });
  318. }
  319. else
  320. {
  321. return Ok(new { study, status = 404 });
  322. }
  323. }
  324. catch (Exception e)
  325. {
  326. await _dingDing.SendBotMsg($"OS,{_option.Location},study/FindSummary()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  327. return Ok(new { status = 404 });
  328. }
  329. }
  330. /// <param name="request"></param>
  331. /// <returns></returns>
  332. [ProducesDefaultResponseType]
  333. //[AuthToken(Roles = "teacher")]
  334. [HttpPost("find-by-teacher")]
  335. public async Task<IActionResult> FindByTeacher(JsonElement requert)
  336. {
  337. try
  338. {
  339. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  340. if (!requert.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  341. var client = _azureCosmos.GetCosmosClient();
  342. 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}'";
  343. List<object> studies = new();
  344. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Study-{code}") }))
  345. {
  346. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  347. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  348. {
  349. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  350. {
  351. studies.Add(obj.ToObject<object>());
  352. }
  353. }
  354. }
  355. return Ok(new { studies });
  356. }
  357. catch (Exception e)
  358. {
  359. await _dingDing.SendBotMsg($"OS,{_option.Location},study/find-by-teacher()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  360. return BadRequest();
  361. }
  362. }
  363. [ProducesDefaultResponseType]
  364. //[AuthToken(Roles = "teacher")]
  365. [HttpPost("find-summary-by-teacher")]
  366. public async Task<IActionResult> FindSummaryByTeacher(JsonElement requert)
  367. {
  368. try
  369. {
  370. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  371. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  372. if (!requert.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  373. var client = _azureCosmos.GetCosmosClient();
  374. List<object> studies = new();
  375. 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($"Study-{code}") }))
  376. {
  377. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  378. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  379. {
  380. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  381. {
  382. studies.Add(obj.ToObject<object>());
  383. }
  384. }
  385. }
  386. return Ok(new { studies });
  387. }
  388. catch (Exception e)
  389. {
  390. await _dingDing.SendBotMsg($"OS,{_option.Location},study/find-summary-by-teacher()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  391. return BadRequest();
  392. }
  393. }
  394. [ProducesDefaultResponseType]
  395. //[AuthToken(Roles = "teacher")]
  396. [HttpPost("audit")]
  397. public async Task<IActionResult> Audit(JsonElement request)
  398. {
  399. try
  400. {
  401. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  402. //if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  403. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  404. if (!request.TryGetProperty("type", out JsonElement type)) return BadRequest();
  405. var client = _azureCosmos.GetCosmosClient();
  406. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  407. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(id.ToString(), new PartitionKey($"StudyRecord-{tId}"));
  408. if (response.Status == (int)HttpStatusCode.OK)
  409. {
  410. var json = await JsonDocument.ParseAsync(response.ContentStream);
  411. StudyRecord study = json.ToObject<StudyRecord>();
  412. if (study.tId.Equals(tId.GetString()))
  413. {
  414. study.status = type.GetInt32();
  415. study.aTime = now;
  416. }
  417. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  418. }
  419. else
  420. {
  421. StudyRecord setting = new()
  422. {
  423. id = id.GetString(),
  424. tId = tId.GetString(),
  425. code = "StudyRecord-" + tId.GetString(),
  426. status = type.GetInt32(),
  427. aTime = now
  428. };
  429. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(setting, new PartitionKey($"{setting.code}"));
  430. }
  431. return Ok(new { code = HttpStatusCode.OK });
  432. }
  433. catch (Exception ex)
  434. {
  435. await _dingDing.SendBotMsg($"OS,{_option.Location},Study/audit()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  436. return BadRequest();
  437. }
  438. }
  439. }
  440. }