GroupListService.cs 111 KB

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