OnLineController.cs 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574
  1. using Azure.Cosmos;
  2. using Azure.Storage.Blobs;
  3. using Azure.Storage.Blobs.Models;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.AspNetCore.Mvc;
  6. using StackExchange.Redis;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.IO;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Text.Json;
  13. using System.Threading.Tasks;
  14. using TEAMModelBI.Tool;
  15. using TEAMModelOS.SDK.Context.BI;
  16. using TEAMModelOS.SDK.Context.Constant;
  17. using TEAMModelOS.SDK.DI;
  18. using TEAMModelOS.SDK.Extension;
  19. using TEAMModelOS.SDK.Models;
  20. using TEAMModelOS.SDK.Models.Cosmos.BI;
  21. using TEAMModelOS.SDK.Models.Table;
  22. namespace TEAMModelBI.Controllers.BIHome
  23. {
  24. [Route("online")]
  25. [ApiController]
  26. public class OnLineController : ControllerBase
  27. {
  28. private readonly AzureCosmosFactory _azureCosmos;
  29. private readonly AzureStorageFactory _azureStorage;
  30. private readonly AzureRedisFactory _azureRedis;
  31. public OnLineController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis)
  32. {
  33. _azureCosmos = azureCosmos;
  34. _azureStorage = azureStorage;
  35. _azureRedis = azureRedis;
  36. }
  37. /// <summary>
  38. /// 总数统计
  39. /// </summary>
  40. /// <returns></returns>
  41. [HttpPost("get-count")]
  42. public async Task<IActionResult> GetCount(JsonElement jsonElement)
  43. {
  44. var cosmosClient = _azureCosmos.GetCosmosClient();
  45. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
  46. var blobClient = _azureStorage.GetBlobContainerClient($"0-public");
  47. jsonElement.TryGetProperty("site", out JsonElement site);
  48. if ($"{site}".Equals(BIConst.Global))
  49. {
  50. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  51. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("IESLogin");
  52. blobClient = _azureStorage.GetBlobContainerClient($"0-public", BIConst.Global);
  53. }
  54. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  55. string cDay = dateTime.ToString("yyyyMMdd");
  56. var (daySt, dayEt) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间 13位
  57. var (daySf, dayEf) = TimeHelper.GetStartOrEnd(dateTime, dateLenth: false); //今天开始时间 10位
  58. var (lastDayS, lastdayE) = TimeHelper.GetStartOrEnd(dateTime.AddDays(-1)); //昨天开始时间
  59. var near7S = dateTime.AddDays(-7).ToUnixTimeMilliseconds(); //前七天的开始时间
  60. var near7E = dateTime.ToUnixTimeMilliseconds(); //当前结束时间
  61. long hour1 = dateTime.AddHours(-1).ToUnixTimeMilliseconds(); //一小时前时间戳
  62. int areaCnt = 0; //学区总数
  63. int scCnt = 0; //学校总数
  64. int tchCnt = 0; //教师总数
  65. int stuCnt = 0; //学生总数
  66. int apiCnt = 0; //当天接口访问总量
  67. int onStuCnt = 0; //学生在线人数
  68. int onTchCnt = 0; //教师在线人数
  69. int todayScCnt = 0; //今日新增学校数
  70. int todayTchCnt = 0; //今日新增教师
  71. int todayStuCnt = 0; //今日新增学生数
  72. string currentSql = "select value(count(c.id)) from c";
  73. areaCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Normal", currentSql, "Base-Area");
  74. scCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", currentSql, "Base");
  75. tchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", currentSql, "Base");
  76. stuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", "select value(count(c.id)) from c where c.pk='Base'");
  77. string addSql = $"select value(count(c.id)) from c where c.createTime >={daySt} and c.createTime <= {dayEt}";
  78. todayScCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", addSql, "Base");
  79. todayTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", addSql, "Base");
  80. todayStuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", addSql, "Base");
  81. string onStuSql = $"select value(count(c.id)) from c join t in c.loginInfos where array_length(c.loginInfos) > 0 and t.expire > {hour1} and c.pk='Base'";
  82. onTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", onStuSql);
  83. onStuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", onStuSql);
  84. //接口访问量
  85. List<RecCnt> recCnts = new();
  86. await foreach (BlobItem blobItem in blobClient.GetBlobsAsync(BlobTraits.None, BlobStates.None, $"visitCnt/{cDay}"))
  87. {
  88. BlobClient tempBlobClient = blobClient.GetBlobClient(blobItem.Name);
  89. if (await tempBlobClient.ExistsAsync())
  90. {
  91. using (var meomoryStream = new MemoryStream())
  92. {
  93. var response = blobClient.GetBlobClient($"{blobItem.Name}").DownloadTo(meomoryStream);
  94. RecCnt recCnt = Encoding.UTF8.GetString(meomoryStream.ToArray()).ToString().ToObject<RecCnt>();
  95. recCnts.Add(recCnt);
  96. }
  97. }
  98. }
  99. apiCnt = recCnts.Select(x => x.apiCnt.Select(s => s.count).Sum()).Sum();
  100. //List<RecOnLine> recStuOnLines = new();
  101. //await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Student").GetItemQueryIterator<RecOnLine>(queryText: "select c.id,c.name,c.code,c.loginInfos from c where c.pk='Base' and array_length(c.loginInfos) > 0 ", requestOptions:new QueryRequestOptions() { }))
  102. //{
  103. // recStuOnLines.Add(item);
  104. //}
  105. //List<RecOnLine> recTecOnLines = new();
  106. //await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecOnLine>(queryText: "select c.id,c.name,c.code,c.loginInfos from c where array_length(c.loginInfos) > 0 ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  107. //{
  108. // recTecOnLines.Add(item);
  109. //}
  110. ////onStuCnt = (from rs in recStuOnLines from l in rs.loginInfos where l.expire >= hour1 select rs).ToList().Count(); //linq查询 学生在线人数
  111. //onStuCnt = recStuOnLines.Select(rss => new RecOnLine { id = rss.id,code=rss.code, name =rss.name,loginInfos = new List<Teacher.LoginInfo> { rss.loginInfos.Find(f => f.expire >= hour1) } }).Where(w => w.loginInfos.FirstOrDefault() != null).ToList().Count(); //lambda 表达式查询 教师查询人数
  112. ////onTchCnt = (from rs in recTecOnLines from l in rs.loginInfos where l.expire >= hour1 select rs).ToList().Count(); //linq查询 教师查询人数
  113. //onTchCnt = recTecOnLines.Select(rss => new RecOnLine { id = rss.id,code=rss.code, name =rss.name,loginInfos = new List<Teacher.LoginInfo> { rss.loginInfos.Find(f => f.expire >= hour1) } }).Where(w => w.loginInfos.FirstOrDefault() != null).ToList().Count(); //lambda 表达式查询 教师查询人数
  114. return Ok(new { state = 200, areaCnt, scCnt, tchCnt, stuCnt, todayScCnt, todayTchCnt, todayStuCnt, onStuCnt, onTchCnt, apiCnt });
  115. }
  116. /// <summary>
  117. /// 在线人数趋势图
  118. /// </summary>
  119. /// <returns></returns>
  120. [HttpPost("get-trend")]
  121. public async Task<IActionResult> GetTrend(JsonElement jsonElement)
  122. {
  123. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
  124. var redisClinet = _azureRedis.GetRedisClient(8);
  125. jsonElement.TryGetProperty("site", out JsonElement site);
  126. if ($"{site}".Equals(BIConst.Global))
  127. {
  128. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("IESLogin");
  129. redisClinet = _azureRedis.GetRedisClient(dbnum: 8, name: BIConst.Global);
  130. }
  131. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  132. var (daySt, dayEt) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间 13位
  133. var (strDaySt, strDayEt) = TimeHelper.GetUnixToDate(daySt, dayEt, "yyyyMMddHH");
  134. var dateDay = dateTime.ToString("yyyyMMdd"); //获取当天的日期
  135. //daySt.ToString("yyyyMMddHH");
  136. Dictionary<long, int> allDays = new(); //所有在线人数
  137. Dictionary<long, int> tchDays = new(); //教师在线人数
  138. Dictionary<long, int> stuDays = new(); //学生在线人数
  139. Dictionary<long, int> tmdDays = new(); //醍摩豆账户学生
  140. SortedSetEntry[] tchDay = redisClinet.SortedSetRangeByScoreWithScores($"Login:IES:teacher:{dateDay}");
  141. if (tchDay.Length > 0)
  142. {
  143. foreach (var item in tchDay)
  144. {
  145. int val = ((int)item.Score);
  146. int key = ((int)item.Element);
  147. //var utcTo = new DateTimeOffset(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, key, 0, 0)).Hour;
  148. //var hour = int.Parse(DateTime.SpecifyKind(Convert.ToDateTime($"{dateTime.Year}/{dateTime.Month}/{ dateTime.Day} {key}:00:00"), DateTimeKind.Utc).ToLocalTime().ToString("HH"));
  149. tchDays.Add(key, val);
  150. if (allDays.ContainsKey(key))
  151. allDays[key] = (allDays[key] + val);
  152. else
  153. allDays.Add(key, val);
  154. }
  155. }
  156. else
  157. {
  158. string tableSqlTch = $"PartitionKey eq 'HourLogin' and RowKey ge '{strDaySt}' and RowKey le '{strDayEt}'";
  159. List<HourLogin> hourLoginsTch = await table.QueryWhereString<HourLogin>(tableSqlTch);
  160. if (hourLoginsTch.Count > 0)
  161. {
  162. foreach (var item in hourLoginsTch)
  163. {
  164. await redisClinet.SortedSetIncrementAsync($"Login:IES:teacher:{dateDay}", $"{item.Hour}", item.Teacher);//存一天24小时
  165. //var utcTo = new DateTimeOffset(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, item.Hour, 0, 0)).Hour;
  166. //var hour = int.Parse(DateTime.SpecifyKind(Convert.ToDateTime($"{dateTime.Year}/{dateTime.Month}/{ dateTime.Day} {item.Hour}:00:00"), DateTimeKind.Utc).ToLocalTime().ToString("HH"));
  167. tchDays.Add(item.Hour, item.Teacher);
  168. if (allDays.ContainsKey(item.Hour))
  169. allDays[item.Hour] = (allDays[item.Hour] + item.Teacher);
  170. else
  171. allDays.Add(item.Hour, item.Teacher);
  172. }
  173. }
  174. }
  175. SortedSetEntry[] stuDay = redisClinet.SortedSetRangeByScoreWithScores($"Login:IES:student:{dateDay}");
  176. if (stuDay.Length > 0)
  177. {
  178. foreach (var item in stuDay)
  179. {
  180. int val = (int)item.Score;
  181. int key = (int)item.Element;
  182. //var utcTo = new DateTimeOffset(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, key, 0, 0)).Hour;
  183. //var hour = int.Parse(DateTime.SpecifyKind(Convert.ToDateTime($"{dateTime.Year}/{dateTime.Month}/{ dateTime.Day} {key}:00:00"), DateTimeKind.Utc).ToLocalTime().ToString("HH"));
  184. stuDays.Add(key, val);
  185. if (allDays.ContainsKey(key))
  186. allDays[key] = (allDays[key] + val);
  187. else
  188. allDays.Add(key, val);
  189. }
  190. }
  191. else
  192. {
  193. string tableSqlStu = $"PartitionKey eq 'HourLogin' and RowKey ge '{strDaySt}' and RowKey le '{strDayEt}'";
  194. List<HourLogin> hourLoginsStu = await table.QueryWhereString<HourLogin>(tableSqlStu);
  195. //var hourStuCnt = hourLoginsStu.GroupBy(x => x.Hour).Select(k => new { key = int.Parse(k.Key.ToString().Substring(8, 2)), value = k.Count() }).ToList();
  196. if (hourLoginsStu.Count > 0)
  197. {
  198. foreach (var item in hourLoginsStu)
  199. {
  200. await redisClinet.SortedSetIncrementAsync($"Login:IES:student:{dateDay}", $"{item.Hour}", item.Student);//存一天24小时
  201. //var utcTo = new DateTimeOffset(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, item.Hour, 0, 0)).Hour;
  202. //var hour = int.Parse(DateTime.SpecifyKind(Convert.ToDateTime($"{dateTime.Year}/{dateTime.Month}/{ dateTime.Day} {item.Hour}:00:00"), DateTimeKind.Utc).ToLocalTime().ToString("HH"));
  203. stuDays.Add(item.Hour, item.Student);
  204. if (allDays.ContainsKey(item.Hour))
  205. allDays[item.Hour] = (allDays[item.Hour] + item.Student);
  206. else
  207. allDays.Add(item.Hour, item.Student);
  208. }
  209. }
  210. }
  211. SortedSetEntry[] tmdDay = redisClinet.SortedSetRangeByScoreWithScores($"Login:IES:tmduser:{dateDay}");
  212. if (tmdDay.Length > 0)
  213. {
  214. foreach (var item in tmdDay)
  215. {
  216. int val = (int)item.Score;
  217. int key = (int)item.Element;
  218. //var utcTo = new DateTimeOffset(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, key, 00, 00)).Hour;
  219. //var hour = int.Parse(DateTime.SpecifyKind(Convert.ToDateTime($"{dateTime.Year}/{dateTime.Month}/{ dateTime.Day} {key}:00:00"), DateTimeKind.Utc).ToLocalTime().ToString("HH"));
  220. tmdDays.Add(key, val);
  221. if (allDays.ContainsKey(key))
  222. allDays[key] = (allDays[key] + val);
  223. else
  224. allDays.Add(key, val);
  225. }
  226. }
  227. else
  228. {
  229. string tableSqlTmd = $"PartitionKey eq 'HourLogin' and RowKey ge '{strDaySt}' and RowKey le '{strDayEt}'";
  230. List<HourLogin> hourLoginsTmd = await table.QueryWhereString<HourLogin>(tableSqlTmd);
  231. //var hourTmdCnt = hourLoginsTmd.GroupBy(x => x.Hour).Select(k => new { key = int.Parse(k.Key.ToString().Substring(8, 2)), value = k.Count() }).ToList();
  232. if (hourLoginsTmd.Count > 0)
  233. {
  234. foreach (var item in hourLoginsTmd)
  235. {
  236. await redisClinet.SortedSetIncrementAsync($"Login:IES:tmduser:{dateDay}", $"{item.Hour}", item.TmdUser);//存一天24小时
  237. var utcTo = new DateTimeOffset(new DateTime(dateTime.Year, dateTime.Month, dateTime.Day, item.Hour, 00, 00)).Hour;
  238. //var hour = int.Parse(DateTime.SpecifyKind(Convert.ToDateTime($"{dateTime.Year}/{dateTime.Month}/{ dateTime.Day} {item.Hour}:00:00"), DateTimeKind.Utc).ToLocalTime().ToString("HH"));
  239. tmdDays.Add(utcTo, item.TmdUser);
  240. if (allDays.ContainsKey(utcTo))
  241. allDays[utcTo] = (allDays[utcTo] + item.TmdUser);
  242. else
  243. allDays.Add(utcTo, item.TmdUser);
  244. }
  245. }
  246. }
  247. return Ok(new { state = 200,allDays = allDays.OrderBy(o=>o.Key).ToList(), tchDays=tchDays.OrderBy(o => o.Key).ToList(), stuDays= stuDays.OrderBy(o => o.Key).ToList(), tmdDays= tmdDays.OrderBy(o => o.Key).ToList() });
  248. }
  249. /// <summary>
  250. /// 课例趋势图
  251. /// </summary>
  252. /// <returns></returns>
  253. [HttpPost("get-lessontrend")]
  254. public async Task<IActionResult> GetLessonTrend(JsonElement jsonElement)
  255. {
  256. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  257. var cosmosClient = _azureCosmos.GetCosmosClient();
  258. jsonElement.TryGetProperty("site", out JsonElement site);
  259. if ($"{site}".Equals(BIConst.Global))
  260. {
  261. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  262. }
  263. int year = dateTime.Year; //当前年
  264. int month = dateTime.Month; //当前月
  265. int day = dateTime.Day; //当天
  266. var lestDate = dateTime.AddDays(-1); //昨天
  267. int hour = int.Parse(DateTime.SpecifyKind(Convert.ToDateTime($"{dateTime.Year}/{dateTime.Month}/{ dateTime.Day} {dateTime.Hour}:00:00"), DateTimeKind.Utc).ToLocalTime().ToString("HH")); //当前小时
  268. var (daySt, dayEt) = TimeHelper.GetStartOrEnd(dateTime); //今天开始时间 13位
  269. var (lastDayS, lastdayE) = TimeHelper.GetStartOrEnd(lestDate); //昨天开始时间
  270. var (monthDayS, monthDayE) = TimeHelper.GetStartOrEnd(lestDate, "monthDay"); //本月开始/结束时间
  271. var (yearS, yearE) = TimeHelper.GetStartOrEnd(lestDate, "yearMonth"); //本月开始/结束时间
  272. Dictionary<int, int> sdOpenLesn = new(); //今日开课
  273. Dictionary<int, int> sdUpdLesn = new(); //今日上传课例
  274. Dictionary<int, int> ydOpenLesn = new(); //昨日开课
  275. Dictionary<int, int> ydUpdLesn = new(); //昨日上传课例
  276. Dictionary<int, int> mthOpenLesn = new(); //本月开课
  277. Dictionary<int, int> mthUpdLesn = new(); //本月上传课例
  278. Dictionary<int, int> yrOpenLesn = new(); //本年开课
  279. Dictionary<int, int> yrUpdLesn = new(); //本年上传课例
  280. Dictionary<int, int> allOpenLesn = new(); //所有年开课
  281. Dictionary<int, int> allUpdLesn = new(); //所有年上传课例
  282. List<RecLesn> yearLeson = new();
  283. List<RecLesn> allLess = new();
  284. string allSql = "select c.id,c.name,c.code,c.school,c.scope,c.startTime from c where c.pk='LessonRecord'";
  285. //所有的课例
  286. allLess = await CommonFind.GetObject<RecLesn>(cosmosClient, new List<string>() { "School", "Teacher" }, allSql);
  287. if (allLess.Count > 0)
  288. {
  289. for (int i = 0; i < 24; i++)
  290. {
  291. if (hour >= i)
  292. {
  293. DateTimeOffset timeHour = new DateTime(year, month, day, i, 0, 0);
  294. var (hourS, hourE) = TimeHelper.GetStartOrEnd(timeHour, type: "hour");
  295. var openLesn1 = allLess.Where(item => item.startTime >= hourS && item.startTime <= hourE && item.upload == 0).Count();
  296. var openLesn = allLess.Where(item => item.startTime >= hourS && item.startTime <= hourE && item.upload == 0).ToList();
  297. sdOpenLesn.Add(i, openLesn.Count());
  298. var UpdLesn = allLess.Where(item => item.startTime >= hourS && item.startTime <= hourE && item.upload == 1).ToList();
  299. sdUpdLesn.Add(i, UpdLesn.Count());
  300. }
  301. DateTimeOffset yesterday = new DateTime(lestDate.Year, lestDate.Month, lestDate.Day, i, 0, 0);
  302. var (yHourS, yHourE) = TimeHelper.GetStartOrEnd(yesterday, type: "hour");
  303. var yOpenLesn = allLess.Where(item => item.startTime >= yHourS && item.startTime <= yHourE && item.upload == 0).ToList();
  304. ydOpenLesn.Add(i, yOpenLesn.Count);
  305. var yUpdLesn = allLess.Where(item => item.startTime >= yHourS && item.startTime <= yHourE && item.upload == 1).ToList();
  306. ydUpdLesn.Add(i, yUpdLesn.Count);
  307. }
  308. for (int i = 1; i <= day; i++)
  309. {
  310. DateTimeOffset timeDay = new DateTime(year, month, i, 0, 0, 0);
  311. var (dayS, dayE) = TimeHelper.GetStartOrEnd(timeDay); //本月开始/结束时间
  312. var openLesn = allLess.Where(item => item.startTime >= dayS && item.startTime <= dayE && item.upload == 0).ToList();
  313. mthOpenLesn.Add(i, openLesn.Count());
  314. var UpdLesn = allLess.Where(item => item.startTime >= dayS && item.startTime <= dayE && item.upload == 1).ToList();
  315. mthUpdLesn.Add(i, UpdLesn.Count());
  316. }
  317. for (int i = 1; i <= month; i++)
  318. {
  319. DateTimeOffset timeDay = new DateTime(year, i, day, 0, 0, 0);
  320. var (monthS, monthE) = TimeHelper.GetStartOrEnd(timeDay, "month"); //本月开始/结束时间
  321. var openLesn = allLess.Where(item => item.startTime >= monthS && item.startTime <= monthE && item.upload == 0).ToList();
  322. yrOpenLesn.Add(i, openLesn.Count());
  323. var UpdLesn = allLess.Where(item => item.startTime >= monthS && item.startTime <= monthE && item.upload == 1).ToList();
  324. yrUpdLesn.Add(i, UpdLesn.Count());
  325. }
  326. yearLeson = allLess.Select(item => { item.startTime = TimeHelper.GetDateTime(item.startTime).Year; return item; }).ToList();
  327. List<RecLesn> oRecLess = yearLeson.Where(o => o.upload == 0).ToList();
  328. var allo = oRecLess.GroupBy(g => g.startTime).Select(s => new { key = s.Key, value = s.Count() }).ToList();
  329. if (allo.Count > 0)
  330. allo.ForEach(a => { allOpenLesn.Add((int)a.key, a.value); });
  331. List<RecLesn> uRecLesns = yearLeson.Where(o => o.upload == 1).ToList();
  332. var allu = uRecLesns.GroupBy(g => g.startTime).Select(s => new { key = s.Key, value = s.Count() }).ToList();
  333. if (allu.Count > 0)
  334. allu.ForEach(a => { allUpdLesn.Add((int)a.key, a.value); });
  335. }
  336. return Ok(new
  337. {
  338. state = RespondCode.Ok,
  339. hours = new { sdOpenLesn = sdOpenLesn.ToList(), sdUpdLesn = sdUpdLesn.ToList(), ydOpenLesn = ydOpenLesn.ToList(), ydUpdLesn = ydUpdLesn.ToList() },
  340. days = new { sdOpenLesn = mthOpenLesn.ToList(), sdUpdLesn = mthUpdLesn.ToList() },
  341. months = new { sdOpenLesn = yrOpenLesn.ToList(), sdUpdLesn = yrUpdLesn.ToList() },
  342. years = new { sdOpenLesn = allOpenLesn.ToList(), sdUpdLesn = allUpdLesn.ToList() }
  343. }) ;
  344. //string lesnSql = $"select c.id,c.name,c.code,c.school,c.scope,c.startTime from c where c.pk='LessonRecord' and c.startTime >={daySt} and c.startTime <= {dayEt}";
  345. ////今天课例
  346. //sameDayLesn = await CommonFind.GetObject<RecLesn>(cosmosClient, new List<string>() { "School", "Teacher" }, lesnSql);
  347. ////await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecLesn>(queryText: lesnSql, requestOptions: new QueryRequestOptions() { }))
  348. ////{
  349. //// sameDayLesn.Add(item);
  350. ////}
  351. ////await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecLesn>(queryText: lesnSql, requestOptions: new QueryRequestOptions() { }))
  352. ////{
  353. //// sameDayLesn.Add(item);
  354. ////}
  355. ////昨天课例
  356. //string yesterDayLesnSql = $"select c.id,c.name,c.code,c.school,c.scope,c.startTime from c where c.pk='LessonRecord' and c.startTime >={lastDayS} and c.startTime <= {lastdayE}";
  357. //yesterDayLesn = await CommonFind.GetObject<RecLesn>(cosmosClient, new List<string>() { "School", "Teacher" }, yesterDayLesnSql);
  358. ////await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecLesn>(queryText: yesterDayLesnSql, requestOptions: new QueryRequestOptions() { }))
  359. ////{
  360. //// yesterDayLesn.Add(item);
  361. ////}
  362. ////await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Teacher").GetItemQueryIterator<RecLesn>(queryText: yesterDayLesnSql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("LessonRecord") }))
  363. ////{
  364. //// yesterDayLesn.Add(item);
  365. ////}
  366. //for (int i = 0; i < 24; i++)
  367. //{
  368. // if (hour >= i)
  369. // {
  370. // DateTimeOffset timeHour = new DateTime(year, month, day, i, 0, 0);
  371. // var (hourS, hourE) = TimeHelper.GetStartOrEnd(timeHour, type: "hour");
  372. // var openLesn = sameDayLesn.Where(item => item.startTime >= hourS && item.startTime <= hourE && item.upload == 0);
  373. // sdOpenLesn.Add(i, openLesn.Count());
  374. // var UpdLesn = sameDayLesn.Where(item => item.startTime >= hourS && item.startTime <= hourE && item.upload == 1);
  375. // sdUpdLesn.Add(i, openLesn.Count());
  376. // }
  377. // DateTimeOffset yesterday = new DateTime(lestDate.Year, lestDate.Month, lestDate.Day, i, 0, 0);
  378. // var (yHourS, yHourE) = TimeHelper.GetStartOrEnd(yesterday, type: "hour");
  379. // var yOpenLesn = yesterDayLesn.Where(item => item.startTime >= yHourS && item.startTime <= yHourE && item.upload == 0).ToList();
  380. // ydOpenLesn.Add(i, yOpenLesn.Count);
  381. // var yUpdLesn = yesterDayLesn.Where(item => item.startTime >= yHourS && item.startTime <= yHourE && item.upload == 1).ToList();
  382. // ydUpdLesn.Add(i, yUpdLesn.Count);
  383. //}
  384. //return Ok(new { state = 200, sdOpenLesn = sdOpenLesn.ToList(), sdUpdLesn = sdUpdLesn.ToList(), ydOpenLesn = ydOpenLesn.ToList(), ydUpdLesn = ydUpdLesn.ToList() });
  385. }
  386. /// <summary>
  387. /// 版本数量占比
  388. /// </summary>
  389. /// <returns></returns>
  390. [HttpPost("get-edition")]
  391. public async Task<IActionResult> GetEdition(JsonElement jsonElement)
  392. {
  393. var cosmosClient = _azureCosmos.GetCosmosClient();
  394. jsonElement.TryGetProperty("site", out JsonElement site);
  395. if ($"{site}".Equals(BIConst.Global))
  396. {
  397. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  398. }
  399. int beCnt = 0; //基础班
  400. int seCnt = 0; //标准版
  401. int peCnt = 0; //专业版
  402. List<RecScEd> scEdCnt = new();
  403. var ScSql = $"select c.id,c.name,c.size,c.scale from c";
  404. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecScEd>(queryText: ScSql, requestOptions:new QueryRequestOptions() { PartitionKey= new PartitionKey("Base")}))
  405. {
  406. scEdCnt.Add(item);
  407. }
  408. scEdCnt.ForEach(async scProCnt =>
  409. {
  410. var response = await cosmosClient.GetContainer("TEAMModelOS", "School").ReadItemStreamAsync(scProCnt.id, new PartitionKey("ProductSum"));
  411. if (response.Status == 200)
  412. {
  413. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  414. SchoolProductSum ScProductSum = json.ToObject<SchoolProductSum>();
  415. //scProCnt.serial = ScProductSum.serial.Count();
  416. //scProCnt.service = ScProductSum.service.Count();
  417. //scProCnt.hard = ScProductSum.hard.Count();
  418. int pSeriCnt = ScProductSum.serial.Count();
  419. int pServCnt = ScProductSum.service.Count();
  420. int pHardCnt = ScProductSum.hard.Count();
  421. scProCnt.serial = pSeriCnt;
  422. scProCnt.service = pServCnt;
  423. scProCnt.hard = pHardCnt;
  424. if (scProCnt.scale >= 500 && (pSeriCnt > 0 || pServCnt > 0 || pHardCnt > 0)) peCnt += 1;
  425. }
  426. if (scProCnt.scale >= 500 && scProCnt.serial == 0 && scProCnt.service == 0 && scProCnt.hard == 0) seCnt += 1;
  427. if (scProCnt.scale == 0) beCnt += 1;
  428. });
  429. return Ok(new { state = 200, beCnt, seCnt, peCnt, scEdCnt });
  430. }
  431. /// <summary>
  432. /// 开课
  433. /// </summary>
  434. /// <param name="jsonElement"></param>
  435. /// <returns></returns>
  436. public async Task<IActionResult> GetTmdCount(JsonElement jsonElement)
  437. {
  438. if(jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  439. var cosmosClient = _azureCosmos.GetCosmosClient();
  440. var table = _azureStorage.GetCloudTableClient().GetTableReference("IESLogin");
  441. var blobClient = _azureStorage.GetBlobContainerClient($"0-public");
  442. jsonElement.TryGetProperty("site", out JsonElement site);
  443. if ($"{site}".Equals(BIConst.Global))
  444. {
  445. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  446. table = _azureStorage.GetCloudTableClient(BIConst.Global).GetTableReference("IESLogin");
  447. blobClient = _azureStorage.GetBlobContainerClient($"0-public", BIConst.Global);
  448. }
  449. List<string> schools = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  450. return Ok(new { state = 200 });
  451. }
  452. public record YearLesson
  453. {
  454. public long year { get; set; }
  455. public List<int> upload { get; set; }
  456. public List<string> id { get; set; }
  457. }
  458. /// <summary>
  459. /// 记录学校版本信息
  460. /// </summary>
  461. public record RecScEd
  462. {
  463. public string id { get; set; }
  464. public string name { get; set; }
  465. public int size { get; set; }
  466. public int scale { get; set; }
  467. public int serial { get; set; } = 0;//软体
  468. public int service { get; set; } = 0; //服务
  469. public int hard { get; set; } = 0; //硬体
  470. }
  471. /// <summary>
  472. /// 记录课例
  473. /// </summary>
  474. public record RecLesn
  475. {
  476. public string id { get; set; }
  477. public string name { get; set; }
  478. public string code { get; set; }
  479. public string school { get; set; }
  480. public string scope{get;set;}
  481. public long startTime { get; set; }
  482. public int upload { get; set; }
  483. }
  484. }
  485. }