GroupListService.cs 58 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. nickname=item.name,
  576. };
  577. members.Add(member);
  578. }
  579. RGroupList groupList = new RGroupList
  580. {
  581. id = "TeacherAll",
  582. name = "TeacherAll",
  583. code = $"GroupList-{school}",
  584. school = school,
  585. scope = "school",
  586. type = "TeacherAll",
  587. members = members
  588. };
  589. groupLists = new List<RGroupList> { groupList };
  590. }
  591. else
  592. {
  593. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  594. List<Student> students = new List<Student>();
  595. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  596. if (!string.IsNullOrEmpty(school))
  597. {
  598. List<RGroupList> schoolList = new List<RGroupList>();
  599. string queryText = $"select value(c) from c where c.id in ({sql})";
  600. //await _dingDing.SendBotMsg($"{queryText}",GroupNames.成都开发測試群組);
  601. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: queryText,
  602. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  603. {
  604. schoolList.Add(item);
  605. }
  606. if (schoolList.IsNotEmpty())
  607. {
  608. groups.Add("School", schoolList);
  609. }
  610. //取差集,减少二次搜寻
  611. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  612. if (classes.IsNotEmpty())
  613. {
  614. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  615. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  616. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  617. {
  618. students.Add(item);
  619. }
  620. //取差集,减少二次搜寻
  621. classes = classes.Except(students.Select(y => y.classId)).ToList();
  622. }
  623. if (classes.IsNotEmpty())
  624. {
  625. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  626. //搜寻没有关联学生的行政班
  627. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School")
  628. .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})",
  629. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  630. {
  631. ///行政班(学生搜寻classId动态返回)class
  632. List<RMember> smembers = new List<RMember>();
  633. RGroupList group = new RGroupList
  634. {
  635. id = item.id,
  636. code = $"GroupList-{school}",
  637. name = item.name,
  638. periodId = item.periodId,
  639. scope = "school",
  640. school = school,
  641. type = "class",
  642. year = item.year,
  643. members = smembers,
  644. scount = smembers.Count,
  645. pk = "GroupList",
  646. leader = item.leader,
  647. no = item.no,
  648. };
  649. groupLists.Add(group);
  650. }
  651. //取差集,减少二次搜寻
  652. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  653. }
  654. }
  655. if (classes.IsNotEmpty())
  656. {
  657. List<RGroupList> privateList = new List<RGroupList>();
  658. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  659. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: $"select value(c) from c where c.id in ({sql})",
  660. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  661. {
  662. privateList.Add(item);
  663. //if (string.IsNullOrWhiteSpace(school))
  664. //{
  665. // privateList.Add(item);
  666. //}
  667. //else
  668. //{
  669. // if (!string.IsNullOrWhiteSpace(item.school))
  670. // {
  671. // if (item.school.Equals(school))
  672. // {
  673. // privateList.Add(item);
  674. // }
  675. // }
  676. // else
  677. // {
  678. // privateList.Add(item);
  679. // }
  680. //}
  681. }
  682. if (privateList.IsNotEmpty())
  683. {
  684. groups.Add("Teacher", privateList);
  685. }
  686. }
  687. foreach (var item in groups)
  688. {
  689. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  690. foreach (var group in list)
  691. {
  692. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school);
  693. members.AddRange(rmembers);
  694. }
  695. }
  696. groupLists.AddRange(groups.SelectMany(x => x.Value).ToList());
  697. if (students.IsNotEmpty())
  698. {
  699. List<string> sqlList = students.Select(x => x.classId).ToList();
  700. string insql = string.Join(",", sqlList.Select(x => $"'{x}'"));
  701. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  702. 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})",
  703. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  704. {
  705. ///行政班(学生搜寻classId动态返回)class
  706. List<RMember> smembers = students.Where(x => x.classId.Equals(item.id))
  707. .Select(y => new RMember
  708. {
  709. id = y.id,
  710. code = school,
  711. name = y.name,
  712. nickname = y.name,
  713. type = 2,
  714. picture = y.picture,
  715. no = y.no,
  716. classId = y.classId,
  717. //groupId=y.groupId,
  718. groupName = y.groupName,
  719. irs = y.irs,
  720. }).ToList();
  721. members.AddRange(smembers);
  722. RGroupList group = new RGroupList
  723. {
  724. id = item.id,
  725. code = $"GroupList-{school}",
  726. name = item.name,
  727. periodId = item.periodId,
  728. scope = "school",
  729. school = school,
  730. type = "class",
  731. year = item.year,
  732. members = smembers,
  733. scount = smembers.Count,
  734. no = item.no,
  735. leader = item.leader,
  736. pk = "GroupList"
  737. };
  738. groupLists.Add(group);
  739. }
  740. //去重。
  741. 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();
  742. }
  743. }
  744. if (groupids.IsNotEmpty())
  745. {
  746. List<RMember> rmembers = new List<RMember>();
  747. groupLists.ForEach(y => {
  748. (string id, List<string> grpids) = groupids.Find(x => x.Item1.Equals(y.id));
  749. var gpmember = y.members.FindAll(x => !string.IsNullOrEmpty(x.groupName) && grpids.Contains(x.groupName));
  750. if (grpids.Contains("default"))
  751. {
  752. var gpmemberdft = y.members.FindAll(x => string.IsNullOrWhiteSpace(x.groupName));
  753. if (gpmember.IsNotEmpty())
  754. {
  755. gpmember.AddRange(gpmemberdft);
  756. }
  757. else
  758. {
  759. gpmember = gpmemberdft;
  760. }
  761. }
  762. y.members = gpmember;
  763. });
  764. var gpgpmembers = groupLists.SelectMany(x => x.members).ToList();
  765. 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();
  766. 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();
  767. if (tmdids.IsNotEmpty())
  768. {
  769. rmembers.AddRange(tmdids);
  770. }
  771. if (students.IsNotEmpty())
  772. {
  773. rmembers.AddRange(students);
  774. }
  775. return (rmembers, groupLists);
  776. }
  777. else
  778. {
  779. return (members, groupLists);
  780. }
  781. }
  782. public static async Task<List<RGroupList>> GetGroupListMemberByType(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, string type, List<string> scopes, string school, DingDing _dingDing)
  783. {
  784. StringBuilder sql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='{type}'");
  785. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  786. if (scopes.Contains("school"))
  787. {
  788. if (!string.IsNullOrEmpty(school))
  789. {
  790. List<RGroupList> groupLists = new List<RGroupList>();
  791. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  792. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  793. {
  794. groupLists.Add(item);
  795. }
  796. groups.Add("School", groupLists);
  797. }
  798. }
  799. else if (scopes.Contains("private"))
  800. {
  801. List<RGroupList> groupLists = new List<RGroupList>();
  802. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  803. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  804. {
  805. groupLists.Add(item);
  806. //if (string.IsNullOrWhiteSpace(school))
  807. //{
  808. // groupLists.Add(item);
  809. //}
  810. //else
  811. //{
  812. // if (!string.IsNullOrWhiteSpace(item.school))
  813. // {
  814. // if (item.school.Equals(school))
  815. // {
  816. // groupLists.Add(item);
  817. // }
  818. // }
  819. // else {
  820. // groupLists.Add(item);
  821. // }
  822. //}
  823. }
  824. groups.Add("Teacher", groupLists);
  825. }
  826. foreach (var item in groups)
  827. {
  828. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  829. foreach (var group in list)
  830. {
  831. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school);
  832. }
  833. }
  834. var lists = groups.SelectMany(x => x.Value).ToList();
  835. return lists;
  836. }
  837. 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)
  838. {
  839. try
  840. {
  841. HashSet<RGroupList> changes = new HashSet<RGroupList>();
  842. var members = groups.SelectMany(y => y.members).ToList();
  843. //去重
  844. 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();
  845. 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();
  846. var stu = students.GroupBy(x => x.code).Select(y => new { key = y.Key, list = y.ToList() });
  847. List<Student> studentsData = new List<Student>();
  848. if (stu != null)
  849. {
  850. foreach (var item in stu)
  851. {
  852. var ids = item.list.Select(x => x.id).ToList();
  853. if (ids.IsNotEmpty())
  854. {
  855. StringBuilder stuSql = new StringBuilder($"SELECT distinct c.name,c.id,c.code,c.picture,c.no,c.irs,c.classId FROM c ");
  856. string insql = string.Join(",", ids.Select(x => $"'{x}'"));
  857. stuSql.Append($"where c.id in ({insql})");
  858. await foreach (var student in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: stuSql.ToString(),
  859. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{item.key}") }))
  860. {
  861. student.schoolId = item.key;
  862. studentsData.Add(student);
  863. }
  864. }
  865. }
  866. var unexist_student = students.Select(x => (x.id, x.code)).Except(studentsData.Select(y => (y.id, y.schoolId)), new CompareIdCode()).ToList();
  867. groups.ForEach(x =>
  868. {
  869. int item = x.members.RemoveAll(y => y.type == 2 && unexist_student.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  870. if (item > 0)
  871. {
  872. changes.Add(x);
  873. }
  874. });
  875. }
  876. List<TmdUser> tmdsData = new List<TmdUser>();
  877. if (tmdids.IsNotEmpty())
  878. {
  879. string memberTbname = "";
  880. if ($"{type}".Equals("activity"))
  881. {
  882. var mbers = groups.SelectMany(x => x.members).Where(z => !string.IsNullOrEmpty(z.code));
  883. var schoolTeachers = mbers.GroupBy(y => y.code).Select(m => new { key = m.Key, list = m.ToList() });
  884. foreach (var schoolTeacher in schoolTeachers)
  885. {
  886. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  887. string insql = string.Join(",", schoolTeacher.list.Select(x => $"'{x.id}'"));
  888. tmdidSql.Append($" where c.id in ({insql})");
  889. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  890. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{schoolTeacher.key}") }))
  891. {
  892. tmdsData.Add(tmd);
  893. }
  894. }
  895. var unexist_teachers = mbers.Select(x => (x.id, x.code)).Except(tmdsData.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  896. groups.ForEach(x =>
  897. {
  898. int item = x.members.RemoveAll(y => y.type == 2 && unexist_teachers.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  899. if (item > 0)
  900. {
  901. changes.Add(x);
  902. }
  903. });
  904. }
  905. else
  906. {
  907. //处理研修名单,如果是学校老师的,则需要检查SchoolTeacher
  908. //处理 学校教研组,学校管理人员,学校任课教师,学校研修名单。
  909. if (!string.IsNullOrEmpty(school) && ($"{type}".Equals("yxtrain") || $"{type}".Equals("research") || $"{type}".Equals("manage") || $"{type}".Equals("subject")))
  910. {
  911. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  912. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  913. tmdidSql.Append($" where c.id in ({insql})");
  914. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  915. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  916. {
  917. tmdsData.Add(tmd);
  918. }
  919. }
  920. else
  921. {
  922. {
  923. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  924. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  925. tmdidSql.Append($" where c.id in ({insql})");
  926. memberTbname = "Teacher";
  927. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  928. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  929. {
  930. tmdsData.Add(tmd);
  931. }
  932. }
  933. {
  934. //取差集,减少二次搜寻
  935. var tmdidexp = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  936. if (tmdidexp.IsNotEmpty())
  937. {
  938. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  939. string insql = string.Join(",", tmdidexp.Select(x => $"'{x}'"));
  940. tmdidSql.Append($" where c.id in ({insql})");
  941. memberTbname = "Student";
  942. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  943. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  944. {
  945. tmdsData.Add(tmd);
  946. }
  947. }
  948. }
  949. }
  950. tmdsData = tmdsData.Where((x, i) => tmdsData.FindIndex(n => n.id.Equals(x.id)) == i).ToList();
  951. var unexist_tmdid = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  952. groups.ForEach(x =>
  953. {
  954. int item = x.members.RemoveAll(y => unexist_tmdid.Contains(y.id) && y.type == 1);
  955. if (item > 0)
  956. {
  957. changes.Add(x);
  958. }
  959. });
  960. }
  961. }
  962. if (tmdids.IsNotEmpty()) {
  963. ///获取真实的名称
  964. var content = new StringContent(tmdids.Select(x => x.id).ToJsonString(), Encoding.UTF8, "application/json");
  965. string json = null;
  966. try
  967. {
  968. json = await _coreAPIHttpService.GetUserInfos(content);
  969. if (!string.IsNullOrWhiteSpace(json))
  970. {
  971. List<TmdInfo> tmdInfos = json.ToObject<List<TmdInfo>>();
  972. if (tmdInfos.IsNotEmpty())
  973. {
  974. tmdsData.ForEach(y =>
  975. {
  976. var tmd = tmdInfos.Find(x => x.id.Equals(y.id));
  977. if (tmd != null)
  978. {
  979. y.name = tmd?.name;
  980. y.picture = tmd?.picture;
  981. }
  982. else
  983. {
  984. groups.ForEach(x =>
  985. {
  986. int item = x.members.RemoveAll(z => z.id.Equals(y.id) && z.type == 1);
  987. if (item > 0)
  988. {
  989. changes.Add(x);
  990. }
  991. });
  992. }
  993. });
  994. }
  995. }
  996. }
  997. catch (Exception ex)
  998. {
  999. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location}用户转换失败:{_coreAPIHttpService.options.Get("Default").url}{json}\n {ex.Message}{ex.StackTrace}{tmdids.Select(x => x.id).ToJsonString()}", GroupNames.成都开发測試群組);
  1000. }
  1001. }
  1002. tmdids.ForEach(x =>
  1003. {
  1004. var user = tmdsData.Find(y => y.id.Equals(x.id));
  1005. x.name = user?.name;
  1006. x.nickname = string.IsNullOrWhiteSpace(x.nickname) ? user?.name : x.nickname;
  1007. x.picture = user?.picture;
  1008. });
  1009. students.ForEach(x =>
  1010. {
  1011. var student = studentsData.Find(y => y.id.Equals(x.id) && y.schoolId.Equals(x.code));
  1012. x.name = student?.name;
  1013. x.nickname = string.IsNullOrWhiteSpace(x.nickname) ? student?.name:x.nickname;
  1014. x.picture = student?.picture;
  1015. x.classId = student?.classId;
  1016. });
  1017. var mbs = tmdids;
  1018. mbs.AddRange(students);
  1019. if (changes.Count > 0 && !string.IsNullOrEmpty(groupTbname))
  1020. {
  1021. foreach (var change in changes)
  1022. {
  1023. change.tcount = change.members.Where(x => x.type == 1).Count();
  1024. change.scount = change.members.Where(x => x.type == 2).Count();
  1025. GroupList group = change.ToJsonString().ToObject<GroupList>();
  1026. await client.GetContainer(Constant.TEAMModelOS, groupTbname).ReplaceItemAsync(group, group.id, new PartitionKey(group.code));
  1027. }
  1028. }
  1029. groups.ForEach(x => x.members.ForEach(y => {
  1030. if (y.type == 1)
  1031. {
  1032. var tmd = tmdids.Find(t => t.id.Equals(y.id));
  1033. y.name = tmd?.name;
  1034. y.nickname = string.IsNullOrWhiteSpace(y.nickname) ? tmd?.nickname : y.nickname;
  1035. y.picture = tmd?.picture;
  1036. }
  1037. if (y.type == 2)
  1038. {
  1039. var student = students.Find(t => t.id.Equals(y.id) && t.code.Equals(y.code));
  1040. y.name = student?.name;
  1041. y.nickname = string.IsNullOrWhiteSpace(y.nickname) ? student?.nickname : y.nickname;
  1042. y.picture = student?.picture;
  1043. y.classId = student?.classId;
  1044. }
  1045. }));
  1046. HashSet<string> schoolCodes = groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).Select(z => z.code).ToHashSet();
  1047. if (schoolCodes != null && schoolCodes.Count > 0)
  1048. {
  1049. List<School> schools = new List<School>();
  1050. string insql = $"select c.name,c.id from c where c.id in ({string.Join(",", schoolCodes.Select(x => $"'{x}'"))})";
  1051. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: insql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  1052. {
  1053. schools.Add(item);
  1054. }
  1055. if (schools.IsNotEmpty())
  1056. {
  1057. groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).ToList().ForEach(z => {
  1058. var school = schools.Find(j => j.id.Equals(z.code));
  1059. z.schoolName = school?.name;
  1060. });
  1061. }
  1062. }
  1063. return (groups, mbs);
  1064. }
  1065. catch (Exception ex)
  1066. {
  1067. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.Get("Default").location},GetGroupListMemberInfo()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1068. }
  1069. return (null, null);
  1070. }
  1071. }
  1072. public class CompareIdCode : IEqualityComparer<(string id, string code)>
  1073. {
  1074. public bool Equals((string id, string code) x, (string id, string code) y)
  1075. {
  1076. return x.id.Equals(y.id) && x.code.Equals(y.code);
  1077. }
  1078. public int GetHashCode((string id, string code) obj)
  1079. {
  1080. if (obj.id != null && obj.code != null)
  1081. {
  1082. return 1;
  1083. }
  1084. else
  1085. {
  1086. return 0;
  1087. }
  1088. }
  1089. }
  1090. }