GroupListService.cs 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121
  1. using Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. using TEAMModelOS.SDK.DI;
  6. using TEAMModelOS.SDK.Extension;
  7. using TEAMModelOS.SDK.Models.Cosmos.Common;
  8. using HTEXLib.COMM.Helpers;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json;
  12. using TEAMModelOS.Models;
  13. using Azure.Messaging.ServiceBus;
  14. using Microsoft.Extensions.Configuration;
  15. using TEAMModelOS.SDK.Models.Service;
  16. using System.Text.RegularExpressions;
  17. using TEAMModelOS.SDK.Models;
  18. using System.Net.Http;
  19. namespace TEAMModelOS.SDK
  20. {
  21. public class GroupListService
  22. {
  23. /// <summary>
  24. /// 查询名单成员所在的名单列表,
  25. /// </summary>
  26. /// <param name="_coreAPIHttpService"></param>
  27. /// <param name="client"></param>
  28. /// <param name="_dingDing"></param>
  29. /// <param name="memberId">成员id </param>
  30. /// <param name="memberType">成员类型</param>
  31. /// <param name="school">成员所在的学校 ,可为空</param>
  32. /// <param name="groupTypes">过滤名单的类型集合,不传则是全部</param>
  33. /// <returns></returns>
  34. public static async Task<List<GroupListGrp>> GetMemberInGroupList(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, DingDing _dingDing, string memberId, int memberType, string school,List<string> groupTypes) {
  35. List<GroupListGrp> groupLists = new List<GroupListGrp>();
  36. if (groupTypes.IsEmpty() || groupTypes.Contains("class")) {
  37. if (string.IsNullOrWhiteSpace(school) && memberType == 2)
  38. {
  39. Student student = null;
  40. try
  41. {
  42. student = await client.GetContainer(Constant.TEAMModelOS, Constant.Student).ReadItemAsync<Student>(memberId, new PartitionKey($"Base-{school}"));
  43. }
  44. catch (CosmosException ex) when (ex.Status == 404)
  45. {
  46. return null;
  47. }
  48. if (!string.IsNullOrWhiteSpace(student.classId))
  49. {
  50. try
  51. {
  52. Class clazz = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<Class>(memberId, new PartitionKey($"Class-{school}"));
  53. GroupListGrp groupList = new GroupListGrp
  54. {
  55. id = clazz.id,
  56. code = $"GroupList-{clazz.school}",
  57. name = clazz.name,
  58. periodId = clazz.periodId,
  59. pk = "GroupList",
  60. year = clazz.year,
  61. school = clazz.school,
  62. scope = "school",
  63. type = "class",
  64. no = clazz.no,
  65. leader = clazz.teacher?.id,
  66. };
  67. groupLists.Add(groupList);
  68. }
  69. catch (CosmosException ex) when (ex.Status == 404)
  70. {
  71. }
  72. }
  73. }
  74. }
  75. string SummarySql = " c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.leader ";
  76. if (groupTypes.IsEmpty() || groupTypes.Contains("teach")) {
  77. //教学班
  78. StringBuilder teachsql = new StringBuilder();
  79. if (string.IsNullOrWhiteSpace(school) && memberType == 2) {
  80. teachsql.Append($"SELECT distinct {SummarySql} FROM c join m in c.members where c.type='teach' and m.id={memberId} and m.school={school} and m.type=2 ");
  81. }
  82. if (memberType == 1)
  83. {
  84. teachsql.Append($"SELECT distinct {SummarySql} FROM c join m in c.members where c.type='teach' and m.id={memberId} and m.type=1 ");
  85. }
  86. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  87. GetItemQueryIterator<GroupList>(queryText: teachsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  88. {
  89. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  90. groupLists.Add(new GroupListGrp(item, groupName));
  91. }
  92. //个人名单
  93. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, Constant.Teacher).
  94. GetItemQueryIterator<GroupList>(queryText: teachsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList") }))
  95. {
  96. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  97. groupLists.Add(new GroupListGrp(item, groupName));
  98. }
  99. }
  100. if (!string.IsNullOrWhiteSpace(school) && (groupTypes.IsEmpty() || groupTypes.Contains("research")))
  101. {
  102. //教研组
  103. StringBuilder teachsql = new StringBuilder();
  104. teachsql.Append($"SELECT distinct {SummarySql} FROM c join m in c.members where c.type='research' and m.id={memberId} and m.type=1 ");
  105. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  106. GetItemQueryIterator<GroupList>(queryText: teachsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  107. {
  108. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  109. groupLists.Add(new GroupListGrp(item, groupName));
  110. }
  111. }
  112. if (!string.IsNullOrWhiteSpace(school) && (groupTypes.IsEmpty() || groupTypes.Contains("yxtrain")))
  113. {
  114. //研修名单
  115. StringBuilder teachsql = new StringBuilder();
  116. teachsql.Append($"SELECT distinct {SummarySql} FROM c join m in c.members where c.type='yxtrain' and m.id={memberId} and m.type=1 ");
  117. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  118. GetItemQueryIterator<GroupList>(queryText: teachsql.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"GroupList-{school}") }))
  119. {
  120. HashSet<string> groupName = item.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  121. groupLists.Add(new GroupListGrp(item, groupName));
  122. }
  123. }
  124. return groupLists;
  125. }
  126. public static async Task<(int status, GroupList stuList)> CodeJoinList(CosmosClient client, string _stuListNo, string userid, int type, string school)
  127. {
  128. var queryNo = $"SELECT value(c) FROM c where c.no ='{_stuListNo}'";
  129. (int status, GroupList stuList) data = (-1, null);
  130. if (!string.IsNullOrEmpty(school))
  131. {
  132. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupList>(queryText: queryNo,
  133. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  134. {
  135. data = JoinList(item, userid, type, school);
  136. break;
  137. }
  138. }
  139. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<GroupList>(queryText: queryNo,
  140. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  141. {
  142. data = JoinList(item, userid, type, school);
  143. break;
  144. }
  145. return data;
  146. }
  147. public static (int status, GroupList stuList) JoinList(GroupList stuList, string userid, int type, string school)
  148. {
  149. int status = -1;
  150. if (!string.IsNullOrWhiteSpace(stuList.school) && !string.IsNullOrWhiteSpace(school))
  151. {
  152. if (!stuList.school.Equals(school))
  153. {
  154. status = 3;//不允许跨校加入名单
  155. return (status, stuList);
  156. }
  157. }
  158. string irs = string.Empty;
  159. List<string> irsOrder = stuList.members.Select(x => x.irs)?.Where(y => !string.IsNullOrEmpty(y) && Regex.IsMatch(y, @"^\d*$")).OrderBy(x => int.Parse(x)).ToList();
  160. if (!irsOrder.Contains("0"))
  161. {
  162. irsOrder.Insert(0, "0");
  163. }
  164. if (irsOrder != null)
  165. {
  166. if (!irsOrder.Contains("0"))
  167. {
  168. irsOrder.Insert(0, "0");
  169. }
  170. }
  171. else { irsOrder = new List<string>() { "0" }; }
  172. for (int i = 0; i < irsOrder.Count; i++)
  173. {
  174. irs = $"{int.Parse(irsOrder[i]) + 1}";
  175. int index = i + 1;
  176. if (index <= irsOrder.Count - 1)
  177. {
  178. if (!irs.Equals(irsOrder[index]))
  179. {
  180. break;
  181. }
  182. }
  183. }
  184. if (string.IsNullOrEmpty($"{userid}"))
  185. {
  186. //加入学生或醍摩豆ID为空
  187. status = 1;
  188. }
  189. else
  190. {
  191. if (type == 1)
  192. {
  193. var student = stuList.members.Find(x => x.type == 1 && x.id.Equals(userid));
  194. if (student != null)
  195. {
  196. //重复加入
  197. status = 2;
  198. }
  199. else
  200. {
  201. //加入成功
  202. status = 0;
  203. stuList.members.Add(new Member { id = userid, type = type, irs = irs, no = irs });
  204. }
  205. }
  206. else if (type == 2)
  207. {
  208. var student = stuList.members.Find(x => x.type == 2 && x.id.Equals(userid) && x.code.Equals(school));
  209. if (student != null)
  210. {
  211. //重复加入
  212. status = 2;
  213. }
  214. else
  215. {
  216. status = 0;
  217. stuList.members.Add(new Member { id = userid, code = school, type = type, irs = irs, no = irs });
  218. }
  219. }
  220. }
  221. return (status, stuList);
  222. }
  223. public static async Task<GroupList> UpsertList(GroupList list, AzureCosmosFactory _azureCosmos, IConfiguration _configuration, AzureServiceBusFactory _serviceBus)
  224. {
  225. bool isnew = false;
  226. var client = _azureCosmos.GetCosmosClient();
  227. if (string.IsNullOrEmpty(list.id))
  228. {
  229. list.id = Guid.NewGuid().ToString();
  230. isnew = true;
  231. }
  232. string tbname = list.scope.Equals("private") ? "Teacher" : "School";
  233. var tmembers = list.members.Where(x => x.type == 1);
  234. var smembers = list.members.Where(x => x.type == 2);
  235. list.scount = smembers.Count();
  236. list.tcount = tmembers.Count();
  237. //if (smembers.Count() > 0 && smembers.Select(x => x.code).ToHashSet().Count()>=2) {
  238. // //处理移除多个学校的名单,只保留一个学校的。
  239. // if (string.IsNullOrWhiteSpace(list.school))
  240. // {
  241. // HashSet<string> codes = smembers.Select(x => x.code).ToHashSet();
  242. // list.school = codes.First();
  243. // codes.Remove(codes.First());
  244. // list.members.RemoveAll(x => codes.Contains(x.code));
  245. // }
  246. // else {
  247. // list.members.RemoveAll(x => !x.code.Equals(list.school));
  248. // }
  249. //}
  250. if (string.IsNullOrWhiteSpace(list.school) && smembers.Count() >= 1)
  251. {
  252. list.school = smembers.First().code;
  253. }
  254. if (!string.IsNullOrWhiteSpace(list.school) && smembers.Count() == 0 && list.scope.Equals("private"))
  255. {
  256. list.school = null;
  257. }
  258. //学生名单,教研组会触发活动中间表刷新
  259. if (list.type.Equals("teach") || list.type.Equals("research") || list.type.Equals("yxtrain") || list.type.Equals("activity"))
  260. {
  261. GroupChange change = new GroupChange()
  262. {
  263. type = list.type,
  264. listid = list.id,
  265. scope = list.scope,
  266. originCode = list.school,
  267. school = list.school,
  268. creatorId = list.creatorId
  269. };
  270. GroupList oldList = null;
  271. if (!isnew)
  272. {
  273. try
  274. {
  275. oldList = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<GroupList>(list.id, new PartitionKey(list.code));
  276. }
  277. catch (CosmosException)
  278. {
  279. oldList = null;
  280. }
  281. }
  282. if (list.members.IsNotEmpty() && (oldList == null || !oldList.members.IsNotEmpty()))
  283. {
  284. //加入的
  285. var tmdids = list.members.FindAll(x => x.type == 1);
  286. if (tmdids.IsNotEmpty())
  287. {
  288. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  289. {
  290. change.tchjoin.AddRange(tmdids);
  291. }
  292. else
  293. {
  294. change.tmdjoin.AddRange(tmdids);
  295. }
  296. }
  297. var stuids = list.members.FindAll(x => x.type == 2);
  298. if (stuids.IsNotEmpty())
  299. {
  300. change.stujoin.AddRange(stuids);
  301. }
  302. }
  303. else
  304. {
  305. if (list.members.IsNotEmpty())
  306. {
  307. var tmdids = list.members.FindAll(x => x.type == 1);
  308. var oldtmdids = oldList.members.FindAll(x => x.type == 1);
  309. //取各自的差集
  310. //新=》旧差集,表示新增
  311. var jointmdid = tmdids.Select(x => x.id).Except(oldtmdids.Select(y => y.id)).ToList();
  312. //旧=》新差集,表示离开
  313. var leavetmdid = oldtmdids.Select(x => x.id).Except(tmdids.Select(y => y.id)).ToList();
  314. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  315. {
  316. change.tchjoin.AddRange(tmdids.Where(x => jointmdid.Exists(y => y.Equals(x.id))));
  317. change.tchleave.AddRange(oldtmdids.Where(x => leavetmdid.Exists(y => y.Equals(x.id))));
  318. }
  319. else
  320. {
  321. change.tmdjoin.AddRange(tmdids.Where(x => jointmdid.Exists(y => y.Equals(x.id))));
  322. change.tmdleave.AddRange(oldtmdids.Where(x => leavetmdid.Exists(y => y.Equals(x.id))));
  323. }
  324. var stuids = list.members.FindAll(x => x.type == 2);
  325. var oldstuids = oldList.members.FindAll(x => x.type == 2);
  326. var joinstudent = stuids.Select(x => (x.id, x.code)).Except(oldstuids.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  327. var leavestudent = oldstuids.Select(x => (x.id, x.code)).Except(stuids.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  328. change.stujoin.AddRange(stuids.Where(x => joinstudent.Exists(y => y.id.Equals(x.id) && y.code.Equals(x.code))));
  329. change.stuleave.AddRange(oldstuids.Where(x => leavestudent.Exists(y => y.id.Equals(x.id) && y.code.Equals(x.code))));
  330. }
  331. else
  332. {
  333. //离开的
  334. if (oldList != null)
  335. {
  336. var tmdids = oldList.members.FindAll(x => x.type == 1);
  337. if (tmdids.IsNotEmpty())
  338. {
  339. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  340. {
  341. change.tchleave.AddRange(tmdids);
  342. }
  343. else
  344. {
  345. change.tmdleave.AddRange(tmdids);
  346. }
  347. }
  348. var stuids = oldList.members.FindAll(x => x.type == 2);
  349. if (stuids.IsNotEmpty())
  350. {
  351. change.stuleave.AddRange(stuids);
  352. }
  353. }
  354. }
  355. }
  356. if (change.tmdjoin.Count != 0 || change.tmdleave.Count != 0 || change.stujoin.Count != 0 || change.stuleave.Count != 0
  357. || change.tchjoin.Count != 0 || change.tchleave.Count != 0)
  358. {
  359. var messageChange = new ServiceBusMessage(change.ToJsonString());
  360. messageChange.ApplicationProperties.Add("name", "GroupChange");
  361. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  362. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  363. }
  364. }
  365. await client.GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(list, new PartitionKey(list.code));
  366. return list;
  367. }
  368. public static async Task<GroupList> CheckListNo(GroupList list, AzureCosmosFactory _azureCosmos, DingDing _dingDing, Option _option)
  369. {
  370. try
  371. {
  372. var client = _azureCosmos.GetCosmosClient();
  373. if (string.IsNullOrEmpty(list.no))
  374. {
  375. list.no = $"{Utils.CreatSaltString(6, "123456789")}";
  376. for (int i = 0; i < 10; i++)
  377. {
  378. List<string> noStus = new List<string>();
  379. var queryNo = $"SELECT c.no FROM c where c.no ='{list.no}'";
  380. if (list.scope.Equals("school"))
  381. {
  382. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: queryNo,
  383. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{list.code}") }))
  384. {
  385. using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
  386. if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  387. {
  388. var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
  389. while (accounts.MoveNext())
  390. {
  391. JsonElement account = accounts.Current;
  392. noStus.Add(account.GetProperty("no").GetString());
  393. }
  394. }
  395. }
  396. }
  397. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: queryNo,
  398. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey("GroupList") }))
  399. {
  400. using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
  401. if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  402. {
  403. var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
  404. while (accounts.MoveNext())
  405. {
  406. JsonElement account = accounts.Current;
  407. noStus.Add(account.GetProperty("no").GetString());
  408. }
  409. }
  410. }
  411. if (noStus.Count == 0)
  412. {
  413. break;
  414. }
  415. else
  416. {
  417. if (i == 9)
  418. {
  419. string msg = $"OS,{_option.Location},school/course/upsert-list()\n 编号生成异常,重复生成次数超过10次";
  420. await _dingDing.SendBotMsg(msg, GroupNames.醍摩豆服務運維群組);
  421. throw new Exception(msg);
  422. }
  423. else
  424. {
  425. list.no = $"{Utils.CreatSaltString(6, "123456789")}";
  426. }
  427. }
  428. }
  429. }
  430. }
  431. catch (Exception ex)
  432. {
  433. }
  434. return list;
  435. }
  436. public static async Task<List<GroupListDto>> GetGroupListListids(CosmosClient client, DingDing _dingDing, List<string> classes, string school,
  437. string SummarySql = " c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.leader ")
  438. {
  439. List<GroupListDto> groupLists = null;
  440. if (classes.Count == 1 && classes.First().Equals("TeacherAll") && !string.IsNullOrEmpty(school))
  441. {
  442. //默认的教研组
  443. GroupListDto groupList = new GroupListDto
  444. {
  445. id = "TeacherAll",
  446. name = "TeacherAll",
  447. code = $"GroupList-{school}",
  448. school = school,
  449. scope = "school",
  450. type = "yxtrain",
  451. };
  452. groupLists = new List<GroupListDto> { groupList };
  453. }
  454. else
  455. {
  456. Dictionary<string, List<GroupListDto>> groups = new Dictionary<string, List<GroupListDto>>();
  457. //List<Student> students = new List<Student>();
  458. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  459. if (!string.IsNullOrEmpty(school))
  460. {
  461. List<GroupListDto> schoolList = new List<GroupListDto>();
  462. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupListDto>(queryText: $"select {SummarySql} from c where c.id in ({sql})",
  463. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  464. {
  465. schoolList.Add(item);
  466. }
  467. if (schoolList.IsNotEmpty())
  468. {
  469. groups.Add("School", schoolList);
  470. }
  471. //取差集,减少二次搜寻
  472. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  473. if (classes.IsNotEmpty())
  474. {
  475. if (!groupLists.IsNotEmpty())
  476. {
  477. groupLists = new List<GroupListDto>();
  478. }
  479. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  480. //搜寻没有关联学生的行政班
  481. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupListDto>(queryText: $"select c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.teacher.id as leader from c where c.id in ({insql})",
  482. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  483. {
  484. ///行政班(学生搜寻classId动态返回)class
  485. GroupListDto group = new GroupListDto
  486. {
  487. id = item.id,
  488. code = $"GroupList-{school}",
  489. name = item.name,
  490. periodId = item.periodId,
  491. scope = "school",
  492. school = school,
  493. type = "class",
  494. year = item.year,
  495. leader = item.leader,
  496. no = item.no,
  497. pk = "GroupList",
  498. };
  499. groupLists.Add(group);
  500. }
  501. //取差集,减少二次搜寻
  502. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  503. }
  504. }
  505. if (classes.IsNotEmpty())
  506. {
  507. List<GroupListDto> privateList = new List<GroupListDto>();
  508. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  509. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<GroupListDto>(queryText: $"select {SummarySql} from c where c.id in ({sql})",
  510. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  511. {
  512. privateList.Add(item);
  513. //if (string.IsNullOrWhiteSpace(school))
  514. //{
  515. // privateList.Add(item);
  516. //}
  517. //else
  518. //{
  519. // if (!string.IsNullOrWhiteSpace(item.school))
  520. // {
  521. // if (item.school.Equals(school))
  522. // {
  523. // privateList.Add(item);
  524. // }
  525. // }
  526. // else
  527. // {
  528. // privateList.Add(item);
  529. // }
  530. //}
  531. }
  532. if (privateList.IsNotEmpty())
  533. {
  534. groups.Add("Teacher", privateList);
  535. }
  536. }
  537. if (groups.Count != 0)
  538. {
  539. if (groupLists.IsNotEmpty())
  540. {
  541. groupLists.AddRange(groups.SelectMany(x => x.Value).ToList());
  542. }
  543. else
  544. {
  545. groupLists = groups.SelectMany(x => x.Value).ToList();
  546. }
  547. }
  548. }
  549. return groupLists;
  550. }
  551. public static async Task<(List<RMember>, List<RGroupList> groups)> GetStutmdidListids(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, DingDing _dingDing, List<string> classes, string school, List<(string, List<string>)> groupids = null)
  552. {
  553. List<RMember> members = new List<RMember>();
  554. List<RGroupList> groupLists = new List<RGroupList>();
  555. if (classes != null)
  556. {
  557. classes.RemoveAll(x => x == null);
  558. }
  559. if (classes == null || classes.Count <= 0)
  560. {
  561. return (members, groupLists);
  562. }
  563. if (classes.Count == 1 && classes.First().Equals("TeacherAll") && !string.IsNullOrEmpty(school))
  564. {
  565. //默认的教研组
  566. members = new List<RMember>();
  567. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdInfo>(queryText: $"SELECT c.id,c.name,c.picture FROM c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  568. {
  569. RMember member = new RMember
  570. {
  571. id = item.id,
  572. name = item.name,
  573. picture = item.picture,
  574. type = 1,
  575. };
  576. members.Add(member);
  577. }
  578. RGroupList groupList = new RGroupList
  579. {
  580. id = "TeacherAll",
  581. name = "TeacherAll",
  582. code = $"GroupList-{school}",
  583. school = school,
  584. scope = "school",
  585. type = "TeacherAll",
  586. members = members
  587. };
  588. groupLists = new List<RGroupList> { groupList };
  589. }
  590. else
  591. {
  592. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  593. List<Student> students = new List<Student>();
  594. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  595. if (!string.IsNullOrEmpty(school))
  596. {
  597. List<RGroupList> schoolList = new List<RGroupList>();
  598. string queryText = $"select value(c) from c where c.id in ({sql})";
  599. //await _dingDing.SendBotMsg($"{queryText}",GroupNames.成都开发測試群組);
  600. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: queryText,
  601. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  602. {
  603. schoolList.Add(item);
  604. }
  605. if (schoolList.IsNotEmpty())
  606. {
  607. groups.Add("School", schoolList);
  608. }
  609. //取差集,减少二次搜寻
  610. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  611. if (classes.IsNotEmpty())
  612. {
  613. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  614. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  615. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  616. {
  617. students.Add(item);
  618. }
  619. //取差集,减少二次搜寻
  620. classes = classes.Except(students.Select(y => y.classId)).ToList();
  621. }
  622. if (classes.IsNotEmpty())
  623. {
  624. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  625. //搜寻没有关联学生的行政班
  626. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School")
  627. .GetItemQueryIterator<RGroupList>(queryText: $"select c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.teacher.id as leader from c where c.id in ({insql})",
  628. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  629. {
  630. ///行政班(学生搜寻classId动态返回)class
  631. List<RMember> smembers = new List<RMember>();
  632. RGroupList group = new RGroupList
  633. {
  634. id = item.id,
  635. code = $"GroupList-{school}",
  636. name = item.name,
  637. periodId = item.periodId,
  638. scope = "school",
  639. school = school,
  640. type = "class",
  641. year = item.year,
  642. members = smembers,
  643. scount = smembers.Count,
  644. pk = "GroupList",
  645. leader = item.leader,
  646. no = item.no,
  647. };
  648. groupLists.Add(group);
  649. }
  650. //取差集,减少二次搜寻
  651. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  652. }
  653. }
  654. if (classes.IsNotEmpty())
  655. {
  656. List<RGroupList> privateList = new List<RGroupList>();
  657. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  658. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: $"select value(c) from c where c.id in ({sql})",
  659. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  660. {
  661. privateList.Add(item);
  662. //if (string.IsNullOrWhiteSpace(school))
  663. //{
  664. // privateList.Add(item);
  665. //}
  666. //else
  667. //{
  668. // if (!string.IsNullOrWhiteSpace(item.school))
  669. // {
  670. // if (item.school.Equals(school))
  671. // {
  672. // privateList.Add(item);
  673. // }
  674. // }
  675. // else
  676. // {
  677. // privateList.Add(item);
  678. // }
  679. //}
  680. }
  681. if (privateList.IsNotEmpty())
  682. {
  683. groups.Add("Teacher", privateList);
  684. }
  685. }
  686. foreach (var item in groups)
  687. {
  688. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  689. foreach (var group in list)
  690. {
  691. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school);
  692. members.AddRange(rmembers);
  693. }
  694. }
  695. groupLists.AddRange(groups.SelectMany(x => x.Value).ToList());
  696. if (students.IsNotEmpty())
  697. {
  698. List<string> sqlList = students.Select(x => x.classId).ToList();
  699. string insql = string.Join(",", sqlList.Select(x => $"'{x}'"));
  700. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  701. GetItemQueryIterator<RGroupList>(queryText: $"select c.id,c.code,c.name,c.no,c.periodId,c.scope,c.school,c.creatorId,c.type,c.year,c.tcount,c.scount,c.teacher.id as leader from c where c.id in ({insql})",
  702. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  703. {
  704. ///行政班(学生搜寻classId动态返回)class
  705. List<RMember> smembers = students.Where(x => x.classId.Equals(item.id))
  706. .Select(y => new RMember
  707. {
  708. id = y.id,
  709. code = school,
  710. name = y.name,
  711. type = 2,
  712. picture = y.picture,
  713. no = y.no,
  714. classId = y.classId,
  715. //groupId=y.groupId,
  716. groupName = y.groupName,
  717. irs = y.irs,
  718. }).ToList();
  719. members.AddRange(smembers);
  720. RGroupList group = new RGroupList
  721. {
  722. id = item.id,
  723. code = $"GroupList-{school}",
  724. name = item.name,
  725. periodId = item.periodId,
  726. scope = "school",
  727. school = school,
  728. type = "class",
  729. year = item.year,
  730. members = smembers,
  731. scount = smembers.Count,
  732. no = item.no,
  733. leader = item.leader,
  734. pk = "GroupList"
  735. };
  736. groupLists.Add(group);
  737. }
  738. //去重。
  739. members = members.FindAll(x => x.type == 2).Where((x, i) => members.FindAll(x => x.type == 2).FindIndex(n => n.id.Equals(x.id) && n.code.Equals(x.code)) == i).ToList();
  740. }
  741. }
  742. if (groupids.IsNotEmpty())
  743. {
  744. List<RMember> rmembers = new List<RMember>();
  745. groupLists.ForEach(y => {
  746. (string id, List<string> grpids) = groupids.Find(x => x.Item1.Equals(y.id));
  747. var gpmember = y.members.FindAll(x => !string.IsNullOrEmpty(x.groupName) && grpids.Contains(x.groupName));
  748. if (grpids.Contains("default"))
  749. {
  750. var gpmemberdft = y.members.FindAll(x => string.IsNullOrWhiteSpace(x.groupName));
  751. if (gpmember.IsNotEmpty())
  752. {
  753. gpmember.AddRange(gpmemberdft);
  754. }
  755. else
  756. {
  757. gpmember = gpmemberdft;
  758. }
  759. }
  760. y.members = gpmember;
  761. });
  762. var gpgpmembers = groupLists.SelectMany(x => x.members).ToList();
  763. List<RMember> tmdids = gpgpmembers.FindAll(x => x.type == 1).Where((x, i) => gpgpmembers.FindAll(x => x.type == 1).FindIndex(n => n.id.Equals(x.id)) == i).ToList();
  764. List<RMember> students = gpgpmembers.FindAll(x => x.type == 2).Where((x, i) => gpgpmembers.FindAll(x => x.type == 2).FindIndex(n => n.id.Equals(x.id) && n.code.Equals(x.code)) == i).ToList();
  765. if (tmdids.IsNotEmpty())
  766. {
  767. rmembers.AddRange(tmdids);
  768. }
  769. if (students.IsNotEmpty())
  770. {
  771. rmembers.AddRange(students);
  772. }
  773. return (rmembers, groupLists);
  774. }
  775. else
  776. {
  777. return (members, groupLists);
  778. }
  779. }
  780. public static async Task<List<RGroupList>> GetGroupListMemberByType(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, string type, List<string> scopes, string school, DingDing _dingDing)
  781. {
  782. StringBuilder sql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='{type}'");
  783. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  784. if (scopes.Contains("school"))
  785. {
  786. if (!string.IsNullOrEmpty(school))
  787. {
  788. List<RGroupList> groupLists = new List<RGroupList>();
  789. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  790. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  791. {
  792. groupLists.Add(item);
  793. }
  794. groups.Add("School", groupLists);
  795. }
  796. }
  797. else if (scopes.Contains("private"))
  798. {
  799. List<RGroupList> groupLists = new List<RGroupList>();
  800. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  801. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  802. {
  803. groupLists.Add(item);
  804. //if (string.IsNullOrWhiteSpace(school))
  805. //{
  806. // groupLists.Add(item);
  807. //}
  808. //else
  809. //{
  810. // if (!string.IsNullOrWhiteSpace(item.school))
  811. // {
  812. // if (item.school.Equals(school))
  813. // {
  814. // groupLists.Add(item);
  815. // }
  816. // }
  817. // else {
  818. // groupLists.Add(item);
  819. // }
  820. //}
  821. }
  822. groups.Add("Teacher", groupLists);
  823. }
  824. foreach (var item in groups)
  825. {
  826. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  827. foreach (var group in list)
  828. {
  829. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school);
  830. }
  831. }
  832. var lists = groups.SelectMany(x => x.Value).ToList();
  833. return lists;
  834. }
  835. public static async Task<(List<RGroupList> groups, List<RMember> members)> GetGroupListMemberInfo(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, string type, List<RGroupList> groups, string groupTbname, DingDing _dingDing, string school)
  836. {
  837. try
  838. {
  839. HashSet<RGroupList> changes = new HashSet<RGroupList>();
  840. var members = groups.SelectMany(y => y.members).ToList();
  841. //去重
  842. List<RMember> tmdids = members.FindAll(x => x.type == 1).Where((x, i) => members.FindAll(x => x.type == 1).FindIndex(n => n.id.Equals(x.id)) == i).ToList();
  843. List<RMember> students = members.FindAll(x => x.type == 2).Where((x, i) => members.FindAll(x => x.type == 2).FindIndex(n => n.id.Equals(x.id) && n.code.Equals(x.code)) == i).ToList();
  844. var stu = students.GroupBy(x => x.code).Select(y => new { key = y.Key, list = y.ToList() });
  845. List<Student> studentsData = new List<Student>();
  846. if (stu != null)
  847. {
  848. foreach (var item in stu)
  849. {
  850. var ids = item.list.Select(x => x.id).ToList();
  851. if (ids.IsNotEmpty())
  852. {
  853. StringBuilder stuSql = new StringBuilder($"SELECT distinct c.name,c.id,c.code,c.picture,c.no,c.irs,c.classId FROM c ");
  854. string insql = string.Join(",", ids.Select(x => $"'{x}'"));
  855. stuSql.Append($"where c.id in ({insql})");
  856. await foreach (var student in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: stuSql.ToString(),
  857. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{item.key}") }))
  858. {
  859. student.schoolId = item.key;
  860. studentsData.Add(student);
  861. }
  862. }
  863. }
  864. var unexist_student = students.Select(x => (x.id, x.code)).Except(studentsData.Select(y => (y.id, y.schoolId)), new CompareIdCode()).ToList();
  865. groups.ForEach(x =>
  866. {
  867. int item = x.members.RemoveAll(y => y.type == 2 && unexist_student.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  868. if (item > 0)
  869. {
  870. changes.Add(x);
  871. }
  872. });
  873. }
  874. List<TmdUser> tmdsData = new List<TmdUser>();
  875. if (tmdids.IsNotEmpty())
  876. {
  877. string memberTbname = "";
  878. if ($"{type}".Equals("activity"))
  879. {
  880. var mbers = groups.SelectMany(x => x.members).Where(z => !string.IsNullOrEmpty(z.code));
  881. var schoolTeachers = mbers.GroupBy(y => y.code).Select(m => new { key = m.Key, list = m.ToList() });
  882. foreach (var schoolTeacher in schoolTeachers)
  883. {
  884. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  885. string insql = string.Join(",", schoolTeacher.list.Select(x => $"'{x.id}'"));
  886. tmdidSql.Append($" where c.id in ({insql})");
  887. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  888. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{schoolTeacher.key}") }))
  889. {
  890. tmdsData.Add(tmd);
  891. }
  892. }
  893. var unexist_teachers = mbers.Select(x => (x.id, x.code)).Except(tmdsData.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  894. groups.ForEach(x =>
  895. {
  896. int item = x.members.RemoveAll(y => y.type == 2 && unexist_teachers.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  897. if (item > 0)
  898. {
  899. changes.Add(x);
  900. }
  901. });
  902. }
  903. else
  904. {
  905. //处理研修名单,如果是学校老师的,则需要检查SchoolTeacher
  906. //处理 学校教研组,学校管理人员,学校任课教师,学校研修名单。
  907. if (!string.IsNullOrEmpty(school) && ($"{type}".Equals("yxtrain") || $"{type}".Equals("research") || $"{type}".Equals("manage") || $"{type}".Equals("subject")))
  908. {
  909. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  910. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  911. tmdidSql.Append($" where c.id in ({insql})");
  912. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  913. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  914. {
  915. tmdsData.Add(tmd);
  916. }
  917. }
  918. else
  919. {
  920. {
  921. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  922. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  923. tmdidSql.Append($" where c.id in ({insql})");
  924. memberTbname = "Teacher";
  925. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  926. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  927. {
  928. tmdsData.Add(tmd);
  929. }
  930. }
  931. {
  932. //取差集,减少二次搜寻
  933. var tmdidexp = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  934. if (tmdidexp.IsNotEmpty())
  935. {
  936. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  937. string insql = string.Join(",", tmdidexp.Select(x => $"'{x}'"));
  938. tmdidSql.Append($" where c.id in ({insql})");
  939. memberTbname = "Student";
  940. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  941. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  942. {
  943. tmdsData.Add(tmd);
  944. }
  945. }
  946. }
  947. }
  948. tmdsData = tmdsData.Where((x, i) => tmdsData.FindIndex(n => n.id.Equals(x.id)) == i).ToList();
  949. var unexist_tmdid = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  950. groups.ForEach(x =>
  951. {
  952. int item = x.members.RemoveAll(y => unexist_tmdid.Contains(y.id) && y.type == 1);
  953. if (item > 0)
  954. {
  955. changes.Add(x);
  956. }
  957. });
  958. }
  959. }
  960. ///获取真实的名称
  961. var content = new StringContent(tmdids.Select(x => x.id).ToJsonString(), Encoding.UTF8, "application/json");
  962. string json = null;
  963. try
  964. {
  965. json = await _coreAPIHttpService.GetUserInfos(content);
  966. if (!string.IsNullOrWhiteSpace(json))
  967. {
  968. List<TmdInfo> tmdInfos = json.ToObject<List<TmdInfo>>();
  969. if (tmdInfos.IsNotEmpty())
  970. {
  971. tmdsData.ForEach(y =>
  972. {
  973. var tmd = tmdInfos.Find(x => x.id.Equals(y.id));
  974. if (tmd != null)
  975. {
  976. y.name = tmd?.name;
  977. y.picture = tmd?.picture;
  978. }
  979. else
  980. {
  981. groups.ForEach(x =>
  982. {
  983. int item = x.members.RemoveAll(z => z.id.Equals(y.id) && z.type == 1);
  984. if (item > 0)
  985. {
  986. changes.Add(x);
  987. }
  988. });
  989. }
  990. });
  991. }
  992. }
  993. }
  994. catch (Exception ex)
  995. {
  996. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location}用户转换失败:{_coreAPIHttpService.options.Get("Default").url}{json}\n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  997. }
  998. tmdids.ForEach(x =>
  999. {
  1000. var user = tmdsData.Find(y => y.id.Equals(x.id));
  1001. x.name = user?.name;
  1002. x.picture = user?.picture;
  1003. });
  1004. students.ForEach(x =>
  1005. {
  1006. var student = studentsData.Find(y => y.id.Equals(x.id) && y.schoolId.Equals(x.code));
  1007. x.name = student?.name;
  1008. x.picture = student?.picture;
  1009. x.classId = student?.classId;
  1010. });
  1011. var mbs = tmdids;
  1012. mbs.AddRange(students);
  1013. if (changes.Count > 0 && !string.IsNullOrEmpty(groupTbname))
  1014. {
  1015. foreach (var change in changes)
  1016. {
  1017. change.tcount = change.members.Where(x => x.type == 1).Count();
  1018. change.scount = change.members.Where(x => x.type == 2).Count();
  1019. GroupList group = change.ToJsonString().ToObject<GroupList>();
  1020. await client.GetContainer(Constant.TEAMModelOS, groupTbname).ReplaceItemAsync(group, group.id, new PartitionKey(group.code));
  1021. }
  1022. }
  1023. groups.ForEach(x => x.members.ForEach(y => {
  1024. if (y.type == 1)
  1025. {
  1026. var tmd = tmdids.Find(t => t.id.Equals(y.id));
  1027. y.name = tmd?.name;
  1028. y.picture = tmd?.picture;
  1029. }
  1030. if (y.type == 2)
  1031. {
  1032. var student = students.Find(t => t.id.Equals(y.id) && t.code.Equals(y.code));
  1033. y.name = student?.name;
  1034. y.picture = student?.picture;
  1035. y.classId = student?.classId;
  1036. }
  1037. }));
  1038. HashSet<string> schoolCodes = groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).Select(z => z.code).ToHashSet();
  1039. if (schoolCodes != null && schoolCodes.Count > 0)
  1040. {
  1041. List<School> schools = new List<School>();
  1042. string insql = $"select c.name,c.id from c where c.id in ({string.Join(",", schoolCodes.Select(x => $"'{x}'"))})";
  1043. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: insql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  1044. {
  1045. schools.Add(item);
  1046. }
  1047. if (schools.IsNotEmpty())
  1048. {
  1049. groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).ToList().ForEach(z => {
  1050. var school = schools.Find(j => j.id.Equals(z.code));
  1051. z.schoolName = school?.name;
  1052. });
  1053. }
  1054. }
  1055. return (groups, mbs);
  1056. }
  1057. catch (Exception ex)
  1058. {
  1059. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location},GetGroupListMemberInfo()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1060. }
  1061. return (null, null);
  1062. }
  1063. }
  1064. public class CompareIdCode : IEqualityComparer<(string id, string code)>
  1065. {
  1066. public bool Equals((string id, string code) x, (string id, string code) y)
  1067. {
  1068. return x.id.Equals(y.id) && x.code.Equals(y.code);
  1069. }
  1070. public int GetHashCode((string id, string code) obj)
  1071. {
  1072. if (obj.id != null && obj.code != null)
  1073. {
  1074. return 1;
  1075. }
  1076. else
  1077. {
  1078. return 0;
  1079. }
  1080. }
  1081. }
  1082. }