GroupListService.cs 95 KB

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