GroupListService.cs 72 KB

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