CourseServiceBus.cs 19 KB

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