GroupListController.cs 28 KB

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