GroupListService.cs 72 KB

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