TriggerStuActivity.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. using Azure;
  2. using Azure.Cosmos;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Text.Json;
  9. using System.Threading.Tasks;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Extension;
  12. using TEAMModelOS.SDK;
  13. using TEAMModelOS.SDK.Models;
  14. using TEAMModelOS.SDK.Models.Cosmos.Common;
  15. namespace TEAMModelFunction
  16. {
  17. public class TriggerStuActivity
  18. {
  19. public static async Task RefreshStuActivity(CosmosClient client, DingDing _dingDing, string id, string code)
  20. {
  21. MQActivity activity = null;
  22. try
  23. {
  24. var aactivity = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(id, new Azure.Cosmos.PartitionKey(code));
  25. using var da = await JsonDocument.ParseAsync(aactivity.ContentStream);
  26. activity = da.ToObject<MQActivity>();
  27. }
  28. catch (CosmosException ex)
  29. {
  30. }
  31. if (activity != null)
  32. {
  33. (List<TmdInfo> tmdids, List<StuInfo> students,List<ClassListInfo> classLists) = await GetStuList(client, _dingDing, activity.classes, activity.school);
  34. if (tmdids.IsNotEmpty())
  35. {
  36. foreach (TmdInfo tmdid in tmdids)
  37. {
  38. var stucourse = new StuActivity
  39. {
  40. id = activity.id,
  41. scode = activity.code,
  42. name = activity.name,
  43. code = $"Activity-{tmdid.id}",
  44. scope = activity.scope,
  45. school = activity.school,
  46. creatorId = activity.creatorId,
  47. pk = "Activity",
  48. type = activity.pk,
  49. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  50. startTime = activity.startTime,
  51. endTime = activity.endTime,
  52. blob = activity.blob,
  53. owner = activity.owner
  54. };
  55. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  56. }
  57. }
  58. if (students.IsNotEmpty())
  59. {
  60. foreach (StuInfo student in students)
  61. {
  62. var stucourse = new StuActivity
  63. {
  64. id = activity.id,
  65. scode = activity.code,
  66. name = activity.name,
  67. code = $"Activity-{activity.school}-{student.id}",
  68. scope = activity.scope,
  69. school = activity.school,
  70. creatorId = activity.creatorId,
  71. pk = "Activity",
  72. type = activity.pk,
  73. subjects = activity.pk.ToLower().Equals("exam") && activity.subjects.IsNotEmpty() ? new List<string>() { activity.subjects[0].id } : new List<string>() { "" },
  74. startTime = activity.startTime,
  75. endTime = activity.endTime,
  76. blob = activity.blob,
  77. owner = activity.owner
  78. };
  79. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(stucourse, new PartitionKey(stucourse.code));
  80. }
  81. }
  82. }
  83. }
  84. public static async Task<string> SaveStuActivity(CosmosClient client, DingDing _dingDing, List<StuActivity> stuActivities,List<StuActivity> tmdActivities) {
  85. try {
  86. if (stuActivities.IsNotEmpty())
  87. {
  88. foreach (var x in stuActivities)
  89. {
  90. await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(x, new PartitionKey(x.code));
  91. }
  92. }
  93. if (tmdActivities.IsNotEmpty())
  94. {
  95. foreach (var x in tmdActivities)
  96. {
  97. await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(x, new PartitionKey(x.code));
  98. }
  99. }
  100. } catch (Exception ex) {
  101. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-SaveStuActivity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  102. }
  103. return "";
  104. }
  105. /// <summary>
  106. /// 获取行政班接口
  107. /// </summary>
  108. /// <param name="client"></param>
  109. /// <param name="_dingDing"></param>
  110. /// <param name="classes"></param>
  111. /// <param name="school"></param>
  112. /// <returns></returns>
  113. public static async Task<List<ClassInfo>> GetClassInfo(CosmosClient client, DingDing _dingDing, List<string> classes, string school)
  114. {
  115. try {
  116. List<ClassInfo> classInfos = new List<ClassInfo>();
  117. if (classes.IsNotEmpty()) {
  118. List<string> sqlList = new List<string>();
  119. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  120. string sql = string.Join(" , ", sqlList);
  121. if (!string.IsNullOrEmpty(school))
  122. {
  123. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ClassInfo>(queryText: $"select c.id,c.name ,c.periodId,c.year from c where c.id in ({sql})",
  124. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Class-{school}") }))
  125. {
  126. classInfos.Add(item);
  127. }
  128. //await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<ClassListInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  129. // requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  130. //{
  131. // item.from = "SchStuList";
  132. // classInfos.Add(item);
  133. //}
  134. }
  135. //List<StuList> tchLists = new List<StuList>();
  136. //await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<ClassListInfo>(queryText: $"select c.id,c.name from c where c.id in ({sql})",
  137. // requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  138. //{
  139. // item.from = "TchStuList";
  140. // classInfos.Add(item);
  141. //}
  142. }
  143. return classInfos;
  144. } catch (Exception ex) {
  145. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetClassInfo\n{ex.Message}\n{ex.StackTrace}\n class{classes.ToJsonString()},{school}", GroupNames.醍摩豆服務運維群組);
  146. throw new Exception(ex.Message, ex);
  147. }
  148. }
  149. public static async Task<(List<TmdInfo> tmdinfos, List<StuInfo> students,List<ClassListInfo>classInfo)> GetStuList(CosmosClient client, DingDing _dingDing, List<string> claes, string school)
  150. {
  151. try {
  152. List<string> classes = new List<string>();
  153. foreach (string ss in claes)
  154. {
  155. classes.Add(ss);
  156. }
  157. List<TmdInfo> tmdinfos = new List<TmdInfo>();
  158. List<Students> studentss = new List<Students>();
  159. List<string> tmdids = new List<string>();
  160. List<StuInfo> stuInfos = new List<StuInfo>();
  161. if (!classes.IsNotEmpty()) { return (tmdinfos, new List<StuInfo>(),null); }
  162. List<string> sqlList = new List<string>();
  163. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  164. string sql = string.Join(" , ", sqlList);
  165. List<StuList> schList = new List<StuList>();
  166. List<Student> students = new List<Student>();
  167. if (!string.IsNullOrEmpty(school)) {
  168. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  169. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList-{school}") }))
  170. {
  171. schList.Add(item);
  172. }
  173. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<Student>(queryText: $"select value(c) from c where c.classId in ({sql})",
  174. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"Base-{school}") }))
  175. {
  176. students.Add(item);
  177. }
  178. }
  179. List<StuList> tchLists = new List<StuList>();
  180. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<StuList>(queryText: $"select value(c) from c where c.id in ({sql})",
  181. requestOptions: new QueryRequestOptions() { PartitionKey = new Azure.Cosmos.PartitionKey($"StuList") }))
  182. {
  183. tchLists.Add(item);
  184. }
  185. foreach (var x in schList) {
  186. if (x.students.IsNotEmpty())
  187. {
  188. studentss.AddRange(x.students);
  189. }
  190. if (x.tmids.IsNotEmpty())
  191. {
  192. tmdids.AddRange(x.tmids);
  193. }
  194. classes.Remove(x.id);
  195. }
  196. foreach (var x in tchLists)
  197. {
  198. if (x.students.IsNotEmpty()) {
  199. studentss.AddRange(x.students);
  200. }
  201. if (x.tmids.IsNotEmpty())
  202. {
  203. tmdids.AddRange(x.tmids);
  204. }
  205. classes.Remove(x.id);
  206. }
  207. if (tmdids.IsNotEmpty()) {
  208. List<TmdInfo> infos = new List<TmdInfo>();
  209. List<string> inids = new List<string>();
  210. tmdids.ForEach(x => { inids.Add($"'{x}'"); });
  211. var insql = string.Join(",", inids);
  212. var queryslt = $"SELECT value(c) FROM c where c.id in ({insql})";
  213. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<TmdInfo>(queryText: queryslt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  214. {
  215. infos.Add(item);
  216. }
  217. tmdinfos.AddRange(infos);
  218. }
  219. if (studentss.IsNotEmpty()) {
  220. List<string> inidstus = new List<string>();
  221. studentss.Select(x => x.id).ToList().ForEach(x => { inidstus.Add($"'{x}'"); });
  222. var insqlstu = string.Join(",", inidstus);
  223. var querystu = $"SELECT c.id,c.code,c.name,c.picture,c.classId,c.year,c.schoolId FROM c where c.id in ({insqlstu})";
  224. await foreach (var item in client.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<StuInfo>(queryText: querystu, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{school}") }))
  225. {
  226. stuInfos.Add(item);
  227. }
  228. }
  229. students.ForEach(x =>
  230. {
  231. if (!stuInfos.Select(y => y.classId).ToList().Contains(x.id)) {
  232. stuInfos.Add(new StuInfo { id = x.id, code = x.code, schoolId = x.schoolId, classId = x.classId, name = x.name, picture = x.picture, year = x.year });
  233. }
  234. });
  235. List<ClassListInfo> classInfo = new List<ClassListInfo>();
  236. schList.ForEach(x => {
  237. ClassListInfo classListInfo = new ClassListInfo {id=x.id,name=x.name };
  238. if (x.students.IsNotEmpty()) {
  239. x.students.ForEach(y => {
  240. var stuinfo= stuInfos.Where(z => z.id == y.id).FirstOrDefault();
  241. if (stuinfo != null) {
  242. classListInfo.stuInfos.Add(stuinfo);
  243. }
  244. });
  245. }
  246. if (x.tmids.IsNotEmpty())
  247. {
  248. x.tmids.ForEach(y => {
  249. var tmdinfo = tmdinfos.Where(z => z.id == y).FirstOrDefault();
  250. if (tmdinfo != null)
  251. {
  252. classListInfo.tmdInfos.Add(tmdinfo);
  253. }
  254. });
  255. }
  256. classInfo.Add(classListInfo);
  257. });
  258. tchLists.ForEach(x => {
  259. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  260. if (x.students.IsNotEmpty())
  261. {
  262. x.students.ForEach(y => {
  263. var stuinfo = stuInfos.Where(z => z.id == y.id).FirstOrDefault();
  264. if (stuinfo != null)
  265. {
  266. classListInfo.stuInfos.Add(stuinfo);
  267. }
  268. });
  269. }
  270. if (x.tmids.IsNotEmpty())
  271. {
  272. x.tmids.ForEach(y => {
  273. var tmdinfo = tmdinfos.Where(z => z.id == y).FirstOrDefault();
  274. if (tmdinfo != null)
  275. {
  276. classListInfo.tmdInfos.Add(tmdinfo);
  277. }
  278. });
  279. }
  280. classInfo.Add(classListInfo);
  281. });
  282. //var classeids= students.GroupBy(x => x.classId).Select(x => x.Key).ToList();
  283. List<ClassInfo> classInfos= await GetClassInfo(client, _dingDing, classes, school);
  284. classInfos.ForEach(x =>
  285. {
  286. ClassListInfo classListInfo = new ClassListInfo { id = x.id, name = x.name };
  287. var list= students.Where(y => y.classId == x.id).ToList();
  288. list.ForEach(z => { classListInfo.stuInfos.Add(new StuInfo { id = z.id, code = z.code, schoolId = z.schoolId, classId = z.classId, name = z.name, picture = z.picture, year = z.year }); });
  289. classInfo.Add(classListInfo);
  290. });
  291. return (tmdinfos, stuInfos, classInfo) ;
  292. }
  293. catch (Exception ex)
  294. {
  295. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-OS,TriggerStuActivity-GetStuList\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  296. }
  297. return (null, null,null);
  298. }
  299. }
  300. public class ClassListInfo
  301. {
  302. public string id { get; set; }
  303. public string name { get; set; }
  304. // public string from { get; set; }
  305. public List<StuInfo> stuInfos { get; set; } = new List<StuInfo>();
  306. public List<TmdInfo> tmdInfos { get; set; } = new List<TmdInfo>();
  307. }
  308. }