LessonService.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.Extensions.Configuration;
  5. using System;
  6. using System.Collections.Generic;
  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.Helper.Common.DateTimeHelper;
  13. using TEAMModelOS.SDK.Models.Cosmos.Common;
  14. namespace TEAMModelOS.SDK.Models.Service
  15. {
  16. public class LessonService
  17. {
  18. public static async void DoAutoDeleteSchoolLessonRecord(LessonRecord lessonRecord,string scope ,CosmosClient client,string school,string tmdid,
  19. Teacher teacher, NotificationService _notificationService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, IConfiguration _configuration) {
  20. if (lessonRecord.scope.Equals("school"))
  21. {
  22. SchoolSetting setting = null;
  23. Azure.Response schoolSetting = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemStreamAsync(school, new PartitionKey("SchoolSetting"));
  24. School schoolBase = await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>(school, new PartitionKey("Base"));
  25. if (schoolSetting.Status == 200)
  26. {
  27. setting = JsonDocument.Parse(schoolSetting.Content).RootElement.Deserialize<SchoolSetting>();
  28. if (setting.lessonSetting != null)
  29. {
  30. if (setting.lessonSetting.openAutoClean != 0 || setting.lessonSetting.openAutoClean != 1)
  31. {
  32. setting.lessonSetting.openAutoClean = 0;
  33. setting.lessonSetting.expireDays = Constant.school_lesson_expire;
  34. }
  35. }
  36. else
  37. {
  38. setting.lessonSetting = new LessonSetting() { openAutoClean = 0, expireDays = Constant.school_lesson_expire };
  39. }
  40. }
  41. else
  42. {
  43. setting = new SchoolSetting() { lessonSetting = new LessonSetting { openAutoClean = 0, expireDays = Constant.school_lesson_expire } };
  44. }
  45. int school_lesson_expire =0;
  46. bool save = true;
  47. List<string> msg = new List<string>();
  48. if (setting.lessonSetting.openAutoClean == 1)
  49. {
  50. if (setting.lessonSetting.conds.IsEmpty())
  51. {
  52. save = false;
  53. }
  54. else {
  55. school_lesson_expire = setting.lessonSetting.expireDays;
  56. foreach (var item in setting.lessonSetting.conds)
  57. {
  58. switch (item.type)
  59. {
  60. case ">=":
  61. switch (item.key)
  62. {
  63. case "attendRate":
  64. if (!(lessonRecord.attendRate >= item.val))
  65. {
  66. save = false;
  67. msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
  68. }
  69. break;
  70. case "groupCount":
  71. if (!(lessonRecord.groupCount >= item.val))
  72. {
  73. save = false;
  74. msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
  75. }
  76. break;
  77. case "totalPoint":
  78. if (!(lessonRecord.totalPoint >= item.val))
  79. {
  80. save = false;
  81. msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
  82. }
  83. break;
  84. case "collateTaskCount":
  85. if (!(lessonRecord.collateTaskCount >= item.val))
  86. {
  87. save = false;
  88. msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
  89. }
  90. break;
  91. case "collateCount":
  92. if (!(lessonRecord.collateCount >= item.val))
  93. {
  94. save = false;
  95. msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
  96. }
  97. break;
  98. case "pushCount":
  99. if (!(lessonRecord.pushCount >= item.val))
  100. {
  101. save = false;
  102. msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
  103. }
  104. break;
  105. case "totalInteractPoint":
  106. if (!(lessonRecord.totalInteractPoint >= item.val))
  107. {
  108. save = false;
  109. msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
  110. }
  111. break;
  112. case "interactionCount":
  113. if (!(lessonRecord.interactionCount >= item.val))
  114. {
  115. save = false;
  116. msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
  117. }
  118. break;
  119. case "clientInteractionCount":
  120. if (!(lessonRecord.clientInteractionCount >= item.val))
  121. {
  122. save = false;
  123. msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
  124. }
  125. break;
  126. case "examQuizCount":
  127. if (!(lessonRecord.examQuizCount >= item.val))
  128. {
  129. save = false;
  130. msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
  131. }
  132. break;
  133. case "examPointRate":
  134. if (!(lessonRecord.examPointRate >= item.val))
  135. {
  136. save = false;
  137. msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
  138. }
  139. break;
  140. }
  141. break;
  142. case "<=":
  143. switch (item.key)
  144. {
  145. case "attendRate":
  146. if (!(lessonRecord.attendRate <= item.val))
  147. {
  148. save = false;
  149. msg.Add($"{item.key}:{lessonRecord.attendRate}{item.type}{item.val}");
  150. }
  151. break;
  152. case "groupCount":
  153. if (!(lessonRecord.groupCount <= item.val))
  154. {
  155. save = false;
  156. msg.Add($"{item.key}:{lessonRecord.groupCount}{item.type}{item.val}");
  157. }
  158. break;
  159. case "totalPoint":
  160. if (!(lessonRecord.totalPoint <= item.val))
  161. {
  162. save = false;
  163. msg.Add($"{item.key}:{lessonRecord.totalPoint}{item.type}{item.val}");
  164. }
  165. break;
  166. case "collateTaskCount":
  167. if (!(lessonRecord.collateTaskCount <= item.val))
  168. {
  169. save = false;
  170. msg.Add($"{item.key}:{lessonRecord.collateTaskCount}{item.type}{item.val}");
  171. }
  172. break;
  173. case "collateCount":
  174. if (!(lessonRecord.collateCount <= item.val))
  175. {
  176. save = false;
  177. msg.Add($"{item.key}:{lessonRecord.collateCount}{item.type}{item.val}");
  178. }
  179. break;
  180. case "pushCount":
  181. if (!(lessonRecord.pushCount <= item.val))
  182. {
  183. save = false;
  184. msg.Add($"{item.key}:{lessonRecord.pushCount}{item.type}{item.val}");
  185. }
  186. break;
  187. case "totalInteractPoint":
  188. if (!(lessonRecord.totalInteractPoint <= item.val))
  189. {
  190. save = false;
  191. msg.Add($"{item.key}:{lessonRecord.totalInteractPoint}{item.type}{item.val}");
  192. }
  193. break;
  194. case "interactionCount":
  195. if (!(lessonRecord.interactionCount <= item.val))
  196. {
  197. save = false;
  198. msg.Add($"{item.key}:{lessonRecord.interactionCount}{item.type}{item.val}");
  199. }
  200. break;
  201. case "clientInteractionCount":
  202. if (!(lessonRecord.clientInteractionCount <= item.val))
  203. {
  204. save = false;
  205. msg.Add($"{item.key}:{lessonRecord.clientInteractionCount}{item.type}{item.val}");
  206. }
  207. break;
  208. case "examQuizCount":
  209. if (!(lessonRecord.examQuizCount <= item.val))
  210. {
  211. save = false;
  212. msg.Add($"{item.key}:{lessonRecord.examQuizCount}{item.type}{item.val}");
  213. }
  214. break;
  215. case "examPointRate":
  216. if (!(lessonRecord.examPointRate <= item.val))
  217. {
  218. save = false;
  219. msg.Add($"{item.key}:{lessonRecord.examPointRate}{item.type}{item.val}");
  220. }
  221. break;
  222. }
  223. break;
  224. }
  225. }
  226. }
  227. }
  228. else {
  229. save = false;
  230. school_lesson_expire = Constant.school_lesson_expire;
  231. }
  232. if (!save && school_lesson_expire > 0)
  233. {
  234. // 1-时间戳,7-时间戳
  235. Dictionary<int, ExpireTag> result = new Dictionary<int, ExpireTag>();
  236. //暂定7天
  237. var now = DateTimeOffset.UtcNow;
  238. //剩余3天的通知
  239. //var day3= now.AddDays(school_lesson_expire - 3).ToUnixTimeMilliseconds();
  240. //result.Add(3, day3);
  241. //剩余1天的通知
  242. var day1 = now.AddDays(school_lesson_expire - (school_lesson_expire - 1)).ToUnixTimeMilliseconds();
  243. result.Add(1, new ExpireTag { expire= day1 ,tag= "notification" });
  244. //到期通知
  245. //不到五点上传的课例,七天之后直接删除。
  246. int addSecond = 0;
  247. if (now.Hour > 5)
  248. {
  249. // 到凌晨00点还差 (24 - now.Hour) *60 * 60 分钟,再加天数;
  250. addSecond = school_lesson_expire * 86400 + (24 - now.Hour) * 3600-(now.Hour* 3600);
  251. //再加 00到05小时内的 随机秒数
  252. Random rand = new Random();
  253. int randInt = rand.Next(0, 18000);
  254. addSecond += randInt;
  255. }
  256. else
  257. {
  258. addSecond = school_lesson_expire * 24 * 60 * 60;
  259. }
  260. lessonRecord.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
  261. result.Add(school_lesson_expire, new ExpireTag { expire = lessonRecord.expire, tag = "delete" });
  262. // result.Add(school_lesson_expire, lessonRecord.expire);
  263. string biz = "expire";
  264. Notification notification = new Notification
  265. {
  266. hubName = "hita",
  267. type = "msg",
  268. from = $"ies5:{ Environment.GetEnvironmentVariable("Option:Location")}:private",
  269. to = new List<string> { tmdid },
  270. label = $"{biz}_lessonRecord",
  271. body = new
  272. {
  273. location = $"{Environment.GetEnvironmentVariable("Option:Location")}",
  274. biz = biz,
  275. tmdid = tmdid,
  276. tmdname = teacher.name,
  277. scope = scope,
  278. school = school,
  279. schoolName = schoolBase.name,
  280. sid = lessonRecord.id,
  281. sname = lessonRecord.name,
  282. stime = lessonRecord.startTime,
  283. expire = lessonRecord.expire,
  284. status = 1,
  285. //day = school_lesson_expire,
  286. time = now
  287. }.ToJsonString(),
  288. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  289. };
  290. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  291. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  292. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  293. var location = $"{Environment.GetEnvironmentVariable("Option:Location")}";
  294. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  295. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  296. List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
  297. if (records.Count <= 0)
  298. {
  299. foreach (var item in result)
  300. {
  301. string PartitionKey = string.Format("{0}{1}{2}", lessonRecord.code, "-", $"expire-{item.Key}");
  302. //课堂的id ,
  303. //课堂的通知时间类型progress, 默认就会发送一条,到期前一天发送一条,最后已到期发送一条。
  304. var message = new ServiceBusMessage(new
  305. {
  306. id = lessonRecord.id,
  307. progress = item.Key,
  308. code = lessonRecord.code,
  309. scope = lessonRecord.scope,
  310. school = lessonRecord.school,
  311. opt = "delete",
  312. expire = lessonRecord.expire,
  313. tmdid = tmdid,
  314. tmdname = teacher.name,
  315. name = lessonRecord.name,
  316. startTime = lessonRecord.startTime,
  317. tag = item.Value.tag
  318. }.ToJsonString());
  319. message.ApplicationProperties.Add("name", "LessonRecordExpire");
  320. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value.expire));
  321. ChangeRecord changeRecord = new ChangeRecord
  322. {
  323. RowKey = lessonRecord.id,
  324. PartitionKey = PartitionKey,
  325. sequenceNumber = start,
  326. msgId = message.MessageId
  327. };
  328. await table.Save<ChangeRecord>(changeRecord);
  329. }
  330. }
  331. }
  332. else {
  333. if (lessonRecord.expire > 0)
  334. {
  335. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  336. List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
  337. foreach (var record in records)
  338. {
  339. try
  340. {
  341. await table.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  342. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  343. }
  344. catch (Exception)
  345. {
  346. continue;
  347. }
  348. }
  349. }
  350. lessonRecord.save = 1;
  351. lessonRecord.expire = -1;
  352. }
  353. }
  354. }
  355. public record ExpireTag {
  356. public long expire { get; set; }
  357. public string tag { get; set; }
  358. }
  359. /// <summary>
  360. ///
  361. /// </summary>
  362. /// <param name="client"></param>
  363. /// <param name="_dingDing"></param>
  364. /// <param name="data"></param>
  365. /// <returns></returns>
  366. public static LessonDis DisLessonCount(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
  367. {
  368. //创建课堂的情况
  369. if (oldRecord == null && newRecord != null)
  370. {
  371. lessonDis.record = 1;
  372. }
  373. //删除数据的情况
  374. else if (oldRecord != null && newRecord == null)
  375. {
  376. lessonDis.record = -1;
  377. //P分数量加减
  378. if (oldRecord.pScore >= 70)
  379. {
  380. lessonDis.disPCount = -1;
  381. }
  382. //T分数量加减
  383. if (oldRecord.tScore >= 70)
  384. {
  385. lessonDis.disTCount = -1;
  386. }
  387. if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
  388. {
  389. lessonDis.disTCount = -1;
  390. }
  391. }
  392. //无效操作
  393. else if (oldRecord == null && newRecord == null)
  394. {
  395. }
  396. //前后操作都有值,则表示更新
  397. else
  398. {
  399. //P分数量加减
  400. if (oldRecord.pScore >= 70)
  401. {
  402. if (newRecord.pScore < 70)
  403. {
  404. lessonDis.disPCount = -1;
  405. }
  406. }
  407. else
  408. {
  409. if (newRecord.pScore >= 70)
  410. {
  411. lessonDis.disPCount = 1;
  412. }
  413. }
  414. //T分数量加减
  415. if (oldRecord.tScore >= 70)
  416. {
  417. if (newRecord.tScore < 70)
  418. {
  419. lessonDis.disTCount = -1;
  420. }
  421. }
  422. else
  423. {
  424. if (newRecord.tScore >= 70)
  425. {
  426. lessonDis.disTCount = 1;
  427. }
  428. }
  429. //双绿灯数量
  430. if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
  431. {
  432. if (newRecord.tScore < 70 || newRecord.pScore < 70)
  433. {
  434. lessonDis.disDCount = -1;
  435. }
  436. }
  437. else
  438. {
  439. if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
  440. {
  441. lessonDis.disDCount = 1;
  442. }
  443. }
  444. }
  445. return lessonDis;
  446. }
  447. public static LessonDis DisLessonCount_2(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
  448. {
  449. //创建课堂的情况
  450. if (oldRecord == null && newRecord != null)
  451. {
  452. lessonDis.record = 1;
  453. //P分数量加减
  454. if (newRecord.pScore >= 70)
  455. {
  456. lessonDis.disPCount = 1;
  457. }
  458. //T分数量加减
  459. if (newRecord.tScore >= 70)
  460. {
  461. lessonDis.disTCount = 1;
  462. }
  463. //双绿灯数量
  464. if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
  465. {
  466. lessonDis.disDCount = 1;
  467. }
  468. }
  469. return lessonDis;
  470. }
  471. public static async Task FixLessonCount(CosmosClient client, DingDing _dingDing, LessonRecord record, LessonRecord oldRecord, LessonDis lessonDis)
  472. {
  473. LessonRecord data = null;
  474. try
  475. {
  476. if (record != null && oldRecord == null)
  477. {
  478. data = record;
  479. }
  480. if (record == null && oldRecord != null)
  481. {
  482. data = oldRecord;
  483. }
  484. if (record != null && oldRecord != null)
  485. {
  486. data = record;
  487. }
  488. int day = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).DayOfYear;
  489. int year = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).Year;
  490. int days = DateTimeHelper.getDays(year);
  491. //int years = DateTimeOffset.UtcNow.DayOfYear;
  492. string tbname = string.Empty;
  493. string code = string.Empty;
  494. if (data.scope != null && data.scope.Equals("school"))
  495. {
  496. code = $"LessonCount-{data.school}-{year}-{data.periodId}";
  497. tbname = "School";
  498. }
  499. else
  500. {
  501. code = $"LessonCount-{year}";
  502. tbname = "Teacher";
  503. }
  504. var response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(data.tmdid.ToString(), new PartitionKey(code));
  505. if (response.Status == 200)
  506. {
  507. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  508. LessonCount count = json.ToObject<LessonCount>();
  509. count.tCount[day - 1] += lessonDis.disTCount;
  510. count.pCount[day - 1] += lessonDis.disPCount;
  511. count.ptCount[day - 1] += lessonDis.disDCount;
  512. count.beginCount[day - 1] += lessonDis.record;
  513. /*if (!count.courseIds.Contains(data.courseId))
  514. {
  515. count.courseIds.Add(data.courseId);
  516. count.beginCount[day] += 1;
  517. }*/
  518. await client.GetContainer("TEAMModelOS", tbname).ReplaceItemAsync(count, count.id, new PartitionKey(code));
  519. }
  520. else
  521. {
  522. LessonCount count = new LessonCount
  523. {
  524. id = data.tmdid,
  525. code = code,
  526. ttl = -1
  527. };
  528. double[] da = new double[days];
  529. List<double> list = new (da);
  530. List<double> listT = new (da);
  531. List<double> listP = new (da);
  532. List<double> listPT = new (da);
  533. list[day - 1] += lessonDis.record;
  534. listT[day - 1] += lessonDis.disTCount;
  535. listP[day - 1] += lessonDis.disPCount;
  536. listPT[day - 1] += lessonDis.disDCount;
  537. count.beginCount.AddRange(list);
  538. count.tCount.AddRange(listT);
  539. count.pCount.AddRange(listP);
  540. count.ptCount.AddRange(listPT);
  541. //count.courseIds.Add(data.courseId);
  542. await client.GetContainer("TEAMModelOS", tbname).CreateItemAsync(count, new PartitionKey(code));
  543. }
  544. }
  545. catch (Exception ex)
  546. {
  547. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-LessonCount-FixLessonCount\n{ex.Message}\n{ex.StackTrace}{data.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  548. }
  549. }
  550. }
  551. }