GroupListService.cs 67 KB

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