LessonService.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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;
  251. //再加 00到05小时内的 随机秒数
  252. Random rand = new Random();
  253. addSecond += rand.Next(0 - 18000);
  254. }
  255. else
  256. {
  257. addSecond = school_lesson_expire * 24 * 60 * 60;
  258. }
  259. lessonRecord.expire = now.AddSeconds(addSecond).ToUnixTimeMilliseconds();
  260. result.Add(school_lesson_expire, new ExpireTag { expire = lessonRecord.expire, tag = "delete" });
  261. // result.Add(school_lesson_expire, lessonRecord.expire);
  262. string biz = "expire";
  263. Notification notification = new Notification
  264. {
  265. hubName = "hita",
  266. type = "msg",
  267. from = $"ies5:{ Environment.GetEnvironmentVariable("Option:Location")}:private",
  268. to = new List<string> { tmdid },
  269. label = $"{biz}_lessonRecord",
  270. body = new
  271. {
  272. location = $"{Environment.GetEnvironmentVariable("Option:Location")}",
  273. biz = biz,
  274. tmdid = tmdid,
  275. tmdname = teacher.name,
  276. scope = scope,
  277. school = school,
  278. schoolName = schoolBase.name,
  279. sid = lessonRecord.id,
  280. sname = lessonRecord.name,
  281. stime = lessonRecord.startTime,
  282. expire = lessonRecord.expire,
  283. status = 1,
  284. //day = school_lesson_expire,
  285. time = now
  286. }.ToJsonString(),
  287. expires = DateTimeOffset.UtcNow.AddDays(7).ToUnixTimeSeconds()
  288. };
  289. var url = _configuration.GetValue<string>("HaBookAuth:CoreService:sendnotification");
  290. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  291. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  292. var location = $"{Environment.GetEnvironmentVariable("Option:Location")}";
  293. await _notificationService.SendNotification(clientID, clientSecret, location, url, notification); //站内发送消息
  294. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  295. List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
  296. if (records.Count <= 0)
  297. {
  298. foreach (var item in result)
  299. {
  300. string PartitionKey = string.Format("{0}{1}{2}", lessonRecord.code, "-", $"expire-{item.Key}");
  301. //课堂的id ,
  302. //课堂的通知时间类型progress, 默认就会发送一条,到期前一天发送一条,最后已到期发送一条。
  303. var message = new ServiceBusMessage(new
  304. {
  305. id = lessonRecord.id,
  306. progress = item.Key,
  307. code = lessonRecord.code,
  308. scope = lessonRecord.scope,
  309. school = lessonRecord.school,
  310. opt = "delete",
  311. expire = lessonRecord.expire,
  312. tmdid = tmdid,
  313. tmdname = teacher.name,
  314. name = lessonRecord.name,
  315. startTime = lessonRecord.startTime,
  316. tag = item.Value.tag
  317. }.ToJsonString());
  318. message.ApplicationProperties.Add("name", "LessonRecordExpire");
  319. long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), message, DateTimeOffset.FromUnixTimeMilliseconds(item.Value.expire));
  320. ChangeRecord changeRecord = new ChangeRecord
  321. {
  322. RowKey = lessonRecord.id,
  323. PartitionKey = PartitionKey,
  324. sequenceNumber = start,
  325. msgId = message.MessageId
  326. };
  327. await table.Save<ChangeRecord>(changeRecord);
  328. }
  329. }
  330. }
  331. else {
  332. if (lessonRecord.expire > 0)
  333. {
  334. var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
  335. List<ChangeRecord> records = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", lessonRecord.id } });
  336. foreach (var record in records)
  337. {
  338. try
  339. {
  340. await table.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
  341. await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
  342. }
  343. catch (Exception)
  344. {
  345. continue;
  346. }
  347. }
  348. }
  349. lessonRecord.expire = -1;
  350. }
  351. }
  352. }
  353. public record ExpireTag {
  354. public long expire { get; set; }
  355. public string tag { get; set; }
  356. }
  357. /// <summary>
  358. ///
  359. /// </summary>
  360. /// <param name="client"></param>
  361. /// <param name="_dingDing"></param>
  362. /// <param name="data"></param>
  363. /// <returns></returns>
  364. public static LessonDis DisLessonCount(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
  365. {
  366. //创建课堂的情况
  367. if (oldRecord == null && newRecord != null)
  368. {
  369. lessonDis.record = 1;
  370. }
  371. //删除数据的情况
  372. else if (oldRecord != null && newRecord == null)
  373. {
  374. lessonDis.record = -1;
  375. //P分数量加减
  376. if (oldRecord.pScore >= 70)
  377. {
  378. lessonDis.disPCount = -1;
  379. }
  380. //T分数量加减
  381. if (oldRecord.tScore >= 70)
  382. {
  383. lessonDis.disTCount = -1;
  384. }
  385. if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
  386. {
  387. lessonDis.disTCount = -1;
  388. }
  389. }
  390. //无效操作
  391. else if (oldRecord == null && newRecord == null)
  392. {
  393. }
  394. //前后操作都有值,则表示更新
  395. else
  396. {
  397. //P分数量加减
  398. if (oldRecord.pScore >= 70)
  399. {
  400. if (newRecord.pScore < 70)
  401. {
  402. lessonDis.disPCount = -1;
  403. }
  404. }
  405. else
  406. {
  407. if (newRecord.pScore >= 70)
  408. {
  409. lessonDis.disPCount = 1;
  410. }
  411. }
  412. //T分数量加减
  413. if (oldRecord.tScore >= 70)
  414. {
  415. if (newRecord.tScore < 70)
  416. {
  417. lessonDis.disTCount = -1;
  418. }
  419. }
  420. else
  421. {
  422. if (newRecord.tScore >= 70)
  423. {
  424. lessonDis.disTCount = 1;
  425. }
  426. }
  427. //双绿灯数量
  428. if (oldRecord.tScore >= 70 && oldRecord.pScore >= 70)
  429. {
  430. if (newRecord.tScore < 70 || newRecord.pScore < 70)
  431. {
  432. lessonDis.disDCount = -1;
  433. }
  434. }
  435. else
  436. {
  437. if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
  438. {
  439. lessonDis.disDCount = 1;
  440. }
  441. }
  442. }
  443. return lessonDis;
  444. }
  445. public static LessonDis DisLessonCount_2(LessonRecord oldRecord, LessonRecord newRecord, LessonDis lessonDis)
  446. {
  447. //创建课堂的情况
  448. if (oldRecord == null && newRecord != null)
  449. {
  450. lessonDis.record = 1;
  451. //P分数量加减
  452. if (newRecord.pScore >= 70)
  453. {
  454. lessonDis.disPCount = 1;
  455. }
  456. //T分数量加减
  457. if (newRecord.tScore >= 70)
  458. {
  459. lessonDis.disTCount = 1;
  460. }
  461. //双绿灯数量
  462. if (newRecord.tScore >= 70 && newRecord.pScore >= 70)
  463. {
  464. lessonDis.disDCount = 1;
  465. }
  466. }
  467. return lessonDis;
  468. }
  469. public static async Task FixLessonCount(CosmosClient client, DingDing _dingDing, LessonRecord record, LessonRecord oldRecord, LessonDis lessonDis)
  470. {
  471. LessonRecord data = null;
  472. try
  473. {
  474. if (record != null && oldRecord == null)
  475. {
  476. data = record;
  477. }
  478. if (record == null && oldRecord != null)
  479. {
  480. data = oldRecord;
  481. }
  482. if (record != null && oldRecord != null)
  483. {
  484. data = record;
  485. }
  486. int day = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).DayOfYear;
  487. int year = DateTimeOffset.FromUnixTimeMilliseconds(data.startTime).Year;
  488. int days = DateTimeHelper.getDays(year);
  489. //int years = DateTimeOffset.UtcNow.DayOfYear;
  490. string tbname = string.Empty;
  491. string code = string.Empty;
  492. if (data.scope != null && data.scope.Equals("school"))
  493. {
  494. code = $"LessonCount-{data.school}-{year}-{data.periodId}";
  495. tbname = "School";
  496. }
  497. else
  498. {
  499. code = $"LessonCount-{year}";
  500. tbname = "Teacher";
  501. }
  502. var response = await client.GetContainer(Constant.TEAMModelOS, tbname).ReadItemStreamAsync(data.tmdid.ToString(), new PartitionKey(code));
  503. if (response.Status == 200)
  504. {
  505. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  506. LessonCount count = json.ToObject<LessonCount>();
  507. count.tCount[day - 1] += lessonDis.disTCount;
  508. count.pCount[day - 1] += lessonDis.disPCount;
  509. count.ptCount[day - 1] += lessonDis.disDCount;
  510. count.beginCount[day - 1] += lessonDis.record;
  511. /*if (!count.courseIds.Contains(data.courseId))
  512. {
  513. count.courseIds.Add(data.courseId);
  514. count.beginCount[day] += 1;
  515. }*/
  516. await client.GetContainer("TEAMModelOS", tbname).ReplaceItemAsync(count, count.id, new PartitionKey(code));
  517. }
  518. else
  519. {
  520. LessonCount count = new LessonCount
  521. {
  522. id = data.tmdid,
  523. code = code,
  524. ttl = -1
  525. };
  526. double[] da = new double[days];
  527. List<double> list = new (da);
  528. List<double> listT = new (da);
  529. List<double> listP = new (da);
  530. List<double> listPT = new (da);
  531. list[day - 1] += lessonDis.record;
  532. listT[day - 1] += lessonDis.disTCount;
  533. listP[day - 1] += lessonDis.disPCount;
  534. listPT[day - 1] += lessonDis.disDCount;
  535. count.beginCount.AddRange(list);
  536. count.tCount.AddRange(listT);
  537. count.pCount.AddRange(listP);
  538. count.ptCount.AddRange(listPT);
  539. //count.courseIds.Add(data.courseId);
  540. await client.GetContainer("TEAMModelOS", tbname).CreateItemAsync(count, new PartitionKey(code));
  541. }
  542. }
  543. catch (Exception ex)
  544. {
  545. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-LessonCount-FixLessonCount\n{ex.Message}{ex.StackTrace}{data.ToJsonString()}", GroupNames.醍摩豆服務運維群組);
  546. }
  547. }
  548. }
  549. }