GroupListController.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.Models;
  7. using TEAMModelOS.SDK;
  8. using TEAMModelOS.SDK.DI;
  9. using System.Text.Json;
  10. using TEAMModelOS.SDK.Models;
  11. using TEAMModelOS.SDK.Extension;
  12. using Azure.Cosmos;
  13. using Microsoft.AspNetCore.Http;
  14. using Microsoft.Extensions.Options;
  15. using System.IO;
  16. using System.Dynamic;
  17. using System.Net.Http;
  18. using System.Net;
  19. using Newtonsoft.Json;
  20. using System.Linq;
  21. using StackExchange.Redis;
  22. using static TEAMModelOS.SDK.Models.Teacher;
  23. using Microsoft.Extensions.Configuration;
  24. using TEAMModelOS.Filter;
  25. using Microsoft.AspNetCore.Authorization;
  26. using HTEXLib.COMM.Helpers;
  27. using TEAMModelOS.SDK.Models.Service;
  28. namespace TEAMModelAPI.Controllers
  29. {
  30. [ProducesResponseType(StatusCodes.Status200OK)]
  31. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  32. [ApiController]
  33. [Route("{scope}")]
  34. public class GroupListController : ControllerBase
  35. {
  36. public AzureCosmosFactory _azureCosmos;
  37. private readonly AzureStorageFactory _azureStorage;
  38. private readonly AzureRedisFactory _azureRedis;
  39. private readonly DingDing _dingDing;
  40. private readonly Option _option;
  41. private readonly IConfiguration _configuration;
  42. private readonly CoreAPIHttpService _coreAPIHttpService;
  43. private readonly AzureServiceBusFactory _serviceBus;
  44. public GroupListController(CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration, AzureServiceBusFactory serviceBus)
  45. {
  46. _azureCosmos = azureCosmos;
  47. _azureStorage = azureStorage;
  48. _azureRedis = azureRedis;
  49. _dingDing = dingDing;
  50. _option = option?.Value;
  51. _configuration = configuration;
  52. _coreAPIHttpService = coreAPIHttpService;
  53. _serviceBus = serviceBus;
  54. }
  55. /*
  56. "periodId":"学段(选填)"
  57. */
  58. /// <summary>
  59. /// 获取学校的行政班,教学班,教研组,研修名单
  60. /// </summary>
  61. /// <param name="request"></param>
  62. /// <returns></returns>
  63. [ProducesDefaultResponseType]
  64. [HttpPost("get-group-list")]
  65. [ApiToken(Auth = "1201", Name = "学校名单列表", RW = "R", Limit = false)]
  66. public async Task<IActionResult> GetGroupList(JsonElement json)
  67. {
  68. var client = _azureCosmos.GetCosmosClient();
  69. var (id, school) = HttpContext.GetApiTokenInfo();
  70. json.TryGetProperty("periodId", out JsonElement periodId);
  71. List<GroupListGrp> groupLists = new List<GroupListGrp>();
  72. //包含,学校的行政班,教学班
  73. json.TryGetProperty("type", out JsonElement _type);
  74. List<string> types = null;
  75. if (_type.ValueKind.Equals(JsonValueKind.Array))
  76. {
  77. types = _type.ToObject<List<string>>();
  78. }
  79. else if (_type.ValueKind.Equals(JsonValueKind.String))
  80. {
  81. types = new List<string> { $"{types}" };
  82. }
  83. if (types.IsEmpty() || types.Contains("class"))
  84. {
  85. StringBuilder classsql = new StringBuilder($"SELECT c.id,c.name,c.periodId ,c.year FROM c ");
  86. if (!string.IsNullOrEmpty($"{periodId}"))
  87. {
  88. classsql.Append($" where c.periodId='{periodId}' ");
  89. }
  90. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<ClassInfo>(queryText: classsql.ToString(),
  91. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Class-{school}") }))
  92. {
  93. HashSet<string> groupNames = new HashSet<string>();
  94. string gpsql = $"SELECT distinct c.groupId,c.groupName FROM c where c.classId='{item.id}'and c.groupName <>null";
  95. await foreach (var gp in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: gpsql,
  96. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  97. {
  98. groupNames.Add(gp.groupName);
  99. }
  100. ///行政班(学生搜寻classId动态返回)class
  101. GroupListGrp group = new GroupListGrp
  102. {
  103. id = item.id,
  104. code = $"GroupList-{school}",
  105. name = item.name,
  106. periodId = item.periodId,
  107. scope = "school",
  108. school = $"{school}",
  109. type = "class",
  110. year = item.year,
  111. groupName = groupNames
  112. };
  113. groupLists.Add(group);
  114. }
  115. }
  116. if (types.IsEmpty() || types.Contains("teach"))
  117. {
  118. //教学班
  119. StringBuilder teachsql = new StringBuilder($" SELECT distinct value(c) FROM c where c.type='teach'");
  120. if (!string.IsNullOrEmpty($"{periodId}"))
  121. {
  122. teachsql.Append($" and c.periodId='{periodId}'");
  123. }
  124. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  125. GetItemQueryIterator<GroupList>(queryText: teachsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  126. {
  127. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  128. groupLists.Add(new GroupListGrp(item, groupName));
  129. }
  130. }
  131. if (types.IsEmpty() || types.Contains("research"))
  132. {
  133. //教研组
  134. StringBuilder researchsql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='research'");
  135. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  136. GetItemQueryIterator<GroupList>(queryText: researchsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  137. {
  138. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  139. groupLists.Add(new GroupListGrp(item, groupName));
  140. }
  141. }
  142. if (types.IsEmpty() || types.Contains("yxtrain"))
  143. {
  144. //研修名单
  145. StringBuilder yxtrainsql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='yxtrain'");
  146. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  147. GetItemQueryIterator<GroupList>(queryText: yxtrainsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  148. {
  149. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  150. groupLists.Add(new GroupListGrp(item, groupName));
  151. }
  152. }
  153. return Ok(new
  154. {
  155. groupLists = groupLists.Select(x => new { x.id, x.type, x.name, x.periodId, x.school, x.scope, x.year })
  156. });
  157. }
  158. [ProducesDefaultResponseType]
  159. [HttpPost("get-group-members")]
  160. [ApiToken(Auth = "1202", Name = "获取名单详细信息和成员信息", RW = "R", Limit = false)]
  161. public async Task<IActionResult> GetGroupMembers(JsonElement json)
  162. {
  163. var client = _azureCosmos.GetCosmosClient();
  164. if (!json.TryGetProperty("ids", out JsonElement ids)) return BadRequest();
  165. var (id, school) = HttpContext.GetApiTokenInfo();
  166. List<string> listids = ids.ToObject<List<string>>();
  167. (List<RMember> members, List<RGroupList> groups) = await GroupListService.GetStutmdidListids(_coreAPIHttpService, client, _dingDing, listids, $"{school}");
  168. return Ok(new { groups = groups.Select(x => new { x.name, x.no, x.periodId, x.school, x.type, x.year, x.tcount, x.scount, x.leader, x.members, x.id }), members });
  169. }
  170. /// <summary>
  171. /// 导入行政班学生
  172. /// </summary>
  173. /// <param name="request"></param>
  174. /// <returns></returns>
  175. [ProducesDefaultResponseType]
  176. [HttpPost("import-class-members")]
  177. [ApiToken(Auth = "1203", Name = "导入行政班学生", RW = "W", Limit = false)]
  178. public async Task<IActionResult> ImportClassMembers(JsonElement json)
  179. {
  180. var (id, school) = HttpContext.GetApiTokenInfo();
  181. if (!json.TryGetProperty("periodId", out JsonElement _periodId)) { return Ok(new { error = 2, msg = "学段信息错误!" }); }
  182. if (!json.TryGetProperty("students", out JsonElement _students)) { return Ok(new { error = 1, msg = "学生列表格式错误!" }); }
  183. if (_students.ValueKind.Equals(JsonValueKind.Array))
  184. {
  185. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  186. Period period = data.period.Find(x => x.id.Equals($"{_periodId}"));
  187. if (period != null)
  188. {
  189. List<Student> webStudents = _students.ToObject<List<Student>>();
  190. webStudents.ForEach(x => x.periodId = $"{_periodId}");
  191. List<Student> preStudents = await StudentService.GeStudentData(_azureCosmos, school, webStudents?.Select(x=>x.id));
  192. var retUpsert = await StudentService.upsertStudents(_azureCosmos, _dingDing, _option, school, json.GetProperty("students").EnumerateArray());
  193. await StudentService.CheckStudent(_serviceBus, _configuration, _azureCosmos, school, webStudents, preStudents);
  194. return this.Ok(new { code = $"{school}", students = retUpsert.studs, retUpsert.classDuplNos, retUpsert.errorIds });
  195. }
  196. else
  197. {
  198. return Ok(new { error = 2, msg = "学段信息错误!" });
  199. }
  200. }
  201. else {
  202. return Ok(new { error = 1, msg = "学生列表格式错误" });
  203. }
  204. }
  205. /// <summary>
  206. ///更新學生資料,批量密碼重置,基本資訊更新(姓名、教室ID、性別、學年及座號)
  207. /// </summary>
  208. /// <param name="request"></param>
  209. /// <returns></returns>
  210. [ProducesDefaultResponseType]
  211. [HttpPost("update-class-members")]
  212. [ApiToken(Auth = "1204", Name = "更新行政班学生", RW = "W", Limit = false)]
  213. public async Task<IActionResult> UpdateClassMembers(JsonElement json)
  214. {
  215. var (id, school) = HttpContext.GetApiTokenInfo();
  216. if (!json.TryGetProperty("periodId", out JsonElement _periodId)) { return Ok(new { error = 2, msg = "学段信息错误!" }); }
  217. if (!json.TryGetProperty("students", out JsonElement _students)) { return Ok(new { error = 1, msg = "学生列表格式错误!" }); }
  218. if (_students.ValueKind.Equals(JsonValueKind.Array))
  219. {
  220. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  221. Period period = data.period.Find(x => x.id.Equals($"{_periodId}"));
  222. if (period != null)
  223. {
  224. //更新學生資料,批量密碼重置,基本資訊更新(姓名、教室ID、性別、學年及座號)
  225. List<Student> webStudents = json.GetProperty("students").ToObject<List<Student>>();
  226. webStudents.ForEach(x => x.periodId = $"{_periodId}");
  227. List<Student> preStudents = await StudentService.GeStudentData(_azureCosmos, school, webStudents?.Select(x => x.id));
  228. var retUpdate = await StudentService.updateStudents(_azureCosmos, _dingDing, _option, school, json.GetProperty("students").EnumerateArray());
  229. await StudentService.CheckStudent(_serviceBus, _configuration, _azureCosmos, school, webStudents, preStudents);
  230. return this.Ok(new { code = school, students = retUpdate.studs, retUpdate.classDuplNos, retUpdate.nonexistentIds, retUpdate.errorNos, retUpdate.errorClassId });
  231. }
  232. else
  233. {
  234. return Ok(new { error = 2, msg = "学段信息错误!" });
  235. }
  236. }
  237. else
  238. {
  239. return Ok(new { error = 1, msg = "学生列表格式错误" });
  240. }
  241. }
  242. /// <summary>
  243. ////將學生基本資料內的classId、no、groupId及groupName寫入null
  244. /// </summary>
  245. /// <param name="request"></param>
  246. /// <returns></returns>
  247. [ProducesDefaultResponseType]
  248. [HttpPost("remove-class-members")]
  249. [ApiToken(Auth = "1205", Name = "移除行政班学生", RW = "W", Limit = false)]
  250. public async Task<IActionResult> RemoveClassMembers(JsonElement json)
  251. {
  252. var (id, school) = HttpContext.GetApiTokenInfo();
  253. if (!json.TryGetProperty("periodId", out JsonElement _periodId)) { return Ok(new { error = 2, msg = "学段信息错误!" }); }
  254. if (!json.TryGetProperty("students", out JsonElement _students)) { return Ok(new { error = 1, msg = "学生列表格式错误!" }); }
  255. if (_students.ValueKind.Equals(JsonValueKind.Array))
  256. {
  257. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  258. Period period = data.period.Find(x => x.id.Equals($"{_periodId}"));
  259. if (period != null)
  260. {
  261. //將學生基本資料內的classId、no、groupId及groupName寫入null
  262. List<string> stus = json.GetProperty("students").ToObject<List<string>>();
  263. List<Student> webStudents = new List<Student>();
  264. foreach (string idstu in stus)
  265. {
  266. webStudents.Add(new Student { id = idstu, code = $"Base-{school}" });
  267. }
  268. List<Student> preStudents = await StudentService.GeStudentData(_azureCosmos, school, webStudents?.Select(x => x.id));
  269. (List<string> studs, List<string> nonexistentIds, List<string> errorIds) retRemove = await StudentService.removeStudentClassInfo( _azureCosmos, _dingDing, _option,school, json.GetProperty("students").EnumerateArray());
  270. await StudentService.CheckStudent(_serviceBus, _configuration, _azureCosmos, school, webStudents, preStudents);
  271. return Ok(new { code = $"{school}", ids = retRemove.studs, retRemove.nonexistentIds, retRemove.errorIds });
  272. }
  273. else
  274. {
  275. return Ok(new { error = 2, msg = "学段信息错误!" });
  276. }
  277. }
  278. else
  279. {
  280. return Ok(new { error = 1, msg = "学生列表格式错误" });
  281. }
  282. }
  283. /// <summary>
  284. /// 创建或更新教学班基本信息
  285. /// </summary>
  286. /// <param name="request"></param>
  287. /// <returns></returns>
  288. [ProducesDefaultResponseType]
  289. [HttpPost("upsert-teach-groups")]
  290. [ApiToken(Auth = "1206", Name = "创建或更新教学班", RW = "W", Limit = false)]
  291. public async Task<IActionResult> UpsertTeachGroups(GroupListDtoImpt json) {
  292. var (_, school) = HttpContext.GetApiTokenInfo();
  293. var groupListsDto = json.groupLists;
  294. List<GroupList> groupLists = new List<GroupList>(); ;
  295. List<Dictionary<string, string>> errorData = new List<Dictionary<string, string>>();
  296. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  297. foreach (var list in groupListsDto) {
  298. Period period = data.period.Find(x => x.id.Equals($"{list.periodId}"));
  299. if (period != null)
  300. {
  301. GroupList groupList = null;
  302. if (string.IsNullOrWhiteSpace(list.id))
  303. {
  304. groupList = new GroupList()
  305. {
  306. pk = "GroupList",
  307. id = Guid.NewGuid().ToString(),
  308. code = $"GroupList-{school}",
  309. name = list.name,
  310. periodId = list.periodId,
  311. scope = "school",
  312. school = school,
  313. type = "teach",
  314. year = list.year,
  315. froms = 3
  316. };
  317. groupList = await GroupListService.CheckListNo(groupList, _azureCosmos, _dingDing, _option);
  318. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(groupList, new PartitionKey(groupList.code));
  319. }
  320. else
  321. {
  322. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{list.id}", new PartitionKey($"GroupList-{school}"));
  323. if (response.Status == 200)
  324. {
  325. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  326. groupList = jsonDocument.RootElement.ToObject<GroupList>();
  327. groupList.name = string.IsNullOrWhiteSpace(list.name) ? groupList.name : list.name;
  328. groupList.periodId = string.IsNullOrWhiteSpace(list.periodId) ? groupList.periodId : list.periodId;
  329. groupList.school = school;
  330. groupList.scope = "school";
  331. groupList.froms = 3;
  332. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(groupList, groupList.id, new PartitionKey(groupList.code));
  333. }
  334. else
  335. {
  336. groupList = new GroupList()
  337. {
  338. id = Guid.NewGuid().ToString(),
  339. code = $"GroupList-{school}",
  340. name = list.name,
  341. periodId = list.periodId,
  342. scope = "school",
  343. school = school,
  344. type = "teach",
  345. year = list.year,
  346. froms = 3
  347. };
  348. groupList = await GroupListService.CheckListNo(groupList, _azureCosmos, _dingDing, _option);
  349. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(groupList, new PartitionKey(groupList.code));
  350. }
  351. }
  352. if (groupList != null) {
  353. groupLists.Add(groupList);
  354. }
  355. }
  356. else
  357. {
  358. errorData.Add(new Dictionary<string, string> { { "group",list.name}, { "periodId",list.periodId} });
  359. // return Ok(new { error = 2, msg ="学段不存在!" });
  360. }
  361. }
  362. return Ok(new { groupLists, errorData });
  363. }
  364. public class MemberImpt {
  365. public List<Member> members { get; set; } = new List<Member>();
  366. public string groupId { get; set; }
  367. }
  368. /// <summary>
  369. /// 导入教学班学生
  370. /// </summary>
  371. /// <param name="request"></param>
  372. /// <returns></returns>
  373. [ProducesDefaultResponseType]
  374. [HttpPost("import-teach-members")]
  375. [ApiToken(Auth = "1207", Name = "导入教学班学生", RW = "W", Limit = false)]
  376. public async Task<IActionResult> ImportTeachMembers(MemberImpt json)
  377. {
  378. var (id, school) = HttpContext.GetApiTokenInfo();
  379. //School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  380. List<Member> members = json.members;
  381. var tmds = members.Where(x => x.type == 1);
  382. var stus = members.Where(x => x.type == 2);
  383. List<Student> students = await StudentService.GeStudentData(_azureCosmos, school, stus?.Select(x => x.id));
  384. List<TmdInfo> infos = null;
  385. string tmdstr = "";
  386. try
  387. {
  388. var content = new StringContent(tmds.Select(x => x.id).ToJsonString(), Encoding.UTF8, "application/json");
  389. tmdstr = await _coreAPIHttpService.GetUserInfos(content);
  390. infos = tmdstr.ToObject<List<TmdInfo>>();
  391. }
  392. catch (Exception ex)
  393. {
  394. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location}用户转换失败:{_coreAPIHttpService.options.Get("Default").url}{tmdstr}\n {ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  395. //return Ok(new { error =3, msg = "醍摩豆ID验证错误!" });
  396. }
  397. var unexist_student = stus.Select(x => x.id).Except(students.Select(y => y.id));
  398. var unexist_tmdids = tmds.Select(x => x.id).Except(infos.Select(y => y.id));
  399. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{json.groupId}", new PartitionKey($"GroupList-{school}"));
  400. if (response.Status == 200)
  401. {
  402. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  403. var list = jsonDocument.RootElement.ToObject<GroupList>();
  404. if (list.type.Equals("teach") && list.school.Equals(school))
  405. {
  406. if (infos.Any())
  407. {
  408. infos.ToList().ForEach(x => {
  409. if (!list.members.Where(z => z.type == 1).Select(x => x.id).Contains(x.id))
  410. {
  411. GroupListService.JoinList(list, x.id , 1 , school);
  412. }
  413. });
  414. }
  415. if (stus.Any())
  416. {
  417. stus.ToList().ForEach(x => {
  418. if (!list.members.Where(z => z.type == 2).Select(x => x.id).Contains(x.id)) {
  419. GroupListService.JoinList(list, x.id ,2 , school);
  420. }
  421. });
  422. }
  423. list = await GroupListService.CheckListNo(list, _azureCosmos, _dingDing, _option);
  424. list = await GroupListService.UpsertList(list, _azureCosmos, _configuration, _serviceBus);
  425. return Ok(new { unexist_student, unexist_tmdids, import_list=list });
  426. }
  427. else {
  428. return Ok(new { error = 3, msg = $"名单类型不是教学班或者不是当前学校的名单!{list.type},{list.school}" });
  429. }
  430. }
  431. else
  432. {
  433. return Ok(new { error = 2, msg = "名单错误!" });
  434. }
  435. }
  436. /// <summary>
  437. /// 移除教学班学生
  438. /// </summary>
  439. /// <param name="request"></param>
  440. /// <returns></returns>
  441. [ProducesDefaultResponseType]
  442. [HttpPost("remove-teach-members")]
  443. [ApiToken(Auth = "1208", Name = "移除教学班学生", RW = "W", Limit = false)]
  444. public async Task<IActionResult> RemoveTeachMembers(JsonElement json) {
  445. var (id, school) = HttpContext.GetApiTokenInfo();
  446. json.TryGetProperty("stuids", out JsonElement _stuids);
  447. json.TryGetProperty("tmdids", out JsonElement _tmdids);
  448. if (json.TryGetProperty("groupId", out JsonElement _groupId)) { return Ok(new { error = 1, msg = "名单错误!" }); }
  449. List<string> stuids = null;
  450. if (_stuids.ValueKind.Equals(JsonValueKind.Array)) {
  451. stuids = _stuids.ToObject<List<string>>();
  452. }
  453. List<string> tmdids = null;
  454. if (_tmdids.ValueKind.Equals(JsonValueKind.Array))
  455. {
  456. tmdids = _tmdids.ToObject<List<string>>();
  457. }
  458. if (tmdids.Any() || stuids.Any())
  459. {
  460. // School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  461. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{_groupId}", new PartitionKey($"GroupList-{school}"));
  462. if (response.Status == 200)
  463. {
  464. JsonDocument document= JsonDocument.Parse(response.Content);
  465. var list= document.RootElement.Deserialize<GroupList>();
  466. List<string> remove_tmdids = new List<string>();
  467. if (tmdids.Any()) {
  468. tmdids.ForEach(x => {
  469. int len= list.members.RemoveAll(z => z.id.Equals(x) && z.type == 1);
  470. if (len > 0) {
  471. remove_tmdids.Add(x);
  472. }
  473. });
  474. }
  475. List<string> remove_stuids = new List<string>();
  476. if (stuids.Any())
  477. {
  478. stuids.ForEach(x => {
  479. int len = list.members.RemoveAll(z => z.id.Equals(x) && z.type == 2);
  480. if (len > 0)
  481. {
  482. remove_stuids.Add(x);
  483. }
  484. });
  485. }
  486. list = await GroupListService.UpsertList(list, _azureCosmos, _configuration, _serviceBus);
  487. return Ok(new { remove_stuids, remove_tmdids, list });
  488. }
  489. else {
  490. return Ok(new { error = 2, msg = "名单错误!" });
  491. }
  492. }
  493. else
  494. {
  495. return Ok(new { error = 2, msg = "移除的名单人员为空!" });
  496. }
  497. }
  498. }
  499. }