GroupListService.cs 96 KB

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