GroupListService.cs 61 KB

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