LessonService.cs 34 KB

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