LessonRecordController.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. namespace TEAMModelOS.Controllers
  25. {
  26. /// <summary>
  27. /// 课堂记录
  28. /// </summary>
  29. [ProducesResponseType(StatusCodes.Status200OK)]
  30. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  31. [Route("common/lesson-record")]
  32. [ApiController]
  33. public class LessonRecordController : ControllerBase
  34. {
  35. public static readonly DateTime dateTime1970= new DateTime(1970,1,1).ToLocalTime();
  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. public async Task<IActionResult> UpdateLessonBaseInfo(JsonElement request)
  64. {
  65. var client = _azureCosmos.GetCosmosClient();
  66. if (!request.TryGetProperty("lesson_id", out JsonElement _lessonId)) return BadRequest();
  67. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  68. request.TryGetProperty("school", out JsonElement _school);
  69. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  70. if (!request.TryGetProperty("grant_types", out JsonElement _grant_types)) return BadRequest();
  71. string tbname ;
  72. string code ;
  73. if (_scope.GetString().Equals("school") && !string.IsNullOrWhiteSpace(_school.GetString()))
  74. {
  75. code = $"LessonRecord-{_school}";
  76. tbname = "School";
  77. }
  78. else if ($"{_scope}".Equals("private"))
  79. {
  80. code = $"LessonRecord-{_tmdid}";
  81. tbname = "Teacher";
  82. }
  83. else
  84. {
  85. return BadRequest();
  86. }
  87. try
  88. {
  89. LessonRecord lessonRecord = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<LessonRecord>($"{_lessonId}", new PartitionKey(code));
  90. if (_grant_types.ValueKind.Equals(JsonValueKind.Array)) {
  91. List<LessonUpdate> updates = _grant_types.ToObject<List<LessonUpdate>>();
  92. if (updates.IsNotEmpty() && updates[0].grant_type.Equals("up-baseinfo"))
  93. {
  94. JsonElement element= updates[0].data.ToJsonString().ToObject<JsonElement>();
  95. element.TryGetProperty("name", out JsonElement _name);
  96. element.TryGetProperty("subjectId", out JsonElement _subjectId);
  97. element.TryGetProperty("grade", out JsonElement _grade);
  98. element.TryGetProperty("category", out JsonElement _category);
  99. if (_name.ValueKind.Equals(JsonValueKind.String) && !string.IsNullOrWhiteSpace($"{_name}"))
  100. {
  101. lessonRecord.name = $"{_name}";
  102. }
  103. if (_subjectId.ValueKind.Equals(JsonValueKind.String) && !string.IsNullOrWhiteSpace($"{_subjectId}"))
  104. {
  105. lessonRecord.subjectId = $"{_subjectId}";
  106. }
  107. if (_grade.ValueKind.Equals(JsonValueKind.Array))
  108. {
  109. lessonRecord.grade = _grade.ToObject<List<string>>();
  110. }
  111. if (_category.ValueKind.Equals(JsonValueKind.Array))
  112. {
  113. lessonRecord.category = _category.ToObject<List<string>>();
  114. }
  115. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync<LessonRecord>(lessonRecord, $"{_lessonId}", new PartitionKey(code));
  116. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  117. var messageChange = new ServiceBusMessage(request.ToJsonString());
  118. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  119. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  120. return Ok(new { status = 200 });
  121. }
  122. else
  123. {
  124. return BadRequest();
  125. }
  126. }
  127. return Ok(new { lessonRecord });
  128. }
  129. catch (CosmosException ex) when (ex.Status == 404)
  130. {
  131. return BadRequest("课堂记录不存在");
  132. }
  133. catch (Exception ex)
  134. {
  135. return BadRequest();
  136. }
  137. }
  138. /*
  139. {
  140. "scope":"school/private",
  141. "tmdid":"1595321354",
  142. "delete_id":"asdeeeqq-adfghnlr-pfkcmsss-ssadffgtre",
  143. "opt":"delete",
  144. "school":"hbcn" //如果scope=school 这 school字段必须有值
  145. }
  146. */
  147. /// <summary>
  148. /// 获取开课记录
  149. /// </summary>
  150. /// <param name="request"></param>
  151. /// <returns></returns>
  152. [ProducesDefaultResponseType]
  153. // [AuthToken(Roles = "teacher,admin")]
  154. [HttpPost("delete-lesson-record")]
  155. [Authorize(Roles = "IES")]
  156. public async Task<IActionResult> DeleteLessonRecord(JsonElement request)
  157. {
  158. string lessonId;
  159. string school;
  160. string tbname;
  161. string code;
  162. if (request.TryGetProperty("delete_id", out JsonElement _delete_id) && !string.IsNullOrEmpty($"{_delete_id}")
  163. && request.TryGetProperty("tmdid", out JsonElement _dtmdid) && !string.IsNullOrEmpty($"{_dtmdid}")
  164. && request.TryGetProperty("scope", out JsonElement _dscope) && !string.IsNullOrEmpty($"{_dscope}")
  165. && request.TryGetProperty("opt", out JsonElement _opt) && !string.IsNullOrEmpty($"{_opt}"))
  166. {
  167. request.TryGetProperty("school", out JsonElement _dschool);
  168. school = $"{_dschool}";
  169. var client = _azureCosmos.GetCosmosClient();
  170. if ($"{_opt}".Equals("delete"))
  171. {
  172. if ($"{_dscope}".Equals("school") && !string.IsNullOrEmpty($"{school}"))
  173. {
  174. code = $"LessonRecord-{school}";
  175. tbname = "School";
  176. }
  177. else if ($"{_dscope}".Equals("private"))
  178. {
  179. code = $"LessonRecord-{_dtmdid}";
  180. tbname = "Teacher";
  181. }
  182. else
  183. {
  184. return BadRequest() ;
  185. }
  186. lessonId = $"{_delete_id}";
  187. Azure.Response response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(lessonId, new PartitionKey(code));
  188. if (response.Status == 200)
  189. {
  190. LessonRecord lessonRecord ;
  191. var doc = JsonDocument.Parse(response.ContentStream);
  192. lessonRecord = doc.RootElement.ToObject<LessonRecord>();
  193. lessonRecord.status =404;
  194. await client.GetContainer(Constant.TEAMModelOS, tbname).ReplaceItemAsync(lessonRecord, lessonRecord.id, new PartitionKey(lessonRecord.code));
  195. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  196. var messageChange = new ServiceBusMessage(request.ToJsonString());
  197. messageChange.ApplicationProperties.Add("name", "LessonRecordEvent");
  198. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  199. return Ok(new { status = 200 });
  200. }
  201. else { return BadRequest(); }
  202. }
  203. else { return BadRequest() ; }
  204. }
  205. else
  206. {
  207. return BadRequest();
  208. }
  209. }
  210. /// <summary>
  211. /// 获取开课记录 (c.status<>404 or IS_DEFINED(c.status)=false )
  212. /// </summary>
  213. /// <param name="request"></param>
  214. /// <returns></returns>
  215. [ProducesDefaultResponseType]
  216. //[AuthToken(Roles = "teacher,admin")]
  217. [HttpPost("get-lesson-record-count")]
  218. [Authorize(Roles = "IES")]
  219. public async Task<IActionResult> GetLessonRecordCont(JsonElement request)
  220. {
  221. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  222. StringBuilder sql = new StringBuilder();
  223. sql.Append("select value(count(1)) from c ");
  224. Dictionary<string ,object> dict = GetLessonCond(request);
  225. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  226. string tbname = "";
  227. string code = "";
  228. if (_scope.GetString().Equals("school"))
  229. {
  230. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  231. if (!string.IsNullOrEmpty($"{_school}"))
  232. {
  233. code = $"LessonRecord-{_school}";
  234. tbname = "School";
  235. }
  236. else {
  237. return BadRequest();
  238. }
  239. }
  240. else if ($"{_scope}".Equals("private"))
  241. {
  242. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  243. if (!string.IsNullOrEmpty($"{_tmdid}"))
  244. {
  245. code = $"LessonRecord-{_tmdid}";
  246. tbname = "Teacher";
  247. }
  248. else
  249. {
  250. return BadRequest();
  251. }
  252. }
  253. else
  254. {
  255. return BadRequest();
  256. }
  257. int count=0;
  258. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", " where (c.status<>404 or IS_DEFINED(c.status) = false ) and ");
  259. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<int>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  260. {
  261. count = item;
  262. }
  263. return Ok(new { count=count });
  264. }
  265. /// <summary>
  266. /// 获取开课记录
  267. /// </summary>
  268. /// <param name="request"></param>
  269. /// <returns></returns>
  270. [ProducesDefaultResponseType]
  271. // [AuthToken(Roles = "teacher,admin")]
  272. [HttpPost("get-lesson-record")]
  273. [Authorize(Roles = "IES")]
  274. public async Task<IActionResult> GetLessonRecord(JsonElement request)
  275. {
  276. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  277. StringBuilder sql = new StringBuilder();
  278. sql.Append("select value(c) from c ");
  279. int pageCount = 10;
  280. Dictionary<string, object> dict = GetLessonCond(request);
  281. if (request.TryGetProperty("pageCount", out JsonElement _pageCount))
  282. {
  283. int.TryParse($"{_pageCount}", out int pcount);
  284. if (pcount > 0)
  285. {
  286. pageCount = pcount;
  287. }
  288. }
  289. if (request.TryGetProperty("DESC", out JsonElement desc))
  290. {
  291. dict.Add("@DESC", desc.ToString());
  292. }
  293. if (request.TryGetProperty("ASC", out JsonElement asc))
  294. {
  295. dict.Add("@ASC", asc.ToString());
  296. }
  297. string continuationToken = null;
  298. if (request.TryGetProperty("continuationToken", out JsonElement _continuationToken))
  299. {
  300. if (!string.IsNullOrEmpty($"{_continuationToken}"))
  301. {
  302. continuationToken = $"{_continuationToken}";
  303. }
  304. }
  305. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  306. string tbname = "";
  307. string code = "";
  308. string school = null;
  309. if (_scope.GetString().Equals("school"))
  310. {
  311. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  312. if (!string.IsNullOrEmpty($"{_school}"))
  313. {
  314. code = $"LessonRecord-{_school}";
  315. tbname = "School";
  316. school = $"{_school}";
  317. }
  318. else
  319. {
  320. return BadRequest();
  321. }
  322. }
  323. else if ($"{_scope}".Equals("private"))
  324. {
  325. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  326. if (!string.IsNullOrEmpty($"{_tmdid}"))
  327. {
  328. code = $"LessonRecord-{_tmdid}";
  329. tbname = "Teacher";
  330. }
  331. else
  332. {
  333. return BadRequest();
  334. }
  335. }
  336. else
  337. {
  338. return BadRequest();
  339. }
  340. List<LessonRecord> lessonRecords = new List<LessonRecord>();
  341. try
  342. {
  343. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", " where (c.status<>404 or IS_DEFINED(c.status) = false ) and ");
  344. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  345. .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, continuationToken: continuationToken,
  346. requestOptions: new QueryRequestOptions() { MaxItemCount = pageCount, PartitionKey = new PartitionKey(code) }))
  347. {
  348. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  349. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  350. {
  351. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  352. {
  353. lessonRecords.Add(obj.ToObject<LessonRecord>());
  354. }
  355. continuationToken = item.GetContinuationToken();
  356. break;
  357. }
  358. }
  359. var tmdids = lessonRecords.Select(x => x.tmdid).ToHashSet();
  360. if (tmdids != null && tmdids.Count > 0)
  361. {
  362. List<IdNameCode> codes = new List<IdNameCode>();
  363. string sqltmd = $"select c.id,c.name,c.picture from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
  364. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText: sqltmd, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  365. {
  366. codes.Add(item);
  367. }
  368. if (codes.IsNotEmpty())
  369. {
  370. lessonRecords.ForEach(x => {
  371. var tmd = codes.Find(z => z.id.Equals(x.tmdid));
  372. if (tmd != null)
  373. {
  374. x.tmdname = tmd.name;
  375. x.tmdpicture = tmd.picture;
  376. }
  377. });
  378. }
  379. }
  380. var groupIds = lessonRecords.SelectMany(x => x.groupIds);
  381. if (groupIds.Any()) {
  382. List<GroupListDto> groupLists= await GroupListService.GetGroupListListids(_azureCosmos.GetCosmosClient(), _dingDing, groupIds.ToList(), school);
  383. lessonRecords.ForEach(x => {
  384. List<string> groupNmae = new List<string>();
  385. x.groupIds.ForEach(y => {
  386. var dto= groupLists.Find(z => z.id.Equals(y));
  387. string name = dto != null ? dto.name : "-";
  388. groupNmae.Add(name);
  389. });
  390. x.groupIds = groupNmae;
  391. });
  392. }
  393. return Ok(new { currCount = lessonRecords.Count, continuationToken, lessonRecords });
  394. }
  395. catch (Exception ex)
  396. {
  397. continuationToken = null;
  398. return Ok(new { currCount = 0, continuationToken = continuationToken, lessonRecords });
  399. }
  400. }
  401. /// <summary>
  402. /// 获取开课记录
  403. /// </summary>
  404. /// <param name="request"></param>
  405. /// <returns></returns>
  406. [ProducesDefaultResponseType]
  407. // [AuthToken(Roles = "teacher,admin")]
  408. [HttpPost("get-other-lesson-record")]
  409. [Authorize(Roles = "IES")]
  410. public async Task<IActionResult> GetOtherLessonRecord(JsonElement request)
  411. {
  412. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  413. string tbname = "";
  414. string code = "";
  415. if (_scope.GetString().Equals("school") )
  416. {
  417. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  418. if (!string.IsNullOrEmpty($"{_school}"))
  419. {
  420. code = $"LessonRecord-{_school}";
  421. tbname = "School";
  422. }
  423. else
  424. {
  425. return BadRequest();
  426. }
  427. }
  428. else if ($"{_scope}".Equals("private"))
  429. {
  430. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  431. if (!string.IsNullOrEmpty($"{_tmdid}"))
  432. {
  433. code = $"LessonRecord-{_tmdid}";
  434. tbname = "Teacher";
  435. }
  436. else {
  437. return BadRequest();
  438. }
  439. }
  440. else
  441. {
  442. return BadRequest();
  443. }
  444. request.TryGetProperty("periodId", out JsonElement _periodId);
  445. request.TryGetProperty("stime", out JsonElement _stime);
  446. request.TryGetProperty("etime", out JsonElement _etime);
  447. //优质课程
  448. var excellent = new JsonObject
  449. {
  450. ["excellent"] = true,
  451. ["periodId"] = $"{_periodId}",
  452. ["stime"] = $"{_stime}",
  453. ["etime"] = $"{_etime}",
  454. }.ToJsonString().ToObject<JsonElement>();
  455. //专家好客
  456. var doubleGreen = new JsonObject
  457. {
  458. ["doubleGreen"] = true,
  459. ["periodId"] = $"{_periodId}",
  460. ["stime"] = $"{_stime}",
  461. ["etime"] = $"{_etime}",
  462. }.ToJsonString().ToObject<JsonElement>();
  463. //今日课程
  464. var today = new JsonObject
  465. {
  466. ["today"] = true,
  467. ["periodId"] = $"{_periodId}",
  468. }.ToJsonString().ToObject<JsonElement>();
  469. List<LessonRecord> excellentRcd =await GetOtherLessonCond(excellent, code, tbname);
  470. List<LessonRecord> doubleGreenRcd = await GetOtherLessonCond(doubleGreen, code, tbname);
  471. List<LessonRecord> todayRcd = await GetOtherLessonCond(today, code, tbname);
  472. try {
  473. var tmdids_excellent = excellentRcd.Select(x => x.tmdid);
  474. var tmdids_doubleGreen = excellentRcd.Select(x => x.tmdid);
  475. var tmdids_today = excellentRcd.Select(x => x.tmdid);
  476. List<string> tmdids=new List<string>();
  477. tmdids.AddRange(tmdids_excellent);
  478. tmdids.AddRange(tmdids_doubleGreen);
  479. tmdids.AddRange(tmdids_today);
  480. if (tmdids != null && tmdids.Count > 0) {
  481. List< IdNameCode > codes= new List<IdNameCode> ();
  482. string sqltmd = $"select c.id,c.name,c.picture from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
  483. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText:sqltmd,requestOptions:new QueryRequestOptions { PartitionKey=new PartitionKey("Base") })){
  484. codes.Add(item);
  485. }
  486. if (codes.IsNotEmpty()) {
  487. excellentRcd.ForEach(x => {
  488. var tmd= codes.Find(z => z.id.Equals(x.tmdid));
  489. if (tmd != null) {
  490. x.tmdname = tmd.name;
  491. x.tmdpicture= tmd.picture;
  492. }
  493. });
  494. doubleGreenRcd.ForEach(x => {
  495. var tmd = codes.Find(z => z.id.Equals(x.tmdid));
  496. if (tmd != null)
  497. {
  498. x.tmdname = tmd.name;
  499. x.tmdpicture = tmd.picture;
  500. }
  501. });
  502. todayRcd.ForEach(x => {
  503. var tmd = codes.Find(z => z.id.Equals(x.tmdid));
  504. if (tmd != null)
  505. {
  506. x.tmdname = tmd.name;
  507. x.tmdpicture = tmd.picture;
  508. }
  509. });
  510. }
  511. }
  512. return Ok(new { excellentRcd , doubleGreenRcd, todayRcd });
  513. } catch (Exception ex) {
  514. return Ok(new { excellentRcd, doubleGreenRcd, todayRcd });
  515. }
  516. }
  517. private async Task<List<LessonRecord>> GetOtherLessonCond(JsonElement request,string code ,string tbname ) {
  518. StringBuilder sql = new StringBuilder();
  519. sql.Append("select value(c) from c ");
  520. Dictionary<string, object> dict = GetLessonCond(request);
  521. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  522. List<LessonRecord> lessonRecords = new List<LessonRecord>();
  523. cosmosDbQuery.QueryText = cosmosDbQuery.QueryText.Replace("where", " where (c.status<>404 or IS_DEFINED(c.status) = false ) and ");
  524. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  525. .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition,
  526. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  527. {
  528. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  529. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  530. {
  531. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  532. {
  533. lessonRecords.Add(obj.ToObject<LessonRecord>());
  534. }
  535. }
  536. }
  537. return lessonRecords;
  538. }
  539. private Dictionary<string, object> GetLessonCond(JsonElement request)
  540. {
  541. Dictionary<string, object> dict = new Dictionary<string, object>();
  542. if (request.TryGetProperty("tmdid", out JsonElement tmdid) && !string.IsNullOrWhiteSpace($"{tmdid}"))
  543. {
  544. dict.Add("tmdid", tmdid);
  545. }
  546. if (request.TryGetProperty("courseId", out JsonElement courseId) && !string.IsNullOrWhiteSpace($"{courseId}"))
  547. {
  548. dict.Add("courseId", courseId);
  549. }
  550. if (request.TryGetProperty("periodId", out JsonElement periodId) && !string.IsNullOrWhiteSpace($"{periodId}"))
  551. {
  552. dict.Add("periodId", periodId);
  553. }
  554. if (request.TryGetProperty("subjectId", out JsonElement subjectId))
  555. {
  556. dict.Add("subjectId[*]", subjectId);
  557. }
  558. if (request.TryGetProperty("groupIds", out JsonElement groupIds))
  559. {
  560. dict.Add("groupIds[*]", groupIds);
  561. }
  562. if (request.TryGetProperty("grade", out JsonElement grade))
  563. {
  564. dict.Add("grade[*]", grade);
  565. }
  566. if (request.TryGetProperty("category", out JsonElement category))
  567. {
  568. dict.Add("category[*]", category);
  569. }
  570. if (request.TryGetProperty("doubleGreen", out JsonElement doubleGreen) && doubleGreen.GetBoolean())
  571. {
  572. dict.Add(">=.tScore", 70);
  573. dict.Add(">=.pScore", 70);
  574. }
  575. if (request.TryGetProperty("quality", out JsonElement quality) && quality.GetBoolean())
  576. {
  577. dict.Add(">=.discuss", 1);
  578. }
  579. if (request.TryGetProperty("excellent", out JsonElement excellent) && excellent.GetBoolean())
  580. {
  581. dict.Add(">=.excellent", 1);
  582. }
  583. if (request.TryGetProperty("name", out JsonElement name) && !string.IsNullOrWhiteSpace($"{name}"))
  584. {
  585. dict.Add("$.name", name);
  586. }
  587. if (request.TryGetProperty("today", out JsonElement today) && today.GetBoolean())
  588. {
  589. DateTime dateTimeA= Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D"));
  590. DateTime dateTimeB = Convert.ToDateTime(DateTimeOffset.UtcNow.ToString("D")).AddDays(1);
  591. double dayOf00_00_00= (dateTimeA - dateTime1970).TotalMilliseconds;
  592. double day1Of00_00_00 = (dateTimeB - dateTime1970).TotalMilliseconds;
  593. dict.Add(">=.startTime", dayOf00_00_00);
  594. dict.Add("<.startTime", day1Of00_00_00);
  595. }
  596. if (request.TryGetProperty("stime", out JsonElement stime) && !string.IsNullOrWhiteSpace($"{stime}"))
  597. {
  598. dict.Add(">=.startTim", stime);
  599. }
  600. if (request.TryGetProperty("etime", out JsonElement etime) && !string.IsNullOrWhiteSpace($"{etime}"))
  601. {
  602. dict.Add("<=.startTim", etime);
  603. }
  604. return dict;
  605. }
  606. }
  607. }