GroupListService.cs 93 KB

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