GroupListService.cs 94 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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 HTEXLib.COMM.Helpers;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Text.Json;
  11. using TEAMModelOS.Models;
  12. using Azure.Messaging.ServiceBus;
  13. using Microsoft.Extensions.Configuration;
  14. using System.Text.RegularExpressions;
  15. using TEAMModelOS.SDK.Models;
  16. using System.Net.Http;
  17. using DocumentFormat.OpenXml.Drawing.Charts;
  18. using System.Reflection;
  19. namespace TEAMModelOS.SDK
  20. {
  21. public class GroupListService
  22. {
  23. public static async Task DeleteGrouplistEvent(string id , string code,string tbname ,CosmosClient client, IConfiguration _configuration, AzureServiceBusFactory _serviceBus)
  24. {
  25. GroupChange change = new GroupChange();
  26. GroupList groupList = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<GroupList>(id.ToString(), new PartitionKey(code));
  27. var tleave = groupList.members.FindAll(x => x.type == 1);
  28. if (tleave.IsNotEmpty())
  29. {
  30. if (groupList.type.Equals("research") || groupList.type.Equals("yxtrain"))
  31. {
  32. change.tchleave.AddRange(tleave);
  33. }
  34. else
  35. {
  36. change.tmdleave.AddRange(tleave);
  37. }
  38. }
  39. var sleave = groupList.members.FindAll(x => x.type == 2);
  40. if (sleave.IsNotEmpty())
  41. {
  42. change.stuleave.AddRange(sleave);
  43. }
  44. change.listid = groupList.id;
  45. change.scope = groupList.scope;
  46. change.originCode = $"{code}";
  47. change.school = groupList.school;
  48. change.creatorId = groupList.creatorId;
  49. change.type = groupList.type;
  50. change.status = "delete";
  51. if (change.tmdleave.IsNotEmpty() || change.tchleave.IsNotEmpty() || change.stuleave.IsNotEmpty())
  52. {
  53. var messageChange = new ServiceBusMessage(change.ToJsonString());
  54. messageChange.ApplicationProperties.Add("name", "GroupChange");
  55. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  56. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  57. }
  58. await client.GetContainer(Constant.TEAMModelOS, tbname).DeleteItemStreamAsync(id.ToString(), new PartitionKey(code));
  59. }
  60. /// <summary>
  61. /// 查询名单成员所在的名单列表,
  62. /// </summary>
  63. /// <param name="_coreAPIHttpService"></param>
  64. /// <param name="client"></param>
  65. /// <param name="_dingDing"></param>
  66. /// <param name="memberId">成员id </param>
  67. /// <param name="memberType">成员类型</param>
  68. /// <param name="school">成员所在的学校 ,可为空</param>
  69. /// <param name="groupTypes">过滤名单的类型集合,不传则是全部</param>
  70. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  71. /// <returns></returns>
  72. public static async Task<List<GroupListGrp>> GetMemberInGroupList(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, DingDing _dingDing, string memberId, int memberType, string school,List<string> groupTypes,int graduate =-1,long time =-1)
  73. {
  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. List<GroupListSemester> groupListSemesters = new List<GroupListSemester>();
  176. List<RMember> rmembers = new List<RMember>();
  177. List<RGroupList> rgroupList = new List<RGroupList>();
  178. List<GroupListDto> groupListDtos = new List<GroupListDto>();
  179. if (!string.IsNullOrWhiteSpace(school) && time>0 && groupLists.IsNotEmpty())
  180. {
  181. School schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
  182. foreach (var period in schoolBase.period)
  183. {
  184. var dataSemester = SchoolService.GetSemester(period,time);
  185. //string id = $"{dataSemester.studyYear}-{dataSemester.currSemester.id}-{student.classId}";
  186. string code = $"GroupListSemester-{school}";
  187. string sql = $"select value c from c join m in c.members where m.id='{memberId}' and m.type={memberType} and c.studyYear={dataSemester.studyYear} and c.semesterId='{dataSemester.currSemester.id}' and c.groupListId not in({string.Join(",", groupLists.Select(c => $"'{c.id}'"))}) ";
  188. var result = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<GroupListSemester>(sql, code);
  189. if (result.list.IsNotEmpty())
  190. {
  191. foreach (var x in result.list) {
  192. HashSet<string> groupName = x.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  193. groupLists.Add(new GroupListGrp {
  194. id = x.id,
  195. code = x.code,
  196. periodId = x.periodId,
  197. pk = x.pk,
  198. name = x.name,
  199. school = x.school,
  200. creatorId = x.creatorId,
  201. no = x.no,
  202. scope = x.scope,
  203. type = x.type,
  204. scount = x.scount,
  205. tcount = x.tcount,
  206. leader = x.leader,
  207. year = x.year,
  208. expire = x.expire,
  209. groupName = groupName,
  210. froms = x.froms,
  211. joinLock = x.joinLock,
  212. qrcodeExpire = x.qrcodeExpire,
  213. qrcodeDays = x.qrcodeDays,
  214. review=x.review,
  215. limitCount = x.limitCount,
  216. optNo = x.optNo,
  217. grades = x.grades,
  218. });
  219. }
  220. }
  221. }
  222. }
  223. return groupLists;
  224. }
  225. /// <summary>
  226. ///-1状态异常,0加入成功, 1加入学生或醍摩豆ID为空,2重复加入 ,3不允许跨校加入名单, 4表示个人名单未开放加入,状态=5 ,人数已满。状态=6 ,需要审核通过再加入。状态7 二维码设置已经过期
  227. /// </summary>
  228. /// <param name="client"></param>
  229. /// <param name="_stuListNo"></param>
  230. /// <param name="userid"></param>
  231. /// <param name="type"></param>
  232. /// <param name="school"></param>
  233. /// <returns></returns>
  234. 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,string courseId= null)
  235. {
  236. var queryNo = $"SELECT value(c) FROM c where c.no ='{_stuListNo}'";
  237. (int status, GroupList stuList,Member member) data = (-1, null,null);
  238. if (!string.IsNullOrEmpty(school))
  239. {
  240. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupList>(queryText: queryNo,
  241. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  242. {
  243. data = JoinList(item, userid, type, school,year);
  244. break;
  245. }
  246. }
  247. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<GroupList>(queryText: queryNo,
  248. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  249. {
  250. if (item.joinLock == 0) {
  251. //状态=4 表示未开放加入。
  252. return (4, item, null) ;
  253. }
  254. if (item.review ==1)
  255. {
  256. //状态=6 ,人数已满,需要审核通过再加入。
  257. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  258. if (now>item.qrcodeExpire)
  259. {
  260. //二维码设置已经过期,只有需要审核的时候才检查二维码过期问题,目的是为了未正式加入人员的缓存
  261. return (7, item, null);
  262. }
  263. else
  264. {
  265. GroupWaitingList waitingList = new GroupWaitingList
  266. {
  267. stuListNo=_stuListNo,
  268. userid=userid,
  269. type=type,
  270. school= school,
  271. year = year,
  272. groupId = item.id,
  273. groupScope=item.scope,
  274. name = name,
  275. picture = picture,
  276. lang = lang,
  277. qrcodeExpire=item.qrcodeExpire,
  278. groupName= item.name,
  279. applyTime=now
  280. };
  281. string key = $"GroupList:GroupWaitingList:{item.scope}:{item.id}";
  282. string filed = !string.IsNullOrWhiteSpace(school) ? $"{school}_{userid}" : userid;
  283. await _azureRedis.GetRedisClient(8).HashSetAsync(key, filed, waitingList.ToJsonString());
  284. await _azureRedis.GetRedisClient(8).KeyExpireAsync(key, expiry: DateTimeOffset.FromUnixTimeMilliseconds(item.qrcodeExpire).UtcDateTime);
  285. string stuKey = !string.IsNullOrWhiteSpace(school) ? $"GroupList:StudentWaitinglist:{school}_{userid}" :$"GroupList:StudentWaitinglist:{userid}" ;
  286. string sutFiled = $"{item.scope}:{item.id}";
  287. await _azureRedis.GetRedisClient(8).HashSetAsync(stuKey, sutFiled, waitingList.ToJsonString());
  288. //三十天都不登录,则不需要查看申请清单,30*24=720
  289. await _azureRedis.GetRedisClient(8).KeyExpireAsync(stuKey, new TimeSpan(720 , 0, 0));
  290. return (6, item, null);
  291. }
  292. }
  293. if (item.members!=null &&item.limitCount>0 && item.members.Count>=item.limitCount) {
  294. //状态=5 ,人数已满
  295. return (5, item, null);
  296. }
  297. data = JoinList(item, userid, type, school,year);
  298. //TODO 需要考虑已经通过别的途径加入名单,但是缓存数据仍记录数据的问题。 还要考虑手动加入过的。或者在获取名未加入名单成员的临时缓存数据的时候过滤已经加入的。
  299. break;
  300. }
  301. return data;
  302. }
  303. /// <summary>
  304. /// 加入名单等候列表
  305. /// </summary>
  306. public class GroupWaitingList
  307. {
  308. /// <summary>
  309. ///
  310. /// </summary>
  311. public string stuListNo { get; set; }
  312. public string userid { get; set; }
  313. //学生类型,1醍摩豆id,2 校内账号
  314. public int type { get; set; }
  315. public string school { get; set; }
  316. public int year { get; set; }
  317. public string groupId { get; set; }
  318. public string groupScope { get; set; }
  319. public string name { get; set; }
  320. public string picture { get; set; }
  321. public string lang { get; set; }
  322. public long qrcodeExpire { get; set; }
  323. public string groupName { get; set; }
  324. public long applyTime{ get; set; }
  325. /// <summary>
  326. /// 申请状态,-1 申请中,0 通过,1 拒绝,2 已过期。
  327. /// </summary>
  328. public int status { get; set; } = -1;
  329. public List<IdName> courses { get; set; }= new List<IdName>();
  330. }
  331. public static (int status, GroupList stuList, Member member) JoinList(GroupList stuList, string userid, int type, string school,int year)
  332. {
  333. int status = -1;
  334. if (!string.IsNullOrWhiteSpace(stuList.school) && !string.IsNullOrWhiteSpace(school))
  335. {
  336. if (!stuList.school.Equals(school))
  337. {
  338. status = 3;//不允许跨校加入名单
  339. return (status, stuList,null);
  340. }
  341. }
  342. string irs = string.Empty;
  343. List<string> irsOrder = stuList.members.Select(x => x.irs)?.Where(y => !string.IsNullOrEmpty(y) && Regex.IsMatch(y, @"^\d*$")).OrderBy(x => int.Parse(x)).ToList();
  344. if (!irsOrder.Contains("0"))
  345. {
  346. irsOrder.Insert(0, "0");
  347. }
  348. if (irsOrder != null)
  349. {
  350. if (!irsOrder.Contains("0"))
  351. {
  352. irsOrder.Insert(0, "0");
  353. }
  354. }
  355. else { irsOrder = new List<string>() { "0" }; }
  356. for (int i = 0; i < irsOrder.Count; i++)
  357. {
  358. irs = $"{int.Parse(irsOrder[i]) + 1}";
  359. int index = i + 1;
  360. if (index <= irsOrder.Count - 1)
  361. {
  362. if (!irs.Equals(irsOrder[index]))
  363. {
  364. break;
  365. }
  366. }
  367. }
  368. Member member = null;
  369. if (string.IsNullOrEmpty($"{userid}"))
  370. {
  371. //加入学生或醍摩豆ID为空
  372. status = 1;
  373. }
  374. else
  375. {
  376. if (type == 1)
  377. {
  378. member = stuList.members.Find(x => x.type == 1 && x.id.Equals(userid));
  379. if (member != null)
  380. {
  381. //重复加入
  382. status = 2;
  383. }
  384. else
  385. {
  386. //加入成功
  387. status = 0;
  388. member = new Member { id = userid, type = type, irs = irs, no = irs,year=year };
  389. stuList.members.Add(member);
  390. }
  391. }
  392. else if (type == 2)
  393. {
  394. member = stuList.members.Find(x => x.type == 2 && x.id.Equals(userid) && x.code.Equals(school));
  395. if (member != null)
  396. {
  397. //重复加入
  398. status = 2;
  399. }
  400. else
  401. {
  402. status = 0;
  403. member = new Member { id = userid, code = school, type = type, irs = irs, no = irs, year = year };
  404. stuList.members.Add(member);
  405. }
  406. }
  407. }
  408. return (status, stuList, member);
  409. }
  410. public static async Task<GroupList> UpsertList(GroupList list, AzureCosmosFactory _azureCosmos, IConfiguration _configuration, AzureServiceBusFactory _serviceBus)
  411. {
  412. bool isnew = false;
  413. var client = _azureCosmos.GetCosmosClient();
  414. if (string.IsNullOrEmpty(list.id))
  415. {
  416. list.id = Guid.NewGuid().ToString();
  417. isnew = true;
  418. }
  419. string tbname = list.scope.Equals("private") ? "Teacher" : "School";
  420. var tmembers = list.members.Where(x => x.type == 1);
  421. var smembers = list.members.Where(x => x.type == 2);
  422. //处理年级
  423. HashSet<int> grades = new HashSet<int>();
  424. if (smembers.Any()) {
  425. grades = smembers.Where(z => z.year > 2000).Select(z=>z.year).ToHashSet();
  426. }
  427. list.grades = grades;
  428. list.scount = smembers.Count();
  429. list.tcount = tmembers.Count();
  430. //if (smembers.Count() > 0 && smembers.Select(x => x.code).ToHashSet().Count()>=2) {
  431. // //处理移除多个学校的名单,只保留一个学校的。
  432. // if (string.IsNullOrWhiteSpace(list.school))
  433. // {
  434. // HashSet<string> codes = smembers.Select(x => x.code).ToHashSet();
  435. // list.school = codes.First();
  436. // codes.Remove(codes.First());
  437. // list.members.RemoveAll(x => codes.Contains(x.code));
  438. // }
  439. // else {
  440. // list.members.RemoveAll(x => !x.code.Equals(list.school));
  441. // }
  442. //}
  443. if (string.IsNullOrWhiteSpace(list.school) && smembers.Count() >= 1)
  444. {
  445. list.school = smembers.First().code;
  446. }
  447. if (!string.IsNullOrWhiteSpace(list.school) && smembers.Count() == 0 && list.scope.Equals("private"))
  448. {
  449. list.school = null;
  450. }
  451. //学生名单,教研组会触发活动中间表刷新
  452. if (list.type.Equals("teach") || list.type.Equals("research") || list.type.Equals("yxtrain") || list.type.Equals("activity"))
  453. {
  454. GroupChange change = new GroupChange()
  455. {
  456. type = list.type,
  457. listid = list.id,
  458. scope = list.scope,
  459. originCode = list.school,
  460. school = list.school,
  461. creatorId = list.creatorId
  462. };
  463. GroupList oldList = null;
  464. if (!isnew)
  465. {
  466. try
  467. {
  468. oldList = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemAsync<GroupList>(list.id, new PartitionKey(list.code));
  469. }
  470. catch (CosmosException)
  471. {
  472. oldList = null;
  473. }
  474. }
  475. if (list.members.IsNotEmpty() && (oldList == null || !oldList.members.IsNotEmpty()))
  476. {
  477. //加入的
  478. var tmdids = list.members.FindAll(x => x.type == 1);
  479. if (tmdids.IsNotEmpty())
  480. {
  481. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  482. {
  483. change.tchjoin.AddRange(tmdids);
  484. }
  485. else
  486. {
  487. change.tmdjoin.AddRange(tmdids);
  488. }
  489. }
  490. var stuids = list.members.FindAll(x => x.type == 2);
  491. if (stuids.IsNotEmpty())
  492. {
  493. change.stujoin.AddRange(stuids);
  494. }
  495. }
  496. else
  497. {
  498. if (list.members.IsNotEmpty())
  499. {
  500. var tmdids = list.members.FindAll(x => x.type == 1);
  501. var oldtmdids = oldList.members.FindAll(x => x.type == 1);
  502. //取各自的差集
  503. //新=》旧差集,表示新增
  504. var jointmdid = tmdids.Select(x => x.id).Except(oldtmdids.Select(y => y.id)).ToList();
  505. //旧=》新差集,表示离开
  506. var leavetmdid = oldtmdids.Select(x => x.id).Except(tmdids.Select(y => y.id)).ToList();
  507. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  508. {
  509. if (tmdids!=null && jointmdid!=null)
  510. {
  511. change.tchjoin.AddRange(tmdids.Where(x => jointmdid.Exists(y => y.Equals(x.id))));
  512. }
  513. if (oldtmdids!= null &&leavetmdid != null) {
  514. change.tchleave.AddRange(oldtmdids.Where(x => leavetmdid.Exists(y => y.Equals(x.id))));
  515. }
  516. }
  517. else
  518. {
  519. if (tmdids!=null && jointmdid!=null)
  520. {
  521. change.tmdjoin.AddRange(tmdids.Where(x => jointmdid.Exists(y => y.Equals(x.id))));
  522. }
  523. if (oldtmdids!= null &&leavetmdid != null)
  524. {
  525. change.tmdleave.AddRange(oldtmdids.Where(x => leavetmdid.Exists(y => y.Equals(x.id))));
  526. }
  527. }
  528. var stuids = list.members.FindAll(x => x.type == 2);
  529. var oldstuids = oldList.members.FindAll(x => x.type == 2);
  530. var joinstudent = stuids.Select(x => (x.id, x.code)).Except(oldstuids.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  531. var leavestudent = oldstuids.Select(x => (x.id, x.code)).Except(stuids.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  532. change.stujoin.AddRange(stuids.Where(x => joinstudent.Exists(y => y.id.Equals(x.id) && y.code.Equals(x.code))));
  533. change.stuleave.AddRange(oldstuids.Where(x => leavestudent.Exists(y => y.id.Equals(x.id) && y.code.Equals(x.code))));
  534. }
  535. else
  536. {
  537. //离开的
  538. if (oldList != null)
  539. {
  540. var tmdids = oldList.members.FindAll(x => x.type == 1);
  541. if (tmdids.IsNotEmpty())
  542. {
  543. if (list.type.Equals("research") || list.type.Equals("yxtrain"))
  544. {
  545. change.tchleave.AddRange(tmdids);
  546. }
  547. else
  548. {
  549. change.tmdleave.AddRange(tmdids);
  550. }
  551. }
  552. var stuids = oldList.members.FindAll(x => x.type == 2);
  553. if (stuids.IsNotEmpty())
  554. {
  555. change.stuleave.AddRange(stuids);
  556. }
  557. }
  558. }
  559. }
  560. if (change.tmdjoin.Count != 0 || change.tmdleave.Count != 0 || change.stujoin.Count != 0 || change.stuleave.Count != 0
  561. || change.tchjoin.Count != 0 || change.tchleave.Count != 0)
  562. {
  563. var messageChange = new ServiceBusMessage(change.ToJsonString());
  564. messageChange.ApplicationProperties.Add("name", "GroupChange");
  565. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  566. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  567. }
  568. }
  569. await client.GetContainer(Constant.TEAMModelOS, tbname).UpsertItemAsync(list, new PartitionKey(list.code));
  570. return list;
  571. }
  572. public static async Task<GroupList> CheckListNo(GroupList list, AzureCosmosFactory _azureCosmos, DingDing _dingDing, Option _option)
  573. {
  574. try
  575. {
  576. var client = _azureCosmos.GetCosmosClient();
  577. if (string.IsNullOrEmpty(list.no))
  578. {
  579. list.no = $"{Utils.CreatSaltString(6, "123456789")}";
  580. for (int i = 0; i < 10; i++)
  581. {
  582. List<string> noStus = new List<string>();
  583. var queryNo = $"SELECT c.no FROM c where c.no ='{list.no}'";
  584. if (list.scope.Equals("school"))
  585. {
  586. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: queryNo,
  587. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"{list.code}") }))
  588. {
  589. using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
  590. if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  591. {
  592. var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
  593. while (accounts.MoveNext())
  594. {
  595. JsonElement account = accounts.Current;
  596. noStus.Add(account.GetProperty("no").GetString());
  597. }
  598. }
  599. }
  600. }
  601. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: queryNo,
  602. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey("GroupList") }))
  603. {
  604. using var jsonNo = await JsonDocument.ParseAsync(item.ContentStream);
  605. if (jsonNo.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  606. {
  607. var accounts = jsonNo.RootElement.GetProperty("Documents").EnumerateArray();
  608. while (accounts.MoveNext())
  609. {
  610. JsonElement account = accounts.Current;
  611. noStus.Add(account.GetProperty("no").GetString());
  612. }
  613. }
  614. }
  615. if (noStus.Count == 0)
  616. {
  617. break;
  618. }
  619. else
  620. {
  621. if (i == 9)
  622. {
  623. string msg = $"OS,{_option.Location},school/course/upsert-list()\n 编号生成异常,重复生成次数超过10次";
  624. await _dingDing.SendBotMsg(msg, GroupNames.醍摩豆服務運維群組);
  625. throw new Exception(msg);
  626. }
  627. else
  628. {
  629. list.no = $"{Utils.CreatSaltString(6, "123456789")}";
  630. }
  631. }
  632. }
  633. }
  634. }
  635. catch (Exception ex)
  636. {
  637. }
  638. return list;
  639. }
  640. /// <summary>
  641. ///根据任意名单id获取名单摘要信息。
  642. /// </summary>
  643. /// <param name="client"></param>
  644. /// <param name="_dingDing"></param>
  645. /// <param name="classes"></param>
  646. /// <param name="school"></param>
  647. /// <param name="SummarySql"></param>
  648. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  649. /// <returns></returns>
  650. public static async Task<List<GroupListDto>> GetGroupListByListids(CosmosClient client, DingDing _dingDing, List<string> classes, string school,
  651. 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,long time=-1)
  652. {
  653. List<GroupListDto> groupLists = new List<GroupListDto>();
  654. try
  655. {
  656. classes.RemoveAll(x => x == null);
  657. if (!classes.IsNotEmpty())
  658. {
  659. return null;
  660. }
  661. if (classes.Count == 1 && classes.First().Equals("TeacherAll") && !string.IsNullOrEmpty(school))
  662. {
  663. //默认的教研组
  664. GroupListDto groupList = new GroupListDto
  665. {
  666. id = "TeacherAll",
  667. name = "TeacherAll",
  668. code = $"GroupList-{school}",
  669. school = school,
  670. scope = "school",
  671. type = "yxtrain",
  672. };
  673. groupLists = new List<GroupListDto> { groupList };
  674. }
  675. else
  676. {
  677. Dictionary<string, List<GroupListDto>> groups = new Dictionary<string, List<GroupListDto>>();
  678. var semesterGroupList = await GetGroupListSemester(client, classes, school, type: null, periodId: null, no: null, time);
  679. groupLists.AddRange(semesterGroupList.groupListDtos);
  680. classes = classes.Except(semesterGroupList.rgroupList.Select(y => y.id)).ToList();
  681. if (classes.IsNotEmpty())
  682. {
  683. //List<Student> students = new List<Student>();
  684. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  685. if (!string.IsNullOrEmpty(school))
  686. {
  687. List<GroupListDto> schoolList = new List<GroupListDto>();
  688. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupListDto>(queryText: $"select {SummarySql} from c where c.id in ({sql})",
  689. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  690. {
  691. schoolList.Add(item);
  692. }
  693. if (schoolList.IsNotEmpty())
  694. {
  695. groups.Add("School", schoolList);
  696. }
  697. //取差集,减少二次搜寻
  698. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  699. if (classes.IsNotEmpty())
  700. {
  701. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  702. //搜寻没有关联学生的行政班
  703. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<GroupListDto>(queryText:
  704. $"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})",
  705. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  706. {
  707. ///行政班(学生搜寻classId动态返回)class
  708. GroupListDto group = new GroupListDto
  709. {
  710. id = item.id,
  711. code = $"GroupList-{school}",
  712. name = item.name,
  713. periodId = item.periodId,
  714. scope = "school",
  715. school = school,
  716. type = "class",
  717. year = item.year,
  718. expire = item.expire,
  719. leader = item.leader,
  720. no = item.no,
  721. pk = "GroupList",
  722. graduate = item.graduate,
  723. grades= new HashSet<int> { item.year }
  724. };
  725. //graduate传入的状态>=0 表示指定状态查询,且传入的状态与数据库的状态一致。
  726. if (graduate >= 0)
  727. {
  728. if (group.graduate == graduate)
  729. {
  730. groupLists.Add(group);
  731. }
  732. }
  733. //全部。
  734. else
  735. {
  736. groupLists.Add(group);
  737. }
  738. // groupLists.Add(group);
  739. }
  740. //取差集,减少二次搜寻
  741. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  742. }
  743. }
  744. }
  745. if (classes.IsNotEmpty())
  746. {
  747. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  748. List<GroupListDto> privateList = new List<GroupListDto>();
  749. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  750. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<GroupListDto>(queryText: $"select {SummarySql} from c where c.id in ({sql})",
  751. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  752. {
  753. privateList.Add(item);
  754. //if (string.IsNullOrWhiteSpace(school))
  755. //{
  756. // privateList.Add(item);
  757. //}
  758. //else
  759. //{
  760. // if (!string.IsNullOrWhiteSpace(item.school))
  761. // {
  762. // if (item.school.Equals(school))
  763. // {
  764. // privateList.Add(item);
  765. // }
  766. // }
  767. // else
  768. // {
  769. // privateList.Add(item);
  770. // }
  771. //}
  772. }
  773. if (privateList.IsNotEmpty())
  774. {
  775. groups.Add("Teacher", privateList);
  776. }
  777. }
  778. if (groups.Count != 0)
  779. {
  780. if (groupLists.IsNotEmpty())
  781. {
  782. groupLists.AddRange(groups.SelectMany(x => x.Value).ToList());
  783. }
  784. else
  785. {
  786. groupLists = groups.SelectMany(x => x.Value).ToList();
  787. }
  788. }
  789. }
  790. } catch (Exception ex ) {
  791. await _dingDing.SendBotMsg($"{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  792. }
  793. return groupLists;
  794. }
  795. public static async Task<(List<GroupListSemester> groupListSemesters, List<RMember> rmembers, List<RGroupList> rgroupList, List<GroupListDto> groupListDtos)>
  796. GetGroupListSemester(CosmosClient client, List<string> classes, string school,string type,string periodId,string no , long time =-1) {
  797. List<GroupListSemester> groupListSemesters = new List<GroupListSemester>();
  798. List<RMember> rmembers = new List<RMember>();
  799. List<RGroupList> rgroupList = new List<RGroupList>();
  800. List<GroupListDto> groupListDtos = new List<GroupListDto>();
  801. if (!string.IsNullOrWhiteSpace(school) && time>0) {
  802. School schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
  803. foreach (var period in schoolBase.period) {
  804. var dataSemester = SchoolService.GetSemester(period, time);
  805. //string id = $"{dataSemester.studyYear}-{dataSemester.currSemester.id}-{student.classId}";
  806. string code = $"GroupListSemester-{school}";
  807. StringBuilder sql =new StringBuilder($"select value c from c where c.studyYear={dataSemester.studyYear} and c.semesterId='{dataSemester.currSemester.id}' ");
  808. if (classes.IsNotEmpty())
  809. {
  810. sql.Append($" and c.groupListId in({string.Join(",", classes.Select(c => $"'{c}'"))})");
  811. }
  812. if (!string.IsNullOrWhiteSpace(type)) {
  813. sql.Append($" and c.type ='{type}'");
  814. }
  815. if (!string.IsNullOrWhiteSpace(periodId))
  816. {
  817. sql.Append($" and c.periodId ='{periodId}'");
  818. }
  819. if (!string.IsNullOrWhiteSpace(no))
  820. {
  821. sql.Append($" and c.no ='{no}'");
  822. }
  823. var result = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<GroupListSemester>(sql.ToString(), code);
  824. if (result.list.IsNotEmpty()) {
  825. groupListSemesters.AddRange(result.list);
  826. }
  827. }
  828. if (groupListSemesters.IsNotEmpty()) {
  829. rmembers.AddRange(groupListSemesters.SelectMany(x => x.members));
  830. rmembers= rmembers.FindAll(x => x.type == 2).Where((x, i) => rmembers.FindAll(x => x.type == 2).FindIndex(n => n.id.Equals(x.id) && n.code.Equals(x.code)) == i).ToList();
  831. rgroupList.AddRange(groupListSemesters.Select(z => new RGroupList
  832. {
  833. id = z.groupListId,
  834. code= $"GroupList-{school}",
  835. pk="GroupList",
  836. name=z.name,
  837. no=z.no,
  838. periodId=z.periodId,
  839. school=z.school,
  840. scope=z.scope,
  841. creatorId=z.creatorId,
  842. type=z.type,
  843. year=z.year,
  844. expire=z.expire,
  845. tcount=z.tcount,
  846. scount=z.scount,
  847. members=z.members,
  848. leader=z.leader,
  849. froms=z.froms,
  850. joinLock=z.joinLock,
  851. graduate=z.graduate,
  852. review=z.review,
  853. optNo=z.optNo,
  854. limitCount=z.limitCount,
  855. qrcodeDays=z.qrcodeDays,
  856. qrcodeExpire=z.qrcodeExpire,
  857. grades =z.grades,
  858. }));
  859. groupListDtos.AddRange(groupListSemesters.Select(z => new GroupListDto
  860. {
  861. id = z.groupListId,
  862. code= $"GroupList-{school}",
  863. pk="GroupList",
  864. name=z.name,
  865. no=z.no,
  866. periodId=z.periodId,
  867. school=z.school,
  868. scope=z.scope,
  869. creatorId=z.creatorId,
  870. type=z.type,
  871. year=z.year,
  872. expire=z.expire,
  873. tcount=z.tcount,
  874. scount=z.scount,
  875. leader=z.leader,
  876. froms=z.froms,
  877. joinLock=z.joinLock,
  878. graduate=z.graduate,
  879. review=z.review,
  880. optNo=z.optNo,
  881. limitCount=z.limitCount,
  882. qrcodeDays=z.qrcodeDays,
  883. qrcodeExpire=z.qrcodeExpire,
  884. grades =z.grades,
  885. }));
  886. }
  887. }
  888. return (groupListSemesters,rmembers, rgroupList,groupListDtos);
  889. }
  890. /// <summary>
  891. /// 根据任意名单id获取成员信息。rmembers是去重的信息,groups是名单及人员信息,同一个人可能在不同的名单内。
  892. /// </summary>
  893. /// <param name="_coreAPIHttpService"></param>
  894. /// <param name="client"></param>
  895. /// <param name="_dingDing"></param>
  896. /// <param name="classes"></param>
  897. /// <param name="school"></param>
  898. /// <param name="groupids"></param>
  899. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  900. /// <returns></returns>
  901. public static async Task<(List<RMember>rmembers, List<RGroupList> groups)> GetMemberByListids(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, DingDing _dingDing,
  902. List<string> classes, string school, List<(string, List<string>)> groupids = null, int graduate = -1,long time =-1)
  903. {
  904. List<RMember> members = new List<RMember>();
  905. List<RGroupList> groupLists = new List<RGroupList>();
  906. if (classes != null)
  907. {
  908. classes.RemoveAll(x => x == null);
  909. }
  910. if (classes == null || classes.Count <= 0)
  911. {
  912. return (members, groupLists);
  913. }
  914. string periodId = string.Empty;
  915. if (classes.Count == 1 && classes.First().Equals("TeacherAll") && !string.IsNullOrEmpty(school))
  916. {
  917. //默认的教研组
  918. // members = new List<RMember>();
  919. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  920. GetItemQueryIterator<TmdInfo>(queryText: $"SELECT c.id,c.name,c.picture FROM c ",
  921. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  922. {
  923. RMember member = new RMember
  924. {
  925. id = item.id,
  926. name = item.name,
  927. picture = item.picture,
  928. type = 1,
  929. // nickname=item.name,
  930. };
  931. members.Add(member);
  932. }
  933. RGroupList groupList = new RGroupList
  934. {
  935. id = "TeacherAll",
  936. name = "TeacherAll",
  937. code = $"GroupList-{school}",
  938. school = school,
  939. scope = "school",
  940. type = "TeacherAll",
  941. members = members
  942. };
  943. groupLists = new List<RGroupList> { groupList };
  944. }
  945. else
  946. {
  947. var semesterGroupList = await GetGroupListSemester(client, classes, school,type:null,periodId:null ,no:null, time);
  948. classes = classes.Except(semesterGroupList.rgroupList.Select(y => y.id)).ToList();
  949. if (classes.IsNotEmpty()) {
  950. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  951. List<Student> students = new List<Student>();
  952. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  953. if (!string.IsNullOrEmpty(school))
  954. {
  955. List<RGroupList> schoolList = new List<RGroupList>();
  956. string queryText = $"select value(c) from c where c.id in ({sql})";
  957. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: queryText,
  958. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  959. {
  960. schoolList.Add(item);
  961. }
  962. if (schoolList.IsNotEmpty())
  963. {
  964. groups.Add("School", schoolList);
  965. }
  966. //取差集,减少二次搜寻
  967. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  968. if (classes.IsNotEmpty())
  969. {
  970. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  971. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  972. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  973. {
  974. //graduate传入的状态>=0 表示指定状态查询,且传入的状态与数据库的状态一致。
  975. if (graduate >= 0)
  976. {
  977. if (item.graduate == graduate)
  978. {
  979. students.Add(item);
  980. }
  981. }
  982. //全部。
  983. else
  984. {
  985. students.Add(item);
  986. }
  987. // students.Add(item);
  988. }
  989. //取差集,减少二次搜寻
  990. classes = classes.Except(students.Select(y => y.classId)).ToList();
  991. }
  992. if (classes.IsNotEmpty())
  993. {
  994. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  995. //搜寻没有关联学生的行政班
  996. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School")
  997. .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})",
  998. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  999. {
  1000. ///行政班(学生搜寻classId动态返回)class
  1001. List<RMember> smembers = new List<RMember>();
  1002. RGroupList group = new RGroupList
  1003. {
  1004. id = item.id,
  1005. code = $"GroupList-{school}",
  1006. name = item.name,
  1007. periodId = item.periodId,
  1008. scope = "school",
  1009. school = school,
  1010. type = "class",
  1011. year = item.year,
  1012. expire = item.expire,
  1013. members = smembers,
  1014. scount = smembers.Count,
  1015. pk = "GroupList",
  1016. leader = item.leader,
  1017. no = item.no,
  1018. graduate = item.graduate,
  1019. grades= new HashSet<int>() { item.year }
  1020. };
  1021. //graduate传入的状态>=0 表示指定状态查询,且传入的状态与数据库的状态一致。
  1022. if (graduate >= 0)
  1023. {
  1024. if (group.graduate == graduate)
  1025. {
  1026. groupLists.Add(group);
  1027. }
  1028. }
  1029. //全部。
  1030. else
  1031. {
  1032. groupLists.Add(group);
  1033. }
  1034. //groupLists.Add(group);
  1035. }
  1036. //取差集,减少二次搜寻
  1037. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  1038. }
  1039. }
  1040. if (classes.IsNotEmpty())
  1041. {
  1042. List<RGroupList> privateList = new List<RGroupList>();
  1043. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  1044. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: $"select value(c) from c where c.id in ({sql})",
  1045. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  1046. {
  1047. privateList.Add(item);
  1048. //if (string.IsNullOrWhiteSpace(school))
  1049. //{
  1050. // privateList.Add(item);
  1051. //}
  1052. //else
  1053. //{
  1054. // if (!string.IsNullOrWhiteSpace(item.school))
  1055. // {
  1056. // if (item.school.Equals(school))
  1057. // {
  1058. // privateList.Add(item);
  1059. // }
  1060. // }
  1061. // else
  1062. // {
  1063. // privateList.Add(item);
  1064. // }
  1065. //}
  1066. }
  1067. if (privateList.IsNotEmpty())
  1068. {
  1069. groups.Add("Teacher", privateList);
  1070. }
  1071. }
  1072. foreach (var item in groups)
  1073. {
  1074. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  1075. foreach (var group in list)
  1076. {
  1077. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school, graduate);
  1078. members.AddRange(rmembers);
  1079. }
  1080. }
  1081. groupLists.AddRange(groups.SelectMany(x => x.Value).ToList());
  1082. if (students.IsNotEmpty())
  1083. {
  1084. List<string> sqlList = students.Select(x => x.classId).ToList();
  1085. string insql = string.Join(",", sqlList.Select(x => $"'{x}'"));
  1086. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  1087. 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})",
  1088. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  1089. {
  1090. ///行政班(学生搜寻classId动态返回)class
  1091. List<RMember> smembers = students.Where(x => x.classId.Equals(item.id))
  1092. .Select(y => new RMember
  1093. {
  1094. id = y.id,
  1095. code = school,
  1096. name = y.name,
  1097. //nickname = y.name,
  1098. type = 2,
  1099. picture = y.picture,
  1100. no = y.no,
  1101. classId = y.classId,
  1102. groupId = y.groupId,
  1103. groupName = y.groupName,
  1104. irs = y.irs,
  1105. year= y.year,
  1106. graduate = y.graduate,
  1107. gender= y.gender,
  1108. periodId=y.periodId,
  1109. }).ToList();
  1110. members.AddRange(smembers);
  1111. RGroupList group = new RGroupList
  1112. {
  1113. id = item.id,
  1114. code = $"GroupList-{school}",
  1115. name = item.name,
  1116. periodId = item.periodId,
  1117. scope = "school",
  1118. school = school,
  1119. type = "class",
  1120. expire= item.expire,
  1121. year = item.year,
  1122. members = smembers,
  1123. scount = smembers.Count,
  1124. no = item.no,
  1125. leader = item.leader,
  1126. pk = "GroupList",
  1127. graduate = item.graduate,
  1128. grades= new HashSet<int> { item.year }
  1129. };
  1130. //graduate传入的状态>=0 表示指定状态查询,且传入的状态与数据库的状态一致。
  1131. if (graduate >= 0)
  1132. {
  1133. if (group.graduate == graduate)
  1134. {
  1135. groupLists.Add(group);
  1136. }
  1137. }
  1138. //全部。
  1139. else
  1140. {
  1141. groupLists.Add(group);
  1142. }
  1143. // groupLists.Add(group);
  1144. }
  1145. //去重。
  1146. 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();
  1147. }
  1148. }
  1149. }
  1150. if (groupids.IsNotEmpty())
  1151. {
  1152. List<RMember> rmembers = new List<RMember>();
  1153. groupLists.ForEach(y => {
  1154. (string id, List<string> grpids) = groupids.Find(x => x.Item1.Equals(y.id));
  1155. var gpmember = y.members.FindAll(x => !string.IsNullOrEmpty(x.groupName) && grpids.Contains(x.groupName));
  1156. if (grpids.Contains("default"))
  1157. {
  1158. var gpmemberdft = y.members.FindAll(x => string.IsNullOrWhiteSpace(x.groupName));
  1159. if (gpmember.IsNotEmpty())
  1160. {
  1161. gpmember.AddRange(gpmemberdft);
  1162. }
  1163. else
  1164. {
  1165. gpmember = gpmemberdft;
  1166. }
  1167. }
  1168. y.members = gpmember;
  1169. });
  1170. var gpgpmembers = groupLists.SelectMany(x => x.members).ToList();
  1171. 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();
  1172. 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();
  1173. if (tmdids.IsNotEmpty())
  1174. {
  1175. rmembers.AddRange(tmdids);
  1176. }
  1177. if (students.IsNotEmpty())
  1178. {
  1179. rmembers.AddRange(students);
  1180. }
  1181. groupLists.ForEach(x => {
  1182. if (string.IsNullOrWhiteSpace(x.periodId)) {
  1183. var meb = x.members.Where(z => !string.IsNullOrWhiteSpace(z.periodId)).FirstOrDefault();
  1184. if (meb!= null)
  1185. {
  1186. x.periodId=meb?.periodId;
  1187. }
  1188. }
  1189. });
  1190. return (rmembers, groupLists);
  1191. }
  1192. else
  1193. {
  1194. groupLists.ForEach(x => {
  1195. if (string.IsNullOrWhiteSpace(x.periodId))
  1196. {
  1197. var meb = x.members.Where(z => !string.IsNullOrWhiteSpace(z.periodId)).FirstOrDefault();
  1198. if (meb!= null)
  1199. {
  1200. x.periodId=meb?.periodId;
  1201. }
  1202. }
  1203. });
  1204. return (members, groupLists);
  1205. }
  1206. }
  1207. /// <summary>
  1208. /// 根据名单类型获取名单及成员信息。
  1209. /// </summary>
  1210. /// <param name="_coreAPIHttpService"></param>
  1211. /// <param name="client"></param>
  1212. /// <param name="type"></param>
  1213. /// <param name="scopes"></param>
  1214. /// <param name="school"></param>
  1215. /// <param name="_dingDing"></param>
  1216. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1217. /// <returns></returns>
  1218. public static async Task<List<GroupListDto>> GetGroupListByType(CosmosClient client, string type, School school, Models.Period period, int graduate = -1)
  1219. {
  1220. List<GroupListDto> classes = new List<GroupListDto>();
  1221. List<int> grd = new List<int> ();
  1222. int index = 0;
  1223. period.grades.ForEach(z => { grd.Add(index); index+=1; });
  1224. var yearsdata = SchoolService.GetYears(school, period.id, grd);
  1225. List<int> years = yearsdata.years.ToList();
  1226. if (years.IsNotEmpty()) {
  1227. StringBuilder sql = new StringBuilder($"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.year in ({string.Join(",", years)}) and c.periodId='{period.id}' ");
  1228. if (graduate!=-1) {
  1229. sql.Append($" and c.graduate={graduate} ");
  1230. }
  1231. var result = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<RGroupList>(sql.ToString(), $"Class-{school.id}");
  1232. foreach (var item in result.list)
  1233. {
  1234. GroupListDto group = new GroupListDto
  1235. {
  1236. id = item.id,
  1237. code = $"GroupList-{school.id}",
  1238. name = item.name,
  1239. periodId = item.periodId,
  1240. scope = "school",
  1241. school = school.id,
  1242. type = "class",
  1243. expire= item.expire,
  1244. year = item.year,
  1245. no = item.no,
  1246. leader = item.leader,
  1247. pk = "GroupList",
  1248. graduate = item.graduate,
  1249. grades= new HashSet<int> { item.year }
  1250. };
  1251. classes.Add(group);
  1252. }
  1253. }
  1254. return classes;
  1255. }
  1256. /// <summary>
  1257. /// 根据名单类型获取名单及成员信息。
  1258. /// </summary>
  1259. /// <param name="_coreAPIHttpService"></param>
  1260. /// <param name="client"></param>
  1261. /// <param name="type"></param>
  1262. /// <param name="scopes"></param>
  1263. /// <param name="school"></param>
  1264. /// <param name="_dingDing"></param>
  1265. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1266. /// <returns></returns>
  1267. public static async Task<List<RGroupList>> GetGroupListMemberByType(CoreAPIHttpService _coreAPIHttpService, CosmosClient client, string type, List<string> scopes, string school, DingDing _dingDing, int graduate = -1,long time =-1)
  1268. {
  1269. StringBuilder sql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='{type}'");
  1270. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  1271. if (scopes.Contains("school"))
  1272. {
  1273. if (!string.IsNullOrEmpty(school))
  1274. {
  1275. List<RGroupList> groupLists = new List<RGroupList>();
  1276. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  1277. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  1278. {
  1279. groupLists.Add(item);
  1280. }
  1281. groups.Add("School", groupLists);
  1282. if (!string.IsNullOrWhiteSpace(school) && time>0 && groupLists.IsNotEmpty())
  1283. {
  1284. School schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
  1285. foreach (var period in schoolBase.period)
  1286. {
  1287. var dataSemester = SchoolService.GetSemester(period, time);
  1288. //string id = $"{dataSemester.studyYear}-{dataSemester.currSemester.id}-{student.classId}";
  1289. string code = $"GroupListSemester-{school}";
  1290. string sqlSem = $"select value c from c where c.type='{type}' and c.studyYear={dataSemester.studyYear} and c.semesterId='{dataSemester.currSemester.id}' and c.groupListId not in({string.Join(",", groupLists.Select(c => $"'{c.id}'"))}) ";
  1291. var result = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<GroupListSemester>(sqlSem, code);
  1292. if (result.list.IsNotEmpty())
  1293. {
  1294. foreach (var x in result.list)
  1295. {
  1296. HashSet<string> groupName = x.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  1297. groupLists.Add(new RGroupList
  1298. {
  1299. id = x.groupListId,
  1300. code = $"GroupList-{school}",
  1301. periodId = x.periodId,
  1302. pk = x.pk,
  1303. name = x.name,
  1304. school = x.school,
  1305. creatorId = x.creatorId,
  1306. no = x.no,
  1307. scope = x.scope,
  1308. type = x.type,
  1309. scount = x.scount,
  1310. tcount = x.tcount,
  1311. leader = x.leader,
  1312. year = x.year,
  1313. expire = x.expire,
  1314. graduate = x.graduate,
  1315. members = x.members,
  1316. froms = x.froms,
  1317. joinLock = x.joinLock,
  1318. qrcodeExpire = x.qrcodeExpire,
  1319. optNo = x.optNo,
  1320. qrcodeDays = x.qrcodeDays,
  1321. review=x.review,
  1322. limitCount = x.limitCount,
  1323. grades = x.grades,
  1324. });
  1325. }
  1326. }
  1327. }
  1328. }
  1329. }
  1330. }
  1331. else if (scopes.Contains("private"))
  1332. {
  1333. List<RGroupList> groupLists = new List<RGroupList>();
  1334. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  1335. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  1336. {
  1337. groupLists.Add(item);
  1338. //if (string.IsNullOrWhiteSpace(school))
  1339. //{
  1340. // groupLists.Add(item);
  1341. //}
  1342. //else
  1343. //{
  1344. // if (!string.IsNullOrWhiteSpace(item.school))
  1345. // {
  1346. // if (item.school.Equals(school))
  1347. // {
  1348. // groupLists.Add(item);
  1349. // }
  1350. // }
  1351. // else {
  1352. // groupLists.Add(item);
  1353. // }
  1354. //}
  1355. }
  1356. groups.Add("Teacher", groupLists);
  1357. }
  1358. foreach (var item in groups)
  1359. {
  1360. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  1361. foreach (var group in list)
  1362. {
  1363. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school,graduate);
  1364. }
  1365. }
  1366. var lists = groups.SelectMany(x => x.Value).ToList();
  1367. return lists;
  1368. }
  1369. /// <summary>
  1370. /// 根据名单类型,名单分组信息获取成员信息, rmembers是去重的信息,groups是名单及人员信息,同一个人可能在不同的名单内。
  1371. /// </summary>
  1372. /// <param name="_coreAPIHttpService"></param>
  1373. /// <param name="client"></param>
  1374. /// <param name="type"></param>
  1375. /// <param name="groups"></param>
  1376. /// <param name="groupTbname"></param>
  1377. /// <param name="_dingDing"></param>
  1378. /// <param name="school"></param>
  1379. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1380. /// <returns></returns>
  1381. public static async Task<(List<RGroupList> groups, List<RMember> members)> GetGroupListMemberInfo(CoreAPIHttpService _coreAPIHttpService, CosmosClient client,
  1382. string type, List<RGroupList> groups, string groupTbname, DingDing _dingDing, string school, int graduate = -1)
  1383. {
  1384. try
  1385. {
  1386. HashSet<RGroupList> changes = new HashSet<RGroupList>();
  1387. var members = groups.SelectMany(y => y.members).ToList();
  1388. //去重
  1389. 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();
  1390. 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();
  1391. var stu = students.GroupBy(x => x.code).Select(y => new { key = y.Key, list = y.ToList() });
  1392. List<Student> studentsData = new List<Student>();
  1393. string periodId = "";
  1394. if (stu != null)
  1395. {
  1396. foreach (var item in stu)
  1397. {
  1398. var ids = item.list.Select(x => x.id).ToList();
  1399. if (ids.IsNotEmpty())
  1400. {
  1401. StringBuilder stuSql = new StringBuilder($"SELECT distinct c.name,c.id,c.code,c.picture,c.no,c.irs,c.classId ,c.graduate,c.year ,c.periodId FROM c ");
  1402. string insql = string.Join(",", ids.Select(x => $"'{x}'"));
  1403. stuSql.Append($"where c.id in ({insql})");
  1404. await foreach (var student in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: stuSql.ToString(),
  1405. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{item.key}") }))
  1406. {
  1407. periodId=student.periodId;
  1408. student.schoolId = item.key;
  1409. studentsData.Add(student);
  1410. }
  1411. }
  1412. }
  1413. var unexist_student = students.Select(x => (x.id, x.code)).Except(studentsData.Select(y => (y.id, y.schoolId)), new CompareIdCode()).ToList();
  1414. groups.ForEach(x =>
  1415. {
  1416. int item = x.members.RemoveAll(y => y.type == 2 && unexist_student.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  1417. if (item > 0)
  1418. {
  1419. changes.Add(x);
  1420. }
  1421. });
  1422. }
  1423. List<TmdUser> tmdsData = new List<TmdUser>();
  1424. if (tmdids.IsNotEmpty())
  1425. {
  1426. string memberTbname = "";
  1427. if ($"{type}".Equals("activity"))
  1428. {
  1429. var mbers = groups.SelectMany(x => x.members).Where(z => !string.IsNullOrEmpty(z.code));
  1430. var schoolTeachers = mbers.GroupBy(y => y.code).Select(m => new { key = m.Key, list = m.ToList() });
  1431. foreach (var schoolTeacher in schoolTeachers)
  1432. {
  1433. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1434. string insql = string.Join(",", schoolTeacher.list.Select(x => $"'{x.id}'"));
  1435. tmdidSql.Append($" where c.id in ({insql})");
  1436. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1437. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{schoolTeacher.key}") }))
  1438. {
  1439. tmdsData.Add(tmd);
  1440. }
  1441. }
  1442. var unexist_teachers = mbers.Select(x => (x.id, x.code)).Except(tmdsData.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  1443. groups.ForEach(x =>
  1444. {
  1445. int item = x.members.RemoveAll(y => y.type == 2 && unexist_teachers.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  1446. if (item > 0)
  1447. {
  1448. changes.Add(x);
  1449. }
  1450. });
  1451. }
  1452. else
  1453. {
  1454. //处理研修名单,如果是学校老师的,则需要检查SchoolTeacher
  1455. //处理 学校教研组,学校管理人员,学校任课教师,学校研修名单。
  1456. if (!string.IsNullOrEmpty(school) && ($"{type}".Equals("yxtrain") || $"{type}".Equals("research") || $"{type}".Equals("manage") || $"{type}".Equals("subject")))
  1457. {
  1458. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1459. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  1460. tmdidSql.Append($" where c.id in ({insql})");
  1461. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1462. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  1463. {
  1464. tmdsData.Add(tmd);
  1465. }
  1466. }
  1467. else
  1468. {
  1469. {
  1470. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1471. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  1472. tmdidSql.Append($" where c.id in ({insql})");
  1473. memberTbname = "Teacher";
  1474. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1475. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  1476. {
  1477. tmdsData.Add(tmd);
  1478. }
  1479. }
  1480. {
  1481. //取差集,减少二次搜寻
  1482. var tmdidexp = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  1483. if (tmdidexp.IsNotEmpty())
  1484. {
  1485. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1486. string insql = string.Join(",", tmdidexp.Select(x => $"'{x}'"));
  1487. tmdidSql.Append($" where c.id in ({insql})");
  1488. memberTbname = "Student";
  1489. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1490. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  1491. {
  1492. tmdsData.Add(tmd);
  1493. }
  1494. }
  1495. }
  1496. }
  1497. tmdsData = tmdsData.Where((x, i) => tmdsData.FindIndex(n => n.id.Equals(x.id)) == i).ToList();
  1498. var unexist_tmdid = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  1499. groups.ForEach(x =>
  1500. {
  1501. int item = x.members.RemoveAll(y => unexist_tmdid.Contains(y.id) && y.type == 1);
  1502. if (item > 0)
  1503. {
  1504. changes.Add(x);
  1505. }
  1506. });
  1507. }
  1508. }
  1509. if (tmdids.IsNotEmpty() && _coreAPIHttpService.check) {
  1510. ///获取真实的名称 ,大于50则不处理
  1511. if (tmdids.Count < 60)
  1512. {
  1513. var content = new StringContent(tmdids.Select(x => x.id).ToHashSet().ToJsonString(), Encoding.UTF8, "application/json");
  1514. string json = null;
  1515. try
  1516. {
  1517. json = await _coreAPIHttpService.GetUserInfos(content);
  1518. if (!string.IsNullOrWhiteSpace(json))
  1519. {
  1520. List<TmdInfo> tmdInfos = json.ToObject<List<TmdInfo>>();
  1521. if (tmdInfos.IsNotEmpty())
  1522. {
  1523. tmdsData.ForEach(y =>
  1524. {
  1525. var tmd = tmdInfos.Find(x => x.id.Equals(y.id));
  1526. if (tmd != null)
  1527. {
  1528. y.name = tmd?.name;
  1529. y.picture = tmd?.picture;
  1530. }
  1531. else
  1532. {
  1533. groups.ForEach(x =>
  1534. {
  1535. int item = x.members.RemoveAll(z => z.id.Equals(y.id) && z.type == 1);
  1536. if (item > 0)
  1537. {
  1538. changes.Add(x);
  1539. }
  1540. });
  1541. }
  1542. });
  1543. }
  1544. }
  1545. }
  1546. catch (Exception ex)
  1547. {
  1548. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.location}用户转换失败:{_coreAPIHttpService.options.coreUrl}{json}\n {ex.Message}\n{ex.StackTrace}{tmdids.Select(x => x.id).ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  1549. }
  1550. }
  1551. }
  1552. tmdids.ForEach(x =>
  1553. {
  1554. var user = tmdsData.Find(y => y.id.Equals(x.id));
  1555. x.name = user?.name;
  1556. // x.nickname = string.IsNullOrWhiteSpace(x.nickname) ? user?.name : x.nickname;
  1557. x.picture = user?.picture;
  1558. });
  1559. students.ForEach(x =>
  1560. {
  1561. var student = studentsData.Find(y => y.id.Equals(x.id) && y.schoolId.Equals(x.code));
  1562. if (student != null)
  1563. {
  1564. x.name = student?.name;
  1565. // x.nickname = string.IsNullOrWhiteSpace(x.nickname) ? student?.name:x.nickname;
  1566. x.picture = student?.picture;
  1567. x.classId = student?.classId;
  1568. x.graduate = student.graduate;
  1569. x.year = student.year;
  1570. x.gender= student.gender;
  1571. x.periodId = student.periodId;
  1572. }
  1573. });
  1574. var mbs = tmdids;
  1575. mbs.AddRange(students);
  1576. if (changes.Count > 0 && !string.IsNullOrEmpty(groupTbname))
  1577. {
  1578. foreach (var change in changes)
  1579. {
  1580. change.tcount = change.members.Where(x => x.type == 1).Count();
  1581. change.scount = change.members.Where(x => x.type == 2).Count();
  1582. GroupList group = change.ToJsonString().ToObject<GroupList>();
  1583. await client.GetContainer(Constant.TEAMModelOS, groupTbname).ReplaceItemAsync(group, group.id, new PartitionKey(group.code));
  1584. }
  1585. }
  1586. groups.ForEach(x =>
  1587. {
  1588. x.members.ForEach(y =>
  1589. {
  1590. if (y.type == 1)
  1591. {
  1592. var tmd = tmdids.Find(t => t.id.Equals(y.id));
  1593. y.name = tmd?.name;
  1594. //y.nickname = string.IsNullOrWhiteSpace(y.nickname) ? tmd?.nickname : y.nickname;
  1595. y.picture = tmd?.picture;
  1596. }
  1597. if (y.type == 2)
  1598. {
  1599. var student = students.Find(t => t.id.Equals(y.id) && t.code.Equals(y.code));
  1600. if (student != null)
  1601. {
  1602. y.name = student?.name;
  1603. // y.nickname = string.IsNullOrWhiteSpace(y.nickname) ? student?.nickname : y.nickname;
  1604. y.picture = student?.picture;
  1605. y.classId = student?.classId;
  1606. y.graduate = student.graduate;
  1607. y.gender = student.gender;
  1608. y.year = student.year;
  1609. x.periodId = student.periodId;
  1610. x.grades.Add(student.year);
  1611. }
  1612. }
  1613. });
  1614. if (!x.type.Equals("class")) {
  1615. 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);
  1616. if (yearc.Any())
  1617. {
  1618. //有一半的人是同一个班的,则以
  1619. if (yearc.First().count * 1.0 / x.members.Count > 0.51)
  1620. {
  1621. x.year = yearc.First().key;
  1622. }
  1623. }
  1624. }
  1625. if (!x.scope.Equals("private")) {
  1626. x.periodId=periodId;
  1627. }
  1628. });
  1629. HashSet<string> schoolCodes = groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).Select(z => z.code).ToHashSet();
  1630. if (schoolCodes != null && schoolCodes.Count > 0)
  1631. {
  1632. List<School> schools = new List<School>();
  1633. string insql = $"select c.name,c.id from c where c.id in ({string.Join(",", schoolCodes.Select(x => $"'{x}'"))})";
  1634. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: insql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  1635. {
  1636. schools.Add(item);
  1637. }
  1638. if (schools.IsNotEmpty())
  1639. {
  1640. groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).ToList().ForEach(z => {
  1641. var school = schools.Find(j => j.id.Equals(z.code));
  1642. z.schoolName = school?.name;
  1643. });
  1644. }
  1645. }
  1646. if (graduate >= 0)
  1647. {
  1648. groups= groups.FindAll(x => x.graduate == graduate);
  1649. if (groups != null) {
  1650. groups.ForEach(x =>
  1651. {
  1652. x.members.RemoveAll(y => y.graduate != graduate);
  1653. });
  1654. }
  1655. mbs.RemoveAll(z => z.graduate != graduate);
  1656. return (groups, mbs);
  1657. }
  1658. //直接返回
  1659. else {
  1660. return (groups, mbs);
  1661. }
  1662. }
  1663. catch (Exception ex)
  1664. {
  1665. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.location},GetGroupListMemberInfo()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1666. }
  1667. return (null, null);
  1668. }
  1669. /// <summary>
  1670. /// 处理活动结束的
  1671. /// </summary>
  1672. /// <param name="_coreAPIHttpService"></param>
  1673. /// <param name="cosmosClient">数据库</param>
  1674. /// <param name="_dingDing">钉钉消息</param>
  1675. /// <param name="school">学校编码</param>
  1676. /// <param name="classes">行政班</param>
  1677. /// <param name="stuLists">学生</param>
  1678. /// <param name="tchLists">教师</param>
  1679. /// <param name="_groupLists">分组</param>
  1680. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1681. /// <returns></returns>
  1682. 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)
  1683. {
  1684. List<FMember> fMembers = new();
  1685. if (classes != null && classes.Count > 0)
  1686. {
  1687. (List<RMember> staffList, List<RGroupList> classInfos) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, classes, school);
  1688. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 1, }));
  1689. }
  1690. if (stuLists != null && stuLists.Count > 0)
  1691. {
  1692. (List<RMember> staffList, List<RGroupList> classInfos) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, stuLists, school);
  1693. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 2 }));
  1694. }
  1695. /* if (tchLists != null && tchLists.Count > 0)
  1696. {
  1697. (List<RMember> staffList, List<RGroupList> classInfos) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, tchLists, school);
  1698. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 2 }));
  1699. }*/
  1700. if (tchLists != null && tchLists.Count > 0)
  1701. {
  1702. (List<RMember> staffList, List<RGroupList> groupLists1) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, tchLists, school, _groupLists);
  1703. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 2 }));
  1704. }
  1705. return fMembers;
  1706. }
  1707. }
  1708. public class CompareIdCode : IEqualityComparer<(string id, string code)>
  1709. {
  1710. public bool Equals((string id, string code) x, (string id, string code) y)
  1711. {
  1712. return x.id.Equals(y.id) && x.code.Equals(y.code);
  1713. }
  1714. public int GetHashCode((string id, string code) obj)
  1715. {
  1716. if (obj.id != null && obj.code != null)
  1717. {
  1718. return 1;
  1719. }
  1720. else
  1721. {
  1722. return 0;
  1723. }
  1724. }
  1725. }
  1726. }