GroupListService.cs 112 KB

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