LessonRecordController.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. using Azure.Cosmos;
  2. using HTEXLib.COMM.Helpers;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using Microsoft.Extensions.Configuration;
  7. using Microsoft.Extensions.Options;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. using System.Net;
  12. using System.Text;
  13. using System.Text.Json;
  14. using System.Threading.Tasks;
  15. using TEAMModelOS.Filter;
  16. using TEAMModelOS.Models;
  17. using TEAMModelOS.SDK;
  18. using TEAMModelOS.SDK.DI;
  19. using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
  20. using TEAMModelOS.SDK.Extension;
  21. using TEAMModelOS.SDK.Models;
  22. namespace TEAMModelOS.Controllers
  23. {
  24. /// <summary>
  25. /// 课堂记录
  26. /// </summary>
  27. [ProducesResponseType(StatusCodes.Status200OK)]
  28. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  29. [Route("common/lesson-record")]
  30. [ApiController]
  31. public class LessonRecordController : ControllerBase
  32. {
  33. private readonly AzureCosmosFactory _azureCosmos;
  34. private readonly SnowflakeId _snowflakeId;
  35. private readonly AzureServiceBusFactory _serviceBus;
  36. private readonly DingDing _dingDing;
  37. private readonly Option _option;
  38. private readonly AzureStorageFactory _azureStorage;
  39. private readonly AzureRedisFactory _azureRedis;
  40. public IConfiguration _configuration { get; set; }
  41. public LessonRecordController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing,
  42. IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, IConfiguration configuration)
  43. {
  44. _azureCosmos = azureCosmos;
  45. _serviceBus = serviceBus;
  46. _snowflakeId = snowflakeId;
  47. _dingDing = dingDing;
  48. _option = option?.Value;
  49. _azureStorage = azureStorage;
  50. _azureRedis = azureRedis;
  51. _configuration = configuration;
  52. }
  53. /// <summary>
  54. /// 获取开课记录
  55. /// </summary>
  56. /// <param name="request"></param>
  57. /// <returns></returns>
  58. [ProducesDefaultResponseType]
  59. //[AuthToken(Roles = "teacher,admin")]
  60. [HttpPost("get-lesson-record-count")]
  61. public async Task<IActionResult> GetLessonRecordCont(JsonElement request)
  62. {
  63. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  64. StringBuilder sql = new StringBuilder();
  65. sql.Append("select value(count(1)) from c ");
  66. Dictionary<string ,object> dict = GetLessonCond(request);
  67. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  68. string tbname = "";
  69. string code = "";
  70. if (_scope.GetString().Equals("school"))
  71. {
  72. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  73. if (!string.IsNullOrEmpty($"{_school}"))
  74. {
  75. code = $"LessonRecord-{_school}";
  76. tbname = "School";
  77. }
  78. else {
  79. return BadRequest();
  80. }
  81. }
  82. else if ($"{_scope}".Equals("private"))
  83. {
  84. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  85. if (!string.IsNullOrEmpty($"{_tmdid}"))
  86. {
  87. code = $"LessonRecord-{_tmdid}";
  88. tbname = "Teacher";
  89. }
  90. else
  91. {
  92. return BadRequest();
  93. }
  94. }
  95. else
  96. {
  97. return BadRequest();
  98. }
  99. int count=0;
  100. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname).GetItemQueryIterator<int>(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(code) }))
  101. {
  102. count = item;
  103. }
  104. return Ok(new { count=count });
  105. }
  106. /// <summary>
  107. /// 获取开课记录
  108. /// </summary>
  109. /// <param name="request"></param>
  110. /// <returns></returns>
  111. [ProducesDefaultResponseType]
  112. // [AuthToken(Roles = "teacher,admin")]
  113. [HttpPost("get-lesson-record")]
  114. public async Task<IActionResult> GetLessonRecord(JsonElement request)
  115. {
  116. if (!request.TryGetProperty("scope", out JsonElement _scope)) return BadRequest();
  117. StringBuilder sql = new StringBuilder();
  118. sql.Append("select value(c) from c ");
  119. int pageCount = 10;
  120. Dictionary<string, object> dict = GetLessonCond(request);
  121. if (request.TryGetProperty("pageCount", out JsonElement _pageCount))
  122. {
  123. int.TryParse($"{_pageCount}", out int pcount);
  124. if (pcount > 0)
  125. {
  126. pageCount = pcount;
  127. }
  128. }
  129. if (request.TryGetProperty("DESC", out JsonElement desc))
  130. {
  131. dict.Add("@DESC", desc.ToString());
  132. }
  133. if (request.TryGetProperty("ASC", out JsonElement asc))
  134. {
  135. dict.Add("@ASC", asc.ToString());
  136. }
  137. string continuationToken = null;
  138. if (request.TryGetProperty("continuationToken", out JsonElement _continuationToken))
  139. {
  140. if (!string.IsNullOrEmpty($"{_continuationToken}"))
  141. {
  142. continuationToken = $"{_continuationToken}";
  143. }
  144. }
  145. AzureCosmosQuery cosmosDbQuery = SQLHelper.GetSQL(dict, sql);
  146. string tbname = "";
  147. string code = "";
  148. if (_scope.GetString().Equals("school") )
  149. {
  150. if (!request.TryGetProperty("school", out JsonElement _school)) return BadRequest();
  151. if (!string.IsNullOrEmpty($"{_school}"))
  152. {
  153. code = $"LessonRecord-{_school}";
  154. tbname = "School";
  155. }
  156. else
  157. {
  158. return BadRequest();
  159. }
  160. }
  161. else if ($"{_scope}".Equals("private"))
  162. {
  163. if (!request.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  164. if (!string.IsNullOrEmpty($"{_tmdid}"))
  165. {
  166. code = $"LessonRecord-{_tmdid}";
  167. tbname = "Teacher";
  168. }
  169. else {
  170. return BadRequest();
  171. }
  172. }
  173. else
  174. {
  175. return BadRequest();
  176. }
  177. List<LessonRecord> lessonRecords = new List<LessonRecord>();
  178. try {
  179. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, tbname)
  180. .GetItemQueryStreamIterator(queryDefinition: cosmosDbQuery.CosmosQueryDefinition, continuationToken: continuationToken,
  181. requestOptions: new QueryRequestOptions() { MaxItemCount = pageCount, PartitionKey = new PartitionKey(code) }))
  182. {
  183. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  184. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  185. {
  186. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  187. {
  188. lessonRecords.Add(obj.ToObject<LessonRecord>());
  189. }
  190. continuationToken = item.GetContinuationToken();
  191. break;
  192. }
  193. }
  194. var tmdids= lessonRecords.Select(x => x.tmdid).ToHashSet();
  195. if (tmdids != null && tmdids.Count > 0) {
  196. List< IdNameCode > codes= new List<IdNameCode> ();
  197. string sqltmd = $"select c.id,c.name,c.picture from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
  198. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<IdNameCode>(queryText:sqltmd,requestOptions:new QueryRequestOptions { PartitionKey=new PartitionKey("Base") })){
  199. codes.Add(item);
  200. }
  201. if (codes.IsNotEmpty()) {
  202. lessonRecords.ForEach(x => {
  203. var tmd= codes.Find(z => z.id.Equals(x.tmdid));
  204. if (tmd != null) {
  205. x.tmdname = tmd.name;
  206. x.tmdpicture= tmd.picture;
  207. }
  208. });
  209. }
  210. }
  211. return Ok(new { currCount=lessonRecords.Count, continuationToken, lessonRecords });
  212. } catch (Exception ex) {
  213. continuationToken = null;
  214. return Ok(new { currCount=0, continuationToken = continuationToken, lessonRecords });
  215. }
  216. }
  217. private Dictionary<string, object> GetLessonCond(JsonElement request)
  218. {
  219. Dictionary<string, object> dict = new Dictionary<string, object>();
  220. if (request.TryGetProperty("tmdid", out JsonElement tmdid) && !string.IsNullOrWhiteSpace($"{tmdid}"))
  221. {
  222. dict.Add("tmdid", tmdid);
  223. }
  224. if (request.TryGetProperty("courseId", out JsonElement courseId) && !string.IsNullOrWhiteSpace($"{courseId}"))
  225. {
  226. dict.Add("courseId", courseId);
  227. }
  228. if (request.TryGetProperty("periodId", out JsonElement periodId) && !string.IsNullOrWhiteSpace($"{periodId}"))
  229. {
  230. dict.Add("periodId", periodId);
  231. }
  232. if (request.TryGetProperty("subjectId", out JsonElement subjectId))
  233. {
  234. dict.Add("subjectId[*]", subjectId);
  235. }
  236. if (request.TryGetProperty("groupIds", out JsonElement groupIds))
  237. {
  238. dict.Add("groupIds[*]", groupIds);
  239. }
  240. if (request.TryGetProperty("grade", out JsonElement grade))
  241. {
  242. dict.Add("grade[*]", grade);
  243. }
  244. if (request.TryGetProperty("category", out JsonElement category))
  245. {
  246. dict.Add("category[*]", category);
  247. }
  248. if (request.TryGetProperty("doubleGreen", out JsonElement doubleGreen) && doubleGreen.GetBoolean())
  249. {
  250. dict.Add(">=.tScore", 70);
  251. dict.Add(">=.pScore", 70);
  252. }
  253. if (request.TryGetProperty("quality", out JsonElement quality) && quality.GetBoolean())
  254. {
  255. dict.Add(">=.discuss", 1);
  256. }
  257. if (request.TryGetProperty("name", out JsonElement name) && !string.IsNullOrWhiteSpace($"{name}"))
  258. {
  259. dict.Add("$.name", name);
  260. }
  261. return dict;
  262. }
  263. }
  264. }