GroupListController.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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("school")]
  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. List<Student> preStudents = await StudentService.GeStudentData(_azureCosmos, school, webStudents?.Select(x=>x.id));
  191. var retUpsert = await StudentService.upsertStudents(_azureCosmos, _dingDing, _option, school, json.GetProperty("students").EnumerateArray());
  192. await StudentService.CheckStudent(_serviceBus, _configuration, _azureCosmos, school, webStudents, preStudents);
  193. return this.Ok(new { code = $"{school}", students = retUpsert.studs, retUpsert.classDuplNos, retUpsert.errorIds });
  194. }
  195. else
  196. {
  197. return Ok(new { error = 2, msg = "学段信息错误!" });
  198. }
  199. }
  200. else {
  201. return Ok(new { error = 1, msg = "学生列表格式错误" });
  202. }
  203. }
  204. /// <summary>
  205. ///更新學生資料,批量密碼重置,基本資訊更新(姓名、教室ID、性別、學年及座號)
  206. /// </summary>
  207. /// <param name="request"></param>
  208. /// <returns></returns>
  209. [ProducesDefaultResponseType]
  210. [HttpPost("update-class-members")]
  211. [ApiToken(Auth = "1204", Name = "更新行政班学生", RW = "W", Limit = false)]
  212. public async Task<IActionResult> UpdateClassMembers(JsonElement json)
  213. {
  214. var (id, school) = HttpContext.GetApiTokenInfo();
  215. if (!json.TryGetProperty("periodId", out JsonElement _periodId)) { return Ok(new { error = 2, msg = "学段信息错误!" }); }
  216. if (json.TryGetProperty("students", out JsonElement _students)) { return Ok(new { error = 1, msg = "学生列表格式错误!" }); }
  217. if (_students.ValueKind.Equals(JsonValueKind.Array))
  218. {
  219. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  220. Period period = data.period.Find(x => x.id.Equals($"{_periodId}"));
  221. if (period != null)
  222. {
  223. //更新學生資料,批量密碼重置,基本資訊更新(姓名、教室ID、性別、學年及座號)
  224. List<Student> webStudents = json.GetProperty("students").ToObject<List<Student>>();
  225. List<Student> preStudents = await StudentService.GeStudentData(_azureCosmos, school, webStudents?.Select(x => x.id));
  226. var retUpdate = await StudentService.updateStudents(_azureCosmos, _dingDing, _option, school, json.GetProperty("students").EnumerateArray());
  227. await StudentService.CheckStudent(_serviceBus, _configuration, _azureCosmos, school, webStudents, preStudents);
  228. return this.Ok(new { code = school, students = retUpdate.studs, retUpdate.classDuplNos, retUpdate.nonexistentIds, retUpdate.errorNos, retUpdate.errorClassId });
  229. }
  230. else
  231. {
  232. return Ok(new { error = 2, msg = "学段信息错误!" });
  233. }
  234. }
  235. else
  236. {
  237. return Ok(new { error = 1, msg = "学生列表格式错误" });
  238. }
  239. }
  240. /// <summary>
  241. ////將學生基本資料內的classId、no、groupId及groupName寫入null
  242. /// </summary>
  243. /// <param name="request"></param>
  244. /// <returns></returns>
  245. [ProducesDefaultResponseType]
  246. [HttpPost("remove-class-members")]
  247. [ApiToken(Auth = "1205", Name = "移除行政班学生", RW = "W", Limit = false)]
  248. public async Task<IActionResult> RemoveClassMembers(JsonElement json)
  249. {
  250. var (id, school) = HttpContext.GetApiTokenInfo();
  251. if (!json.TryGetProperty("periodId", out JsonElement _periodId)) { return Ok(new { error = 2, msg = "学段信息错误!" }); }
  252. if (json.TryGetProperty("students", out JsonElement _students)) { return Ok(new { error = 1, msg = "学生列表格式错误!" }); }
  253. if (_students.ValueKind.Equals(JsonValueKind.Array))
  254. {
  255. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  256. Period period = data.period.Find(x => x.id.Equals($"{_periodId}"));
  257. if (period != null)
  258. {
  259. //將學生基本資料內的classId、no、groupId及groupName寫入null
  260. List<string> stus = json.GetProperty("students").ToObject<List<string>>();
  261. List<Student> webStudents = new List<Student>();
  262. foreach (string idstu in stus)
  263. {
  264. webStudents.Add(new Student { id = idstu, code = $"Base-{school}" });
  265. }
  266. List<Student> preStudents = await StudentService.GeStudentData(_azureCosmos, school, webStudents?.Select(x => x.id));
  267. (List<string> studs, List<string> nonexistentIds, List<string> errorIds) retRemove = await StudentService.removeStudentClassInfo( _azureCosmos, _dingDing, _option,school, json.GetProperty("students").EnumerateArray());
  268. await StudentService.CheckStudent(_serviceBus, _configuration, _azureCosmos, school, webStudents, preStudents);
  269. return Ok(new { code = $"{school}", ids = retRemove.studs, retRemove.nonexistentIds, retRemove.errorIds });
  270. }
  271. else
  272. {
  273. return Ok(new { error = 2, msg = "学段信息错误!" });
  274. }
  275. }
  276. else
  277. {
  278. return Ok(new { error = 1, msg = "学生列表格式错误" });
  279. }
  280. }
  281. /// <summary>
  282. /// 创建或更新教学班基本信息
  283. /// </summary>
  284. /// <param name="request"></param>
  285. /// <returns></returns>
  286. [ProducesDefaultResponseType]
  287. [HttpPost("upsert-teach-groups")]
  288. [ApiToken(Auth = "1206", Name = "创建或更新教学班", RW = "W", Limit = false)]
  289. public async Task<IActionResult> UpsertTeachGroups(GroupListDtoImpt json) {
  290. var (_, school) = HttpContext.GetApiTokenInfo();
  291. var groupListsDto = json.groupLists;
  292. List<GroupList> groupLists = new List<GroupList>(); ;
  293. List<Dictionary<string, string>> errorData = new List<Dictionary<string, string>>();
  294. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  295. foreach (var list in groupListsDto) {
  296. Period period = data.period.Find(x => x.id.Equals($"{list.periodId}"));
  297. if (period != null)
  298. {
  299. GroupList groupList = null;
  300. if (string.IsNullOrWhiteSpace(list.id))
  301. {
  302. groupList = new GroupList()
  303. {
  304. pk = "GroupList",
  305. id = Guid.NewGuid().ToString(),
  306. code = $"GroupList-{school}",
  307. name = list.name,
  308. periodId = list.periodId,
  309. scope = "school",
  310. school = school,
  311. type = "teach",
  312. year = list.year,
  313. froms = 3
  314. };
  315. groupList = await GroupListService.CheckListNo(groupList, _azureCosmos, _dingDing, _option);
  316. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(groupList, new PartitionKey(groupList.code));
  317. }
  318. else
  319. {
  320. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{list.id}", new PartitionKey($"GroupList-{school}"));
  321. if (response.Status == 200)
  322. {
  323. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  324. groupList = jsonDocument.RootElement.ToObject<GroupList>();
  325. groupList.name = string.IsNullOrWhiteSpace(list.name) ? groupList.name : list.name;
  326. groupList.periodId = string.IsNullOrWhiteSpace(list.periodId) ? groupList.periodId : list.periodId;
  327. groupList.school = school;
  328. groupList.scope = "school";
  329. groupList.froms = 3;
  330. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(groupList, groupList.id, new PartitionKey(groupList.code));
  331. }
  332. else
  333. {
  334. groupList = new GroupList()
  335. {
  336. id = Guid.NewGuid().ToString(),
  337. code = $"GroupList-{school}",
  338. name = list.name,
  339. periodId = list.periodId,
  340. scope = "school",
  341. school = school,
  342. type = "teach",
  343. year = list.year,
  344. froms = 3
  345. };
  346. groupList = await GroupListService.CheckListNo(groupList, _azureCosmos, _dingDing, _option);
  347. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(groupList, new PartitionKey(groupList.code));
  348. }
  349. }
  350. if (groupList != null) {
  351. groupLists.Add(groupList);
  352. }
  353. }
  354. else
  355. {
  356. errorData.Add(new Dictionary<string, string> { { "group",list.name}, { "periodId",list.periodId} });
  357. // return Ok(new { error = 2, msg ="学段不存在!" });
  358. }
  359. }
  360. return Ok(new { groupLists, errorData });
  361. }
  362. public class MemberImpt {
  363. public List<Member> members { get; set; } = new List<Member>();
  364. public string groupId { get; set; }
  365. }
  366. /// <summary>
  367. /// 导入教学班学生
  368. /// </summary>
  369. /// <param name="request"></param>
  370. /// <returns></returns>
  371. [ProducesDefaultResponseType]
  372. [HttpPost("import-teach-members")]
  373. [ApiToken(Auth = "1207", Name = "导入教学班学生", RW = "W", Limit = false)]
  374. public async Task<IActionResult> ImportTeachMembers(MemberImpt json)
  375. {
  376. var (id, school) = HttpContext.GetApiTokenInfo();
  377. //School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  378. List<Member> members = json.members;
  379. var tmds = members.Where(x => x.type == 1);
  380. var stus = members.Where(x => x.type == 2);
  381. List<Student> students = await StudentService.GeStudentData(_azureCosmos, school, stus?.Select(x => x.id));
  382. List<TmdInfo> infos = null;
  383. string tmdstr = "";
  384. try
  385. {
  386. var content = new StringContent(tmds.Select(x => x.id).ToJsonString(), Encoding.UTF8, "application/json");
  387. tmdstr = await _coreAPIHttpService.GetUserInfos(content);
  388. infos = tmdstr.ToObject<List<TmdInfo>>();
  389. }
  390. catch (Exception ex)
  391. {
  392. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location}用户转换失败:{_coreAPIHttpService.options.Get("Default").url}{tmdstr}\n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  393. //return Ok(new { error =3, msg = "醍摩豆ID验证错误!" });
  394. }
  395. var unexist_student = stus.Select(x => x.id).Except(students.Select(y => y.id));
  396. var unexist_tmdids = tmds.Select(x => x.id).Except(infos.Select(y => y.id));
  397. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{json.groupId}", new PartitionKey($"GroupList-{school}"));
  398. if (response.Status == 200)
  399. {
  400. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  401. var list = jsonDocument.RootElement.ToObject<GroupList>();
  402. if (list.type.Equals("teach") && list.school.Equals(school))
  403. {
  404. if (infos.Any())
  405. {
  406. infos.ToList().ForEach(x => {
  407. if (!list.members.Where(z => z.type == 1).Select(x => x.id).Contains(x.id))
  408. {
  409. GroupListService.JoinList(list, x.id , 1 , school);
  410. }
  411. });
  412. }
  413. if (stus.Any())
  414. {
  415. stus.ToList().ForEach(x => {
  416. if (!list.members.Where(z => z.type == 2).Select(x => x.id).Contains(x.id)) {
  417. GroupListService.JoinList(list, x.id ,2 , school);
  418. }
  419. });
  420. }
  421. list = await GroupListService.CheckListNo(list, _azureCosmos, _dingDing, _option);
  422. list = await GroupListService.UpsertList(list, _azureCosmos, _configuration, _serviceBus);
  423. return Ok(new { unexist_student, unexist_tmdids, import_list=list });
  424. }
  425. else {
  426. return Ok(new { error = 3, msg = $"名单类型不是教学班或者不是当前学校的名单!{list.type},{list.school}" });
  427. }
  428. }
  429. else
  430. {
  431. return Ok(new { error = 2, msg = "名单错误!" });
  432. }
  433. }
  434. /// <summary>
  435. /// 移除教学班学生
  436. /// </summary>
  437. /// <param name="request"></param>
  438. /// <returns></returns>
  439. [ProducesDefaultResponseType]
  440. [HttpPost("remove-teach-members")]
  441. [ApiToken(Auth = "1208", Name = "移除教学班学生", RW = "W", Limit = false)]
  442. public async Task<IActionResult> RemoveTeachMembers(JsonElement json) {
  443. var (id, school) = HttpContext.GetApiTokenInfo();
  444. json.TryGetProperty("stuids", out JsonElement _stuids);
  445. json.TryGetProperty("tmdids", out JsonElement _tmdids);
  446. if (json.TryGetProperty("groupId", out JsonElement _groupId)) { return Ok(new { error = 1, msg = "名单错误!" }); }
  447. List<string> stuids = null;
  448. if (_stuids.ValueKind.Equals(JsonValueKind.Array)) {
  449. stuids = _stuids.ToObject<List<string>>();
  450. }
  451. List<string> tmdids = null;
  452. if (_tmdids.ValueKind.Equals(JsonValueKind.Array))
  453. {
  454. tmdids = _tmdids.ToObject<List<string>>();
  455. }
  456. if (tmdids.Any() || stuids.Any())
  457. {
  458. // School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  459. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{_groupId}", new PartitionKey($"GroupList-{school}"));
  460. if (response.Status == 200)
  461. {
  462. JsonDocument document= JsonDocument.Parse(response.Content);
  463. var list= document.RootElement.Deserialize<GroupList>();
  464. List<string> remove_tmdids = new List<string>();
  465. if (tmdids.Any()) {
  466. tmdids.ForEach(x => {
  467. int len= list.members.RemoveAll(z => z.id.Equals(x) && z.type == 1);
  468. if (len > 0) {
  469. remove_tmdids.Add(x);
  470. }
  471. });
  472. }
  473. List<string> remove_stuids = new List<string>();
  474. if (stuids.Any())
  475. {
  476. stuids.ForEach(x => {
  477. int len = list.members.RemoveAll(z => z.id.Equals(x) && z.type == 2);
  478. if (len > 0)
  479. {
  480. remove_stuids.Add(x);
  481. }
  482. });
  483. }
  484. list = await GroupListService.UpsertList(list, _azureCosmos, _configuration, _serviceBus);
  485. return Ok(new { remove_stuids, remove_tmdids, list });
  486. }
  487. else {
  488. return Ok(new { error = 2, msg = "名单错误!" });
  489. }
  490. }
  491. else
  492. {
  493. return Ok(new { error = 2, msg = "移除的名单人员为空!" });
  494. }
  495. }
  496. }
  497. }