LessonRecordController.cs 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.AspNetCore.Authorization;
  5. using Microsoft.AspNetCore.Http;
  6. using Microsoft.AspNetCore.Mvc;
  7. using Microsoft.Extensions.Configuration;
  8. using Microsoft.Extensions.Options;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.Linq;
  12. using System.Net;
  13. using System.Text;
  14. using System.Text.Json;
  15. using System.Text.Json.Nodes;
  16. using System.Threading.Tasks;
  17. using TEAMModelOS.Filter;
  18. using TEAMModelOS.Models;
  19. using TEAMModelOS.SDK;
  20. using TEAMModelOS.SDK.DI;
  21. using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
  22. using TEAMModelOS.SDK.Extension;
  23. using TEAMModelOS.SDK.Models;
  24. using TEAMModelOS.SDK.Models.Service;
  25. namespace TEAMModelOS.Controllers
  26. {
  27. /// <summary>
  28. /// 课堂记录
  29. /// </summary>
  30. [ProducesResponseType(StatusCodes.Status200OK)]
  31. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  32. [Route("common/lesson-record")]
  33. [ApiController]
  34. public class LessonRecordController : ControllerBase
  35. {
  36. private readonly AzureCosmosFactory _azureCosmos;
  37. private readonly SnowflakeId _snowflakeId;
  38. private readonly AzureServiceBusFactory _serviceBus;
  39. private readonly DingDing _dingDing;
  40. private readonly Option _option;
  41. private readonly AzureStorageFactory _azureStorage;
  42. private readonly AzureRedisFactory _azureRedis;
  43. public IConfiguration _configuration { get; set; }
  44. public LessonRecordController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
  45. IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
  46. {
  47. _azureCosmos = azureCosmos;
  48. _serviceBus = serviceBus;
  49. _snowflakeId = snowflakeId;
  50. _dingDing = dingDing;
  51. _option = option?.Value;
  52. _azureStorage = azureStorage;
  53. _azureRedis = azureRedis;
  54. _configuration = configuration;
  55. }
  56. /// <summary>
  57. /// 更新课堂记录
  58. /// </summary>
  59. /// <param name="request"></param>
  60. /// <returns></returns>
  61. [ProducesDefaultResponseType]
  62. [HttpPost("update-lesson-baseinfo")]
  63. [AuthToken(Roles = "admin,teacher")]
  64. public async Task<IActionResult> UpdateLessonBaseInfo(JsonElement request)
  65. {
  66. var client = _azureCosmos.GetCosmosClient();
  67. if (!request.TryGetProperty("lesson_id", out JsonElement _lessonId)) return BadRequest();
  68. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  69. request.TryGetProperty("school", out JsonElement _school);
  70. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  71. if (!request.TryGetProperty("grant_types", out JsonElement _grant_types)) return BadRequest();
  72. string tbname ;
  73. string code ;
  74. if (_scope.GetString().Equals("school") && !string.IsNullOrWhiteSpace(_school.GetString()))
  75. {
  76. code = $"LessonRecord-{_school}";
  77. tbname = "School";
  78. }
  79. else if ($"{_scope}".Equals("private"))
  80. {
  81. code = $"LessonRecord";
  82. tbname = "Teacher";
  83. }
  84. else
  85. {
  86. return BadRequest();
  87. }
  88. try
  89. {
  90. Dictionary<string, object> dict = new Dictionary<string, object>();
  91. if (_grant_types.ValueKind.Equals(JsonValueKind.Array)) {
  92. LessonRecord lessonRecord = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<LessonRecord>($"{_lessonId}", new PartitionKey(code));
  93. List<LessonUpdate> updates = _grant_types.ToObject<List<LessonUpdate>>();
  94. foreach (var item in updates) {
  95. if (item.grant_type.Equals("up-baseinfo")) {
  96. JsonElement element = item.data.ToJsonString().ToObject<JsonElement>();
  97. element.TryGetProperty("name", out JsonElement _name);
  98. element.TryGetProperty("subjectId", out JsonElement _subjectId);
  99. element.TryGetProperty("grade", out JsonElement _grade);
  100. element.TryGetProperty("category", out JsonElement _category);
  101. element.TryGetProperty("show", out JsonElement _show);
  102. if (_name.ValueKind.Equals(JsonValueKind.String) && !string.IsNullOrWhiteSpace($"{_name}"))
  103. {
  104. lessonRecord.name = $"{_name}";
  105. }
  106. if (_subjectId.ValueKind.Equals(JsonValueKind.String) && !string.IsNullOrWhiteSpace($"{_subjectId}"))
  107. {
  108. lessonRecord.subjectId = $"{_subjectId}";
  109. }
  110. if (_grade.ValueKind.Equals(JsonValueKind.Array))
  111. {
  112. lessonRecord.grade = _grade.ToObject<List<string>>();
  113. }
  114. if (_category.ValueKind.Equals(JsonValueKind.Array))
  115. {
  116. lessonRecord.category = _category.ToObject<List<string>>();
  117. }
  118. if (_show.ValueKind.Equals(JsonValueKind.Array))
  119. {
  120. lessonRecord.show = _show.ToObject<List<string>>();
  121. }
  122. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<LessonRecord>(lessonRecord, $"{_lessonId}", new PartitionKey(code));
  123. dict["up-baseinfo"] = new { status= 1 ,code="ok"};
  124. }
  125. if (item.grant_type.Equals("up-expire")) {
  126. if (_scope.Equals("private"))
  127. {
  128. Teacher teacher = await client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).ReadItemAsync<Teacher>($"{_tmdid}", new PartitionKey("Base"));
  129. HashSet<string> ids = new HashSet<string>();
  130. //未定义的 以及过期时间小于等于0 的 课例
  131. string private_count_sql = $"select value(c.id) from c where ( c.expire<=0 or IS_DEFINED(c.expire) = false ) and c.tmdid='{_tmdid}' ";
  132. await foreach (var lid in client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetItemQueryIterator<string>(
  133. queryText: private_count_sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey(code) }))
  134. {
  135. ids.Add(lid);
  136. }
  137. //包含收藏的本人的个人课例
  138. string favorite_count_sql = $"select value(c.id) from c where c.type='LessonRecord' and c.owner='{_tmdid}' and c.scope='private' ";
  139. await foreach (var lid in client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).GetItemQueryIterator<string>(
  140. queryText: favorite_count_sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Favorite-{_tmdid}") }))
  141. {
  142. ids.Add(lid);
  143. }
  144. //教师个人预设的,可以通过设置的方式增加
  145. int limit = teacher.lessonLimit;
  146. if (teacher.lessonLimit == 0)
  147. {
  148. //未设置的的采用系统设置的默认值50
  149. limit = Constant.private_lesson_limit;
  150. }
  151. if (ids.Count < limit)
  152. {
  153. lessonRecord.expire = -1;
  154. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<LessonRecord>(lessonRecord, $"{_lessonId}", new PartitionKey(code));
  155. dict["up-expire"] = new { status = 1, code = "ok" };
  156. }
  157. else
  158. {
  159. dict["up-expire"] = new { status = 0, code = "lessonLimit", data = limit };
  160. }
  161. }
  162. else if (_scope.Equals("school")) {
  163. lessonRecord.expire = -1;
  164. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<LessonRecord>(lessonRecord, $"{_lessonId}", new PartitionKey(code));
  165. dict["up-expire"] = new { status = 1, code = "ok" };
  166. }
  167. }
  168. }
  169. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  170. var messageChange = new ServiceBusMessage(request.ToJsonString());
  171. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  172. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  173. return Ok(new { status = 200,updateInfo= dict });
  174. }
  175. return BadRequest();
  176. }
  177. catch (CosmosException ex) when (ex.Status == 404)
  178. {
  179. return BadRequest("课堂记录不存在");
  180. }
  181. catch (Exception ex)
  182. {
  183. return BadRequest();
  184. }
  185. }
  186. /*
  187. {
  188. "scope":"school/private",
  189. "tmdid":"1595321354",
  190. "delete_id":"asdeeeqq-adfghnlr-pfkcmsss-ssadffgtre",
  191. "opt":"delete",
  192. "school":"hbcn" //如果scope=school 这 school字段必须有值
  193. }
  194. */
  195. /// <summary>
  196. /// 删除课堂记录
  197. /// </summary>
  198. /// <param name="request"></param>
  199. /// <returns></returns>
  200. [ProducesDefaultResponseType]
  201. // [AuthToken(Roles = "teacher,admin")]
  202. [HttpPost("delete-lesson-record")]
  203. [Authorize(Roles = "IES")]
  204. [AuthToken(Roles = "admin,teacher")]
  205. public async Task<IActionResult> DeleteLessonRecord(JsonElement request)
  206. {
  207. string school;
  208. string tbname;
  209. string code;
  210. if (request.TryGetProperty("delete_id", out JsonElement _delete_id) && !string.IsNullOrEmpty($"{_delete_id}")
  211. && request.TryGetProperty("tmdid", out JsonElement _dtmdid) && !string.IsNullOrEmpty($"{_dtmdid}")
  212. && request.TryGetProperty("scope", out JsonElement _dscope) && !string.IsNullOrEmpty($"{_dscope}")
  213. && request.TryGetProperty("opt", out JsonElement _opt) && !string.IsNullOrEmpty($"{_opt}"))
  214. {
  215. request.TryGetProperty("school", out JsonElement _dschool);
  216. school = $"{_dschool}";
  217. var client = _azureCosmos.GetCosmosClient();
  218. if ($"{_opt}".Equals("delete"))
  219. {
  220. if ($"{_dscope}".Equals("school") && !string.IsNullOrEmpty($"{school}"))
  221. {
  222. code = $"LessonRecord-{school}";
  223. tbname = "School";
  224. }
  225. else if ($"{_dscope}".Equals("private"))
  226. {
  227. code = $"LessonRecord";
  228. tbname = "Teacher";
  229. }
  230. else
  231. {
  232. return BadRequest() ;
  233. }
  234. List<string> lessonIds = new List<string>();
  235. //string lessonId = $"{_delete_id}";
  236. if (_delete_id.ValueKind.Equals(JsonValueKind.Array))
  237. {
  238. lessonIds.AddRange(_delete_id.ToObject<List<string>>());
  239. }
  240. else {
  241. lessonIds.Add($"{_delete_id}");
  242. }
  243. if (lessonIds.IsNotEmpty())
  244. {
  245. foreach (var lessonId in lessonIds) {
  246. Azure.Response response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(lessonId, new PartitionKey(code));
  247. if (response.Status == 200)
  248. {
  249. LessonRecord lessonRecord;
  250. var doc = JsonDocument.Parse(response.ContentStream);
  251. lessonRecord = doc.RootElement.ToObject<LessonRecord>();
  252. lessonRecord.status = 404;
  253. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync(lessonRecord, lessonRecord.id, new PartitionKey(lessonRecord.code));
  254. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  255. var messageChange = new ServiceBusMessage(request.ToJsonString());
  256. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  257. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  258. }
  259. }
  260. return Ok(new { status = 200 });
  261. }
  262. else { return BadRequest(); }
  263. }
  264. else { return BadRequest() ; }
  265. }
  266. else
  267. {
  268. return BadRequest();
  269. }
  270. }
  271. /// <summary>
  272. /// 获取开课记录 (c.status<>404 or IS_DEFINED(c.status)=false )
  273. /// </summary>
  274. /// <param name="request"></param>
  275. /// <returns></returns>
  276. [ProducesDefaultResponseType]
  277. //[AuthToken(Roles = "teacher,admin")]
  278. [HttpPost("get-lesson-record-count")]
  279. [Authorize(Roles = "IES")]
  280. [AuthToken(Roles = "admin,teacher,student")]
  281. public async Task<IActionResult> GetLessonRecordCont(JsonElement request)
  282. {
  283. object _roles = null;
  284. HttpContext?.Items.TryGetValue("Roles", out _roles);
  285. List<string> roles = new List<string>();
  286. if (_roles != null)
  287. {
  288. roles = _roles.ToJsonString().ToObject<List<string>>();
  289. }
  290. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  291. request.TryGetProperty("managePage", out JsonElement _managePage);
  292. bool managePage = false;
  293. if (_managePage.ValueKind.Equals(JsonValueKind.True)) {
  294. managePage = true;
  295. }
  296. StringBuilder sql = new StringBuilder();
  297. sql.Append("select c.id,c.groupIds,c.courseId from c ");
  298. Dictionary<string ,object> dict = LessonService.GetLessonCond(request);
  299. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  300. string tbname = "";
  301. string code = "";
  302. string school = null;
  303. List<string> autoTch = new List<string>();
  304. string sqlPrivate = "";
  305. if (_scope.GetString().Equals("school"))
  306. {
  307. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  308. if (!string.IsNullOrEmpty($"{_school}"))
  309. {
  310. school = $"{_school}";
  311. code = $"LessonRecord-{_school}";
  312. tbname = "School";
  313. List<string> ids = new List<string>();
  314. //只查询某个老师的课例
  315. if (request.TryGetProperty("tmdid", out JsonElement tmdid) && !string.IsNullOrWhiteSpace($"{tmdid}"))
  316. {
  317. ids.Add($"{tmdid}");
  318. }
  319. else
  320. {
  321. string sqltch = "select distinct value(c.id) from c ";
  322. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
  323. .GetItemQueryIterator<string>(queryText: sqltch, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Teacher-{_school}") }))
  324. {
  325. ids.Add(item);
  326. }
  327. }
  328. if (ids.Any())
  329. {
  330. string sqlTechbase = $"select distinct value(c.id) from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))}) and (array_contains(c.lessonShow,'student') or array_contains(c.lessonShow,'all')) ";
  331. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  332. .GetItemQueryIterator<string>(queryText: sqlTechbase, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
  333. {
  334. autoTch.Add(item);
  335. }
  336. }
  337. }
  338. else {
  339. return BadRequest();
  340. }
  341. }
  342. else if ($"{_scope}".Equals("private"))
  343. {
  344. code = $"LessonRecord";
  345. tbname = "Teacher";
  346. request.TryGetProperty("tmdid", out JsonElement _tmdid);
  347. if (!string.IsNullOrEmpty($"{_tmdid}"))
  348. {
  349. sqlPrivate = $" and c.tmdid='{_tmdid}'";
  350. List<string> ids = new List<string>();
  351. ids.Add($"{_tmdid}");
  352. if (ids.Any())
  353. {
  354. string sqlTechbase = $"select distinct value(c.id) from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))}) and (array_contains(c.lessonShow,'student') or array_contains(c.lessonShow,'all')) ";
  355. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  356. .GetItemQueryIterator<string>(queryText: sqlTechbase, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
  357. {
  358. autoTch.Add(item);
  359. }
  360. }
  361. }
  362. else
  363. {
  364. //如果不传tmdid, 则必须传递,课程id或者名单列表
  365. // 如果不传递tmdid
  366. if (!request.TryGetProperty("groupIds", out JsonElement groupIds))
  367. {
  368. return BadRequest("个人课例不传醍摩豆id则必传 groupIds");
  369. }
  370. }
  371. }
  372. else
  373. {
  374. return BadRequest();
  375. }
  376. int count=0;
  377. string sqlShow = "";
  378. if (roles.Count == 1 && roles.Contains("student"))
  379. {
  380. string autoSql = "";
  381. if (autoTch.Any())
  382. {
  383. autoSql = $" or c.tmdid in ({string.Join(",", autoTch.Select(x => $"'{x}'"))})";
  384. }
  385. sqlShow = $" and (array_contains(c.show,'student') or array_contains(c.show,'all') {autoSql} ) ";
  386. }
  387. string sql_status_managePage = " (c.status<>404 or IS_DEFINED(c.status) = false ) and ";
  388. if (managePage) {
  389. sql_status_managePage = "";
  390. }
  391. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", $" where {sql_status_managePage} array_length(c.groupIds)>0 {sqlPrivate} {sqlShow} and ");
  392. if (request.TryGetProperty("singleGreen", out JsonElement doubleGreen) && doubleGreen.GetBoolean())
  393. {
  394. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", $" where ((c.tLevel=2 and c.pLevel<2 )or(c.tLevel<2 and c.pLevel=2 )) and ");
  395. }
  396. List<LessonRecord> records = new List<LessonRecord>();
  397. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<LessonRecord>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  398. {
  399. records.Add(item);
  400. }
  401. if (records.Any()) {
  402. var groupIds= records.SelectMany(x => x.groupIds).ToHashSet();
  403. if (groupIds.Any()) {
  404. var groups = await GroupListService.GetGroupListByListids(_azureCosmos.GetCosmosClient(), _dingDing, groupIds.ToList(), school," c.id ");
  405. //获取已经被删除的名单。
  406. var idsExp = groupIds.Except(groups.Select(x => x.id));
  407. if (idsExp.Any()) {
  408. foreach(var item in records)
  409. {
  410. int countRmv = item.groupIds.RemoveAll(x => idsExp.Contains(x));
  411. if (countRmv > 0)
  412. {
  413. try {
  414. LessonRecord record = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<LessonRecord>(item.id, new PartitionKey(code));
  415. record.groupIds = item.groupIds;
  416. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<LessonRecord>(record, item.id, new PartitionKey(code));
  417. } catch (CosmosException ex) when (ex.Status == 404) {
  418. continue;
  419. }
  420. }
  421. }
  422. }
  423. }
  424. }
  425. count = records.Count;
  426. return Ok(new { count=count, records });
  427. }
  428. /// <summary>
  429. /// 获取开课记录
  430. /// </summary>
  431. /// <param name="request"></param>
  432. /// <returns></returns>
  433. [ProducesDefaultResponseType]
  434. // [AuthToken(Roles = "teacher,admin")]
  435. [HttpPost("get-lesson-record-schorpvt")]
  436. [Authorize(Roles = "IES")]
  437. [AuthToken(Roles = "admin,teacher,student")]
  438. public async Task<IActionResult> GetLessonRecordSchOrPvt(JsonElement request) {
  439. object _roles = null;
  440. HttpContext?.Items.TryGetValue("Roles", out _roles);
  441. List<string> roles = new List<string>();
  442. if (_roles != null)
  443. {
  444. roles = _roles.ToJsonString().ToObject<List<string>>();
  445. }
  446. StringBuilder sql = new StringBuilder();
  447. sql.Append("select value(c) from c ");
  448. Dictionary<string, object> dict = LessonService.GetLessonCond(request);
  449. if (request.TryGetProperty("DESC", out JsonElement desc))
  450. {
  451. dict.Add("@DESC", desc.ToString());
  452. }
  453. if (request.TryGetProperty("ASC", out JsonElement asc))
  454. {
  455. dict.Add("@ASC", asc.ToString());
  456. }
  457. string school = null;
  458. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  459. List<LessonRecord> lessonRecords = new List<LessonRecord>();
  460. //获取学校的
  461. {
  462. string tbname = "School";
  463. string code = "";
  464. string sqlPrivate = "";
  465. List<string> autoTch = new List<string>();
  466. request.TryGetProperty("school", out JsonElement _school);
  467. if (!string.IsNullOrEmpty($"{_school}"))
  468. {
  469. code = $"LessonRecord-{_school}";
  470. tbname = "School";
  471. school = $"{_school}";
  472. List<string> ids = new List<string>();
  473. //只查询某个老师的课例
  474. if (request.TryGetProperty("tmdid", out JsonElement tmdid) && !string.IsNullOrWhiteSpace($"{tmdid}"))
  475. {
  476. ids.Add($"{tmdid}");
  477. }
  478. else
  479. {
  480. string sqltch = "select distinct value(c.id) from c ";
  481. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
  482. .GetItemQueryIterator<string>(queryText: sqltch, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Teacher-{_school}") }))
  483. {
  484. ids.Add(item);
  485. }
  486. }
  487. if (ids.Any())
  488. {
  489. string sqlTechbase = $"select distinct value(c.id) from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))}) and (array_contains(c.lessonShow,'student') or array_contains(c.lessonShow,'all')) ";
  490. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  491. .GetItemQueryIterator<string>(queryText: sqlTechbase, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
  492. {
  493. autoTch.Add(item);
  494. }
  495. }
  496. string sqlShow = "";
  497. if (roles.Count == 1 && roles.Contains("student"))
  498. {
  499. string autoSql = "";
  500. if (autoTch.Any())
  501. {
  502. autoSql = $" or c.tmdid in ({string.Join(",", autoTch.Select(x => $"'{x}'"))})";
  503. }
  504. sqlShow = $" and (array_contains(c.show,'student') or array_contains(c.show,'all') {autoSql} ) ";
  505. }
  506. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", $" where (c.status<>404 or IS_DEFINED(c.status) = false ) and array_length(c.groupIds)>0 {sqlPrivate} {sqlShow} and ");
  507. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  508. .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition,
  509. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  510. {
  511. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  512. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  513. {
  514. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  515. {
  516. lessonRecords.Add(obj.ToObject<LessonRecord>());
  517. }
  518. break;
  519. }
  520. }
  521. }
  522. }
  523. //获取个人的
  524. {
  525. string code = $"LessonRecord";
  526. string tbname = "Teacher";
  527. List<string> autoTch = new List<string>();
  528. request.TryGetProperty("tmdid", out JsonElement _tmdid);
  529. if (!string.IsNullOrEmpty($"{_tmdid}"))
  530. {
  531. string sqlPrivate = $" and c.tmdid='{_tmdid}'";
  532. List<string> ids = new List<string>();
  533. ids.Add($"{_tmdid}");
  534. if (ids.Any())
  535. {
  536. string sqlTechbase = $"select distinct value(c.id) from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))}) and (array_contains(c.lessonShow,'student') or array_contains(c.lessonShow,'all')) ";
  537. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  538. .GetItemQueryIterator<string>(queryText: sqlTechbase, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
  539. {
  540. autoTch.Add(item);
  541. }
  542. }
  543. string sqlShow = "";
  544. if (roles.Count == 1 && roles.Contains("student"))
  545. {
  546. string autoSql = "";
  547. if (autoTch.Any())
  548. {
  549. autoSql = $" or c.tmdid in ({string.Join(",", autoTch.Select(x => $"'{x}'"))})";
  550. }
  551. sqlShow = $" and (array_contains(c.show,'student') or array_contains(c.show,'all') {autoSql} ) ";
  552. }
  553. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", $" where (c.status<>404 or IS_DEFINED(c.status) = false ) and array_length(c.groupIds)>0 {sqlPrivate} {sqlShow} and ");
  554. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  555. .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition,
  556. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  557. {
  558. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  559. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  560. {
  561. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  562. {
  563. lessonRecords.Add(obj.ToObject<LessonRecord>());
  564. }
  565. break;
  566. }
  567. }
  568. }
  569. }
  570. var tmdids = lessonRecords.Select(x => x.tmdid).ToHashSet();
  571. if (tmdids != null && tmdids.Count > 0)
  572. {
  573. List<IdNameCode> codes = new List<IdNameCode>();
  574. string sqltmd = $"select c.id,c.name,c.picture from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
  575. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText: sqltmd, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  576. {
  577. codes.Add(item);
  578. }
  579. if (codes.IsNotEmpty())
  580. {
  581. lessonRecords.ForEach(x => {
  582. var tmd = codes.Find(z => z.id.Equals(x.tmdid));
  583. if (tmd != null)
  584. {
  585. x.tmdname = tmd.name;
  586. x.tmdpicture = tmd.picture;
  587. }
  588. });
  589. }
  590. }
  591. var groupIds = lessonRecords.SelectMany(x => x.groupIds);
  592. if (groupIds.Any())
  593. {
  594. List<GroupListDto> groupLists = await GroupListService.GetGroupListByListids(_azureCosmos.GetCosmosClient(), _dingDing, groupIds.ToList(), school);
  595. lessonRecords.ForEach(x => {
  596. List<string> groupNmae = new List<string>();
  597. x.groupIds.ForEach(y => {
  598. var dto = groupLists.Find(z => z.id.Equals(y));
  599. string name = dto != null ? dto.name : "-";
  600. groupNmae.Add(name);
  601. });
  602. x.groupNames = groupNmae;
  603. });
  604. }
  605. return Ok(new { currCount = lessonRecords.Count, lessonRecords });
  606. }
  607. /// <summary>
  608. /// 获取开课记录
  609. /// </summary>
  610. /// <param name="request"></param>
  611. /// <returns></returns>
  612. [ProducesDefaultResponseType]
  613. // [AuthToken(Roles = "teacher,admin")]
  614. [HttpPost("get-lesson-record")]
  615. [Authorize(Roles = "IES")]
  616. [AuthToken(Roles ="admin,teacher,student")]
  617. public async Task<IActionResult> GetLessonRecord(JsonElement request)
  618. {
  619. object _roles = null;
  620. HttpContext?.Items.TryGetValue("Roles", out _roles);
  621. List<string> roles = new List<string>();
  622. if (_roles != null) {
  623. roles = _roles.ToJsonString().ToObject<List<string>>();
  624. }
  625. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  626. request.TryGetProperty("managePage", out JsonElement _managePage);
  627. bool managePage = false;
  628. if (_managePage.ValueKind.Equals(JsonValueKind.True))
  629. {
  630. managePage = true;
  631. }
  632. StringBuilder sql = new StringBuilder();
  633. sql.Append("select value(c) from c ");
  634. int pageCount = 10;
  635. Dictionary<string, object> dict = LessonService.GetLessonCond(request);
  636. if (request.TryGetProperty("pageCount", out JsonElement _pageCount))
  637. {
  638. int.TryParse($"{_pageCount}", out int pcount);
  639. if (pcount > 0)
  640. {
  641. pageCount = pcount;
  642. }
  643. }
  644. if (request.TryGetProperty("DESC", out JsonElement desc))
  645. {
  646. dict.Add("@DESC", desc.ToString());
  647. }
  648. if (request.TryGetProperty("ASC", out JsonElement asc))
  649. {
  650. dict.Add("@ASC", asc.ToString());
  651. }
  652. string continuationToken = null;
  653. if (request.TryGetProperty("continuationToken", out JsonElement _continuationToken))
  654. {
  655. if (!string.IsNullOrEmpty($"{_continuationToken}"))
  656. {
  657. continuationToken = $"{_continuationToken}";
  658. }
  659. }
  660. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  661. string tbname = "";
  662. string code = "";
  663. string school = null;
  664. string sqlPrivate = "";
  665. List<string> autoTch = new List<string>();
  666. if (_scope.GetString().Equals("school"))
  667. {
  668. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  669. if (!string.IsNullOrEmpty($"{_school}"))
  670. {
  671. code = $"LessonRecord-{_school}";
  672. tbname = "School";
  673. school = $"{_school}";
  674. List<string> ids = new List<string>();
  675. //只查询某个老师的课例
  676. if (request.TryGetProperty("tmdid", out JsonElement tmdid) && !string.IsNullOrWhiteSpace($"{tmdid}"))
  677. {
  678. ids.Add($"{tmdid}");
  679. }
  680. else {
  681. string sqltch = "select distinct value(c.id) from c ";
  682. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School)
  683. .GetItemQueryIterator<string>(queryText: sqltch, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Teacher-{_school}") }))
  684. {
  685. ids.Add(item);
  686. }
  687. }
  688. if (ids.Any()) {
  689. string sqlTechbase = $"select distinct value(c.id) from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))}) and (array_contains(c.lessonShow,'student') or array_contains(c.lessonShow,'all')) ";
  690. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  691. .GetItemQueryIterator<string>(queryText: sqlTechbase, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
  692. {
  693. autoTch.Add(item);
  694. }
  695. }
  696. }
  697. else
  698. {
  699. return BadRequest();
  700. }
  701. }
  702. else if ($"{_scope}".Equals("private"))
  703. {
  704. code = $"LessonRecord";
  705. tbname = "Teacher";
  706. request.TryGetProperty("tmdid", out JsonElement _tmdid);
  707. if (!string.IsNullOrEmpty($"{_tmdid}"))
  708. {
  709. sqlPrivate = $" and c.tmdid='{_tmdid}'";
  710. List<string> ids = new List<string>();
  711. ids.Add($"{_tmdid}");
  712. if (ids.Any())
  713. {
  714. string sqlTechbase = $"select distinct value(c.id) from c where c.id in ({string.Join(",", ids.Select(x => $"'{x}'"))}) and (array_contains(c.lessonShow,'student') or array_contains(c.lessonShow,'all')) ";
  715. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  716. .GetItemQueryIterator<string>(queryText: sqlTechbase, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Base") }))
  717. {
  718. autoTch.Add(item);
  719. }
  720. }
  721. }
  722. else
  723. {
  724. //如果不传tmdid, 则必须传递,课程id或者名单列表
  725. // 如果不传递tmdid
  726. if (!request.TryGetProperty("groupIds", out JsonElement groupIds)) {
  727. return BadRequest("个人课例不传醍摩豆id则必传 groupIds");
  728. }
  729. }
  730. }
  731. else
  732. {
  733. return BadRequest();
  734. }
  735. List<LessonRecord> lessonRecords = new List<LessonRecord>();
  736. try
  737. {
  738. string sqlShow = "";
  739. if (roles.Count == 1 && roles.Contains("student"))
  740. {
  741. string autoSql = "";
  742. if (autoTch.Any()) {
  743. autoSql = $" or c.tmdid in ({string.Join(",",autoTch.Select(x=>$"'{x}'"))})";
  744. }
  745. sqlShow = $" and (array_contains(c.show,'student') or array_contains(c.show,'all') {autoSql} ) ";
  746. }
  747. string sql_status_managePage = "(c.status<>404 or IS_DEFINED(c.status) = false ) and ";
  748. if (managePage)
  749. {
  750. sql_status_managePage = "";
  751. }
  752. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", $" where {sql_status_managePage} array_length(c.groupIds)>0 {sqlPrivate} {sqlShow} and ");
  753. if (request.TryGetProperty("singleGreen", out JsonElement doubleGreen) && doubleGreen.GetBoolean())
  754. {
  755. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", $" where ((c.tLevel=2 and c.pLevel<2 )or(c.tLevel<2 and c.pLevel=2 )) and ");
  756. }
  757. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  758. .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, continuationToken: continuationToken,
  759. requestOptions: new QueryRequestOptions() { MaxItemCount = pageCount, PartitionKey = new PartitionKey(code) }))
  760. {
  761. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  762. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  763. {
  764. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  765. {
  766. var rcd = obj.ToObject<LessonRecord>();
  767. if (rcd.hitaClientCmpCount <= 0 && rcd.collateTaskCount > 0) {
  768. rcd.hitaClientCmpCount= rcd.collateTaskCount;
  769. }
  770. lessonRecords.Add(rcd);
  771. }
  772. continuationToken = item.GetContinuationToken();
  773. break;
  774. }
  775. }
  776. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  777. //查询时处理已经过期的课例。防止ServiceBus未触发的。
  778. var expireRecords= lessonRecords.Where(x=> x.expire > 0 && now > x.expire);
  779. try {
  780. foreach (var item in expireRecords)
  781. {
  782. //item.status = 404;
  783. //await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync(item, item.id, new PartitionKey(item.code));
  784. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  785. var messageChange = new ServiceBusMessage(new { delete_id = item.id, tmdid = item.tmdid, scope = item.scope, opt = "delete", school = item.school }.ToJsonString());
  786. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  787. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  788. }
  789. } catch (Exception ex) {
  790. await _dingDing.SendBotMsg($"{_option.Location},ServiceBus ,LessonRecordEvent 发送消息失败,检查是否配置正常。", GroupNames.成都开发測試群組);
  791. }
  792. var tmdids = lessonRecords.Select(x => x.tmdid).ToHashSet();
  793. if (tmdids != null && tmdids.Count > 0)
  794. {
  795. List<IdNameCode> codes = new List<IdNameCode>();
  796. string sqltmd = $"select c.id,c.name,c.picture from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
  797. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText: sqltmd, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  798. {
  799. codes.Add(item);
  800. }
  801. if (codes.IsNotEmpty())
  802. {
  803. lessonRecords.ForEach(x => {
  804. var tmd = codes.Find(z => z.id.Equals(x.tmdid));
  805. if (tmd != null)
  806. {
  807. x.tmdname = tmd.name;
  808. x.tmdpicture = tmd.picture;
  809. }
  810. });
  811. }
  812. }
  813. var groupIds = lessonRecords.SelectMany(x => x.groupIds);
  814. if (groupIds.Any()) {
  815. List<GroupListDto> groupLists= await GroupListService.GetGroupListByListids(_azureCosmos.GetCosmosClient(), _dingDing, groupIds.ToList(), school);
  816. lessonRecords.ForEach(x => {
  817. List<string> groupNmae = new List<string>();
  818. x.groupIds.ForEach(y => {
  819. var dto= groupLists.Find(z => z.id.Equals(y));
  820. string name = dto != null ? dto.name : "-";
  821. groupNmae.Add(name);
  822. });
  823. x.groupNames = groupNmae;
  824. });
  825. }
  826. return Ok(new { currCount = lessonRecords.Count, continuationToken, lessonRecords });
  827. }
  828. catch (Exception )
  829. {
  830. continuationToken = null;
  831. return Ok(new { currCount = 0, continuationToken = continuationToken, lessonRecords });
  832. }
  833. }
  834. /// <summary>
  835. /// 获取开课记录
  836. /// </summary>
  837. /// <param name="request"></param>
  838. /// <returns></returns>
  839. [ProducesDefaultResponseType]
  840. // [AuthToken(Roles = "teacher,admin")]
  841. [HttpPost("get-lesson-record-id")]
  842. [Authorize(Roles = "IES")]
  843. [AuthToken(Roles = "admin,teacher,student")]
  844. public async Task<IActionResult> GetLessonRecordId(JsonElement request)
  845. {
  846. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  847. if (!request.TryGetProperty("id", out JsonElement _id)) return BadRequest();
  848. string tbname = "";
  849. string code = "";
  850. if (_scope.GetString().Equals("school"))
  851. {
  852. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  853. if (!string.IsNullOrEmpty($"{_school}"))
  854. {
  855. code = $"LessonRecord-{_school}";
  856. tbname = "School";
  857. }
  858. else
  859. {
  860. return BadRequest();
  861. }
  862. }
  863. else if ($"{_scope}".Equals("private"))
  864. {
  865. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  866. if (!string.IsNullOrEmpty($"{_tmdid}"))
  867. {
  868. code = $"LessonRecord";
  869. tbname = "Teacher";
  870. }
  871. else
  872. {
  873. return BadRequest();
  874. }
  875. }
  876. else
  877. {
  878. return BadRequest();
  879. }
  880. try
  881. {
  882. LessonRecord lessonRecord = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<LessonRecord>($"{_id}", new PartitionKey(code));
  883. return Ok(new { lessonRecord });
  884. }
  885. catch (CosmosException ex) when (ex.Status == 404)
  886. {
  887. return Ok(new { status = 404 });
  888. }
  889. }
  890. /// <summary>
  891. /// 获取开课记录
  892. /// </summary>
  893. /// <param name="request"></param>
  894. /// <returns></returns>
  895. [ProducesDefaultResponseType]
  896. // [AuthToken(Roles = "teacher,admin")]
  897. [HttpPost("get-other-lesson-record")]
  898. [Authorize(Roles = "IES")]
  899. [AuthToken(Roles = "admin,teacher,student")]
  900. public async Task<IActionResult> GetOtherLessonRecord(JsonElement request)
  901. {
  902. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  903. string tbname = "";
  904. string code = "";
  905. string sqlPrivate = "";
  906. if (_scope.GetString().Equals("school") )
  907. {
  908. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  909. if (!string.IsNullOrEmpty($"{_school}"))
  910. {
  911. code = $"LessonRecord-{_school}";
  912. tbname = "School";
  913. }
  914. else
  915. {
  916. return BadRequest();
  917. }
  918. }
  919. else if ($"{_scope}".Equals("private"))
  920. {
  921. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  922. if (!string.IsNullOrEmpty($"{_tmdid}"))
  923. {
  924. sqlPrivate = $" and c.tmdid='{_tmdid}'";
  925. code = $"LessonRecord";
  926. tbname = "Teacher";
  927. }
  928. else {
  929. return BadRequest();
  930. }
  931. }
  932. else
  933. {
  934. return BadRequest();
  935. }
  936. request.TryGetProperty("periodId", out JsonElement _periodId);
  937. request.TryGetProperty("stime", out JsonElement _stime);
  938. request.TryGetProperty("etime", out JsonElement _etime);
  939. //优质课程
  940. var excellent = new JsonObject
  941. {
  942. ["excellent"] = true,
  943. ["periodId"] = $"{_periodId}",
  944. ["stime"] = $"{_stime}",
  945. ["etime"] = $"{_etime}",
  946. }.ToJsonString().ToObject<JsonElement>();
  947. //专家好客
  948. var doubleGreen = new JsonObject
  949. {
  950. ["doubleGreen"] = true,
  951. ["periodId"] = $"{_periodId}",
  952. ["stime"] = $"{_stime}",
  953. ["etime"] = $"{_etime}",
  954. }.ToJsonString().ToObject<JsonElement>();
  955. //今日课程
  956. var today = new JsonObject
  957. {
  958. ["today"] = true,
  959. ["periodId"] = $"{_periodId}",
  960. }.ToJsonString().ToObject<JsonElement>();
  961. List<LessonRecord> excellentRcd =await GetOtherLessonCond(excellent, code, tbname, sqlPrivate);
  962. List<LessonRecord> doubleGreenRcd = await GetOtherLessonCond(doubleGreen, code, tbname, sqlPrivate);
  963. List<LessonRecord> todayRcd = await GetOtherLessonCond(today, code, tbname, sqlPrivate);
  964. try {
  965. var tmdids_excellent = excellentRcd.Select(x => x.tmdid);
  966. var tmdids_doubleGreen = excellentRcd.Select(x => x.tmdid);
  967. var tmdids_today = excellentRcd.Select(x => x.tmdid);
  968. List<string> tmdids=new List<string>();
  969. tmdids.AddRange(tmdids_excellent);
  970. tmdids.AddRange(tmdids_doubleGreen);
  971. tmdids.AddRange(tmdids_today);
  972. if (tmdids != null && tmdids.Count > 0) {
  973. List< IdNameCode > codes= new List<IdNameCode> ();
  974. string sqltmd = $"select c.id,c.name,c.picture from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
  975. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText:sqltmd,requestOptions:new QueryRequestOptions { PartitionKey=new PartitionKey("Base") })){
  976. codes.Add(item);
  977. }
  978. if (codes.IsNotEmpty()) {
  979. excellentRcd.ForEach(x => {
  980. var tmd= codes.Find(z => z.id.Equals(x.tmdid));
  981. if (tmd != null) {
  982. x.tmdname = tmd.name;
  983. x.tmdpicture= tmd.picture;
  984. }
  985. });
  986. doubleGreenRcd.ForEach(x => {
  987. var tmd = codes.Find(z => z.id.Equals(x.tmdid));
  988. if (tmd != null)
  989. {
  990. x.tmdname = tmd.name;
  991. x.tmdpicture = tmd.picture;
  992. }
  993. });
  994. todayRcd.ForEach(x => {
  995. var tmd = codes.Find(z => z.id.Equals(x.tmdid));
  996. if (tmd != null)
  997. {
  998. x.tmdname = tmd.name;
  999. x.tmdpicture = tmd.picture;
  1000. }
  1001. });
  1002. }
  1003. }
  1004. return Ok(new { excellentRcd , doubleGreenRcd, todayRcd });
  1005. } catch (Exception ex) {
  1006. return Ok(new { excellentRcd, doubleGreenRcd, todayRcd });
  1007. }
  1008. }
  1009. private async Task<List<LessonRecord>> GetOtherLessonCond(JsonElement request,string code ,string tbname,string sqlPrivate) {
  1010. StringBuilder sql = new StringBuilder();
  1011. sql.Append("select value(c) from c ");
  1012. Dictionary<string, object> dict = LessonService.GetLessonCond(request);
  1013. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  1014. List<LessonRecord> lessonRecords = new List<LessonRecord>();
  1015. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", $" where (c.status<>404 or IS_DEFINED(c.status) = false ) and array_length(c.groupIds)>0 {sqlPrivate} and ");
  1016. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  1017. .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition,
  1018. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  1019. {
  1020. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1021. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  1022. {
  1023. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1024. {
  1025. lessonRecords.Add(obj.ToObject<LessonRecord>());
  1026. }
  1027. }
  1028. }
  1029. return lessonRecords;
  1030. }
  1031. }
  1032. }