LessonController.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. using Azure.Cosmos;
  2. using MathNet.Numerics.LinearAlgebra.Double;
  3. using Microsoft.AspNetCore.Http;
  4. using Microsoft.AspNetCore.Mvc;
  5. using StackExchange.Redis;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text.Json;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. using TEAMModelBI.Tool;
  13. using TEAMModelOS.SDK.Context.BI;
  14. using TEAMModelOS.SDK.DI;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelOS.SDK.Models;
  17. using TEAMModelOS.SDK.Models.Cosmos.BI.BISchool;
  18. using TEAMModelOS.SDK.Models.Cosmos.BI.BITable;
  19. using TEAMModelOS.SDK.Models.Service.BI;
  20. namespace TEAMModelBI.Controllers.BISchool
  21. {
  22. [Route("Lesson")]
  23. [ApiController]
  24. public class LessonController : ControllerBase
  25. {
  26. private readonly AzureCosmosFactory _azureCosmos;
  27. private readonly AzureStorageFactory _azureStorage;
  28. private readonly AzureRedisFactory _azureRedis;
  29. private readonly DingDing _dingDing;
  30. public LessonController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing)
  31. {
  32. _azureCosmos = azureCosmos;
  33. _azureStorage = azureStorage;
  34. _azureRedis = azureRedis;
  35. _dingDing = dingDing;
  36. }
  37. /// <summary>
  38. /// 获取课例统计数据
  39. /// </summary>
  40. /// <param name="jsonElement"></param>
  41. /// <returns></returns>
  42. [HttpPost("get-less")]
  43. public async Task<IActionResult> GetLess(JsonElement jsonElement)
  44. {
  45. if (!jsonElement.TryGetProperty("school", out JsonElement school)) return BadRequest();
  46. var table = _azureStorage.GetCloudTableClient().GetTableReference("BIStats");
  47. var tableSql = $" PartitionKey eq 'LessonHour' and school eq '{school}'";
  48. //lambda 表达式排序
  49. var lessStats = await table.QueryWhereString<LessStats>(tableSql.ToString());
  50. DenseMatrix openDense = null;
  51. DenseMatrix lessDense = null;
  52. List<List<double>> tempOpens = new();
  53. List<List<double>> tempLesss = new();
  54. foreach (var item in lessStats)
  55. {
  56. if (item.open != null)
  57. {
  58. List<double> opens = item.open.Split(',').ToList().ConvertAll(c => Convert.ToDouble(c));
  59. tempOpens.Add(opens);
  60. }
  61. if(item.lesson != null)
  62. {
  63. List<double> lesss = item.lesson.Split(',').ToList().ConvertAll(c => Convert.ToDouble(c));
  64. tempLesss.Add(lesss);
  65. }
  66. }
  67. openDense = DenseMatrix.OfColumns(tempOpens);
  68. lessDense = DenseMatrix.OfColumns(tempLesss);
  69. return Ok(new { state = 200 , openDense ,lessDense});
  70. }
  71. /// <summary>
  72. /// 测试 通过时间戳保存统计类型接口
  73. /// </summary>
  74. /// <param name="jsonElement"></param>
  75. /// <returns></returns>
  76. [HttpPost("set-bilessonstats")]
  77. public async Task<IActionResult> SetBILeesonStats(JsonElement jsonElement)
  78. {
  79. if (!jsonElement.TryGetProperty("unix", out JsonElement unix)) return BadRequest();
  80. if (!jsonElement.TryGetProperty("num", out JsonElement num)) return BadRequest();
  81. jsonElement.TryGetProperty("schoolId", out JsonElement schoolId);
  82. jsonElement.TryGetProperty("type", out JsonElement type);
  83. jsonElement.TryGetProperty("unixs", out JsonElement unixs);
  84. var cosmosClient = _azureCosmos.GetCosmosClient();
  85. DateTime expireYear = DateTime.UtcNow.AddYears(1); //一个月到期
  86. var UUID = Guid.NewGuid().ToString();
  87. List<long> unixsT = unixs.ToObject<List<long>>();
  88. RedisValue token = Environment.MachineName;
  89. foreach (var item in unixsT)
  90. {
  91. //await SetBILeesonStats(cosmosClient, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
  92. //await BILeeson.SetCosmosDBStats(cosmosClient, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
  93. await BILeesonService.SetTableStats(_azureStorage, _azureRedis, _dingDing, item, num.GetInt32(), type: type.GetInt32(), "school", schoolId: schoolId.GetString());
  94. }
  95. //await BILeeson.SetBILeesonStats(cosmosClient, _azureRedis, unix.GetInt64(), num.GetInt32(), type: type.GetInt32(), schoolId: schoolId.GetString());
  96. //await SetBILeesonStats(cosmosClient, unix.GetInt64(), num.GetInt32(), type: -1, schoolId: schoolId.GetString());
  97. return Ok(new { state = 200 });
  98. }
  99. /// <summary>
  100. /// 通过时间戳保存统计类型
  101. /// </summary>
  102. /// <param name="cosmosClient">连接字符串</param>
  103. /// <param name="unix">时间戳</param>
  104. /// <param name="num">加减数量,可为负数</param>
  105. /// <param name="type">课例类型,0开课 1 课例</param>
  106. /// <param name="schoolId">学校ID,可不传</param>
  107. /// <returns></returns>
  108. public static async Task SetBILeesonStats(CosmosClient cosmosClient, long unix, int num, int type = 0, string schoolId = null)
  109. {
  110. //Monitor.TryEnter(unix); //锁对象
  111. SemaphoreSlim slimlock = new(1, 1); //对可同时访问资源或资源池的线程数加以限制
  112. await slimlock.WaitAsync();
  113. DateTimeOffset dateTime = DateTimeOffset.FromUnixTimeMilliseconds(unix);
  114. int year, month, day, hour, days;
  115. year = dateTime.Year;
  116. month = dateTime.Month;
  117. day = dateTime.Day;
  118. hour = dateTime.Hour;
  119. string hourId = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
  120. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  121. days = dateTime.DayOfYear;
  122. double[] daDays = new double[yearDays];
  123. daDays[days] = num;
  124. List<double> yDays = new(daDays);
  125. double[] daHours = new double[23];
  126. daHours[hour] = num;
  127. List<double> yHours = new(daHours);
  128. var openRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey("LessonYear"));
  129. if (openRes.Status == 200)
  130. {
  131. using var json = await JsonDocument.ParseAsync(openRes.ContentStream);
  132. LessonStats lessonYear = json.ToObject<LessonStats>();
  133. if (type == 0)
  134. {
  135. if (lessonYear.open.Count == 0)
  136. lessonYear.open = yDays;
  137. else lessonYear.open[days] = lessonYear.open[days] + num;
  138. }
  139. else if (type == 1)
  140. {
  141. if (lessonYear.lesson.Count == 0)
  142. lessonYear.lesson = yDays;
  143. else lessonYear.lesson[days] = lessonYear.lesson[days] + num;
  144. }
  145. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
  146. }
  147. else
  148. {
  149. LessonStats lessonYear = new();
  150. lessonYear.id = $"{year}";
  151. lessonYear.code = "LessonYear";
  152. lessonYear.pk = "LessonYear";
  153. if (type == 0)
  154. lessonYear.open = yDays;
  155. else if (type == 1)
  156. lessonYear.lesson = yDays;
  157. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYear, new PartitionKey($"{lessonYear.code}"));
  158. }
  159. var lessRes = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{hourId}", new PartitionKey("LessonHour"));
  160. if (lessRes.Status == 200)
  161. {
  162. using var json = await JsonDocument.ParseAsync(lessRes.ContentStream);
  163. LessonStats lessonHour = json.ToObject<LessonStats>();
  164. if (type == 0)
  165. {
  166. if (lessonHour.open.Count == 0)
  167. lessonHour.open = yHours;
  168. else lessonHour.open[hour] = lessonHour.open[hour] + num;
  169. }
  170. else if (type == 1)
  171. {
  172. if (lessonHour.lesson.Count == 0)
  173. lessonHour.lesson = yHours;
  174. else lessonHour.lesson[hour] = lessonHour.lesson[hour] + num;
  175. }
  176. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHour, lessonHour.id, new PartitionKey($"LessonHour"));
  177. }
  178. else
  179. {
  180. LessonStats lessonHour = new();
  181. lessonHour.id = $"{hourId}";
  182. lessonHour.code = "LessonHour";
  183. lessonHour.pk = "LessonHour";
  184. if (type == 0)
  185. lessonHour.open = yDays;
  186. else if (type == 1)
  187. lessonHour.lesson = yDays;
  188. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonHour, new PartitionKey("LessonHour"));
  189. }
  190. //学校统计
  191. if (!string.IsNullOrEmpty(schoolId))
  192. {
  193. var openResSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{year}", new PartitionKey($"LessonYear-{schoolId}"));
  194. if (openResSc.Status == 200)
  195. {
  196. using var jsonSc = await JsonDocument.ParseAsync(openResSc.ContentStream);
  197. LessonStats lessonYearSc = jsonSc.ToObject<LessonStats>();
  198. if (type == 0)
  199. if (lessonYearSc.open.Count == 0)
  200. lessonYearSc.open = yDays;
  201. else lessonYearSc.open[days] = lessonYearSc.open[days] + num;
  202. else if (type == 1)
  203. {
  204. if (lessonYearSc.lesson.Count == 0)
  205. lessonYearSc.lesson = yDays;
  206. else lessonYearSc.lesson[days] = lessonYearSc.lesson[days] + num;
  207. }
  208. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYearSc, lessonYearSc.id, new PartitionKey($"LessonYear-{schoolId}"));
  209. }
  210. else
  211. {
  212. LessonStats lessonYearSc = new();
  213. lessonYearSc.id = $"{year}";
  214. lessonYearSc.code = $"LessonYear-{schoolId}";
  215. lessonYearSc.pk = "LessonYear";
  216. if (type == 0)
  217. lessonYearSc.open = yDays;
  218. else if (type == 1)
  219. lessonYearSc.lesson = yDays;
  220. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYearSc, new PartitionKey($"LessonYear-{schoolId}"));
  221. }
  222. var lessResSc = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync($"{hourId}", new PartitionKey($"LessonHour-{schoolId}"));
  223. if (lessResSc.Status == 200)
  224. {
  225. using var jsonSc = await JsonDocument.ParseAsync(lessResSc.ContentStream);
  226. LessonStats lessonHourSc = jsonSc.ToObject<LessonStats>();
  227. if (type == 0)
  228. if (lessonHourSc.open.Count == 0)
  229. lessonHourSc.open = yDays;
  230. else lessonHourSc.open[days] = lessonHourSc.open[days] + num;
  231. else if (type == 1)
  232. {
  233. if (lessonHourSc.lesson.Count == 0)
  234. lessonHourSc.lesson = yHours;
  235. else lessonHourSc.lesson[hour] = lessonHourSc.lesson[hour] + num;
  236. }
  237. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHourSc, lessonHourSc.id, new PartitionKey($"LessonHour-{schoolId}"));
  238. }
  239. else
  240. {
  241. LessonStats lessonYearSc = new();
  242. lessonYearSc.id = $"{hourId}";
  243. lessonYearSc.code = $"LessonHour-{schoolId}";
  244. lessonYearSc.pk = "LessonHour";
  245. if (type == 0)
  246. lessonYearSc.open = yHours;
  247. else if (type == 1)
  248. lessonYearSc.lesson = yHours;
  249. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(lessonYearSc, new PartitionKey($"LessonHour-{schoolId}"));
  250. }
  251. }
  252. slimlock.Release();
  253. //Monitor.Enter(unix);
  254. }
  255. /// <summary>
  256. /// 历史记录读取,并统计存储
  257. /// </summary>
  258. /// <param name="jsonElement"></param>
  259. /// <returns></returns>
  260. [HttpPost("get-alllesson")]
  261. public async Task<IActionResult> GetAllLessonRecords(JsonElement jsonElement)
  262. {
  263. jsonElement.TryGetProperty("site", out JsonElement site);
  264. var cosmosClient = _azureCosmos.GetCosmosClient();
  265. if ($"{site}".Equals(BIConst.Global))
  266. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  267. List<LessonRecord> allLesson = new();
  268. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>(queryText: $"select value(c) from c where c.pk='LessonRecord'", requestOptions: new QueryRequestOptions() { }))
  269. {
  270. allLesson.Add(item);
  271. }
  272. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<LessonRecord>(queryText: $"select value(c) from c where c.pk='LessonRecord'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("LessonRecord") }))
  273. {
  274. allLesson.Add(item);
  275. }
  276. (List<LessonStats> lessYears, List<LessonStats> lessHours) = await AllLessonStats(allLesson, _azureRedis);
  277. foreach (var itemY in lessYears)
  278. {
  279. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(itemY.id, new PartitionKey($"{itemY.code}"));
  280. if (response.Status == 200)
  281. {
  282. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  283. LessonStats lessonYear = json.ToObject<LessonStats>();
  284. lessonYear.open = itemY.open;
  285. lessonYear.lesson = itemY.lesson;
  286. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonYear, lessonYear.id, new PartitionKey($"{lessonYear.code}"));
  287. }
  288. else
  289. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(itemY, new PartitionKey($"{itemY.code}"));
  290. }
  291. foreach (var itemH in lessHours)
  292. {
  293. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(itemH.id, new PartitionKey($"{itemH.code}"));
  294. if (response.Status == 200)
  295. {
  296. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  297. LessonStats lessonHour = json.ToObject<LessonStats>();
  298. lessonHour.open = itemH.open;
  299. lessonHour.lesson = itemH.lesson;
  300. await cosmosClient.GetContainer("TEAMModelOS", "School").ReplaceItemAsync<LessonStats>(lessonHour, lessonHour.id, new PartitionKey($"{lessonHour.code}"));
  301. }
  302. else
  303. await cosmosClient.GetContainer("TEAMModelOS", "School").CreateItemAsync<LessonStats>(itemH, new PartitionKey($"{itemH.code}"));
  304. }
  305. return Ok(new { state = 200, ycnt = lessYears.Count, hcnt = lessHours.Count, lessYears, lessHours });
  306. }
  307. #region 课例历史记录统计
  308. /// <summary>
  309. /// 统计所有课例
  310. /// </summary>
  311. /// <param name="lessRecs"></param>
  312. /// <returns></returns>
  313. public static async Task<(List<LessonStats> lessonYears, List<LessonStats> lessonHours)> AllLessonStats(List<LessonRecord> lessRecs, AzureRedisFactory azureRedis)
  314. {
  315. //统计所有
  316. List<LessCnt> allLess = lessRecs.Select(item => new LessCnt { hour = TimeHelper.GetDateTime(item.startTime).ToString("yyyyMMddHH"), cat = item.id, upload = item.upload, schoolId = item.school }).ToList();
  317. var openL = allLess.FindAll(f => f.upload == 0).GroupBy(g => g.hour).Select(s => new AllLess { key = s.Key, cnt = s.Count() }).ToList();
  318. var lessL = allLess.FindAll(s => s.upload == 1).GroupBy(g => g.hour).Select(s => new AllLess { key = s.Key, cnt = s.Count() }).ToList();
  319. List<LessonStats> lessYears = new();
  320. List<LessonStats> lessHours = new();
  321. //统计开课记录
  322. openL.ForEach(x =>
  323. {
  324. var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
  325. string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
  326. LessonStats finHour = lessHours.Find(f => f.id.Equals(id));
  327. if (finHour != null)
  328. {
  329. if (finHour.open.Count == 0)
  330. {
  331. double[] da = new double[23];
  332. da[hour] = x.cnt;
  333. List<double> tempDays = new(da);
  334. finHour.open = tempDays;
  335. }
  336. else finHour.open[hour] = x.cnt;
  337. azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"BILesson:All:Open:{finHour.id}", $"{hour}", x.cnt);//一天24小时课例数 有上传 base.josn 小时为单位
  338. }
  339. else
  340. {
  341. //小时统计
  342. LessonStats lessonHour = new();
  343. lessonHour.id = id;
  344. lessonHour.code = "LessonHour";
  345. lessonHour.pk = "LessonHour";
  346. double[] daHours = new double[23];
  347. daHours[hour] = x.cnt;
  348. List<double> hourDays = new(daHours);
  349. lessonHour.open = hourDays;
  350. //lessonHour.ttl = 24 * 60 * 60 * 8; //设置过期时间 8天后自动删除
  351. lessHours.Add(lessonHour);
  352. }
  353. LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}"));
  354. if (findLess != null)
  355. {
  356. if (findLess.open.Count == 0)
  357. {
  358. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  359. double[] da = new double[yearDays];
  360. da[days] = x.cnt;
  361. List<double> tempDays = new(da);
  362. findLess.open = tempDays;
  363. }
  364. else findLess.open[days] = x.cnt;
  365. }
  366. else
  367. {
  368. LessonStats lessonYear = new();
  369. lessonYear.id = $"{year}";
  370. lessonYear.code = "LessonYear";
  371. lessonYear.pk = "LessonYear";
  372. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  373. double[] daDays = new double[yearDays];
  374. daDays[days] = x.cnt;
  375. List<double> yDays = new(daDays);
  376. lessonYear.open = yDays;
  377. lessYears.Add(lessonYear);
  378. }
  379. });
  380. //统计课例
  381. lessL.ForEach(x =>
  382. {
  383. var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
  384. string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
  385. LessonStats finHour = lessHours.Find(f => f.id.Equals(id));
  386. if (finHour != null)
  387. {
  388. if (finHour.lesson.Count == 0)
  389. {
  390. double[] da = new double[23];
  391. da[hour] = x.cnt;
  392. List<double> tempDays = new(da);
  393. finHour.lesson = tempDays;
  394. }
  395. else finHour.lesson[hour] = x.cnt;
  396. }
  397. else
  398. {
  399. //小时统计
  400. LessonStats lessonHour = new();
  401. lessonHour.id = id;
  402. lessonHour.code = "LessonHour";
  403. lessonHour.pk = "LessonHour";
  404. double[] daHours = new double[23];
  405. daHours[hour] = x.cnt;
  406. List<double> hourDays = new(daHours);
  407. lessonHour.lesson = hourDays;
  408. lessHours.Add(lessonHour);
  409. }
  410. LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}"));
  411. if (findLess != null)
  412. {
  413. if (findLess.lesson.Count == 0)
  414. {
  415. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  416. double[] da = new double[yearDays];
  417. da[days] = x.cnt;
  418. List<double> tempDays = new(da);
  419. findLess.lesson = tempDays;
  420. }
  421. else findLess.lesson[days] = x.cnt;
  422. }
  423. else
  424. {
  425. LessonStats lessonYear = new();
  426. lessonYear.id = $"{year}";
  427. lessonYear.code = "LessonYear";
  428. lessonYear.pk = "LessonYear";
  429. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  430. double[] da = new double[yearDays];
  431. da[days] = x.cnt;
  432. List<double> tempDays = new(da);
  433. lessonYear.lesson = tempDays;
  434. lessYears.Add(lessonYear);
  435. }
  436. });
  437. //统计学校课例
  438. var scLess = lessRecs.Select(item => new LessCnt { hour = TimeHelper.GetDateTime(item.startTime).ToString("yyyyMMddHH"), cat = item.id, upload = item.upload, schoolId = item.school }).GroupBy(g => g.schoolId).Select(s => new ScLess { key = s.Key, lessCnt = s.ToList() }).ToList();
  439. foreach (var itemLess in scLess)
  440. {
  441. var openSCL = itemLess.lessCnt.FindAll(f => f.upload == 0).GroupBy(g => g.hour).Select(s => new ScLesson { key = s.Key, schoolId = itemLess.key, cnt = s.Count() }).ToList();
  442. var lessSCL = itemLess.lessCnt.FindAll(f => f.upload == 1).GroupBy(g => g.hour).Select(s => new ScLesson { key = s.Key, schoolId = itemLess.key, cnt = s.Count() }).ToList();
  443. openSCL.ForEach(f =>
  444. {
  445. if (!string.IsNullOrEmpty(f.schoolId))
  446. {
  447. var (year, month, day, days, hour) = TimeHelper.GetDateTime(f.key);
  448. string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
  449. string codeHour = $"LessonHour-{f.schoolId}";
  450. LessonStats finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
  451. if (finHour != null)
  452. {
  453. if (finHour.open.Count == 0)
  454. {
  455. double[] da = new double[23];
  456. da[hour] = f.cnt;
  457. List<double> tempDays = new(da);
  458. finHour.open = tempDays;
  459. }
  460. else finHour.open[hour] = f.cnt;
  461. }
  462. else
  463. {
  464. //小时统计
  465. LessonStats lessonHour = new();
  466. lessonHour.id = id;
  467. lessonHour.code = codeHour;
  468. lessonHour.pk = "LessonHour";
  469. double[] daHours = new double[23];
  470. daHours[hour] = f.cnt;
  471. List<double> hourDays = new(daHours);
  472. lessonHour.open = hourDays;
  473. //lessonHour.ttl = 24 * 60 * 60 * 8; //设置过期时间
  474. lessHours.Add(lessonHour);
  475. }
  476. string codeYear = $"LessonYear-{f.schoolId}";
  477. LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
  478. if (findLess != null)
  479. {
  480. if (findLess.open.Count == 0)
  481. {
  482. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  483. double[] da = new double[yearDays];
  484. da[days] = f.cnt;
  485. List<double> tempDays = new(da);
  486. findLess.open = tempDays;
  487. }
  488. else findLess.open[days] = f.cnt;
  489. }
  490. else
  491. {
  492. LessonStats lessonYear = new();
  493. lessonYear.id = $"{year}";
  494. lessonYear.code = codeYear;
  495. lessonYear.code = "LessonYear";
  496. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  497. double[] daDays = new double[yearDays];
  498. daDays[days] = f.cnt;
  499. List<double> yDays = new(daDays);
  500. lessonYear.open = yDays;
  501. lessYears.Add(lessonYear);
  502. }
  503. }
  504. });
  505. lessSCL.ForEach(x =>
  506. {
  507. if (!string.IsNullOrEmpty(x.schoolId))
  508. {
  509. if (!string.IsNullOrEmpty(x.schoolId))
  510. {
  511. var (year, month, day, days, hour) = TimeHelper.GetDateTime(x.key);
  512. string id = $"{year}{month.ToString().PadLeft(2, '0')}{day.ToString().PadLeft(2, '0')}";
  513. string codeHour = $"LessonHour-{x.schoolId}";
  514. LessonStats finHour = lessHours.Find(f => f.id.Equals(id) && f.code.Equals(codeHour));
  515. if (finHour != null)
  516. {
  517. if (finHour.lesson.Count == 0)
  518. {
  519. double[] da = new double[23];
  520. da[hour] = x.cnt;
  521. List<double> tempDays = new(da);
  522. finHour.lesson = tempDays;
  523. }
  524. else finHour.lesson[hour] = x.cnt;
  525. }
  526. else
  527. {
  528. //小时统计
  529. LessonStats lessonHour = new();
  530. lessonHour.id = id;
  531. lessonHour.code = codeHour;
  532. lessonHour.pk = "LessonHour";
  533. double[] daHours = new double[23];
  534. daHours[hour] = x.cnt;
  535. List<double> hourDays = new(daHours);
  536. lessonHour.lesson = hourDays;
  537. lessHours.Add(lessonHour);
  538. }
  539. string codeYear = $"LessonYear-{x.schoolId}";
  540. LessonStats findLess = lessYears.Find(f => f.id.Equals($"{year}") && f.code.Equals(codeYear));
  541. if (findLess != null)
  542. {
  543. if (findLess.lesson.Count == 0)
  544. {
  545. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  546. double[] da = new double[yearDays];
  547. da[days] = x.cnt;
  548. List<double> tempDays = new(da);
  549. findLess.lesson = tempDays;
  550. }
  551. else findLess.lesson[days] = x.cnt;
  552. }
  553. else
  554. {
  555. LessonStats lessonYear = new();
  556. lessonYear.id = $"{year}";
  557. lessonYear.code = codeYear;
  558. lessonYear.pk = "LessonYear";
  559. var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  560. double[] da = new double[yearDays];
  561. da[days] = x.cnt;
  562. List<double> tempDays = new(da);
  563. lessonYear.lesson = tempDays;
  564. lessYears.Add(lessonYear);
  565. }
  566. }
  567. }
  568. });
  569. };
  570. //openL.ForEach(x => {
  571. // var (year, days, hour) = TimeHelper.GetDateTime(x.key);
  572. // LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}"));
  573. // if (findLess != null)
  574. // {
  575. // if (findLess.openYear.Count == 0)
  576. // {
  577. // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  578. // double[] da = new double[yearDays];
  579. // da[days] = x.cnt;
  580. // List<double> tempDays = new(da);
  581. // findLess.openYear = tempDays;
  582. // }
  583. // else findLess.openYear[days] = x.cnt;
  584. // }
  585. // else
  586. // {
  587. // LessonYear lessonYear = new();
  588. // lessonYear.id = $"{year}";
  589. // lessonYear.code = "LessonYear";
  590. // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  591. // double[] da = new double[yearDays];
  592. // da[days] = x.cnt;
  593. // List<double> tempDays = new(da);
  594. // lessonYear.openYear = tempDays;
  595. // lessYears.Add(lessonYear);
  596. // }
  597. //});
  598. //lessL.ForEach(x => {
  599. // var (year, days, hour) = TimeHelper.GetDateTime(x.key);
  600. // LessonYear findLess = lessYears.Find(f => f.id.Equals($"{year}"));
  601. // if (findLess != null)
  602. // {
  603. // if (findLess.lessonYear.Count == 0)
  604. // {
  605. // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  606. // double[] da = new double[yearDays];
  607. // da[days] = x.cnt;
  608. // List<double> tempDays = new(da);
  609. // findLess.lessonYear = tempDays;
  610. // }
  611. // else findLess.lessonYear[days] = x.cnt;
  612. // }
  613. // else
  614. // {
  615. // LessonYear lessonYear = new();
  616. // lessonYear.id = $"{year}";
  617. // lessonYear.code = "LessonYear";
  618. // var yearDays = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? 366 : 365;
  619. // double[] da = new double[yearDays];
  620. // da[days] = x.cnt;
  621. // List<double> tempDays = new(da);
  622. // lessonYear.lessonYear = tempDays;
  623. // lessYears.Add(lessonYear);
  624. // }
  625. //});
  626. return (lessYears, lessHours);
  627. }
  628. /// <summary>
  629. /// 统计学校课例
  630. /// </summary>
  631. public record ScLesson
  632. {
  633. public string key { get; set; }
  634. public string schoolId { get; set; }
  635. public int cnt { get; set; }
  636. }
  637. /// <summary>
  638. /// 统计学校课例详细信息
  639. /// </summary>
  640. public record ScLess
  641. {
  642. public string key { get; set; }
  643. public List<LessCnt> lessCnt { get; set; }
  644. }
  645. /// <summary>
  646. /// 统计所有课例
  647. /// </summary>
  648. public record AllLess
  649. {
  650. public string key { get; set; }
  651. public int cnt { get; set; }
  652. }
  653. /// <summary>
  654. /// 统计所有课例详细
  655. /// </summary>
  656. public record LessCnt
  657. {
  658. public string hour { get; set; }
  659. public string schoolId { get; set; }
  660. public string cat { get; set; }
  661. public int upload { get; set; }
  662. }
  663. #endregion
  664. }
  665. }