CourseServiceBus.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. using Azure.Cosmos;
  2. using Microsoft.Azure.WebJobs;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading.Tasks;
  8. using TEAMModelOS.SDK.DI;
  9. using TEAMModelOS.SDK.Extension;
  10. using TEAMModelOS.SDK;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.SDK.Models.Cosmos.Common;
  13. using HTEXLib.COMM.Helpers;
  14. namespace TEAMModelFunction
  15. {
  16. public class CourseServiceBus
  17. {
  18. private readonly AzureCosmosFactory _azureCosmos;
  19. private readonly DingDing _dingDing;
  20. public CourseServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing)
  21. {
  22. _azureCosmos = azureCosmos;
  23. _dingDing = dingDing;
  24. }
  25. /// <summary>
  26. /// 完善课程变更
  27. /// </summary>
  28. /// <data msg>
  29. /// CourseChange
  30. ///// </data>
  31. /// <param name="msg"></param>
  32. /// <returns></returns>
  33. [FunctionName("Course")]
  34. public async Task Course([ServiceBusTrigger("%Azure:ServiceBus:ActiveTask%", "course", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
  35. {
  36. var client = _azureCosmos.GetCosmosClient();
  37. try
  38. {
  39. //await _dingDing.SendBotMsg($"ServiceBus,CourseChange:{msg}", GroupNames.醍摩豆服務運維群組);
  40. var jsonMsg = JsonDocument.Parse(msg);
  41. CourseChange courseChange = msg.ToObject<CourseChange>();
  42. if (courseChange == null)
  43. {
  44. return;
  45. }
  46. foreach (var cls in courseChange.addClass)
  47. {
  48. (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(client, _dingDing, new List<string> { cls }, courseChange.school);
  49. var addStudentsCls = tchList.FindAll(x => x.type == 2);
  50. var addTmdidsCls = tchList.FindAll(x => x.type == 1);
  51. //(List<TmdInfo> addTmdidsCls, List<StuInfo> addStudentsCls, List<ClassListInfo> classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { cls }, courseChange.school);
  52. foreach (var stu in addStudentsCls)
  53. {
  54. try
  55. {
  56. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  57. if (!stuCourse.Value.classId.Contains(cls))
  58. {
  59. stuCourse.Value.classId.Add(cls);
  60. }
  61. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  62. }
  63. catch (CosmosException ex)
  64. {
  65. if (ex.Response.Status == 404)
  66. {
  67. var course = new StuCourse
  68. {
  69. id = courseChange.id,
  70. scode = courseChange.code,
  71. name = courseChange.name,
  72. code = $"StuCourse-{courseChange.school}-{stu.id}",
  73. scope = courseChange.scope,
  74. school = courseChange.school,
  75. creatorId = courseChange.creatorId,
  76. classId = new List<string> { cls },
  77. pk = "StuCourse",
  78. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  79. };
  80. await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(course, new PartitionKey(course.code));
  81. }
  82. }
  83. }
  84. foreach (var tmd in addTmdidsCls)
  85. {
  86. try
  87. {
  88. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  89. if (!stuCourse.Value.classId.Contains(cls))
  90. {
  91. stuCourse.Value.classId.Add(cls);
  92. }
  93. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  94. }
  95. catch (CosmosException ex)
  96. {
  97. if (ex.Response.Status == 404)
  98. {
  99. var course = new StuCourse
  100. {
  101. id = courseChange.id,
  102. scode = courseChange.code,
  103. name = courseChange.name,
  104. code = $"StuCourse-{tmd.id}",
  105. scope = courseChange.scope,
  106. school = courseChange.school,
  107. creatorId = courseChange.creatorId,
  108. classId = new List<string> { cls },
  109. pk = "StuCourse",
  110. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  111. };
  112. await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(course, new PartitionKey(course.code));
  113. }
  114. }
  115. }
  116. }
  117. foreach (var list in courseChange.addList)
  118. {
  119. //(List<TmdInfo> addTmdidsCls, List<StuInfo> addStudentsCls, List<ClassListInfo> classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { list }, courseChange.school);
  120. (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(client, _dingDing, new List<string> { list }, courseChange.school);
  121. var addStudentsCls = tchList.FindAll(x => x.type == 2);
  122. var addTmdidsCls = tchList.FindAll(x => x.type == 1);
  123. foreach (var stu in addStudentsCls)
  124. {
  125. try
  126. {
  127. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{stu.code}-{stu.id}"));
  128. if (!stuCourse.Value.stulist.Contains(list))
  129. {
  130. stuCourse.Value.stulist.Add(list);
  131. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{stu.code}-{stu.id}"));
  132. }
  133. }
  134. catch (CosmosException ex)
  135. {
  136. if (ex.Response.Status == 404)
  137. {
  138. var course = new StuCourse
  139. {
  140. id = courseChange.id,
  141. scode = courseChange.code,
  142. name = courseChange.name,
  143. code = $"StuCourse-{courseChange.school}-{stu.id}",
  144. scope = courseChange.scope,
  145. school = courseChange.school,
  146. creatorId = courseChange.creatorId,
  147. stulist = new List<string> { list },
  148. pk = "StuCourse",
  149. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  150. };
  151. await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(course, new PartitionKey(course.code));
  152. }
  153. }
  154. }
  155. foreach (var tmd in addTmdidsCls)
  156. {
  157. try
  158. {
  159. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  160. if (!stuCourse.Value.stulist.Contains(list))
  161. {
  162. stuCourse.Value.stulist.Add(list);
  163. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  164. }
  165. }
  166. catch (CosmosException ex)
  167. {
  168. if (ex.Response.Status == 404)
  169. {
  170. var course = new StuCourse
  171. {
  172. id = courseChange.id,
  173. scode = courseChange.code,
  174. name = courseChange.name,
  175. code = $"StuCourse-{tmd.id}",
  176. scope = courseChange.scope,
  177. school = courseChange.school,
  178. creatorId = courseChange.creatorId,
  179. stulist = new List<string> { list },
  180. pk = "StuCourse",
  181. createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
  182. };
  183. await client.GetContainer(Constant.TEAMModelOS, "Student").CreateItemAsync(course, new PartitionKey(course.code));
  184. }
  185. }
  186. }
  187. }
  188. foreach (var delCls in courseChange.delClass)
  189. {
  190. //(List<TmdInfo> delTmdidsCls, List<StuInfo> delStudentsCls, List<ClassListInfo> classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { delCls }, courseChange.school);
  191. (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(client, _dingDing, new List<string> { delCls }, courseChange.school);
  192. var delStudentsCls = tchList.FindAll(x => x.type == 2);
  193. var delTmdidsCls = tchList.FindAll(x => x.type == 1);
  194. foreach (var stu in delStudentsCls)
  195. {
  196. try
  197. {
  198. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  199. if (stuCourse.Value.classId.Contains(delCls))
  200. {
  201. stuCourse.Value.classId.Remove(delCls);
  202. }
  203. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  204. {
  205. //当两个列表都不存在时则直接删除
  206. await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  207. }
  208. else
  209. {
  210. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  211. }
  212. }
  213. catch (CosmosException ex)
  214. {
  215. }
  216. }
  217. foreach (var tmd in delTmdidsCls)
  218. {
  219. try
  220. {
  221. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  222. if (stuCourse.Value.classId.Contains(delCls))
  223. {
  224. stuCourse.Value.classId.Remove(delCls);
  225. }
  226. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  227. {
  228. //当两个列表都不存在时则直接删除
  229. await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  230. }
  231. else
  232. {
  233. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  234. }
  235. }
  236. catch (CosmosException ex)
  237. {
  238. }
  239. }
  240. }
  241. foreach (var delList in courseChange.delList)
  242. {
  243. //(List<TmdInfo> delTmdidsCls, List<StuInfo> delStudentsCls, List<ClassListInfo> classLists) = await TriggerStuActivity.GetStuList(client, _dingDing, new List<string> { delList }, courseChange.school);
  244. (List<RMember> tchList, List<RGroupList> classLists) = await GroupListService.GetStutmdidListids(client, _dingDing, new List<string> { delList }, courseChange.school);
  245. var delStudentsCls = tchList.FindAll(x => x.type == 2);
  246. var delTmdidsCls = tchList.FindAll(x => x.type == 1);
  247. foreach (var stu in delStudentsCls)
  248. {
  249. try
  250. {
  251. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  252. if (stuCourse.Value.stulist.Contains(delList))
  253. {
  254. stuCourse.Value.stulist.Remove(delList);
  255. }
  256. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  257. {
  258. //当两个列表都不存在时则直接删除
  259. await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  260. }
  261. else
  262. {
  263. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{courseChange.school}-{stu.id}"));
  264. }
  265. }
  266. catch (CosmosException ex)
  267. {
  268. }
  269. }
  270. foreach (var tmd in delTmdidsCls)
  271. {
  272. try
  273. {
  274. ItemResponse<StuCourse> stuCourse = await client.GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  275. if (stuCourse.Value.stulist.Contains(delList))
  276. {
  277. stuCourse.Value.stulist.Remove(delList);
  278. }
  279. if (!stuCourse.Value.classId.IsNotEmpty() && !stuCourse.Value.stulist.IsNotEmpty())
  280. {
  281. //当两个列表都不存在时则直接删除
  282. await client.GetContainer(Constant.TEAMModelOS, "Student").DeleteItemAsync<StuCourse>(courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  283. }
  284. else
  285. {
  286. await client.GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuCourse>(stuCourse, courseChange.id, new PartitionKey($"StuCourse-{tmd.id}"));
  287. }
  288. }
  289. catch (CosmosException ex)
  290. {
  291. }
  292. }
  293. }
  294. }
  295. catch (Exception ex)
  296. {
  297. await _dingDing.SendBotMsg($"CourseServiceBus-Course\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  298. }
  299. }
  300. }
  301. }