GroupListService.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  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. namespace TEAMModelOS.SDK.Models
  18. {
  19. public class GroupListService
  20. {
  21. public static async Task<(int status, GroupList stuList)> CodeJoinList(CosmosClient client, string _stuListNo, string userid, int type, string school)
  22. {
  23. var queryNo = $"SELECT value(c) FROM c where c.no ='{_stuListNo}'";
  24. (int status, GroupList stuList) data = (-1, null);
  25. if (!string.IsNullOrEmpty(school))
  26. {
  27. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupList>(queryText: queryNo,
  28. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  29. {
  30. data = JoinList(item, userid , type, school);
  31. break;
  32. }
  33. }
  34. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<GroupList>(queryText: queryNo,
  35. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  36. {
  37. data = JoinList(item, userid, type, school);
  38. break;
  39. }
  40. return data;
  41. }
  42. public static (int status, GroupList stuList) JoinList(GroupList stuList, string userid, int type, string school)
  43. {
  44. string irs=string.Empty;
  45. List<string> irsOrder= stuList.members.Select(x => x.irs)?.Where(y => !string.IsNullOrEmpty(y)&& Regex.IsMatch(y, @"^\d*$")).OrderBy(x => x).ToList();
  46. if (!irsOrder.Contains("0"))
  47. {
  48. irsOrder.Insert(0, "0");
  49. }
  50. if (irsOrder != null)
  51. {
  52. if (!irsOrder.Contains("0"))
  53. {
  54. irsOrder.Insert(0, "0");
  55. }
  56. }
  57. else { irsOrder = new List<string>() { "0" }; }
  58. for (int i = 0; i < irsOrder.Count; i++)
  59. {
  60. irs = $"{int.Parse(irsOrder[i]) + 1}";
  61. int index = i + 1;
  62. if (index <= irsOrder.Count - 1)
  63. {
  64. if (!irs.Equals(irsOrder[index]))
  65. {
  66. break;
  67. }
  68. }
  69. }
  70. int status = -1;
  71. if (string.IsNullOrEmpty($"{userid}"))
  72. {
  73. //加入学生或醍摩豆ID为空
  74. status = 1;
  75. }
  76. else
  77. {
  78. if (type == 1)
  79. {
  80. var student = stuList.members.Find(x => x.type == 1 && x.id.Equals(userid));
  81. if (student != null)
  82. {
  83. //重复加入
  84. status = 2;
  85. }
  86. else
  87. {
  88. status = 0;
  89. stuList.members.Add(new Member { id = userid,type = type, irs = irs, no = irs });
  90. }
  91. }
  92. else if (type == 2)
  93. {
  94. var student = stuList.members.Find(x => x.type == 2 && x.id.Equals(userid) && x.code.Equals(school));
  95. if (student != null)
  96. {
  97. //重复加入
  98. status = 2;
  99. }
  100. else
  101. {
  102. status = 0;
  103. stuList.members.Add(new Member { id = userid, code = school, type = type, irs = irs, no = irs });
  104. }
  105. }
  106. }
  107. return (status, stuList);
  108. }
  109. public static async Task<GroupList> UpsertList(GroupList list, AzureCosmosFactory _azureCosmos, AzureStorageFactory _azureStorage, IConfiguration _configuration, AzureServiceBusFactory _serviceBus)
  110. {
  111. bool isnew = false;
  112. var client = _azureCosmos.GetCosmosClient();
  113. if (string.IsNullOrEmpty(list.id))
  114. {
  115. list.id = Guid.NewGuid().ToString();
  116. isnew = true;
  117. }
  118. string tbname = list.scope.Equals("private") ? "Teacher" : "School";
  119. list.tcount = list.members.Where(x => x.type == 1).Count();
  120. list.scount = list.members.Where(x => x.type == 2).Count();
  121. await client.GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(list, new PartitionKey(list.code));
  122. //学生名单,教研组会触发活动中间表刷新
  123. if (list.type.Equals("teach") || list.type.Equals("research") || list.type.Equals("yxtrain"))
  124. {
  125. GroupChange change = new GroupChange()
  126. {
  127. type = list.type,
  128. listid = list.id,
  129. scope = list.scope,
  130. originCode = list.school,
  131. school = list.school,
  132. creatorId = list.creatorId
  133. };
  134. GroupList oldList = null;
  135. if (!isnew)
  136. {
  137. try
  138. {
  139. oldList = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<GroupList>(list.id, new PartitionKey(list.code));
  140. }
  141. catch (CosmosException)
  142. {
  143. oldList = null;
  144. }
  145. }
  146. if (list.members.IsNotEmpty() && (oldList == null || !oldList.members.IsNotEmpty()))
  147. {
  148. //加入的
  149. var tmdids = list.members.FindAll(x => x.type == 1);
  150. if (tmdids.IsNotEmpty())
  151. {
  152. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  153. {
  154. change.tchjoin.AddRange(tmdids);
  155. }
  156. else
  157. {
  158. change.tmdjoin.AddRange(tmdids);
  159. }
  160. }
  161. var stuids = list.members.FindAll(x => x.type == 2);
  162. if (stuids.IsNotEmpty())
  163. {
  164. change.stujoin.AddRange(stuids);
  165. }
  166. }
  167. else
  168. {
  169. if (list.members.IsNotEmpty())
  170. {
  171. var tmdids = list.members.FindAll(x => x.type == 1);
  172. var oldtmdids = oldList.members.FindAll(x => x.type == 1);
  173. //取各自的差集
  174. //新=》旧差集,表示新增
  175. var jointmdid = tmdids.Select(x => x.id).Except(oldtmdids.Select(y => y.id)).ToList();
  176. //旧=》新差集,表示离开
  177. var leavetmdid = oldtmdids.Select(x => x.id).Except(tmdids.Select(y => y.id)).ToList();
  178. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  179. {
  180. change.tchjoin.AddRange(tmdids.Where(x => jointmdid.Exists(y => y.Equals(x.id))));
  181. change.tchleave.AddRange(oldtmdids.Where(x => leavetmdid.Exists(y => y.Equals(x.id))));
  182. }
  183. else
  184. {
  185. change.tmdjoin.AddRange(tmdids.Where(x => jointmdid.Exists(y => y.Equals(x.id))));
  186. change.tmdleave.AddRange(oldtmdids.Where(x => leavetmdid.Exists(y => y.Equals(x.id))));
  187. }
  188. var stuids = list.members.FindAll(x => x.type == 2);
  189. var oldstuids = oldList.members.FindAll(x => x.type == 2);
  190. var joinstudent = stuids.Select(x => (x.id, x.code)).Except(oldstuids.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  191. var leavestudent = oldstuids.Select(x => (x.id, x.code)).Except(stuids.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  192. change.stujoin.AddRange(stuids.Where(x => joinstudent.Exists(y => y.id.Equals(x.id) && y.code.Equals(x.code))));
  193. change.stuleave.AddRange(oldstuids.Where(x => leavestudent.Exists(y => y.id.Equals(x.id) && y.code.Equals(x.code))));
  194. }
  195. else
  196. {
  197. //离开的
  198. if (oldList != null) {
  199. var tmdids = oldList.members.FindAll(x => x.type == 1);
  200. if (tmdids.IsNotEmpty())
  201. {
  202. if (list.type.Equals("research")|| list.type.Equals("yxtrain"))
  203. {
  204. change.tchleave.AddRange(tmdids);
  205. }
  206. else
  207. {
  208. change.tmdleave.AddRange(tmdids);
  209. }
  210. }
  211. var stuids = oldList.members.FindAll(x => x.type == 2);
  212. if (stuids.IsNotEmpty())
  213. {
  214. change.stuleave.AddRange(stuids);
  215. }
  216. }
  217. }
  218. }
  219. if (change.tmdjoin.Count != 0 || change.tmdleave.Count != 0 || change.stujoin.Count != 0 || change.stuleave.Count != 0
  220. || change.tchjoin.Count != 0 || change.tchleave.Count != 0)
  221. {
  222. var messageChange = new ServiceBusMessage(change.ToJsonString());
  223. messageChange.ApplicationProperties.Add("name", "GroupChange");
  224. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  225. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  226. }
  227. }
  228. return list;
  229. }
  230. public static async Task<GroupList> CheckListNo(GroupList list, AzureCosmosFactory _azureCosmos, DingDing _dingDing, Option _option)
  231. {
  232. try
  233. {
  234. var client = _azureCosmos.GetCosmosClient();
  235. if (string.IsNullOrEmpty(list.no))
  236. {
  237. list.no = $"{Utils.CreatSaltString(6, "0123456789")}";
  238. for (int i = 0; i < 10; i++)
  239. {
  240. List<string> noStus = new List<string>();
  241. var queryNo = $"SELECT c.no FROM c where c.no ='{list.no}'";
  242. if (list.scope.Equals("school"))
  243. {
  244. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: queryNo,
  245. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{list.code}") }))
  246. {
  247. using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
  248. if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  249. {
  250. var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
  251. while (accounts.MoveNext())
  252. {
  253. JsonElement account = accounts.Current;
  254. noStus.Add(account.GetProperty("no").GetString());
  255. }
  256. }
  257. }
  258. }
  259. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: queryNo,
  260. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey("GroupList") }))
  261. {
  262. using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
  263. if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  264. {
  265. var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
  266. while (accounts.MoveNext())
  267. {
  268. JsonElement account = accounts.Current;
  269. noStus.Add(account.GetProperty("no").GetString());
  270. }
  271. }
  272. }
  273. if (noStus.Count == 0)
  274. {
  275. break;
  276. }
  277. else
  278. {
  279. if (i == 9)
  280. {
  281. string msg = $"OS,{_option.Location},school/course/upsert-list()\n 编号生成异常,重复生成次数超过10次";
  282. await _dingDing.SendBotMsg(msg, GroupNames.醍摩豆服務運維群組);
  283. throw new Exception(msg);
  284. }
  285. else
  286. {
  287. list.no = $"{Utils.CreatSaltString(6, "0123456789")}";
  288. }
  289. }
  290. }
  291. }
  292. }
  293. catch (Exception ex)
  294. {
  295. }
  296. return list;
  297. }
  298. public static async Task<List<GroupListDto> > GetGroupListListids(CosmosClient client, DingDing _dingDing, List<string> classes, string school,string SummarySql) {
  299. List<GroupListDto> groupLists = null;
  300. if (classes.Count == 1 && classes.First().Equals("default") && !string.IsNullOrEmpty(school))
  301. {
  302. //默认的教研组
  303. GroupListDto groupList = new GroupListDto
  304. {
  305. id = "default",
  306. name = "default",
  307. code = $"GroupList-{school}",
  308. school = school,
  309. scope = "school",
  310. type = "yxtrain",
  311. };
  312. groupLists = new List<GroupListDto> { groupList };
  313. }
  314. else
  315. {
  316. Dictionary<string, List<GroupListDto>> groups = new Dictionary<string, List<GroupListDto>>();
  317. //List<Student> students = new List<Student>();
  318. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  319. if (!string.IsNullOrEmpty(school))
  320. {
  321. List<GroupListDto> schoolList = new List<GroupListDto>();
  322. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupListDto>(queryText: $"select {SummarySql} from c where c.id in ({sql})",
  323. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  324. {
  325. schoolList.Add(item);
  326. }
  327. if (schoolList.IsNotEmpty())
  328. {
  329. groups.Add("School", schoolList);
  330. }
  331. //取差集,减少二次搜寻
  332. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  333. if (classes.IsNotEmpty())
  334. {
  335. if (!groupLists.IsNotEmpty()) {
  336. groupLists = new List<GroupListDto>();
  337. }
  338. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  339. //搜寻没有关联学生的行政班
  340. 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})",
  341. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  342. {
  343. ///行政班(学生搜寻classId动态返回)class
  344. GroupListDto group = new GroupListDto
  345. {
  346. id = item.id,
  347. code = $"GroupList-{school}",
  348. name = item.name,
  349. periodId = item.periodId,
  350. scope = "school",
  351. school = school,
  352. type = "class",
  353. year = item.year,
  354. leader= item.leader,
  355. no= item.no,
  356. pk= "GroupList",
  357. };
  358. groupLists.Add(group);
  359. }
  360. //取差集,减少二次搜寻
  361. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  362. }
  363. }
  364. if (classes.IsNotEmpty())
  365. {
  366. List<GroupListDto> privateList = new List<GroupListDto>();
  367. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  368. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<GroupListDto>(queryText: $"select {SummarySql} from c where c.id in ({sql})",
  369. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  370. {
  371. privateList.Add(item);
  372. }
  373. if (privateList.IsNotEmpty())
  374. {
  375. groups.Add("Teacher", privateList);
  376. }
  377. }
  378. if (groups.Count != 0)
  379. {
  380. if (groupLists.IsNotEmpty())
  381. {
  382. groupLists .AddRange(groups.SelectMany(x => x.Value).ToList());
  383. }
  384. else {
  385. groupLists = groups.SelectMany(x => x.Value).ToList();
  386. }
  387. }
  388. }
  389. return groupLists;
  390. }
  391. public static async Task<(List<RMember>, List<RGroupList> groups)> GetStutmdidListids(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
  392. {
  393. List<RMember> members = new List<RMember>();
  394. List<RGroupList> groupLists = new List<RGroupList>();
  395. if (classes.IsEmpty()) {
  396. return (members, groupLists);
  397. }
  398. if (classes.Count == 1 && classes.First().Equals("default") && !string.IsNullOrEmpty(school))
  399. {
  400. //默认的教研组
  401. members = new List<RMember>();
  402. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdInfo>(queryText: $"SELECT value(c) FROM c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  403. {
  404. RMember member = new RMember
  405. {
  406. id = item.id,
  407. name = item.name,
  408. picture = item.picture,
  409. type = 1,
  410. };
  411. members.Add(member);
  412. }
  413. RGroupList groupList = new RGroupList
  414. {
  415. id = "default",
  416. name = "default",
  417. code = $"GroupList-{school}",
  418. school = school,
  419. scope = "school",
  420. type = "yxtrain",
  421. members = members
  422. };
  423. groupLists = new List<RGroupList> { groupList };
  424. }
  425. else
  426. {
  427. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  428. List<Student> students = new List<Student>();
  429. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  430. if (!string.IsNullOrEmpty(school))
  431. {
  432. List<RGroupList> schoolList = new List<RGroupList>();
  433. string queryText = $"select value(c) from c where c.id in ({sql})";
  434. //await _dingDing.SendBotMsg($"{queryText}",GroupNames.成都开发測試群組);
  435. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: queryText,
  436. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  437. {
  438. schoolList.Add(item);
  439. }
  440. if (schoolList.IsNotEmpty())
  441. {
  442. groups.Add("School", schoolList);
  443. }
  444. //取差集,减少二次搜寻
  445. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  446. if (classes.IsNotEmpty())
  447. {
  448. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  449. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  450. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  451. {
  452. students.Add(item);
  453. }
  454. //取差集,减少二次搜寻
  455. classes = classes.Except(students.Select(y => y.classId)).ToList();
  456. }
  457. if (classes.IsNotEmpty()) {
  458. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  459. //搜寻没有关联学生的行政班
  460. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School")
  461. .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})",
  462. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  463. {
  464. ///行政班(学生搜寻classId动态返回)class
  465. List<RMember> smembers = new List<RMember>();
  466. RGroupList group = new RGroupList
  467. {
  468. id = item.id,
  469. code = $"GroupList-{school}",
  470. name = item.name,
  471. periodId = item.periodId,
  472. scope = "school",
  473. school = school,
  474. type = "class",
  475. year = item.year,
  476. members = smembers,
  477. scount = smembers.Count,
  478. pk= "GroupList",
  479. leader=item.leader,
  480. no=item.no,
  481. };
  482. groupLists.Add(group);
  483. }
  484. //取差集,减少二次搜寻
  485. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  486. }
  487. }
  488. if (classes.IsNotEmpty())
  489. {
  490. List<RGroupList> privateList = new List<RGroupList>();
  491. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  492. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: $"select value(c) from c where c.id in ({sql})",
  493. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  494. {
  495. privateList.Add(item);
  496. }
  497. if (privateList.IsNotEmpty())
  498. {
  499. groups.Add("Teacher", privateList);
  500. }
  501. }
  502. foreach (var item in groups)
  503. {
  504. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  505. foreach (var group in list)
  506. {
  507. (List<RGroupList> rgroups, List<RMember> rmembers) =await GetGroupListMemberInfo(client, group.key, group.list, item.Key,_dingDing);
  508. members.AddRange(rmembers);
  509. }
  510. }
  511. groupLists .AddRange(groups.SelectMany(x => x.Value).ToList());
  512. if (students.IsNotEmpty())
  513. {
  514. List<string> sqlList = students.Select(x => x.classId).ToList();
  515. string insql = string.Join(",", sqlList.Select(x => $"'{x}'"));
  516. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  517. 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})",
  518. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  519. {
  520. ///行政班(学生搜寻classId动态返回)class
  521. List<RMember> smembers = students.Where(x => x.classId.Equals(item.id))
  522. .Select(y => new RMember
  523. {
  524. id = y.id,
  525. code = school,
  526. name = y.name,
  527. type = 2,
  528. picture = y.picture,
  529. no = y.no,
  530. classId=y.classId,
  531. //groupId=y.groupId,
  532. groupName=y.groupName ,
  533. irs=y.irs,
  534. }).ToList();
  535. members.AddRange(smembers);
  536. RGroupList group = new RGroupList
  537. {
  538. id = item.id,
  539. code = $"GroupList-{school}",
  540. name = item.name,
  541. periodId = item.periodId,
  542. scope = "school",
  543. school = school,
  544. type = "class",
  545. year = item.year,
  546. members = smembers,
  547. scount = smembers.Count,
  548. no=item.no,
  549. leader=item.leader,
  550. pk= "GroupList"
  551. };
  552. groupLists.Add(group);
  553. }
  554. //去重。
  555. 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();
  556. }
  557. }
  558. return (members,groupLists);
  559. }
  560. public static async Task<(List<RGroupList> groups, List<RMember> members)> GetGroupListMemberInfo(CosmosClient client, string type, List<RGroupList> groups, string groupTbname, DingDing _dingDing)
  561. {
  562. try {
  563. var members = groups.SelectMany(y => y.members).ToList();
  564. //去重
  565. 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();
  566. 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();
  567. var stu = students.GroupBy(x => x.code).Select(y => new { key = y.Key, list = y.ToList() });
  568. List<Student> studentsData = new List<Student>();
  569. if (stu != null)
  570. {
  571. foreach (var item in stu)
  572. {
  573. var ids = item.list.Select(x => x.id).ToList();
  574. if (ids.IsNotEmpty())
  575. {
  576. StringBuilder stuSql = new StringBuilder($"SELECT distinct c.name,c.id,c.code,c.picture,c.no,c.irs,c.classId FROM c ");
  577. string insql = string.Join(",", ids.Select(x => $"'{x}'"));
  578. stuSql.Append($"where c.id in ({insql})");
  579. await foreach (var student in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: stuSql.ToString(),
  580. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{item.key}") }))
  581. {
  582. student.schoolId = item.key;
  583. studentsData.Add(student);
  584. }
  585. }
  586. }
  587. }
  588. List<TmdUser> tmdsData = new List<TmdUser>();
  589. if (tmdids.IsNotEmpty())
  590. {
  591. string memberTbname = "";
  592. //可能会出现在两种表中
  593. if ($"{type}".Equals("teach") || $"{type}".Equals("research") || $"{type}".Equals("group")
  594. || $"{type}".Equals("friend") || $"{type}".Equals("manage") || $"{type}".Equals("subject") || $"{type}".Equals("yxtrain"))
  595. {
  596. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  597. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  598. tmdidSql.Append($" where c.id in ({insql})");
  599. memberTbname = "Teacher";
  600. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  601. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  602. {
  603. tmdsData.Add(tmd);
  604. }
  605. }
  606. if ($"{type}".Equals("teach") || $"{type}".Equals("friend") || $"{type}".Equals("group"))
  607. {
  608. //取差集,减少二次搜寻
  609. var tmdidexp = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  610. if (tmdidexp.IsNotEmpty())
  611. {
  612. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  613. string insql = string.Join(",", tmdidexp.Select(x => $"'{x}'"));
  614. tmdidSql.Append($" where c.id in ({insql})");
  615. memberTbname = "Student";
  616. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  617. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  618. {
  619. tmdsData.Add(tmd);
  620. }
  621. }
  622. }
  623. //去重
  624. tmdsData = tmdsData.Where((x, i) => tmdsData.FindIndex(n => n.id.Equals(x.id)) == i).ToList();
  625. }
  626. HashSet<RGroupList> changes = new HashSet<RGroupList>();
  627. var unexist_tmdid = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  628. groups.ForEach(x =>
  629. {
  630. int item = x.members.RemoveAll(y => unexist_tmdid.Contains(y.id) && y.type == 1);
  631. if (item > 0)
  632. {
  633. changes.Add(x);
  634. }
  635. });
  636. var unexist_student = students.Select(x => (x.id, x.code)).Except(studentsData.Select(y => (y.id, y.schoolId)), new CompareIdCode()).ToList();
  637. groups.ForEach(x =>
  638. {
  639. int item = x.members.RemoveAll(y => y.type == 2 && unexist_student.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  640. if (item > 0)
  641. {
  642. changes.Add(x);
  643. }
  644. });
  645. tmdids.ForEach(x =>
  646. {
  647. var user = tmdsData.Find(y => y.id.Equals(x.id));
  648. x.name = user?.name;
  649. x.picture = user?.picture;
  650. });
  651. students.ForEach(x =>
  652. {
  653. var student = studentsData.Find(y => y.id.Equals(x.id) && y.schoolId.Equals(x.code));
  654. x.name = student?.name;
  655. x.picture = student?.picture;
  656. x.classId = student?.classId;
  657. });
  658. var mbs = tmdids;
  659. mbs.AddRange(students);
  660. if (changes.Count > 0 && !string.IsNullOrEmpty(groupTbname))
  661. {
  662. foreach (var change in changes)
  663. {
  664. change.tcount = change.members.Where(x => x.type == 1).Count();
  665. change.scount = change.members.Where(x => x.type == 2).Count();
  666. GroupList group= change.ToJsonString().ToObject<GroupList>();
  667. await client.GetContainer(Constant.TEAMModelOS, groupTbname).ReplaceItemAsync(group, group.id, new PartitionKey(group.code));
  668. }
  669. }
  670. groups.ForEach(x => x.members.ForEach(y=> {
  671. if (y.type == 1) {
  672. var tmd =tmdids.Find(t => t.id.Equals(y.id));
  673. y.name = tmd?.name;
  674. y.picture = tmd?.picture;
  675. }
  676. if (y.type == 2)
  677. {
  678. var student = students.Find(t => t.id.Equals(y.id)&& t.code.Equals(y.code));
  679. y.name = student?.name;
  680. y.picture = student?.picture;
  681. y.classId = student?.classId;
  682. }
  683. }));
  684. return (groups, mbs);
  685. } catch (Exception ex) {
  686. await _dingDing.SendBotMsg($"OS,GetGroupListMemberInfo()\n{ex.Message}{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  687. }
  688. return (null, null);
  689. }
  690. }
  691. public class CompareIdCode : IEqualityComparer<(string id, string code)>
  692. {
  693. public bool Equals((string id, string code) x, (string id, string code) y)
  694. {
  695. return x.id.Equals(y.id) && x.code.Equals(y.code);
  696. }
  697. public int GetHashCode((string id, string code) obj)
  698. {
  699. if (obj.id != null && obj.code != null)
  700. {
  701. return 1;
  702. }
  703. else
  704. {
  705. return 0;
  706. }
  707. }
  708. }
  709. }