GroupListService.cs 57 KB

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