StudyController.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  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. namespace TEAMModelOS.Controllers.Common
  19. {
  20. [ProducesResponseType(StatusCodes.Status200OK)]
  21. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  22. //[Authorize(Roles = "IES5")
  23. [Route("common/study")]
  24. [ApiController]
  25. public class StudyController : ControllerBase
  26. {
  27. private readonly AzureCosmosFactory _azureCosmos;
  28. private readonly SnowflakeId _snowflakeId;
  29. private readonly AzureServiceBusFactory _serviceBus;
  30. private readonly DingDing _dingDing;
  31. private readonly Option _option;
  32. private readonly AzureStorageFactory _azureStorage;
  33. private readonly AzureRedisFactory _azureRedis;
  34. public IConfiguration _configuration { get; set; }
  35. public StudyController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
  36. IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
  37. {
  38. _azureCosmos = azureCosmos;
  39. _serviceBus = serviceBus;
  40. _snowflakeId = snowflakeId;
  41. _dingDing = dingDing;
  42. _option = option?.Value;
  43. _azureStorage = azureStorage;
  44. _azureRedis = azureRedis;
  45. _configuration = configuration;
  46. }
  47. /// <summary>
  48. /// 保存研修信息
  49. /// </summary>
  50. /// <param name="request"></param>
  51. /// <returns></returns>
  52. [ProducesDefaultResponseType]
  53. [AuthToken(Roles = "teacher,admin")]
  54. [HttpPost("save")]
  55. public async Task<IActionResult> Save(JsonElement request)
  56. {
  57. try
  58. {
  59. if (!request.TryGetProperty("study", out JsonElement stu)) return BadRequest();
  60. var client = _azureCosmos.GetCosmosClient();
  61. Study study = stu.ToObject<Study>();
  62. string code = study.school;
  63. study.publish = 0;
  64. study.ttl = -1;
  65. study.progress = "going";
  66. if (!study.owner.Equals("area")) {
  67. study.owner = "school";
  68. study.code = "Study-" + code;
  69. study.scope = "school";
  70. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  71. study.createTime = now;
  72. }
  73. if (string.IsNullOrEmpty(study.id))
  74. {
  75. study.id = Guid.NewGuid().ToString();
  76. await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(study, new PartitionKey($"{study.code}"));
  77. }
  78. else
  79. {
  80. if (request.TryGetProperty("ids", out JsonElement ids))
  81. {
  82. List<string> gIds = study.tchLists;
  83. List<string> tIds = ids.ToObject<List<string>>();
  84. foreach (string gId in gIds)
  85. {
  86. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(gId, new PartitionKey($"GroupList"));
  87. if (response.Status == 200)
  88. {
  89. var json = await JsonDocument.ParseAsync(response.ContentStream);
  90. GroupList group = json.ToObject<GroupList>();
  91. foreach (string id in tIds)
  92. {
  93. /* bool flag = group.members.Exists(m => m.id.Equals(id));
  94. if (flag)
  95. {
  96. continue;
  97. }*/
  98. //else {
  99. Member member = new();
  100. member.id = id;
  101. member.type = 1;
  102. member.code = study.school;
  103. group.members.Add(member);
  104. //}
  105. }
  106. await GroupListService.UpsertList(group,_azureCosmos,_configuration,_serviceBus);
  107. // await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(group, group.id, new PartitionKey($"{group.code}"));
  108. }
  109. }
  110. }
  111. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  112. }
  113. return Ok(new { study });
  114. }
  115. catch (Exception ex)
  116. {
  117. await _dingDing.SendBotMsg($"OS,{_option.Location},study/save()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  118. return BadRequest();
  119. }
  120. }
  121. /// <summary>
  122. /// 签到
  123. /// </summary>
  124. /// <param name="request"></param>
  125. /// <returns></returns>
  126. [ProducesDefaultResponseType]
  127. //[AuthToken(Roles = "teacher,admin")]
  128. [HttpPost("sign-in")]
  129. public async Task<IActionResult> Sign(JsonElement request)
  130. {
  131. await _dingDing.SendBotMsg($"OS,{_option.Location},study/sign-in()\n{request.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  132. try
  133. {
  134. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  135. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  136. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  137. var client = _azureCosmos.GetCosmosClient();
  138. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  139. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(id.ToString(), new PartitionKey($"StudyRecord-{tId}"));
  140. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  141. if (sresponse.Status == (int)HttpStatusCode.OK)
  142. {
  143. var sJson = await JsonDocument.ParseAsync(sresponse.ContentStream);
  144. Study study = sJson.ToObject<Study>();
  145. if (response.Status == (int)HttpStatusCode.OK)
  146. {
  147. var json = await JsonDocument.ParseAsync(response.ContentStream);
  148. StudyRecord record = json.ToObject<StudyRecord>();
  149. record.sign = study.startTime < now ? "2" : "1";
  150. record.signTime = now;
  151. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(record, record.id, new PartitionKey($"{record.code}"));
  152. }
  153. else
  154. {
  155. StudyRecord setting = new();
  156. setting.id = id.GetString();
  157. setting.tId = tId.GetString();
  158. setting.signTime = now;
  159. setting.code = "StudyRecord-" + tId.GetString();
  160. setting.sign = study.startTime < now ? "2" : "1";
  161. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(setting, new PartitionKey($"{setting.code}"));
  162. }
  163. }
  164. else
  165. {
  166. return Ok(new { code = HttpStatusCode.NotFound });
  167. }
  168. return Ok();
  169. }
  170. catch (Exception ex)
  171. {
  172. await _dingDing.SendBotMsg($"OS,{_option.Location},study/sign-in()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  173. return BadRequest();
  174. }
  175. }
  176. /*/// <summary>
  177. /// 上传作业
  178. /// </summary>
  179. /// <param name="request"></param>
  180. /// <returns></returns>
  181. [ProducesDefaultResponseType]
  182. [AuthToken(Roles = "teacher,admin")]
  183. [HttpPost("upload")]
  184. public async Task<IActionResult> Upload(JsonElement request)
  185. {
  186. try
  187. {
  188. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  189. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  190. if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  191. if (!request.TryGetProperty("hw", out JsonElement hw)) return BadRequest();
  192. var client = _azureCosmos.GetCosmosClient();
  193. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  194. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  195. if (response.Status == (int)HttpStatusCode.OK)
  196. {
  197. var json = await JsonDocument.ParseAsync(response.ContentStream);
  198. Study study = json.ToObject<Study>();
  199. bool flag = study.teachers.Exists(s => s.id.Equals(tId.GetString()));
  200. if (flag)
  201. {
  202. foreach (Setting setting in study.teachers)
  203. {
  204. if (setting.id.Equals(tId.GetString()))
  205. {
  206. setting.hwTime = now;
  207. setting.hw = hw.GetString();
  208. }
  209. }
  210. }
  211. else {
  212. Setting setting = new Setting();
  213. setting.id = tId.GetString();
  214. setting.hwTime = now;
  215. setting.hw = hw.GetString();
  216. study.teachers.Add(setting);
  217. }
  218. await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}"));
  219. }
  220. else
  221. {
  222. return Ok(new { code = HttpStatusCode.NotFound });
  223. }
  224. return Ok(new { code = HttpStatusCode.OK});
  225. }
  226. catch (Exception ex)
  227. {
  228. await _dingDing.SendBotMsg($"OS,{_option.Location},study/upload()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  229. return BadRequest();
  230. }
  231. }*/
  232. [ProducesDefaultResponseType]
  233. //[AuthToken(Roles = "teacher")]
  234. [HttpPost("delete")]
  235. public async Task<IActionResult> Delete(JsonElement request)
  236. {
  237. try
  238. {
  239. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  240. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  241. var client = _azureCosmos.GetCosmosClient();
  242. var sresponse = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  243. if (sresponse.Status == 200)
  244. {
  245. using var json = await JsonDocument.ParseAsync(sresponse.ContentStream);
  246. Study study = json.ToObject<Study>();
  247. if (!string.IsNullOrEmpty(study.examId))
  248. {
  249. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(study.examId, new PartitionKey($"ExamLite-{code}"));
  250. }
  251. if (!string.IsNullOrEmpty(study.surveyId))
  252. {
  253. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(study.surveyId, new PartitionKey($"Survey-{code}"));
  254. }
  255. if (!string.IsNullOrEmpty(study.workId))
  256. {
  257. await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(study.workId, new PartitionKey($"Homework-{code}"));
  258. }
  259. }
  260. var response = await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"Study-{code}"));
  261. return Ok(new { id, code = response.Status });
  262. }
  263. catch (Exception e)
  264. {
  265. await _dingDing.SendBotMsg($"OS,{_option.Location},study/delete()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  266. return BadRequest();
  267. }
  268. }
  269. /// <param name="request"></param>
  270. /// <returns></returns>
  271. [ProducesDefaultResponseType]
  272. //[AuthToken(Roles = "teacher")]
  273. [HttpPost("find")]
  274. public async Task<IActionResult> Find(JsonElement requert)
  275. {
  276. try
  277. {
  278. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  279. var client = _azureCosmos.GetCosmosClient();
  280. 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 ";
  281. string continuationToken = string.Empty;
  282. string token = default;
  283. //是否需要进行分页查询,默认不分页
  284. bool iscontinuation = false;
  285. if (requert.TryGetProperty("token", out JsonElement token_1))
  286. {
  287. token = token_1.GetString();
  288. iscontinuation = true;
  289. };
  290. //默认不指定返回大小
  291. int? topcout = null;
  292. if (requert.TryGetProperty("count", out JsonElement jcount))
  293. {
  294. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  295. {
  296. topcout = data;
  297. }
  298. }
  299. List<object> studies = new();
  300. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, continuationToken: token, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Study-{code}") }))
  301. {
  302. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  303. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  304. {
  305. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  306. {
  307. studies.Add(obj.ToObject<object>());
  308. }
  309. }
  310. if (iscontinuation)
  311. {
  312. continuationToken = item.GetContinuationToken();
  313. break;
  314. }
  315. }
  316. return Ok(new { studies });
  317. }
  318. catch (Exception e)
  319. {
  320. await _dingDing.SendBotMsg($"OS,{_option.Location},study/find()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  321. return BadRequest();
  322. }
  323. }
  324. [ProducesDefaultResponseType]
  325. //[AuthToken(Roles = "teacher")]
  326. [HttpPost("find-summary")]
  327. public async Task<IActionResult> FindSummary(JsonElement requert)
  328. {
  329. try
  330. {
  331. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  332. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  333. var client = _azureCosmos.GetCosmosClient();
  334. Study study = await client.GetContainer(Constant.TEAMModelOS, "Common").ReadItemAsync<Study>(id.GetString(), new PartitionKey($"Study-{code}"));
  335. if (study != null)
  336. {
  337. (List<RMember> tchList, List<RGroupList> classInfo) = await GroupListService.GetStutmdidListids(client, _dingDing, study.tchLists, study.school);
  338. List<StudyRecord> records = new();
  339. foreach (var member in tchList)
  340. {
  341. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(id.ToString(), new PartitionKey($"StudyRecord-{member.id}"));
  342. if (response.Status == (int)HttpStatusCode.OK)
  343. {
  344. var json = await JsonDocument.ParseAsync(response.ContentStream);
  345. StudyRecord record = json.ToObject<StudyRecord>();
  346. records.Add(record);
  347. }
  348. }
  349. // (List<TmdInfo> tmdInfos, List<ClassListInfo> classInfos) = await TriggerStuActivity.GetTchList(client, _dingDing, ids, $"{school}");
  350. //(List<TmdInfo> tchList, _) = await TriggerStuActivity.GetTchList(client, _dingDing, study.tchLists, study.school);
  351. return Ok(new { study, records, status = 200 });
  352. }
  353. else
  354. {
  355. return Ok(new { study, status = 404 });
  356. }
  357. }
  358. catch (Exception e)
  359. {
  360. await _dingDing.SendBotMsg($"OS,{_option.Location},study/FindSummary()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  361. return Ok(new { status = 404 });
  362. }
  363. }
  364. /// <param name="request"></param>
  365. /// <returns></returns>
  366. [ProducesDefaultResponseType]
  367. //[AuthToken(Roles = "teacher")]
  368. [HttpPost("find-by-teacher")]
  369. public async Task<IActionResult> FindByTeacher(JsonElement requert)
  370. {
  371. try
  372. {
  373. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  374. if (!requert.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  375. var client = _azureCosmos.GetCosmosClient();
  376. 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}'";
  377. List<object> studies = new();
  378. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Study-{code}") }))
  379. {
  380. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  381. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  382. {
  383. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  384. {
  385. studies.Add(obj.ToObject<object>());
  386. }
  387. }
  388. }
  389. return Ok(new { studies });
  390. }
  391. catch (Exception e)
  392. {
  393. await _dingDing.SendBotMsg($"OS,{_option.Location},study/find-by-teacher()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  394. return BadRequest();
  395. }
  396. }
  397. [ProducesDefaultResponseType]
  398. //[AuthToken(Roles = "teacher")]
  399. [HttpPost("find-summary-by-teacher")]
  400. public async Task<IActionResult> FindSummaryByTeacher(JsonElement requert)
  401. {
  402. try
  403. {
  404. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  405. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  406. if (!requert.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  407. var client = _azureCosmos.GetCosmosClient();
  408. List<object> studies = new();
  409. 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}") }))
  410. {
  411. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  412. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  413. {
  414. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  415. {
  416. studies.Add(obj.ToObject<object>());
  417. }
  418. }
  419. }
  420. return Ok(new { studies });
  421. }
  422. catch (Exception e)
  423. {
  424. await _dingDing.SendBotMsg($"OS,{_option.Location},study/find-summary-by-teacher()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  425. return BadRequest();
  426. }
  427. }
  428. [ProducesDefaultResponseType]
  429. [AuthToken(Roles = "teacher,admin,area")]
  430. [HttpPost("audit")]
  431. public async Task<IActionResult> Audit(JsonElement request)
  432. {
  433. try
  434. {
  435. (string tmdid, _, _, string school) = HttpContext.GetAuthTokenInfo();
  436. if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  437. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  438. //if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  439. //if (!request.TryGetProperty("tId", out JsonElement tId)) return BadRequest();
  440. if (!request.TryGetProperty("type", out JsonElement type)) return BadRequest();
  441. var client = _azureCosmos.GetCosmosClient();
  442. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  443. List<Dictionary<string, List<string>>> acId = id.ToObject<List<Dictionary<string, List<string>>>>();
  444. List<string> value = new List<string>();
  445. List<Task> tasks = new();
  446. List<Task<ItemResponse<StudyRecord>>> tasky = new();
  447. List<(string op, string tId)> opp = new List<(string op, string tId)>();
  448. int statu = type.GetInt32();
  449. (string standard, List<string> tmdid, string school, List<string> update, int statistics) list = ( $"{standard}", null, school, new List<string> { StatisticsService.OfflineRecord } , 1);
  450. HashSet<string> tch= new HashSet<string>();
  451. foreach (var aId in acId)
  452. {
  453. foreach (KeyValuePair<string, List<string>> pair in aId)
  454. {
  455. /* await foreach (var key in TeacTask(pair.Key, pair.Value, client, type.GetInt32(), now, standard, school)) {
  456. opp.Add((key.op,key.tId));
  457. };*/
  458. foreach (var teacId in pair.Value)
  459. {
  460. var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(pair.Key, new PartitionKey($"StudyRecord-{teacId}"));
  461. if (response.Status == 200)
  462. {
  463. var json = await JsonDocument.ParseAsync(response.ContentStream);
  464. StudyRecord study = json.ToObject<StudyRecord>();
  465. if (study.tId.Equals(teacId) && study.status == statu)
  466. {
  467. continue;
  468. }
  469. else
  470. {
  471. study.status = statu;
  472. study.aTime = now;
  473. }
  474. tasky.Add(client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(study, study.id, new PartitionKey($"{study.code}")));
  475. }
  476. else
  477. {
  478. StudyRecord setting = new()
  479. {
  480. id = pair.Key,
  481. tId = teacId,
  482. code = "StudyRecord-" + teacId,
  483. status = statu,
  484. aTime = now
  485. };
  486. tasky.Add(client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(setting, new PartitionKey($"{setting.code}")));
  487. }
  488. tch.Add($"{teacId}");
  489. }
  490. }
  491. }
  492. list.tmdid = new List<string>(tch);
  493. await tasky.TaskPage(10);
  494. await StatisticsService.SendServiceBus(list, _configuration, _serviceBus);
  495. return Ok(new { code = HttpStatusCode.OK });
  496. }
  497. catch (Exception ex)
  498. {
  499. await _dingDing.SendBotMsg($"OS,{_option.Location},Study/audit()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  500. return BadRequest();
  501. }
  502. }
  503. private async IAsyncEnumerable<(string op, string tId)> AuditTask(List<Dictionary<string, List<string>>> ids, CosmosClient client, int type, long now, object standard, string school)
  504. {
  505. List<Task> tasks = new();
  506. List<Task<ItemResponse<StudyRecord>>> tasky = new();
  507. foreach (var id in ids)
  508. {
  509. foreach (KeyValuePair<string, List<string>> pair in id)
  510. {
  511. foreach (var teacId in pair.Value)
  512. {
  513. //var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(pair.Key, new PartitionKey($"StudyRecord-{teacId}"));
  514. List<StudyRecord> studyRecords = new();
  515. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StudyRecord>(queryText: $"select value(c) from c where c.id = '{pair.Key}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StudyRecord-{teacId}") }))
  516. {
  517. studyRecords.Add(item);
  518. }
  519. if (studyRecords.Count > 0)
  520. {
  521. if (studyRecords[0].tId.Equals(teacId))
  522. {
  523. studyRecords[0].status = type;
  524. studyRecords[0].aTime = now;
  525. }
  526. tasky.Add(client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(studyRecords[0], studyRecords[0].id, new PartitionKey($"{studyRecords[0].code}")));
  527. }
  528. else
  529. {
  530. StudyRecord setting = new()
  531. {
  532. id = pair.Key,
  533. tId = teacId,
  534. code = "StudyRecord-" + teacId,
  535. status = type,
  536. aTime = now
  537. };
  538. tasky.Add(client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(setting, new PartitionKey($"{setting.code}")));
  539. }
  540. //tasks.Add(StatisticsService.SendServiceBus($"{standard}", $"{teacId}", $"{school}", StatisticsService.OfflineRecord, 1, _configuration, _serviceBus));
  541. }
  542. yield return ("", "");
  543. }
  544. }
  545. await Task.WhenAll(tasks);
  546. await Task.WhenAll(tasky);
  547. }
  548. private async IAsyncEnumerable<(string op, string tId)> TeacTask(string id, List<string> ids, CosmosClient client, int type, long now, object standard, string school)
  549. {
  550. List<Task> tasks = new();
  551. foreach (var teacId in ids)
  552. {
  553. /* var response = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemStreamAsync(id, new PartitionKey($"StudyRecord-{teacId}"));*/
  554. List<StudyRecord> studyRecords = new();
  555. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StudyRecord>(queryText: $"select value(c) from c where c.id = '{id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StudyRecord-{teacId}") }))
  556. {
  557. studyRecords.Add(item);
  558. }
  559. string op;
  560. if (studyRecords.Count > 0)
  561. {
  562. if (studyRecords[0].tId.Equals(teacId))
  563. {
  564. studyRecords[0].status = type;
  565. studyRecords[0].aTime = now;
  566. }
  567. op = "update";
  568. await client.GetContainer("TEAMModelOS", "Teacher").ReplaceItemAsync(studyRecords[0], studyRecords[0].id, new PartitionKey($"{studyRecords[0].code}"));
  569. }
  570. else
  571. {
  572. StudyRecord setting = new()
  573. {
  574. id = id,
  575. tId = teacId,
  576. code = "StudyRecord-" + teacId,
  577. status = type,
  578. aTime = now
  579. };
  580. op = "insert";
  581. await client.GetContainer("TEAMModelOS", "Teacher").CreateItemAsync(setting, new PartitionKey($"{setting.code}"));
  582. }
  583. //tasks.Add(StatisticsService.SendServiceBus($"{standard}", $"{teacId}", $"{school}", StatisticsService.OfflineRecord, 1, _configuration, _serviceBus));
  584. yield return (op, teacId);
  585. }
  586. await Task.WhenAll(tasks);
  587. }
  588. }
  589. }