GroupListService.cs 75 KB

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