GroupListService.cs 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844
  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. if (semesterGroupList.rgroupList.IsNotEmpty()) {
  988. groupLists.AddRange(semesterGroupList.rgroupList);
  989. members.AddRange(semesterGroupList.rmembers);
  990. }
  991. classes = classes.Except(semesterGroupList.rgroupList.Select(y => y.id)).ToList();
  992. if (classes.IsNotEmpty()) {
  993. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  994. List<Student> students = new List<Student>();
  995. string sql = string.Join(",", classes.Select(x => $"'{x}'"));
  996. if (!string.IsNullOrEmpty(school))
  997. {
  998. List<RGroupList> schoolList = new List<RGroupList>();
  999. string queryText = $"select value(c) from c where c.id in ({sql})";
  1000. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: queryText,
  1001. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  1002. {
  1003. schoolList.Add(item);
  1004. }
  1005. if (schoolList.IsNotEmpty())
  1006. {
  1007. groups.Add("School", schoolList);
  1008. }
  1009. //取差集,减少二次搜寻
  1010. classes = classes.Except(schoolList.Select(y => y.id)).ToList();
  1011. if (classes.IsNotEmpty())
  1012. {
  1013. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  1014. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  1015. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  1016. {
  1017. //graduate传入的状态>=0 表示指定状态查询,且传入的状态与数据库的状态一致。
  1018. if (graduate >= 0)
  1019. {
  1020. if (item.graduate == graduate)
  1021. {
  1022. students.Add(item);
  1023. }
  1024. }
  1025. //全部。
  1026. else
  1027. {
  1028. students.Add(item);
  1029. }
  1030. // students.Add(item);
  1031. }
  1032. //取差集,减少二次搜寻
  1033. classes = classes.Except(students.Select(y => y.classId)).ToList();
  1034. }
  1035. if (classes.IsNotEmpty())
  1036. {
  1037. string insql = string.Join(",", classes.Select(x => $"'{x}'"));
  1038. //搜寻没有关联学生的行政班
  1039. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School")
  1040. .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})",
  1041. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  1042. {
  1043. ///行政班(学生搜寻classId动态返回)class
  1044. List<RMember> smembers = new List<RMember>();
  1045. RGroupList group = new RGroupList
  1046. {
  1047. id = item.id,
  1048. code = $"GroupList-{school}",
  1049. name = item.name,
  1050. periodId = item.periodId,
  1051. scope = "school",
  1052. school = school,
  1053. type = "class",
  1054. year = item.year,
  1055. expire = item.expire,
  1056. members = smembers,
  1057. scount = smembers.Count,
  1058. pk = "GroupList",
  1059. leader = item.leader,
  1060. no = item.no,
  1061. graduate = item.graduate,
  1062. grades= new HashSet<int>() { item.year }
  1063. };
  1064. //graduate传入的状态>=0 表示指定状态查询,且传入的状态与数据库的状态一致。
  1065. if (graduate >= 0)
  1066. {
  1067. if (group.graduate == graduate)
  1068. {
  1069. groupLists.Add(group);
  1070. }
  1071. }
  1072. //全部。
  1073. else
  1074. {
  1075. groupLists.Add(group);
  1076. }
  1077. //groupLists.Add(group);
  1078. }
  1079. //取差集,减少二次搜寻
  1080. classes = classes.Except(groupLists.Select(y => y.id)).ToList();
  1081. }
  1082. }
  1083. if (classes.IsNotEmpty())
  1084. {
  1085. List<RGroupList> privateList = new List<RGroupList>();
  1086. sql = string.Join(",", classes.Select(x => $"'{x}'"));
  1087. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: $"select value(c) from c where c.id in ({sql})",
  1088. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  1089. {
  1090. privateList.Add(item);
  1091. //if (string.IsNullOrWhiteSpace(school))
  1092. //{
  1093. // privateList.Add(item);
  1094. //}
  1095. //else
  1096. //{
  1097. // if (!string.IsNullOrWhiteSpace(item.school))
  1098. // {
  1099. // if (item.school.Equals(school))
  1100. // {
  1101. // privateList.Add(item);
  1102. // }
  1103. // }
  1104. // else
  1105. // {
  1106. // privateList.Add(item);
  1107. // }
  1108. //}
  1109. }
  1110. if (privateList.IsNotEmpty())
  1111. {
  1112. groups.Add("Teacher", privateList);
  1113. }
  1114. }
  1115. foreach (var item in groups)
  1116. {
  1117. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  1118. foreach (var group in list)
  1119. {
  1120. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school, graduate);
  1121. members.AddRange(rmembers);
  1122. }
  1123. }
  1124. groupLists.AddRange(groups.SelectMany(x => x.Value).ToList());
  1125. if (students.IsNotEmpty())
  1126. {
  1127. List<string> sqlList = students.Select(x => x.classId).ToList();
  1128. string insql = string.Join(",", sqlList.Select(x => $"'{x}'"));
  1129. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").
  1130. 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})",
  1131. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  1132. {
  1133. ///行政班(学生搜寻classId动态返回)class
  1134. List<RMember> smembers = students.Where(x => x.classId.Equals(item.id))
  1135. .Select(y => new RMember
  1136. {
  1137. id = y.id,
  1138. code = school,
  1139. name = y.name,
  1140. //nickname = y.name,
  1141. type = 2,
  1142. picture = y.picture,
  1143. no = y.no,
  1144. classId = y.classId,
  1145. groupId = y.groupId,
  1146. groupName = y.groupName,
  1147. irs = y.irs,
  1148. year= y.year,
  1149. graduate = y.graduate,
  1150. gender= y.gender,
  1151. periodId=y.periodId,
  1152. }).ToList();
  1153. members.AddRange(smembers);
  1154. RGroupList group = new RGroupList
  1155. {
  1156. id = item.id,
  1157. code = $"GroupList-{school}",
  1158. name = item.name,
  1159. periodId = item.periodId,
  1160. scope = "school",
  1161. school = school,
  1162. type = "class",
  1163. expire= item.expire,
  1164. year = item.year,
  1165. members = smembers,
  1166. scount = smembers.Count,
  1167. no = item.no,
  1168. leader = item.leader,
  1169. pk = "GroupList",
  1170. graduate = item.graduate,
  1171. grades= new HashSet<int> { item.year }
  1172. };
  1173. //graduate传入的状态>=0 表示指定状态查询,且传入的状态与数据库的状态一致。
  1174. if (graduate >= 0)
  1175. {
  1176. if (group.graduate == graduate)
  1177. {
  1178. groupLists.Add(group);
  1179. }
  1180. }
  1181. //全部。
  1182. else
  1183. {
  1184. groupLists.Add(group);
  1185. }
  1186. // groupLists.Add(group);
  1187. }
  1188. //去重。
  1189. 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();
  1190. }
  1191. }
  1192. }
  1193. if (groupids.IsNotEmpty())
  1194. {
  1195. List<RMember> rmembers = new List<RMember>();
  1196. groupLists.ForEach(y => {
  1197. (string id, List<string> grpids) = groupids.Find(x => x.Item1.Equals(y.id));
  1198. var gpmember = y.members.FindAll(x => !string.IsNullOrEmpty(x.groupName) && grpids.Contains(x.groupName));
  1199. if (grpids.Contains("default"))
  1200. {
  1201. var gpmemberdft = y.members.FindAll(x => string.IsNullOrWhiteSpace(x.groupName));
  1202. if (gpmember.IsNotEmpty())
  1203. {
  1204. gpmember.AddRange(gpmemberdft);
  1205. }
  1206. else
  1207. {
  1208. gpmember = gpmemberdft;
  1209. }
  1210. }
  1211. y.members = gpmember;
  1212. });
  1213. var gpgpmembers = groupLists.SelectMany(x => x.members).ToList();
  1214. 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();
  1215. 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();
  1216. if (tmdids.IsNotEmpty())
  1217. {
  1218. rmembers.AddRange(tmdids);
  1219. }
  1220. if (students.IsNotEmpty())
  1221. {
  1222. rmembers.AddRange(students);
  1223. }
  1224. groupLists.ForEach(x => {
  1225. if (string.IsNullOrWhiteSpace(x.periodId)) {
  1226. var meb = x.members.Where(z => !string.IsNullOrWhiteSpace(z.periodId)).FirstOrDefault();
  1227. if (meb!= null)
  1228. {
  1229. x.periodId=meb?.periodId;
  1230. }
  1231. }
  1232. });
  1233. return (rmembers, groupLists);
  1234. }
  1235. else
  1236. {
  1237. groupLists.ForEach(x => {
  1238. if (string.IsNullOrWhiteSpace(x.periodId))
  1239. {
  1240. var meb = x.members.Where(z => !string.IsNullOrWhiteSpace(z.periodId)).FirstOrDefault();
  1241. if (meb!= null)
  1242. {
  1243. x.periodId=meb?.periodId;
  1244. }
  1245. }
  1246. });
  1247. return (members, groupLists);
  1248. }
  1249. }
  1250. /// <summary>
  1251. /// 根据名单类型获取名单及成员信息。
  1252. /// </summary>
  1253. /// <param name="_coreAPIHttpService"></param>
  1254. /// <param name="client"></param>
  1255. /// <param name="type"></param>
  1256. /// <param name="scopes"></param>
  1257. /// <param name="school"></param>
  1258. /// <param name="_dingDing"></param>
  1259. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1260. /// <returns></returns>
  1261. public static async Task<List<GroupListDto>> GetGroupListByType(CosmosClient client, string type, School school, Models.Period period, int graduate = -1)
  1262. {
  1263. List<GroupListDto> classes = new List<GroupListDto>();
  1264. List<int> grd = new List<int> ();
  1265. int index = 0;
  1266. period.grades.ForEach(z => { grd.Add(index); index+=1; });
  1267. var yearsdata = SchoolService.GetYears(school, period.id, grd);
  1268. List<int> years = yearsdata.years.ToList();
  1269. if (years.IsNotEmpty()) {
  1270. 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}' ");
  1271. if (graduate!=-1) {
  1272. sql.Append($" and c.graduate={graduate} ");
  1273. }
  1274. var result = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<RGroupList>(sql.ToString(), $"Class-{school.id}");
  1275. foreach (var item in result.list)
  1276. {
  1277. GroupListDto group = new GroupListDto
  1278. {
  1279. id = item.id,
  1280. code = $"GroupList-{school.id}",
  1281. name = item.name,
  1282. periodId = item.periodId,
  1283. scope = "school",
  1284. school = school.id,
  1285. type = "class",
  1286. expire= item.expire,
  1287. year = item.year,
  1288. no = item.no,
  1289. leader = item.leader,
  1290. pk = "GroupList",
  1291. graduate = item.graduate,
  1292. grades= new HashSet<int> { item.year }
  1293. };
  1294. classes.Add(group);
  1295. }
  1296. }
  1297. return classes;
  1298. }
  1299. /// <summary>
  1300. /// 根据名单类型获取名单及成员信息。
  1301. /// </summary>
  1302. /// <param name="_coreAPIHttpService"></param>
  1303. /// <param name="client"></param>
  1304. /// <param name="type"></param>
  1305. /// <param name="scopes"></param>
  1306. /// <param name="school"></param>
  1307. /// <param name="_dingDing"></param>
  1308. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1309. /// <returns></returns>
  1310. 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)
  1311. {
  1312. StringBuilder sql = new StringBuilder($"SELECT distinct value(c) FROM c where c.type='{type}'");
  1313. Dictionary<string, List<RGroupList>> groups = new Dictionary<string, List<RGroupList>>();
  1314. if (scopes.Contains("school"))
  1315. {
  1316. if (!string.IsNullOrEmpty(school))
  1317. {
  1318. List<RGroupList> groupLists = new List<RGroupList>();
  1319. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  1320. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList-{school}") }))
  1321. {
  1322. groupLists.Add(item);
  1323. }
  1324. groups.Add("School", groupLists);
  1325. if (!string.IsNullOrWhiteSpace(school) && time>0 && groupLists.IsNotEmpty())
  1326. {
  1327. School schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
  1328. foreach (var period in schoolBase.period)
  1329. {
  1330. var dataSemester = SchoolService.GetSemester(period, time);
  1331. //string id = $"{dataSemester.studyYear}-{dataSemester.currSemester.id}-{student.classId}";
  1332. string code = $"GroupListSemester-{school}";
  1333. 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}'"))}) ";
  1334. var result = await client.GetContainer(Constant.TEAMModelOS, Constant.School).GetList<GroupListSemester>(sqlSem, code);
  1335. if (result.list.IsNotEmpty())
  1336. {
  1337. foreach (var x in result.list)
  1338. {
  1339. HashSet<string> groupName = x.members.Where(x => !string.IsNullOrEmpty(x.groupName)).Select(y => y.groupName).ToHashSet();
  1340. groupLists.Add(new RGroupList
  1341. {
  1342. id = x.groupListId,
  1343. code = $"GroupList-{school}",
  1344. periodId = x.periodId,
  1345. pk = x.pk,
  1346. name = x.name,
  1347. school = x.school,
  1348. creatorId = x.creatorId,
  1349. no = x.no,
  1350. scope = x.scope,
  1351. type = x.type,
  1352. scount = x.scount,
  1353. tcount = x.tcount,
  1354. leader = x.leader,
  1355. year = x.year,
  1356. expire = x.expire,
  1357. graduate = x.graduate,
  1358. members = x.members,
  1359. froms = x.froms,
  1360. joinLock = x.joinLock,
  1361. qrcodeExpire = x.qrcodeExpire,
  1362. optNo = x.optNo,
  1363. qrcodeDays = x.qrcodeDays,
  1364. review=x.review,
  1365. limitCount = x.limitCount,
  1366. grades = x.grades,
  1367. });
  1368. }
  1369. }
  1370. }
  1371. }
  1372. }
  1373. }
  1374. else if (scopes.Contains("private"))
  1375. {
  1376. List<RGroupList> groupLists = new List<RGroupList>();
  1377. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<RGroupList>(queryText: sql.ToString(),
  1378. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"GroupList") }))
  1379. {
  1380. groupLists.Add(item);
  1381. //if (string.IsNullOrWhiteSpace(school))
  1382. //{
  1383. // groupLists.Add(item);
  1384. //}
  1385. //else
  1386. //{
  1387. // if (!string.IsNullOrWhiteSpace(item.school))
  1388. // {
  1389. // if (item.school.Equals(school))
  1390. // {
  1391. // groupLists.Add(item);
  1392. // }
  1393. // }
  1394. // else {
  1395. // groupLists.Add(item);
  1396. // }
  1397. //}
  1398. }
  1399. groups.Add("Teacher", groupLists);
  1400. }
  1401. foreach (var item in groups)
  1402. {
  1403. var list = item.Value.GroupBy(x => x.type).Select(y => new { key = y.Key, list = y.ToList() });
  1404. foreach (var group in list)
  1405. {
  1406. (List<RGroupList> rgroups, List<RMember> rmembers) = await GetGroupListMemberInfo(_coreAPIHttpService, client, group.key, group.list, item.Key, _dingDing, school,graduate);
  1407. }
  1408. }
  1409. var lists = groups.SelectMany(x => x.Value).ToList();
  1410. return lists;
  1411. }
  1412. /// <summary>
  1413. /// 根据名单类型,名单分组信息获取成员信息, rmembers是去重的信息,groups是名单及人员信息,同一个人可能在不同的名单内。
  1414. /// </summary>
  1415. /// <param name="_coreAPIHttpService"></param>
  1416. /// <param name="client"></param>
  1417. /// <param name="type"></param>
  1418. /// <param name="groups"></param>
  1419. /// <param name="groupTbname"></param>
  1420. /// <param name="_dingDing"></param>
  1421. /// <param name="school"></param>
  1422. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1423. /// <returns></returns>
  1424. public static async Task<(List<RGroupList> groups, List<RMember> members)> GetGroupListMemberInfo(CoreAPIHttpService _coreAPIHttpService, CosmosClient client,
  1425. string type, List<RGroupList> groups, string groupTbname, DingDing _dingDing, string school, int graduate = -1)
  1426. {
  1427. try
  1428. {
  1429. HashSet<RGroupList> changes = new HashSet<RGroupList>();
  1430. var members = groups.SelectMany(y => y.members).ToList();
  1431. //去重
  1432. 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();
  1433. 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();
  1434. var stu = students.GroupBy(x => x.code).Select(y => new { key = y.Key, list = y.ToList() });
  1435. List<Student> studentsData = new List<Student>();
  1436. string periodId = "";
  1437. if (stu != null)
  1438. {
  1439. foreach (var item in stu)
  1440. {
  1441. var ids = item.list.Select(x => x.id).ToList();
  1442. if (ids.IsNotEmpty())
  1443. {
  1444. 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 ");
  1445. string insql = string.Join(",", ids.Select(x => $"'{x}'"));
  1446. stuSql.Append($"where c.id in ({insql})");
  1447. await foreach (var student in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryIterator<Student>(queryText: stuSql.ToString(),
  1448. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{item.key}") }))
  1449. {
  1450. periodId=student.periodId;
  1451. student.schoolId = item.key;
  1452. studentsData.Add(student);
  1453. }
  1454. }
  1455. }
  1456. var unexist_student = students.Select(x => (x.id, x.code)).Except(studentsData.Select(y => (y.id, y.schoolId)), new CompareIdCode()).ToList();
  1457. groups.ForEach(x =>
  1458. {
  1459. int item = x.members.RemoveAll(y => y.type == 2 && unexist_student.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  1460. if (item > 0)
  1461. {
  1462. changes.Add(x);
  1463. }
  1464. });
  1465. }
  1466. List<TmdUser> tmdsData = new List<TmdUser>();
  1467. if (tmdids.IsNotEmpty())
  1468. {
  1469. string memberTbname = "";
  1470. if ($"{type}".Equals("activity"))
  1471. {
  1472. var mbers = groups.SelectMany(x => x.members).Where(z => !string.IsNullOrEmpty(z.code));
  1473. var schoolTeachers = mbers.GroupBy(y => y.code).Select(m => new { key = m.Key, list = m.ToList() });
  1474. foreach (var schoolTeacher in schoolTeachers)
  1475. {
  1476. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1477. string insql = string.Join(",", schoolTeacher.list.Select(x => $"'{x.id}'"));
  1478. tmdidSql.Append($" where c.id in ({insql})");
  1479. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1480. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{schoolTeacher.key}") }))
  1481. {
  1482. tmdsData.Add(tmd);
  1483. }
  1484. }
  1485. var unexist_teachers = mbers.Select(x => (x.id, x.code)).Except(tmdsData.Select(y => (y.id, y.code)), new CompareIdCode()).ToList();
  1486. groups.ForEach(x =>
  1487. {
  1488. int item = x.members.RemoveAll(y => y.type == 2 && unexist_teachers.Exists(x => x.id.Equals(y.id) && x.Item2.Equals(y.code)));
  1489. if (item > 0)
  1490. {
  1491. changes.Add(x);
  1492. }
  1493. });
  1494. }
  1495. else
  1496. {
  1497. //处理研修名单,如果是学校老师的,则需要检查SchoolTeacher
  1498. //处理 学校教研组,学校管理人员,学校任课教师,学校研修名单。
  1499. if (!string.IsNullOrEmpty(school) && ($"{type}".Equals("yxtrain") || $"{type}".Equals("research") || $"{type}".Equals("manage") || $"{type}".Equals("subject")))
  1500. {
  1501. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1502. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  1503. tmdidSql.Append($" where c.id in ({insql})");
  1504. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1505. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  1506. {
  1507. tmdsData.Add(tmd);
  1508. }
  1509. }
  1510. else
  1511. {
  1512. {
  1513. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1514. string insql = string.Join(",", tmdids.Select(x => $"'{x.id}'"));
  1515. tmdidSql.Append($" where c.id in ({insql})");
  1516. memberTbname = "Teacher";
  1517. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1518. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  1519. {
  1520. tmdsData.Add(tmd);
  1521. }
  1522. }
  1523. {
  1524. //取差集,减少二次搜寻
  1525. var tmdidexp = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  1526. if (tmdidexp.IsNotEmpty())
  1527. {
  1528. StringBuilder tmdidSql = new StringBuilder($"SELECT distinct c.name,c.id,c.picture FROM c ");
  1529. string insql = string.Join(",", tmdidexp.Select(x => $"'{x}'"));
  1530. tmdidSql.Append($" where c.id in ({insql})");
  1531. memberTbname = "Student";
  1532. await foreach (var tmd in client.GetContainer(Constant.TEAMModelOS, memberTbname).GetItemQueryIterator<TmdUser>(queryText: tmdidSql.ToString(),
  1533. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  1534. {
  1535. tmdsData.Add(tmd);
  1536. }
  1537. }
  1538. }
  1539. }
  1540. tmdsData = tmdsData.Where((x, i) => tmdsData.FindIndex(n => n.id.Equals(x.id)) == i).ToList();
  1541. var unexist_tmdid = tmdids.Select(x => x.id).Except(tmdsData.Select(y => y.id)).ToList();
  1542. groups.ForEach(x =>
  1543. {
  1544. int item = x.members.RemoveAll(y => unexist_tmdid.Contains(y.id) && y.type == 1);
  1545. if (item > 0)
  1546. {
  1547. changes.Add(x);
  1548. }
  1549. });
  1550. }
  1551. }
  1552. if (tmdids.IsNotEmpty() && _coreAPIHttpService.check) {
  1553. ///获取真实的名称 ,大于50则不处理
  1554. if (tmdids.Count < 60)
  1555. {
  1556. var content = new StringContent(tmdids.Select(x => x.id).ToHashSet().ToJsonString(), Encoding.UTF8, "application/json");
  1557. string json = null;
  1558. try
  1559. {
  1560. json = await _coreAPIHttpService.GetUserInfos(content);
  1561. if (!string.IsNullOrWhiteSpace(json))
  1562. {
  1563. List<TmdInfo> tmdInfos = json.ToObject<List<TmdInfo>>();
  1564. if (tmdInfos.IsNotEmpty())
  1565. {
  1566. tmdsData.ForEach(y =>
  1567. {
  1568. var tmd = tmdInfos.Find(x => x.id.Equals(y.id));
  1569. if (tmd != null)
  1570. {
  1571. y.name = tmd?.name;
  1572. y.picture = tmd?.picture;
  1573. }
  1574. else
  1575. {
  1576. groups.ForEach(x =>
  1577. {
  1578. int item = x.members.RemoveAll(z => z.id.Equals(y.id) && z.type == 1);
  1579. if (item > 0)
  1580. {
  1581. changes.Add(x);
  1582. }
  1583. });
  1584. }
  1585. });
  1586. }
  1587. }
  1588. }
  1589. catch (Exception ex)
  1590. {
  1591. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.location}用户转换失败:{_coreAPIHttpService.options.coreUrl}{json}\n {ex.Message}\n{ex.StackTrace}{tmdids.Select(x => x.id).ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  1592. }
  1593. }
  1594. }
  1595. tmdids.ForEach(x =>
  1596. {
  1597. var user = tmdsData.Find(y => y.id.Equals(x.id));
  1598. x.name = user?.name;
  1599. // x.nickname = string.IsNullOrWhiteSpace(x.nickname) ? user?.name : x.nickname;
  1600. x.picture = user?.picture;
  1601. });
  1602. students.ForEach(x =>
  1603. {
  1604. var student = studentsData.Find(y => y.id.Equals(x.id) && y.schoolId.Equals(x.code));
  1605. if (student != null)
  1606. {
  1607. x.name = student?.name;
  1608. // x.nickname = string.IsNullOrWhiteSpace(x.nickname) ? student?.name:x.nickname;
  1609. x.picture = student?.picture;
  1610. x.classId = student?.classId;
  1611. x.graduate = student.graduate;
  1612. x.year = student.year;
  1613. x.gender= student.gender;
  1614. x.periodId = student.periodId;
  1615. }
  1616. });
  1617. var mbs = tmdids;
  1618. mbs.AddRange(students);
  1619. if (changes.Count > 0 && !string.IsNullOrEmpty(groupTbname))
  1620. {
  1621. foreach (var change in changes)
  1622. {
  1623. change.tcount = change.members.Where(x => x.type == 1).Count();
  1624. change.scount = change.members.Where(x => x.type == 2).Count();
  1625. GroupList group = change.ToJsonString().ToObject<GroupList>();
  1626. await client.GetContainer(Constant.TEAMModelOS, groupTbname).ReplaceItemAsync(group, group.id, new PartitionKey(group.code));
  1627. }
  1628. }
  1629. groups.ForEach(x =>
  1630. {
  1631. x.members.ForEach(y =>
  1632. {
  1633. if (y.type == 1)
  1634. {
  1635. var tmd = tmdids.Find(t => t.id.Equals(y.id));
  1636. y.name = tmd?.name;
  1637. //y.nickname = string.IsNullOrWhiteSpace(y.nickname) ? tmd?.nickname : y.nickname;
  1638. y.picture = tmd?.picture;
  1639. }
  1640. if (y.type == 2)
  1641. {
  1642. var student = students.Find(t => t.id.Equals(y.id) && t.code.Equals(y.code));
  1643. if (student != null)
  1644. {
  1645. y.name = student?.name;
  1646. // y.nickname = string.IsNullOrWhiteSpace(y.nickname) ? student?.nickname : y.nickname;
  1647. y.picture = student?.picture;
  1648. y.classId = student?.classId;
  1649. y.graduate = student.graduate;
  1650. y.gender = student.gender;
  1651. y.year = student.year;
  1652. x.periodId = student.periodId;
  1653. x.grades.Add(student.year);
  1654. }
  1655. }
  1656. });
  1657. if (!x.type.Equals("class")) {
  1658. 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);
  1659. if (yearc.Any())
  1660. {
  1661. //有一半的人是同一个班的,则以
  1662. if (yearc.First().count * 1.0 / x.members.Count > 0.51)
  1663. {
  1664. x.year = yearc.First().key;
  1665. }
  1666. }
  1667. }
  1668. if (!x.scope.Equals("private")) {
  1669. x.periodId=periodId;
  1670. }
  1671. });
  1672. HashSet<string> schoolCodes = groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).Select(z => z.code).ToHashSet();
  1673. if (schoolCodes != null && schoolCodes.Count > 0)
  1674. {
  1675. List<School> schools = new List<School>();
  1676. string insql = $"select c.name,c.id from c where c.id in ({string.Join(",", schoolCodes.Select(x => $"'{x}'"))})";
  1677. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: insql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  1678. {
  1679. schools.Add(item);
  1680. }
  1681. if (schools.IsNotEmpty())
  1682. {
  1683. groups.SelectMany(x => x.members).Where(y => !string.IsNullOrEmpty(y.code)).ToList().ForEach(z => {
  1684. var school = schools.Find(j => j.id.Equals(z.code));
  1685. z.schoolName = school?.name;
  1686. });
  1687. }
  1688. }
  1689. if (graduate >= 0)
  1690. {
  1691. groups= groups.FindAll(x => x.graduate == graduate);
  1692. if (groups != null) {
  1693. groups.ForEach(x =>
  1694. {
  1695. x.members.RemoveAll(y => y.graduate != graduate);
  1696. });
  1697. }
  1698. mbs.RemoveAll(z => z.graduate != graduate);
  1699. return (groups, mbs);
  1700. }
  1701. //直接返回
  1702. else {
  1703. return (groups, mbs);
  1704. }
  1705. }
  1706. catch (Exception ex)
  1707. {
  1708. await _dingDing.SendBotMsg($"{_coreAPIHttpService.options.location},GetGroupListMemberInfo()\n{ex.Message}\n{ex.StackTrace}\n", GroupNames.醍摩豆服務運維群組);
  1709. }
  1710. return (null, null);
  1711. }
  1712. /// <summary>
  1713. /// 处理活动结束的
  1714. /// </summary>
  1715. /// <param name="_coreAPIHttpService"></param>
  1716. /// <param name="cosmosClient">数据库</param>
  1717. /// <param name="_dingDing">钉钉消息</param>
  1718. /// <param name="school">学校编码</param>
  1719. /// <param name="classes">行政班</param>
  1720. /// <param name="stuLists">学生</param>
  1721. /// <param name="tchLists">教师</param>
  1722. /// <param name="_groupLists">分组</param>
  1723. /// <param name="graduate">毕业类型0在校,1毕业 , -1查全部。</param>
  1724. /// <returns></returns>
  1725. 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)
  1726. {
  1727. List<FMember> fMembers = new();
  1728. if (classes != null && classes.Count > 0)
  1729. {
  1730. (List<RMember> staffList, List<RGroupList> classInfos) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, classes, school);
  1731. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 1, }));
  1732. }
  1733. if (stuLists != null && stuLists.Count > 0)
  1734. {
  1735. (List<RMember> staffList, List<RGroupList> classInfos) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, stuLists, school);
  1736. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 2 }));
  1737. }
  1738. /* if (tchLists != null && tchLists.Count > 0)
  1739. {
  1740. (List<RMember> staffList, List<RGroupList> classInfos) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, tchLists, school);
  1741. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 2 }));
  1742. }*/
  1743. if (tchLists != null && tchLists.Count > 0)
  1744. {
  1745. (List<RMember> staffList, List<RGroupList> groupLists1) = await GetMemberByListids(_coreAPIHttpService, cosmosClient, _dingDing, tchLists, school, _groupLists);
  1746. staffList.ForEach(x => fMembers.Add(new FMember() { id = x.id, code = school, type = 2 }));
  1747. }
  1748. return fMembers;
  1749. }
  1750. }
  1751. public class CompareIdCode : IEqualityComparer<(string id, string code)>
  1752. {
  1753. public bool Equals((string id, string code) x, (string id, string code) y)
  1754. {
  1755. return x.id.Equals(y.id) && x.code.Equals(y.code);
  1756. }
  1757. public int GetHashCode((string id, string code) obj)
  1758. {
  1759. if (obj.id != null && obj.code != null)
  1760. {
  1761. return 1;
  1762. }
  1763. else
  1764. {
  1765. return 0;
  1766. }
  1767. }
  1768. }
  1769. }