TeacherCommonController.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.Filter;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK.Extension;
  14. using TEAMModelOS.SDK.Models;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. namespace TEAMModelOS.Controllers
  17. {
  18. [ProducesResponseType(StatusCodes.Status200OK)]
  19. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  20. //
  21. [Route("teacher")]
  22. [ApiController]
  23. public class TeacherCommonController:ControllerBase
  24. {
  25. private readonly AzureCosmosFactory _azureCosmos;
  26. private readonly AzureRedisFactory _azureRedis;
  27. public TeacherCommonController(AzureCosmosFactory azureCosmos, AzureRedisFactory azureRedis)
  28. {
  29. _azureCosmos = azureCosmos;
  30. _azureRedis = azureRedis;
  31. }
  32. [ProducesDefaultResponseType]
  33. [HttpPost("tch-activity-count")]
  34. [AuthToken(Roles = "admin,teacher")]
  35. [Authorize(Roles = "IES")]
  36. public async Task<IActionResult> TchActivityCount(JsonElement requert)
  37. {
  38. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  39. var client = _azureCosmos.GetCosmosClient();
  40. HashSet<string> classes = new HashSet<string>();
  41. HashSet<string> stulist = new HashSet<string>();
  42. //获取学校的名单:
  43. List<Schedule> schedules = new List<Schedule>();
  44. string scheduleSql = $"SELECT c.schedule FROM c join scdl in c.schedule where c.pk='Course' and scdl.teacherId='{id}'";
  45. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<Course>(scheduleSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
  46. {
  47. schedules.AddRange(item.schedule);
  48. }
  49. schedules.ForEach(x => {
  50. if (!string.IsNullOrEmpty(x.classId))
  51. {
  52. classes.Add(x.classId);
  53. }
  54. if (!string.IsNullOrEmpty(x.stulist))
  55. {
  56. stulist.Add(x.stulist);
  57. }
  58. });
  59. List<string> types = new List<string> { "Exam", "Survey", "Vote","Homework" };
  60. List<MQActivity> datas = new List<MQActivity>();
  61. List<KeyValuePair<string, int>> count = new List<KeyValuePair<string, int>>();
  62. foreach (var type in types)
  63. {
  64. string pksql = $" c.pk='{type}' ";
  65. string joinSqlClasses = "";
  66. string andSqlClasses = "";
  67. if (classes.ToList().IsNotEmpty())
  68. {
  69. joinSqlClasses = " join A1 in c.classes ";
  70. List<string> sqlList = new List<string>();
  71. classes.ToList().ForEach(x => { sqlList.Add($" '{x}' "); });
  72. string sql = string.Join(" , ", sqlList);
  73. andSqlClasses = $" A1 in ({sql}) ";
  74. }
  75. string joinSqlStulist = "";
  76. string andSqlStulist = "";
  77. if (stulist.ToList().IsNotEmpty())
  78. {
  79. joinSqlStulist = " join A1 in c.stuLists ";
  80. List<string> sqlList = new List<string>();
  81. stulist.ToList().ForEach(x => { sqlList.Add($" '{x}' "); });
  82. string sql = string.Join(" , ", sqlList);
  83. andSqlStulist = $" A1 in ({sql}) ";
  84. }
  85. string classesSql = "";
  86. if (!string.IsNullOrWhiteSpace(joinSqlClasses))
  87. {
  88. classesSql = $" and {andSqlClasses } ";
  89. }
  90. int acount = 0;
  91. if (!string.IsNullOrWhiteSpace(school) && classes.ToList().IsNotEmpty())
  92. {
  93. string querySchool = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c {joinSqlClasses} where {pksql} {classesSql} and (c.status<>404 or IS_DEFINED(c.status) = false ) ";
  94. //查询数据归属学校的
  95. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<MQActivity>(querySchool, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{school}") }))
  96. {
  97. if (item.progress.Equals("going", StringComparison.OrdinalIgnoreCase))
  98. {
  99. datas.Add(item);
  100. }
  101. acount += 1;
  102. }
  103. }
  104. string stuListsSql = "";
  105. if (!string.IsNullOrWhiteSpace(joinSqlStulist))
  106. {
  107. stuListsSql = $" and {andSqlStulist } ";
  108. }
  109. if (!string.IsNullOrWhiteSpace(school) && stulist.ToList().IsNotEmpty())
  110. {
  111. string querySchool = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c {joinSqlStulist} where {pksql} {stuListsSql} and (c.status<>404 or IS_DEFINED(c.status) = false ) ";
  112. //查询数据归属学校的
  113. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<MQActivity>(querySchool, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{school}") }))
  114. {
  115. if (item.progress.Equals("going", StringComparison.OrdinalIgnoreCase))
  116. {
  117. datas.Add(item);
  118. }
  119. acount += 1;
  120. }
  121. }
  122. string queryTeacher = $" SELECT distinct c.owner, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime FROM c where {pksql} and (c.status<>404 or IS_DEFINED(c.status) = false ) ";
  123. //查询数据归属个人的
  124. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<MQActivity>(queryTeacher, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{type}-{id}") }))
  125. {
  126. acount += 1;
  127. if (item.progress.Equals("going", StringComparison.OrdinalIgnoreCase))
  128. {
  129. datas.Add(item);
  130. }
  131. }
  132. KeyValuePair<string, int> valuePair = new KeyValuePair<string, int>(type, acount);
  133. count.Add(valuePair);
  134. }
  135. //课程统计 个人和学校 分别对应的教学的班级有多少
  136. List<CourseCount> privateCourse = new List<CourseCount>();
  137. string courseSql = $" SELECT a.teacherId,a.classId, a.stulist,c.id,c.name FROM c join a in c.schedule where a.teacherId='{id}' ";
  138. await foreach(var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<CourseCount>(courseSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{id}") })){
  139. privateCourse.Add(item);
  140. }
  141. List<CourseCount> schoolCourse = new List<CourseCount>();
  142. if (!string.IsNullOrEmpty(school)) {
  143. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.School).GetItemQueryIterator<CourseCount>(courseSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Course-{school}") }))
  144. {
  145. schoolCourse.Add(item);
  146. }
  147. }
  148. //涉及的课程清单数量(去重),与排课无关,排课涉及周期问题,暂不进行统计
  149. int privateCourseCount= privateCourse.Select(x => x.id).ToHashSet().Count();
  150. int schoolCourseCount = schoolCourse.Select(x => x.id).ToHashSet().Count();
  151. //行政班教学数量,去重。
  152. HashSet<string> classIds = new HashSet<string>();
  153. HashSet<string> teachIds = new HashSet<string>();
  154. HashSet<string> stulists = new HashSet<string>();
  155. privateCourse.ForEach(x => {
  156. if (!string.IsNullOrWhiteSpace(x.classId))
  157. {
  158. classIds.Add(x.classId);
  159. }
  160. if (!string.IsNullOrWhiteSpace(x.stulist))
  161. {
  162. stulists.Add(x.stulist);
  163. }
  164. });
  165. schoolCourse.ForEach(x => {
  166. if (!string.IsNullOrWhiteSpace(x.classId))
  167. {
  168. classIds.Add(x.classId);
  169. }
  170. if (!string.IsNullOrWhiteSpace(x.stulist))
  171. {
  172. teachIds.Add(x.stulist);
  173. }
  174. });
  175. string countItemPaperSql = $" SELECT value(count(1)) FROM c ";
  176. int privateItemCount = 0;
  177. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<int>(countItemPaperSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Item-{id}") }))
  178. {
  179. privateItemCount=item;
  180. }
  181. int privatePaperCount = 0;
  182. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<int>(countItemPaperSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Paper-{id}") }))
  183. {
  184. privatePaperCount = item;
  185. }
  186. int privateSyllabusCount = 0;
  187. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<int>(countItemPaperSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Syllabus-{id}") }))
  188. {
  189. privateSyllabusCount = item;
  190. }
  191. List<string> bloblogTypes = new List<string>() ;
  192. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<string>(
  193. "select value( c.type) from c "
  194. , requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Bloblog-{id}") }))
  195. {
  196. bloblogTypes.Add(item);
  197. }
  198. var bloblogTypeCount= bloblogTypes.GroupBy(x => x).Select(y => new { type = y, count = y.ToList().Count });
  199. return Ok(new { totalCount = count, goingDatas = datas, privateCourseCount, schoolCourseCount , classIdsCount= classIds.Count(), teachIdsCount= teachIds.Count(), stulistsCount = stulists.Count(),
  200. privateItemCount , privatePaperCount, privateSyllabusCount, privateBloblogCount= bloblogTypes.Count,
  201. bloblogTypeCount
  202. });
  203. }
  204. public class CourseCount{
  205. public string id { get; set; }
  206. public string teacherId { get; set; }
  207. public string classId { get; set; }
  208. public string stulist { get; set; }
  209. public string name { get; set; }
  210. }
  211. /// <summary>
  212. /// 教师端,查询活动所有活动类型的列表,班主任,任课教师等
  213. /// 执教班级
  214. /// </summary>
  215. /// <param name="request">
  216. /// 教师tmdid !userid:"1255868536"
  217. ///学校编码 !school:"hbcn"
  218. ///执教的班级信息 !classes:[{"classid":"S-C-00001","scope":"school"},{"classid":"P-C-00004","scope":"private"}] TODO 需要排查 对象和班级字符串id设计原因 {"classid":"S-C-00001","scope":"school"}
  219. ///执教的科目 ?subjects:["subjectid1","subjectid2"]
  220. ///活动类型 !"pk":"Vote"/"Exam"/"Homework"/"Learn"/"Survey"" // Vote投票 Survey问卷 Exam评测 Learn学习活动 Homework作业活动
  221. ///时间筛选范围开始时间 默认30天之前 ?"stime":1608274766154
  222. ///时间筛选范围结束时间 默认当前时间 ?"etime":1608274766666
  223. ///每页大小 ?"count":10/null/Undefined
  224. ///分页Token ?"continuationToken":Undefined/null/"[{\"token\":\"+RID:~omxMAP3ipcSEEwAAAAAAAA==#RT:2#TRC:20#ISV:2#IEO:65551#QCF:1#FPC:AYQTAAAAAAAAiRMAAAAAAAA=\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}]"
  225. ///当前状态 ?"progress":Undefined/null/"" 表示两种状态都要查询/ "going"/"finish" 表示查询进行中/ 或者已完成 学生端只能查询正在进行或已经结束 going 已发布|finish 已结束
  226. /// </param>
  227. /// <returns></returns>
  228. [ProducesDefaultResponseType]
  229. [HttpPost("tch-activity")]
  230. [Authorize(Roles = "IES")]
  231. //[AuthToken(Roles = "teacher")]
  232. public async Task<IActionResult> TchActivity(JsonElement requert)
  233. {
  234. var (id, _, _, school) = HttpContext.GetAuthTokenInfo();
  235. if (string.IsNullOrWhiteSpace(school))
  236. {
  237. if (requert.TryGetProperty("school", out JsonElement schoolcode))
  238. {
  239. if (!schoolcode.ValueKind.Equals(JsonValueKind.Undefined) && !schoolcode.ValueKind.Equals(JsonValueKind.Null) && schoolcode.ValueKind.Equals(JsonValueKind.String))
  240. {
  241. school = schoolcode.GetString();
  242. }
  243. }
  244. }
  245. if (string.IsNullOrWhiteSpace(id))
  246. {
  247. if (requert.TryGetProperty("userid", out JsonElement userid))
  248. {
  249. if (!userid.ValueKind.Equals(JsonValueKind.Undefined) && !userid.ValueKind.Equals(JsonValueKind.Null) && userid.ValueKind.Equals(JsonValueKind.String))
  250. {
  251. id = userid.GetString();
  252. }
  253. }
  254. }
  255. //var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
  256. //if (requert.TryGetProperty("stime", out JsonElement stime))
  257. //{
  258. // if (!stime.ValueKind.Equals(JsonValueKind.Undefined) && !stime.ValueKind.Equals(JsonValueKind.Null) && stime.TryGetInt64(out long data))
  259. // {
  260. // stimestamp = data;
  261. // }
  262. //}
  263. //string stimesql = $" c.startTime >= {stimestamp} ";
  264. //var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  265. //string etimesql = $" and c.startTime <= {etimestamp} ";
  266. //var progresssql = "";
  267. //if (requert.TryGetProperty("progress", out JsonElement progress))
  268. //{
  269. // if (!progress.ValueKind.Equals(JsonValueKind.Undefined) && !progress.ValueKind.Equals(JsonValueKind.Null) && progress.ValueKind.Equals(JsonValueKind.String))
  270. // {
  271. // progresssql = $" and c.progress='{progress}' ";
  272. // }
  273. //}
  274. var pksql = "";
  275. string type = null;
  276. if (requert.TryGetProperty("pk", out JsonElement pk))
  277. {
  278. if (!pk.ValueKind.Equals(JsonValueKind.Undefined) && !pk.ValueKind.Equals(JsonValueKind.Null) && pk.ValueKind.Equals(JsonValueKind.String))
  279. {
  280. pksql = $" c.pk='{pk}' ";
  281. type = $"{pk}";
  282. }
  283. }
  284. if (string.IsNullOrEmpty(type)) { return BadRequest("type is required!"); }
  285. string continuationTokenSchool = null;
  286. //默认不指定返回大小
  287. int? topcout = null;
  288. if (requert.TryGetProperty("count", out JsonElement jcount))
  289. {
  290. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  291. {
  292. topcout = data;
  293. }
  294. }
  295. //是否需要进行分页查询,默认不分页
  296. bool iscontinuation = false;
  297. if (topcout != null && topcout.Value > 0)
  298. {
  299. iscontinuation = true;
  300. }
  301. //如果指定了返回大小
  302. if (requert.TryGetProperty("continuationTokenSchool", out JsonElement continuationSchool))
  303. {
  304. //指定了cancellationToken continuationSchool
  305. if (!continuationSchool.ValueKind.Equals(JsonValueKind.Null) && continuationSchool.ValueKind.Equals(JsonValueKind.String))
  306. {
  307. continuationTokenSchool = continuationSchool.GetString();
  308. }
  309. }
  310. //班级
  311. string joinSqlClasses = "";
  312. string andSqlClasses = "";
  313. string joinSqlStulist = "";
  314. string andSqlStulist = "";
  315. List<string> classes = null;
  316. if (requert.TryGetProperty("classes", out JsonElement jclasses))
  317. {
  318. if (jclasses.ValueKind is JsonValueKind.Array)
  319. {
  320. classes = jclasses.ToObject<List<string>>();
  321. if (classes.IsNotEmpty())
  322. {
  323. //行政班
  324. joinSqlClasses = " join A1 in c.classes ";
  325. List<string> sqlListc = new List<string>();
  326. classes.ForEach(x => { sqlListc.Add($" '{x}' "); });
  327. string sqlc = string.Join(" , ", sqlListc);
  328. andSqlClasses = $" A1 in ({sqlc}) ";
  329. //教学班
  330. joinSqlStulist = " join A1 in c.stuLists ";
  331. List<string> sqlListl = new List<string>();
  332. classes.ForEach(x => { sqlListl.Add($" '{x}' "); });
  333. string sqll = string.Join(" , ", sqlListl);
  334. andSqlStulist = $" A1 in ({sqll}) ";
  335. }
  336. }
  337. }
  338. string classesSql = "";
  339. if (!string.IsNullOrWhiteSpace(joinSqlClasses))
  340. {
  341. classesSql = $" and {andSqlClasses } ";
  342. }
  343. string stuListsSql = "";
  344. if (!string.IsNullOrWhiteSpace(joinSqlStulist))
  345. {
  346. stuListsSql = $" and {andSqlStulist } ";
  347. }
  348. List<JsonElement> datas = new List<JsonElement>();
  349. var client = _azureCosmos.GetCosmosClient();
  350. //班主任 ,任课教师只需要查询两种校园活动 和班级活动 , 不查询私人教室创建的活动。
  351. if (!string.IsNullOrWhiteSpace(school) && classes.IsNotEmpty())
  352. {
  353. //string querySchool = $" SELECT distinct value c FROM c {joinSqlClasses} {joinSqlSubjects} where {stimesql} {etimesql} {progresssql} {typesql} {andSqlSubjects} {tgSql}";
  354. string querySchoolclss = $" SELECT distinct c.owner,c.sStatus, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime,c.source,c.type FROM c {joinSqlClasses} where {pksql} {classesSql} and (c.status<>404 or IS_DEFINED(c.status) = false ) ";
  355. //查询数据归属学校的
  356. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(querySchoolclss, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{school}") }))
  357. {
  358. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  359. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  360. {
  361. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  362. {
  363. datas.Add(obj.ToObject<JsonElement>());
  364. }
  365. //如果需要分页则跳出
  366. if (iscontinuation)
  367. {
  368. continuationTokenSchool = item.GetContinuationToken();
  369. break;
  370. }
  371. }
  372. }
  373. string querySchoollist = $" SELECT distinct c.owner,c.sStatus, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime ,c.source,c.type FROM c {joinSqlStulist} where {pksql} {stuListsSql} and (c.status<>404 or IS_DEFINED(c.status) = false ) ";
  374. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(querySchoollist, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{school}") }))
  375. {
  376. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  377. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  378. {
  379. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  380. {
  381. datas.Add(obj.ToObject<JsonElement>());
  382. }
  383. //如果需要分页则跳出
  384. if (iscontinuation)
  385. {
  386. continuationTokenSchool = item.GetContinuationToken();
  387. break;
  388. }
  389. }
  390. }
  391. }
  392. string continuationTokenTeacher = null;
  393. //如果指定了返回大小
  394. if (requert.TryGetProperty("continuationTokenTeacher", out JsonElement continuationTeacher))
  395. {
  396. //指定了cancellationToken continuationSchool
  397. if (!continuationTeacher.ValueKind.Equals(JsonValueKind.Null) && continuationTeacher.ValueKind.Equals(JsonValueKind.String))
  398. {
  399. continuationTokenTeacher = continuationTeacher.GetString();
  400. }
  401. }
  402. string queryTeacher = $" SELECT distinct c.owner,c.sStatus, c.id,c.code, c.classes,c.stuLists,c.subjects,c.progress,c.scope,c.startTime,c.school,c.creatorId,c.name,c.pk ,c.endTime ,c.source,c.type FROM c where {pksql} and (c.status<>404 or IS_DEFINED(c.status) = false ) ";
  403. //查询数据归属学校的
  404. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryStreamIterator(queryTeacher, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"{type}-{id}") }))
  405. {
  406. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  407. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  408. {
  409. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  410. {
  411. datas.Add(obj.ToObject<JsonElement>());
  412. }
  413. //如果需要分页则跳出
  414. if (iscontinuation)
  415. {
  416. continuationTokenTeacher = item.GetContinuationToken();
  417. break;
  418. }
  419. }
  420. }
  421. return Ok(new { datas, continuationTokenSchool, continuationTokenTeacher });
  422. }
  423. [ProducesDefaultResponseType]
  424. [HttpPost("tec-activity")]
  425. [Authorize(Roles = "IES")]
  426. [AuthToken(Roles = "student,admin,teacher")]
  427. public async Task<IActionResult> TecActivity(JsonElement request)
  428. {
  429. if (!request.TryGetProperty("type", out JsonElement _type)) return BadRequest();
  430. var (id, name, pic, school) = HttpContext.GetAuthTokenInfo();
  431. List<StuActivity> datas = new List<StuActivity>();
  432. var queryc = $"SELECT value(c) from c where c.school = '{school}' and c.type='{_type}' ";
  433. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<StuActivity>(queryText: queryc, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{id}") }))
  434. {
  435. datas.Add(item);
  436. /*using var json = await JsonDocument.ParseAsync(item.ContentStream);
  437. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  438. {
  439. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  440. {
  441. courses.Add(obj.ToObject<object>());
  442. }
  443. }*/
  444. }
  445. //(List<StuActivity> datas, string continuationToken) = await ActivityStudentService.FindActivity(request, id, school, _azureCosmos, _azureRedis);
  446. return Ok(new { datas });
  447. }
  448. /// <summary>
  449. ///
  450. /// </summary>
  451. /// <param name="element"></param>
  452. /// <returns></returns>
  453. [ProducesDefaultResponseType]
  454. [HttpPost("delete-activity")]
  455. [Authorize(Roles = "IES")]
  456. [AuthToken(Roles = "teacher,admin,student")]
  457. public async Task<IActionResult> DeleteActivity(JsonElement element)
  458. {
  459. try
  460. {
  461. if (!element.TryGetProperty("id", out JsonElement id)) return BadRequest();
  462. if (!element.TryGetProperty("code", out JsonElement code)) return BadRequest();
  463. if (!element.TryGetProperty("role", out JsonElement role)) return BadRequest();
  464. var client = _azureCosmos.GetCosmosClient();
  465. if (role.ValueKind.Equals(JsonValueKind.String))
  466. {
  467. if (role.GetString().Equals("teacher") || role.GetString().Equals("admin"))
  468. {
  469. await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemAsync<StuActivity>($"{id}", new PartitionKey($"{code}"));
  470. }
  471. else if (role.GetString().Equals("student"))
  472. {
  473. await client.GetContainer(Constant.TEAMModelOS, "Teacher").DeleteItemAsync<StuActivity>($"{id}", new PartitionKey($"{code}"));
  474. }
  475. else
  476. {
  477. return Ok(new { status = 500 });
  478. }
  479. }
  480. else
  481. {
  482. return Ok(new { status = 500 });
  483. }
  484. return Ok(new { status = 200 });
  485. }
  486. catch (Exception ex)
  487. {
  488. return Ok(new { status = 500 });
  489. }
  490. }
  491. }
  492. }