BILeeson.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using Azure.Cosmos;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Text.Json;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.SDK.DI;
  10. using TEAMModelOS.SDK.Extension;
  11. using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
  12. using TEAMModelOS.SDK.Models.Cosmos.BI.BITable;
  13. namespace TEAMModelOS.SDK.Models.Service.BI
  14. {
  15. public class BILeeson
  16. {
  17. /// <summary>
  18. /// 通过时间戳保存开课记录和课例记录统计
  19. /// 统计数据存储在CosmosDB表中
  20. /// </summary>
  21. /// <param name="cosmosClient">cosmosDB连接</param>
  22. /// <param name="_azureRedis">redis</param>
  23. /// <param name="_dingDing">错误消息发送</param>
  24. /// <param name="unix">13位时间戳</param>
  25. /// <param name="num">数量</param>
  26. /// <param name="type">类型:0 开课记录 1 课例记录</param>
  27. /// <param name="schoolId">学校编码</param>
  28. /// <returns></returns>
  29. public static async Task SetCosmosDBStats(CosmosClient cosmosClient, AzureRedisFactory _azureRedis, DingDing _dingDing, long unix, int num = 1, int type = 0, string schoolId = null)
  30. {
  31. try
  32. {
  33. DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(unix);
  34. int year, month, day, hour, days;
  35. year = dateTime.Year;
  36. month = dateTime.Month;
  37. day = dateTime.Day;
  38. hour = dateTime.Hour;
  39. days = dateTime.DayOfYear;
  40. var dateDay = dateTime.ToString("yyyyMMdd"); //获取当天的日期
  41. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  42. DateTime expireDay = DateTime.UtcNow.AddDays(8); //8天后到期
  43. DateTime expireYear = DateTime.UtcNow.AddDays(396); //一年后到期
  44. var redisClient = _azureRedis.GetRedisClient(8);
  45. string LessType = "Open";
  46. if (type == 1)
  47. LessType = "Lesson";
  48. await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn 小时为单位
  49. await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
  50. var expDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{dateDay}");
  51. if (expDay == null)
  52. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{dateDay}", expireDay); //设置八天后到期
  53. var expYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{year}");
  54. if (expYear == null)
  55. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{year}", expireYear); //设置一年后到期
  56. //保存当天的统计 小时
  57. var dayCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:All:{LessType}:{dateDay}");
  58. if (dayCnt != null && dayCnt.Length > 0)
  59. {
  60. double[] daHour = new double[23];
  61. foreach (var item in dayCnt)
  62. {
  63. double val = ((double)item.Score);
  64. int key = ((int)item.Element);
  65. daHour[key] = val;
  66. }
  67. List<double> lessHours = new(daHour);
  68. var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{dateDay}", new PartitionKey("LessonHour"));
  69. if (lessRes.Status == 200)
  70. {
  71. using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
  72. LessonStats lessonStats = json.ToObject<LessonStats>();
  73. lessonStats.code = "LessonHour";
  74. lessonStats.pk = "LessonHour";
  75. if (type == 1)
  76. lessonStats.lesson = lessHours;
  77. else
  78. lessonStats.open = lessHours;
  79. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonStats, lessonStats.id, new PartitionKey("LessonHour"));
  80. }
  81. else
  82. {
  83. LessonStats lessonStats = new();
  84. lessonStats.id = $"{dateDay}";
  85. lessonStats.code = "LessonHour";
  86. lessonStats.pk = "LessonHour";
  87. if (type == 1)
  88. lessonStats.lesson = lessHours;
  89. else
  90. lessonStats.open = lessHours;
  91. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonStats, new PartitionKey("LessonHour"));
  92. }
  93. }
  94. //保一年的统计 天
  95. var yearCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:All:{LessType}:{year}");
  96. if (yearCnt != null && yearCnt.Length > 0)
  97. {
  98. double[] daYear = new double[yearDays];
  99. foreach (var item in yearCnt)
  100. {
  101. double val = ((double)item.Score);
  102. int key = ((int)item.Element);
  103. daYear[key] = val;
  104. }
  105. List<double> lessYear = new(daYear);
  106. var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey("LessonYear"));
  107. if (lessRes.Status == 200)
  108. {
  109. using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
  110. LessonStats lessonYear = json.ToObject<LessonStats>();
  111. lessonYear.code = "LessonYear";
  112. lessonYear.pk = "LessonYear";
  113. if (type == 1)
  114. lessonYear.lesson = lessYear;
  115. else
  116. lessonYear.open = lessYear;
  117. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey("LessonYear"));
  118. }
  119. else
  120. {
  121. LessonStats lessonYear = new();
  122. lessonYear.id = $"{year}";
  123. lessonYear.code = "LessonYear";
  124. lessonYear.pk = "LessonYear";
  125. if (type == 1)
  126. lessonYear.lesson = lessYear;
  127. else
  128. lessonYear.open = lessYear;
  129. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYear, new PartitionKey("LessonYear"));
  130. }
  131. }
  132. if (!string.IsNullOrEmpty(schoolId))
  133. {
  134. await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn 小时为单位
  135. await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
  136. var scExpDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
  137. if (scExpDay == null)
  138. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", expireDay); //设置八天后到期
  139. var scExpYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{year}");
  140. if (scExpYear == null)
  141. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", expireYear); //设置一年后到期
  142. //保存当天的统计 小时
  143. var dayScCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
  144. if (dayScCnt != null && dayScCnt.Length > 0)
  145. {
  146. double[] daHour = new double[23];
  147. foreach (var item in dayScCnt)
  148. {
  149. double val = ((double)item.Score);
  150. int key = ((int)item.Element);
  151. daHour[key] = val;
  152. }
  153. List<double> lessHours = new(daHour);
  154. var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{dateDay}", new PartitionKey($"LessonHour-{schoolId}"));
  155. if (lessRes.Status == 200)
  156. {
  157. using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
  158. LessonStats lessonStats = json.ToObject<LessonStats>();
  159. lessonStats.code = $"LessonHour-{schoolId}";
  160. lessonStats.pk = "LessonHour";
  161. if (type == 1)
  162. lessonStats.lesson = lessHours;
  163. else
  164. lessonStats.open = lessHours;
  165. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonStats, lessonStats.id, new PartitionKey($"LessonHour-{schoolId}"));
  166. }
  167. else
  168. {
  169. LessonStats lessonStats = new();
  170. lessonStats.id = $"{dateDay}";
  171. lessonStats.code = $"LessonHour-{schoolId}";
  172. lessonStats.pk = "LessonHour";
  173. if (type == 1)
  174. lessonStats.lesson = lessHours;
  175. else
  176. lessonStats.open = lessHours;
  177. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonStats, new PartitionKey($"LessonHour-{schoolId}"));
  178. }
  179. }
  180. //保一年的统计 天
  181. var scYearCnt = redisClient.SortedSetRangeByScoreWithScores($"BILesson:{schoolId}:{LessType}:{year}");
  182. if (scYearCnt != null && scYearCnt.Length > 0)
  183. {
  184. double[] daYear = new double[yearDays];
  185. foreach (var item in scYearCnt)
  186. {
  187. double val = ((double)item.Score);
  188. int key = ((int)item.Element);
  189. daYear[key] = val;
  190. }
  191. List<double> lessYear = new(daYear);
  192. var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey($"LessonYear-{schoolId}"));
  193. if (lessRes.Status == 200)
  194. {
  195. using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
  196. LessonStats lessonYear = json.ToObject<LessonStats>();
  197. lessonYear.code = $"LessonYear-{schoolId}";
  198. lessonYear.pk = "LessonYear";
  199. if (type == 1)
  200. lessonYear.lesson = lessYear;
  201. else
  202. lessonYear.open = lessYear;
  203. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"LessonYear-{schoolId}"));
  204. }
  205. else
  206. {
  207. LessonStats lessonYear = new();
  208. lessonYear.id = $"{year}";
  209. lessonYear.code = $"LessonYear-{schoolId}";
  210. lessonYear.pk = "LessonYear";
  211. if (type == 1)
  212. lessonYear.lesson = lessYear;
  213. else
  214. lessonYear.open = lessYear;
  215. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYear, new PartitionKey($"LessonYear-{schoolId}"));
  216. }
  217. }
  218. }
  219. }
  220. catch (Exception ex)
  221. {
  222. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-SetBICosmosDBStats 课例统计异常 {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  223. }
  224. }
  225. /// <summary>
  226. /// 通过时间戳保存开课记录和课例记录统计
  227. /// 统计数据存储在table表中
  228. /// </summary>
  229. /// <param name="_azureStorage">table连接池</param>
  230. /// <param name="_azureRedis">redis连接池</param>
  231. /// <param name="_dingDing">错误消息发送</param>
  232. /// <param name="unix">13位时间戳</param>
  233. /// <param name="num">数量</param>
  234. /// <param name="type">类型:0 开课记录 1 课例记录</param>
  235. /// <param name="schoolId">学校编码</param>
  236. /// <returns></returns>
  237. public static async Task SetTableStats(AzureStorageFactory _azureStorage, AzureRedisFactory _azureRedis, DingDing _dingDing, long unix, int num = 1, int type = 0, string schoolId = null)
  238. {
  239. try
  240. {
  241. //SemaphoreSlim slimlock = new(1, 1); //对可同时访问资源或资源池的线程数加以限制 结束
  242. //await slimlock.WaitAsync();
  243. Monitor.TryEnter(unix); //锁对象
  244. DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(unix);
  245. int year, month, day, hour, days;
  246. year = dateTime.Year;
  247. month = dateTime.Month;
  248. day = dateTime.Day;
  249. hour = dateTime.Hour;
  250. days = dateTime.DayOfYear;
  251. var dateDay = dateTime.ToString("yyyyMMdd"); //获取当天的日期
  252. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  253. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIStats");
  254. var redisClient = _azureRedis.GetRedisClient(8);
  255. DateTime expireDay = DateTime.UtcNow.AddDays(8); //8天后到期
  256. DateTime expireYear = DateTime.UtcNow.AddDays(396); //一年后到期
  257. string LessType = "Open";
  258. if (type == 1)
  259. LessType = "Lesson";
  260. await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn 小时为单位
  261. await redisClient.SortedSetIncrementAsync($"BIStats:Less:All:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
  262. var expDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{dateDay}");
  263. if (expDay == null)
  264. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{dateDay}", expireDay); //设置八天后到期
  265. var expYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:All:{LessType}:{year}");
  266. if (expYear == null)
  267. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:All:{LessType}:{year}", expireYear); //设置一年后到期
  268. //保存当天的统计 小时
  269. var dayCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:All:{LessType}:{dateDay}");
  270. if (dayCnt != null && dayCnt.Length > 0)
  271. {
  272. double[] daHour = new double[23];
  273. foreach (var item in dayCnt)
  274. {
  275. double val = ((double)item.Score);
  276. int key = ((int)item.Element);
  277. daHour[key] = val;
  278. }
  279. string hoursStats = string.Join(",", daHour);
  280. LessStats lessStats = table.Get<LessStats>("LessonHour", $"{dateDay}");
  281. if (lessStats != null)
  282. {
  283. if (type == 1)
  284. lessStats.lesson = hoursStats;
  285. else
  286. lessStats.open = hoursStats;
  287. }
  288. else
  289. {
  290. lessStats = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}" };
  291. if (type == 1)
  292. lessStats.lesson = hoursStats;
  293. else
  294. lessStats.open = hoursStats;
  295. }
  296. try
  297. {
  298. await table.SaveOrUpdate<LessStats>(lessStats);
  299. }
  300. catch {}
  301. }
  302. //保一年的统计 天
  303. var yearCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:All:{LessType}:{year}");
  304. if (yearCnt != null && yearCnt.Length > 0)
  305. {
  306. double[] daYear = new double[yearDays];
  307. foreach (var item in yearCnt)
  308. {
  309. double val = ((double)item.Score);
  310. int key = ((int)item.Element);
  311. daYear[key] = val;
  312. }
  313. string tempStats = string.Join(",", daYear);
  314. LessStats lessStats = table.Get<LessStats>("LessonYear", $"{year}");
  315. if (lessStats != null)
  316. {
  317. if (type == 1)
  318. lessStats.lesson = tempStats;
  319. else
  320. lessStats.open = tempStats;
  321. }
  322. else
  323. {
  324. lessStats = new() { PartitionKey = "LessonYear", RowKey = $"{year}" };
  325. if (type == 1)
  326. lessStats.lesson = tempStats;
  327. else
  328. lessStats.open = tempStats;
  329. }
  330. try
  331. {
  332. await table.SaveOrUpdate<LessStats>(lessStats);
  333. }
  334. catch { }
  335. }
  336. //保存学校课例数据
  337. if (!string.IsNullOrEmpty(schoolId))
  338. {
  339. await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", $"{hour}", num);//一天24小时课例数 有上传 base.josn 小时为单位
  340. await redisClient.SortedSetIncrementAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", $"{days}", num);//一年的课例数量 有上传 base.josn 小时为单位
  341. var scExpDay = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
  342. if (scExpDay == null)
  343. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{dateDay}", expireDay); //设置八天后到期
  344. var scExpYear = await _azureRedis.GetRedisClient(8).KeyTimeToLiveAsync($"BIStats:Less:{schoolId}:{LessType}:{year}");
  345. if (scExpYear == null)
  346. await _azureRedis.GetRedisClient(8).KeyExpireAsync($"BIStats:Less:{schoolId}:{LessType}:{year}", expireYear); //设置一年后到期
  347. //保存学校当天的统计 小时
  348. var dayScCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:{schoolId}:{LessType}:{dateDay}");
  349. if (dayScCnt != null && dayScCnt.Length > 0)
  350. {
  351. double[] daHourSc = new double[23];
  352. foreach (var item in dayScCnt)
  353. {
  354. double val = ((double)item.Score);
  355. int key = ((int)item.Element);
  356. daHourSc[key] = val;
  357. }
  358. string hoursStatsSc = string.Join(",", daHourSc);
  359. LessStats lessStatsSc = table.Get<LessStats>("LessonHour", $"{dateDay}-{schoolId}");
  360. if (lessStatsSc != null)
  361. {
  362. if (type == 1)
  363. lessStatsSc.lesson = hoursStatsSc;
  364. else
  365. lessStatsSc.open = hoursStatsSc;
  366. }
  367. else
  368. {
  369. lessStatsSc = new() { PartitionKey = "LessonHour", RowKey = $"{dateDay}-{schoolId}" };
  370. if (type == 1)
  371. lessStatsSc.lesson = hoursStatsSc;
  372. else
  373. lessStatsSc.open = hoursStatsSc;
  374. lessStatsSc.time = dateDay;
  375. lessStatsSc.school = schoolId;
  376. }
  377. try
  378. {
  379. await table.SaveOrUpdate<LessStats>(lessStatsSc);
  380. }
  381. catch { }
  382. }
  383. //保学校一年的统计 天
  384. var scYearCnt = redisClient.SortedSetRangeByScoreWithScores($"BIStats:Less:{schoolId}:{LessType}:{year}");
  385. if (scYearCnt != null && scYearCnt.Length > 0)
  386. {
  387. double[] daYearSc = new double[yearDays];
  388. foreach (var item in scYearCnt)
  389. {
  390. double val = ((double)item.Score);
  391. int key = ((int)item.Element);
  392. daYearSc[key] = val;
  393. }
  394. string tempStatsSc = string.Join(",", daYearSc);
  395. LessStats lessStatsSc = table.Get<LessStats>("LessonYear", $"{year}-{schoolId}");
  396. if (lessStatsSc != null)
  397. {
  398. if (type == 1)
  399. lessStatsSc.lesson = tempStatsSc;
  400. else
  401. lessStatsSc.open = tempStatsSc;
  402. }
  403. else
  404. {
  405. lessStatsSc = new() { PartitionKey = "LessonYear", RowKey = $"{year}-{schoolId}" };
  406. if (type == 1)
  407. lessStatsSc.lesson = tempStatsSc;
  408. else
  409. lessStatsSc.open = tempStatsSc;
  410. lessStatsSc.time = $"{year}";
  411. lessStatsSc.school = schoolId;
  412. }
  413. try
  414. {
  415. await table.SaveOrUpdate<LessStats>(lessStatsSc);
  416. }
  417. catch { }
  418. }
  419. }
  420. Monitor.Enter(unix); //锁对象 结束
  421. //slimlock.Release(); // 对可同时访问资源或资源池的线程数加以限制 结束
  422. }
  423. catch (Exception ex)
  424. {
  425. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-SetBITableStats 课例统计异常{unix}|{num}|{type}|{schoolId} ;错误信息:{ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  426. }
  427. }
  428. }
  429. }