GroupListController.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  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(JsonElement json) {
  290. var (_, school) = HttpContext.GetApiTokenInfo();
  291. if (!json.TryGetProperty("groupLists", out JsonElement _groupLists) || !_groupLists.ValueKind.Equals(JsonValueKind.Array)) { return Ok(new { error=1,msg="名单对象不存在"}); }
  292. var groupListsDto = _groupLists.ToObject<List<GroupListDto>>();
  293. List<GroupList> groupLists = new List<GroupList>(); ;
  294. List<Dictionary<string, string>> errorData = new List<Dictionary<string, string>>();
  295. if (groupListsDto != null && groupListsDto.Valid().isVaild)
  296. {
  297. School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  298. foreach (var list in groupListsDto) {
  299. Period period = data.period.Find(x => x.id.Equals($"{list.periodId}"));
  300. if (period != null)
  301. {
  302. GroupList groupList = null;
  303. if (string.IsNullOrWhiteSpace(list.id))
  304. {
  305. groupList = new GroupList()
  306. {
  307. pk = "GroupList",
  308. id = Guid.NewGuid().ToString(),
  309. code = $"GroupList-{school}",
  310. name = list.name,
  311. periodId = list.periodId,
  312. scope = "school",
  313. school = school,
  314. type = "teach",
  315. year = list.year,
  316. froms = 3
  317. };
  318. groupList = await GroupListService.CheckListNo(groupList, _azureCosmos, _dingDing, _option);
  319. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(groupList, new PartitionKey(groupList.code));
  320. }
  321. else
  322. {
  323. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{list.id}", new PartitionKey($"GroupList-{school}"));
  324. if (response.Status == 200)
  325. {
  326. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  327. groupList = jsonDocument.RootElement.ToObject<GroupList>();
  328. groupList.name = string.IsNullOrWhiteSpace(list.name) ? groupList.name : list.name;
  329. groupList.periodId = string.IsNullOrWhiteSpace(list.periodId) ? groupList.periodId : list.periodId;
  330. groupList.school = school;
  331. groupList.scope = "school";
  332. groupList.froms = 3;
  333. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).ReplaceItemAsync(groupList, groupList.id, new PartitionKey(groupList.code));
  334. }
  335. else
  336. {
  337. groupList = new GroupList()
  338. {
  339. id = Guid.NewGuid().ToString(),
  340. code = $"GroupList-{school}",
  341. name = list.name,
  342. periodId = list.periodId,
  343. scope = "school",
  344. school = school,
  345. type = "teach",
  346. year = list.year,
  347. froms = 3
  348. };
  349. groupList = await GroupListService.CheckListNo(groupList, _azureCosmos, _dingDing, _option);
  350. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.School).CreateItemAsync(groupList, new PartitionKey(groupList.code));
  351. }
  352. }
  353. if (groupList != null) {
  354. groupLists.Add(groupList);
  355. }
  356. }
  357. else
  358. {
  359. errorData.Add(new Dictionary<string, string> { { "group",list.name}, { "periodId",list.periodId} });
  360. // return Ok(new { error = 2, msg ="学段不存在!" });
  361. }
  362. }
  363. }
  364. else
  365. {
  366. return Ok(new { error = 3, msg = groupListsDto.Valid() });
  367. }
  368. return Ok(new { groupLists, errorData });
  369. }
  370. /// <summary>
  371. /// 导入教学班学生
  372. /// </summary>
  373. /// <param name="request"></param>
  374. /// <returns></returns>
  375. [ProducesDefaultResponseType]
  376. [HttpPost("import-teach-members")]
  377. [ApiToken(Auth = "1207", Name = "导入教学班学生", RW = "W", Limit = false)]
  378. public async Task<IActionResult> ImportTeachMembers(JsonElement json)
  379. {
  380. var (id, school) = HttpContext.GetApiTokenInfo();
  381. if (!json.TryGetProperty("groupId", out JsonElement _groupId)) { return Ok(new { error = 2, msg = "名单错误!" }); }
  382. if(!json.TryGetProperty("members", out JsonElement _members)) return Ok(new { error = 1, msg = "名单列表格式错误" });
  383. if (_members.ValueKind.Equals(JsonValueKind.Array))
  384. {
  385. //School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  386. List<Member> members = _members.ToObject<List<Member>>();
  387. ValidResult valid = members.Valid();
  388. if (valid.isVaild)
  389. {
  390. var tmds = members.Where(x => x.type == 1);
  391. var stus = members.Where(x => x.type == 2);
  392. List<Student> students = await StudentService.GeStudentData(_azureCosmos, school, stus?.Select(x => x.id));
  393. List<TmdInfo> infos = null;
  394. string tmdstr = "";
  395. try
  396. {
  397. var content = new StringContent(tmds.Select(x => x.id).ToJsonString(), Encoding.UTF8, "application/json");
  398. tmdstr = await _coreAPIHttpService.GetUserInfos(content);
  399. infos = tmdstr.ToObject<List<TmdInfo>>();
  400. }
  401. catch (Exception ex)
  402. {
  403. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location}用户转换失败:{_coreAPIHttpService.options.Get("Default").url}{tmdstr}\n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  404. //return Ok(new { error =3, msg = "醍摩豆ID验证错误!" });
  405. }
  406. var unexist_student = stus.Select(x => x.id).Except(students.Select(y => y.id));
  407. var unexist_tmdids = tmds.Select(x => x.id).Except(infos.Select(y => y.id));
  408. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{_groupId}", new PartitionKey($"GroupList-{school}"));
  409. if (response.Status == 200)
  410. {
  411. JsonDocument jsonDocument = JsonDocument.Parse(response.Content);
  412. var list = jsonDocument.RootElement.ToObject<GroupList>();
  413. if (list.type.Equals("teach") && list.school.Equals(school))
  414. {
  415. if (infos.Any())
  416. {
  417. infos.ToList().ForEach(x => {
  418. if (!list.members.Where(z => z.type == 1).Select(x => x.id).Contains(x.id))
  419. {
  420. GroupListService.JoinList(list, x.id , 1 , school);
  421. }
  422. });
  423. }
  424. if (stus.Any())
  425. {
  426. stus.ToList().ForEach(x => {
  427. if (!list.members.Where(z => z.type == 2).Select(x => x.id).Contains(x.id)) {
  428. GroupListService.JoinList(list, x.id ,2 , school);
  429. }
  430. });
  431. }
  432. list = await GroupListService.CheckListNo(list, _azureCosmos, _dingDing, _option);
  433. list = await GroupListService.UpsertList(list, _azureCosmos, _configuration, _serviceBus);
  434. return Ok(new { unexist_student, unexist_tmdids, import_list=list });
  435. }
  436. else {
  437. return Ok(new { error = 3, msg = $"名单类型不是教学班或者不是当前学校的名单!{list.type},{list.school}" });
  438. }
  439. }
  440. else
  441. {
  442. return Ok(new { error = 2, msg = "名单错误!" });
  443. }
  444. }
  445. else
  446. {
  447. return Ok(new { error = 3, msg = valid });
  448. }
  449. }
  450. else
  451. {
  452. return Ok(new { error = 1, msg = "名单列表格式错误" });
  453. }
  454. }
  455. /// <summary>
  456. /// 移除教学班学生
  457. /// </summary>
  458. /// <param name="request"></param>
  459. /// <returns></returns>
  460. [ProducesDefaultResponseType]
  461. [HttpPost("remove-teach-members")]
  462. [ApiToken(Auth = "1208", Name = "移除教学班学生", RW = "W", Limit = false)]
  463. public async Task<IActionResult> RemoveTeachMembers(JsonElement json) {
  464. var (id, school) = HttpContext.GetApiTokenInfo();
  465. json.TryGetProperty("stuids", out JsonElement _stuids);
  466. json.TryGetProperty("tmdids", out JsonElement _tmdids);
  467. if (json.TryGetProperty("groupId", out JsonElement _groupId)) { return Ok(new { error = 1, msg = "名单错误!" }); }
  468. List<string> stuids = null;
  469. if (_stuids.ValueKind.Equals(JsonValueKind.Array)) {
  470. stuids = _stuids.ToObject<List<string>>();
  471. }
  472. List<string> tmdids = null;
  473. if (_tmdids.ValueKind.Equals(JsonValueKind.Array))
  474. {
  475. tmdids = _tmdids.ToObject<List<string>>();
  476. }
  477. if (tmdids.Any() || stuids.Any())
  478. {
  479. // School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
  480. Azure.Response response = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync($"{_groupId}", new PartitionKey($"GroupList-{school}"));
  481. if (response.Status == 200)
  482. {
  483. JsonDocument document= JsonDocument.Parse(response.Content);
  484. var list= document.RootElement.Deserialize<GroupList>();
  485. List<string> remove_tmdids = new List<string>();
  486. if (tmdids.Any()) {
  487. tmdids.ForEach(x => {
  488. int len= list.members.RemoveAll(z => z.id.Equals(x) && z.type == 1);
  489. if (len > 0) {
  490. remove_tmdids.Add(x);
  491. }
  492. });
  493. }
  494. List<string> remove_stuids = new List<string>();
  495. if (stuids.Any())
  496. {
  497. stuids.ForEach(x => {
  498. int len = list.members.RemoveAll(z => z.id.Equals(x) && z.type == 2);
  499. if (len > 0)
  500. {
  501. remove_stuids.Add(x);
  502. }
  503. });
  504. }
  505. list = await GroupListService.UpsertList(list, _azureCosmos, _configuration, _serviceBus);
  506. return Ok(new { remove_stuids, remove_tmdids, list });
  507. }
  508. else {
  509. return Ok(new { error = 2, msg = "名单错误!" });
  510. }
  511. }
  512. else
  513. {
  514. return Ok(new { error = 2, msg = "移除的名单人员为空!" });
  515. }
  516. }
  517. }
  518. }