GroupListService.cs 59 KB

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