HomeStatisController.cs 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Text.Json;
  8. using TEAMModelOS.SDK.DI;
  9. using Azure.Cosmos;
  10. using Microsoft.Extensions.Options;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.Models;
  13. using System.Text;
  14. using StackExchange.Redis;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelBI.Tool;
  17. namespace TEAMModelBI.Controllers.BIHome
  18. {
  19. [Route("homestatis")]
  20. [ApiController]
  21. public class HomeStatisController : ControllerBase
  22. {
  23. private readonly AzureCosmosFactory _azureCosmos;
  24. private readonly AzureStorageFactory _azureStorage;
  25. private readonly DingDing _dingDing;
  26. private readonly Option _option;
  27. private readonly AzureRedisFactory _azureRedis;
  28. public HomeStatisController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option)
  29. {
  30. _azureCosmos = azureCosmos;
  31. _dingDing = dingDing;
  32. _option = option?.Value;
  33. _azureStorage = azureStorage;
  34. _azureRedis = azureRedis;
  35. }
  36. /// <summary>
  37. /// 获取全部教师数量、学生数量、已创校数量、全部学校总空间大小
  38. /// </summary>
  39. /// <param name="jsonElement"></param>
  40. /// <returns></returns>
  41. [ProducesDefaultResponseType]
  42. [HttpPost("get-allnumber")]
  43. public async Task<IActionResult> GetAllNumber()
  44. {
  45. try
  46. {
  47. var client = _azureCosmos.GetCosmosClient();;
  48. //查询全部教师人数
  49. long teacherCount = await CommonFind.FindTotals(client, "select count(c.id) as totals from c where c.code='Base'", new List<string>() { "Teacher" });
  50. //查询全部教师人数
  51. long studentCount = await CommonFind.FindTotals(client, "select count(c.id) as totals from c where c.pk='Base'", new List<string>() { "Student" });
  52. //查询已创建多少学校
  53. long schoolCount = await CommonFind.FindTotals(client, "select count(c.id) as totals from c where c.code='Base'", new List<string>() { "School" });
  54. //空间
  55. long schoolSize = await CommonFind.FindTotals(client, "select sum(c.size) as totals from c where c.code='Base'", new List<string>() { "School" });
  56. return Ok(new { state = 200, teacherCount, studentCount, schoolCount, schoolSize });
  57. ////依据学校查询教师人数
  58. //List<string> teacherCount_list = new();
  59. ////依据学校查询学生信息
  60. //List<string> studentCount_List = new();
  61. ////学校数
  62. //List<string> schoolCount_List = new();
  63. ////学校空间大小
  64. //long schoolsize = 0
  65. ////查询全部教师人数
  66. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  67. //{
  68. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  69. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  70. // {
  71. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  72. // while (accounts.MoveNext())
  73. // {
  74. // JsonElement account = accounts.Current;
  75. // teacherCount_list.Add(account.GetProperty("id").GetString());
  76. // }
  77. // }
  78. //}
  79. ////查询全部学生人数
  80. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.pk='Base'"))
  81. //{
  82. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  83. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  84. // {
  85. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  86. // while (accounts.MoveNext())
  87. // {
  88. // JsonElement account = accounts.Current;
  89. // studentCount_List.Add(account.GetProperty("id").GetString());
  90. // }
  91. // }
  92. //}
  93. ////查询已创建多少学校
  94. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id,c.size from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  95. //{
  96. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  97. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  98. // {
  99. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  100. // while (accounts.MoveNext())
  101. // {
  102. // JsonElement account = accounts.Current;
  103. // schoolCount_List.Add(account.GetProperty("id").GetString());
  104. // schoolsize += account.GetProperty("size").GetInt64();
  105. // }
  106. // }
  107. //}
  108. ////查询教师的大小和教师集合信息
  109. //await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select sum(c.size) as size from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  110. //{
  111. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  112. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  113. // {
  114. // schoolsize += obj.GetProperty("size").GetInt64();
  115. // }
  116. //}
  117. //return Ok(new { state = 200, teacherCount = teacherCount_list.Count, studentCount = studentCount_List.Count, schoolCount = schoolCount_List.Count, schoolSize = schoolsize });
  118. }
  119. catch (Exception ex)
  120. {
  121. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-numberpeople \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  122. return BadRequest();
  123. }
  124. }
  125. /// <summary>
  126. /// 获取其它类型的统计:问卷调查、评量检测、投票活动
  127. /// </summary>
  128. /// <param name="jsonElement"></param>
  129. /// <returns></returns>
  130. [ProducesDefaultResponseType]
  131. [HttpPost("get-othertypes")]
  132. public async Task<IActionResult> GetOtherTypes(JsonElement jsonElement)
  133. {
  134. try
  135. {
  136. if (!jsonElement.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  137. int surveyJoinCount = 0; //调查参加数量
  138. int surveyDoneCount = 0; //调查完成数量
  139. int surveyAreaJoinCount = 0; //调查区域参加数量
  140. int surveyAreaDoneCount = 0; //调查区域完成数量
  141. int examJoinCount = 0; //评量检测参加数量
  142. int examDoneCount = 0; //评量检测完成数量
  143. int examAreaJoinCount = 0; //评量检测区域参加数量
  144. int examAreaDoneCount = 0; //评量检测区域完成数量
  145. int voteJoinCount = 0; //投票参加数量
  146. int voteDoneCount = 0; //投票完成数量
  147. int voteAreaJoinCount = 0; //投票区域参加数量
  148. int voteAreaDoneCount = 0; //投票区域完成数量
  149. //问卷调查
  150. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  151. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Survey' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  152. {
  153. if (!string.IsNullOrEmpty(item.owner))
  154. {
  155. if (item.owner.Equals("school"))
  156. {
  157. surveyJoinCount += 1;
  158. if (item.taskStatus > 0)
  159. {
  160. surveyDoneCount += 1;
  161. }
  162. }
  163. else if (item.owner.Equals("area"))
  164. {
  165. surveyAreaJoinCount += 1;
  166. if (item.taskStatus > 0)
  167. {
  168. surveyAreaDoneCount += 1;
  169. }
  170. }
  171. }
  172. }
  173. //评量检测
  174. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  175. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'ExamLite' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  176. {
  177. if (!string.IsNullOrEmpty(item.owner))
  178. {
  179. if (item.owner.Equals("school"))
  180. {
  181. examJoinCount += 1;
  182. if (item.taskStatus > 0)
  183. {
  184. examDoneCount += 1;
  185. }
  186. }
  187. else if (item.owner.Equals("area"))
  188. {
  189. examAreaJoinCount += 1;
  190. if (item.taskStatus > 0)
  191. {
  192. examAreaDoneCount += 1;
  193. }
  194. }
  195. }
  196. }
  197. //投票活动
  198. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  199. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Vote' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  200. {
  201. if (!string.IsNullOrEmpty(item.owner))
  202. {
  203. if (item.owner.Equals("school"))
  204. {
  205. voteJoinCount += 1;
  206. if (item.taskStatus > 0)
  207. {
  208. voteDoneCount += 1;
  209. }
  210. }
  211. else if (item.owner.Equals("area"))
  212. {
  213. voteAreaJoinCount += 1;
  214. if (item.taskStatus > 0)
  215. {
  216. voteAreaDoneCount += 1;
  217. }
  218. }
  219. }
  220. }
  221. return Ok(new { state = 200, surveyJoinCount, surveyDoneCount, surveyAreaJoinCount, surveyAreaDoneCount, examJoinCount, examDoneCount, examAreaJoinCount, examAreaDoneCount, voteJoinCount, voteDoneCount, voteAreaJoinCount, voteAreaDoneCount });
  222. }
  223. catch (Exception ex)
  224. {
  225. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-othertypes \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  226. return BadRequest();
  227. }
  228. }
  229. /// <summary>
  230. /// 查询所有区级信息统计数据
  231. /// </summary>
  232. /// <param name="jsonElement"></param>
  233. /// <returns></returns>
  234. [ProducesDefaultResponseType]
  235. [HttpPost("get-alldiststics")]
  236. public async Task<IActionResult> GetAllDistStics()
  237. {
  238. try
  239. {
  240. var cosmosClient = _azureCosmos.GetCosmosClient();
  241. List<SticsCity> standards = new List<SticsCity>();
  242. StringBuilder stringBuder = new StringBuilder("select c.name,c.provName,c.cityName,c.standard from c");
  243. //查询省份区域
  244. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: stringBuder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  245. {
  246. SticsCity sticsCity = new SticsCity
  247. {
  248. provName = item.provName,
  249. cityName = item.cityName,
  250. distName = item.name,
  251. standard = item.standard
  252. };
  253. standards.Add(sticsCity);
  254. }
  255. List<SticsDist> sticsDists = new List<SticsDist>();
  256. //查询所有下面的学校数量
  257. foreach (var itemStandrds in standards)
  258. {
  259. SticsDist sticsDist = new SticsDist();
  260. sticsDist.provName = itemStandrds.provName;
  261. sticsDist.cityName = itemStandrds.cityName;
  262. sticsDist.distName = itemStandrds.distName;
  263. sticsDist.standard = itemStandrds.standard;
  264. //查询区级下的学校ID
  265. string sqlTxt = $"select c.id from c where c.standard='{itemStandrds.standard}'";
  266. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, sqlTxt, "Base");
  267. sticsDist.schoolCount = schoolIds.Count;
  268. int teacherCount = 0; //教师数量
  269. int studentCount = 0; //学生数量
  270. //查询学校下面的教师人数
  271. foreach (var itemSchool in schoolIds)
  272. {
  273. //查询全部教师人数
  274. string sqlT = $"select count(c.id) as totals from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  275. teacherCount += await CommonFind.FindTotals(cosmosClient, sqlT, "Teacher","Base");
  276. //查询全部学生人数
  277. string sqlS = $"select count(c.id) as totals from c";
  278. teacherCount += await CommonFind.FindTotals(cosmosClient, sqlS, "Student", $"Base-{itemSchool}");
  279. }
  280. sticsDist.teacherCount = teacherCount;
  281. sticsDist.studentCount = studentCount;
  282. sticsDists.Add(sticsDist);
  283. }
  284. return Ok(new { state = 200, sticsDists });
  285. }
  286. catch (Exception ex)
  287. {
  288. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-alldiststics \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  289. return BadRequest();
  290. }
  291. }
  292. /// <summary>
  293. /// 依据城市Code查询学校,教师数量,学生数量
  294. /// </summary>
  295. /// <param name="jsonElement"></param>
  296. /// <returns></returns>
  297. [ProducesDefaultResponseType]
  298. [HttpPost("get-citystics")]
  299. public async Task<IActionResult> GetCityStics(JsonElement jsonElement)
  300. {
  301. try
  302. {
  303. if(!jsonElement.TryGetProperty("cityCode", out JsonElement _cityCode)) return BadRequest();
  304. var cosmosClient = _azureCosmos.GetCosmosClient();
  305. List<string> areaStandard = new List<string>();
  306. //查询省份区域
  307. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select c.name,c.cityName,c.standard from c where c.cityCode='{_cityCode}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  308. {
  309. areaStandard.Add(item.standard);
  310. }
  311. int citySchoolCount = 0;
  312. int teachCount = 0;
  313. int stuCount = 0;
  314. foreach (var tempSatndard in areaStandard)
  315. {
  316. string sqlSchool = $"select c.id from c where c.standard='{tempSatndard}'";
  317. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, sqlSchool, "Base");
  318. citySchoolCount += schoolIds.Count;
  319. //查询学校下面的教师人数
  320. foreach (var itemSchool in schoolIds)
  321. {
  322. //查询全部教师人数
  323. string sqlT = $"select count(c.id) as totals from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  324. teachCount += await CommonFind.FindTotals(cosmosClient, sqlT, "Teacher", "Base");
  325. //查询全部学生人数
  326. string sqlS = $"select count(c.id) as totals from c";
  327. stuCount += await CommonFind.FindTotals(cosmosClient, sqlS, "Student", $"Base-{itemSchool}");
  328. }
  329. }
  330. return Ok(new { state = 200, schoolCount = citySchoolCount, teacherCount = teachCount, tudentCount = stuCount });
  331. }
  332. catch (Exception ex)
  333. {
  334. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-citystics \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  335. return BadRequest();
  336. }
  337. }
  338. /// <summary>
  339. /// 统计所有省份下的数据
  340. /// </summary>
  341. /// <returns></returns>
  342. [ProducesDefaultResponseType]
  343. [HttpPost("get-provincestics")]
  344. public async Task<IActionResult> GetProvinceStics()
  345. {
  346. try
  347. {
  348. var cosmosClient = _azureCosmos.GetCosmosClient();
  349. List<ProvinceStandard> standards = new List<ProvinceStandard>();
  350. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryStreamIterator(queryText: $"select c.provCode,c.provName,c.standard from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
  351. {
  352. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  353. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  354. {
  355. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  356. while (accounts.MoveNext())
  357. {
  358. JsonElement account = accounts.Current;
  359. ProvinceStandard provinceStandard = new ProvinceStandard
  360. {
  361. provCode = account.GetProperty("provCode").GetString(),
  362. provName = account.GetProperty("provName").GetString(),
  363. standard = account.GetProperty("standard").GetString()
  364. };
  365. standards.Add(provinceStandard);
  366. }
  367. }
  368. }
  369. List<ProvinceStics> provinceStics = new List<ProvinceStics>();
  370. foreach (var itemStandard in standards)
  371. {
  372. ProvinceStics tempProvinceStics = new ProvinceStics();
  373. //ProvinceStics tempCode = new ProvinceStics();
  374. var tempCode = provinceStics.Find(x => x.provCode == itemStandard.provCode);
  375. if (tempCode != null)
  376. {
  377. string sqlSc = $"select c.id from c where c.standard='{itemStandard.standard}'";
  378. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, sqlSc, "Base");
  379. tempCode.schoolCount += schoolIds.Count;
  380. if (schoolIds.Count > 0)
  381. {
  382. foreach (var itemSchool in schoolIds)
  383. {
  384. //查询学校教师人数
  385. string sqlT = $"select count(c.id) as totals from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  386. tempCode.teacherCount += await CommonFind.FindTotals(cosmosClient, sqlT, "Teacher", "Base");
  387. //查询学校学生人数
  388. string sqlStu = $"select count(c.id) as totals from c";
  389. tempCode.studentCount += await CommonFind.FindTotals(cosmosClient, sqlStu, "Student", $"Base-{itemSchool}");
  390. }
  391. }
  392. var tempModel = provinceStics.Where(x => x.provCode == tempCode.provCode).FirstOrDefault();
  393. if (tempModel != null)
  394. {
  395. tempModel.schoolCount = tempCode.schoolCount;
  396. tempModel.teacherCount = tempCode.teacherCount;
  397. tempModel.studentCount = tempCode.studentCount;
  398. tempModel.standardCount +=1;
  399. }
  400. }
  401. else
  402. {
  403. tempProvinceStics.provCode = itemStandard.provCode;
  404. tempProvinceStics.provName = itemStandard.provName;
  405. tempProvinceStics.standardCount += 1;
  406. string sqlSc = $"select c.id from c where c.standard='{itemStandard.standard}'";
  407. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, sqlSc, "Base");
  408. tempProvinceStics.schoolCount += schoolIds.Count;
  409. if (schoolIds.Count > 0)
  410. {
  411. foreach (var itemSchool in schoolIds)
  412. {
  413. //查询学校教师人数
  414. string sqlT = $"select count(c.id) as totals from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  415. tempProvinceStics.teacherCount += await CommonFind.FindTotals(cosmosClient, sqlT, "Teacher", "Base");
  416. //查询学校学生人数
  417. string sqlStu = "select count(c.id) as totals from c";
  418. tempProvinceStics.studentCount += await CommonFind.FindTotals(cosmosClient, sqlStu, "Student", $"Base-{itemSchool}");
  419. }
  420. }
  421. provinceStics.Add(tempProvinceStics);
  422. }
  423. }
  424. return Ok(new { state = 200, provinceStics });
  425. }
  426. catch (Exception ex)
  427. {
  428. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-provincestics \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  429. return BadRequest();
  430. }
  431. }
  432. /// <summary>
  433. /// 查询所有市级下的学校数量
  434. /// </summary>
  435. /// <returns></returns>
  436. [ProducesDefaultResponseType]
  437. [HttpPost("get-cityschool")]
  438. public async Task<IActionResult> GetCitySchool()
  439. {
  440. try
  441. {
  442. var cosmosClient = _azureCosmos.GetCosmosClient();
  443. List<CityStandard> standards = new List<CityStandard>();
  444. //查询省份区域
  445. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select c.cityCode,c.cityName,c.standard from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  446. {
  447. CityStandard Citystics = new CityStandard
  448. {
  449. cityCode = item.cityCode,
  450. cityName = item.cityName,
  451. standard = item.standard
  452. };
  453. standards.Add(Citystics);
  454. }
  455. List<CitySchool> citySchools = new List<CitySchool>(); //返回数量
  456. foreach (var itemStandrd in standards)
  457. {
  458. CitySchool citySchool = new CitySchool();
  459. var tempCode = citySchools.Find(x => x.cityCode == itemStandrd.cityCode);
  460. if (tempCode != null)
  461. {
  462. string sqlTxt = $"select count(c.id) totals from c where c.standard='{itemStandrd.standard}'";
  463. tempCode.schoolCount += await CommonFind.FindTotals(cosmosClient, sqlTxt, "School", "Base");
  464. var tempModel = citySchools.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  465. if (tempModel != null)
  466. {
  467. tempModel.schoolCount = tempCode.schoolCount;
  468. }
  469. }
  470. else
  471. {
  472. citySchool.cityCode = itemStandrd.cityCode;
  473. citySchool.cityName = itemStandrd.cityName;
  474. string sqlTxt = $"select count(c.id) totals from c where c.standard='{itemStandrd.standard}'";
  475. citySchool.schoolCount += await CommonFind.FindTotals(cosmosClient, sqlTxt, "School", "Base");
  476. citySchools.Add(citySchool);
  477. }
  478. }
  479. return Ok(new { state = 200, citySchools });
  480. }
  481. catch (Exception ex)
  482. {
  483. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityschool \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  484. return BadRequest();
  485. }
  486. }
  487. /// <summary>
  488. /// 依据市级ID查询区域(区、县、郡)的学校、教师、学生数量
  489. /// </summary>
  490. /// <param name="jsonElement"></param>
  491. /// <returns></returns>
  492. [ProducesDefaultResponseType]
  493. [HttpPost("get-districtstics")]
  494. public async Task<IActionResult> GetDistrictStics(JsonElement jsonElement)
  495. {
  496. try
  497. {
  498. if (!jsonElement.TryGetProperty("cityCode", out JsonElement _cityCode)) return BadRequest();
  499. var cosmosClient = _azureCosmos.GetCosmosClient();
  500. List<DistrictStandard> districtStandards = new List<DistrictStandard>();
  501. //查询省份区域
  502. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select c.name,c.cityName,c.standard from c where c.cityCode='{_cityCode}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  503. {
  504. DistrictStandard districtStandard = new DistrictStandard
  505. {
  506. cityName = item.cityName,
  507. distName = item.name,
  508. standard = item.standard
  509. };
  510. districtStandards.Add(districtStandard);
  511. }
  512. List<DistrictStics> districtSticss = new List<DistrictStics>(); //返回数据
  513. foreach (var itemStandrd in districtStandards)
  514. {
  515. DistrictStics districtStics = new DistrictStics();
  516. districtStics.cityName = itemStandrd.cityName;
  517. districtStics.distName = itemStandrd.distName;
  518. string sqlTxt = $"select c.id from c where c.standard='{itemStandrd.standard}'";
  519. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient,sqlTxt, "Base");
  520. districtStics.schoolCount += schoolIds.Count;
  521. if (schoolIds.Count > 0)
  522. {
  523. foreach (var itemSchool in schoolIds)
  524. {
  525. //查询学校教师人数
  526. string sqlT = $"select count(c.id) as totals from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  527. districtStics.teacherCount += await CommonFind.FindTotals(cosmosClient, sqlT, "Teacher", "Base");
  528. //查询学校学生人数
  529. string sqlS = $"select count(c.id) as totals from c ";
  530. districtStics.studentCount += await CommonFind.FindTotals(cosmosClient, sqlS, "Student", $"Base-{itemSchool}");
  531. }
  532. }
  533. districtSticss.Add(districtStics);
  534. }
  535. return Ok(new { state = 200, districtSticss });
  536. }
  537. catch (Exception ex)
  538. {
  539. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-districtstics \n {ex.Message} {ex.StackTrace} ", GroupNames.成都开发測試群組);
  540. return BadRequest();
  541. }
  542. }
  543. /// <summary>
  544. /// 查询市的学校数据,以及市级下的区中的学校,教师、学生数量
  545. /// </summary>
  546. /// <param name="jsonElement"></param>
  547. /// <returns></returns>
  548. [ProducesDefaultResponseType]
  549. [HttpPost("get-cityallstics")]
  550. public async Task<IActionResult> GetCityAllStics(JsonElement jsonElement)
  551. {
  552. try
  553. {
  554. var cosmosClient = _azureCosmos.GetCosmosClient();
  555. List<AllCityStics> tempAllCityStics = new List<AllCityStics>();
  556. //查询省份区域
  557. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select c.name,c.cityCode,c.cityName,c.standard from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  558. {
  559. AllCityStics Citystics = new AllCityStics
  560. {
  561. cityCode = item.cityCode,
  562. cityName = item.cityName,
  563. distName = item.name,
  564. standard = item.standard
  565. };
  566. tempAllCityStics.Add(Citystics);
  567. }
  568. List<SticsCitys> sticsCitys = new List<SticsCitys>();
  569. foreach(var itemStandrd in tempAllCityStics)
  570. {
  571. SticsCitys citySchool = new SticsCitys();
  572. var tempCode = sticsCitys.Find(x => (x.cityCode) == (itemStandrd.cityCode));
  573. if (tempCode != null)
  574. {
  575. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.standard='{itemStandrd.standard}'", "Base");
  576. tempCode.schoolCount += schoolIds.Count;
  577. //List<string> schoolIds = new List<string>();
  578. //await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.standard='{itemStandrd.standard}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  579. //{
  580. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  581. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  582. // {
  583. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  584. // while (accounts.MoveNext())
  585. // {
  586. // JsonElement account = accounts.Current;
  587. // schoolIds.Add(account.GetProperty("id").GetString());
  588. // tempCode.schoolCount += 1;
  589. // }
  590. // }
  591. //}
  592. var tempModel = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  593. if (tempModel != null)
  594. {
  595. tempModel.schoolCount = tempCode.schoolCount;
  596. }
  597. DistrictStics districtStics = new DistrictStics();
  598. districtStics.cityName = itemStandrd.cityName;
  599. districtStics.distName = itemStandrd.distName;
  600. districtStics.schoolCount = schoolIds.Count;
  601. if (schoolIds.Count > 0)
  602. {
  603. foreach (var itemSchool in schoolIds)
  604. {
  605. //查询学校教师人数
  606. districtStics.teacherCount += await CommonFind.FindTotals(cosmosClient, $"select count(c.id) totals from c join a1 in c.schools where a1.schoolId='{itemSchool}'", "Teacher", "Base");
  607. //await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id from c join a1 in c.schools where a1.schoolId='{itemSchool}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  608. //{
  609. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  610. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  611. // {
  612. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  613. // while (accounts.MoveNext())
  614. // {
  615. // JsonElement account = accounts.Current;
  616. // districtStics.teacherCount += 1;
  617. // }
  618. // }
  619. //}
  620. //查询学校学生人数
  621. districtStics.studentCount += await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "Student", $"Base-{itemSchool}");
  622. //await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{itemSchool}") }))
  623. //{
  624. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  625. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  626. // {
  627. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  628. // while (accounts.MoveNext())
  629. // {
  630. // JsonElement account = accounts.Current;
  631. // districtStics.studentCount += 1;
  632. // }
  633. // }
  634. //}
  635. }
  636. }
  637. var tempDistrict = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  638. if (tempDistrict != null)
  639. {
  640. tempDistrict.districtSticss.Add(districtStics);
  641. }
  642. }
  643. else
  644. {
  645. citySchool.cityCode = itemStandrd.cityCode;
  646. citySchool.cityName = itemStandrd.cityName;
  647. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.standard='{itemStandrd.standard}'","Base");
  648. citySchool.schoolCount += schoolIds.Count;
  649. //List<string> schoolIds = new List<string>();
  650. //await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.standard='{itemStandrd.standard}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  651. //{
  652. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  653. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  654. // {
  655. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  656. // while (accounts.MoveNext())
  657. // {
  658. // JsonElement account = accounts.Current;
  659. // schoolIds.Add(account.GetProperty("id").GetString());
  660. // citySchool.schoolCount += 1;
  661. // }
  662. // }
  663. //}
  664. List<DistrictStics> tempDistrictStics = new List<DistrictStics>();
  665. if (schoolIds.Count > 0)
  666. {
  667. DistrictStics districtStics = new DistrictStics();
  668. districtStics.cityName = itemStandrd.cityName;
  669. districtStics.distName= itemStandrd.distName;
  670. districtStics.schoolCount = schoolIds.Count;
  671. foreach (var itemSchool in schoolIds)
  672. {
  673. //查询学校教师人数
  674. districtStics.teacherCount += await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c join a1 in c.schools where a1.schoolId='{itemSchool}'", "Teacher", "Base");
  675. //await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id from c join a1 in c.schools where a1.schoolId='{itemSchool}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  676. //{
  677. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  678. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  679. // {
  680. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  681. // while (accounts.MoveNext())
  682. // {
  683. // JsonElement account = accounts.Current;
  684. // districtStics.teacherCount += 1;
  685. // }
  686. // }
  687. //}
  688. //查询学校学生人数
  689. districtStics.studentCount += await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c", "Student","Base");
  690. //await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{itemSchool}") }))
  691. //{
  692. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  693. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  694. // {
  695. // var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  696. // while (accounts.MoveNext())
  697. // {
  698. // JsonElement account = accounts.Current;
  699. // districtStics.studentCount += 1;
  700. // }
  701. // }
  702. //}
  703. }
  704. tempDistrictStics.Add(districtStics);
  705. }
  706. citySchool.districtSticss = tempDistrictStics;
  707. sticsCitys.Add(citySchool);
  708. }
  709. }
  710. return Ok(new { state = 200, sticsCitys });
  711. }
  712. catch (Exception ex)
  713. {
  714. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityallstics \n {ex.Message} {ex.StackTrace}", GroupNames.成都开发測試群組);
  715. return BadRequest();
  716. }
  717. }
  718. /// <summary>
  719. /// 查询BI权限开放人数
  720. /// </summary>
  721. /// <returns></returns>
  722. [ProducesDefaultResponseType]
  723. [HttpPost("get-sticsbipower")]
  724. public async Task<IActionResult> GetSticsBIPower()
  725. {
  726. try
  727. {
  728. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", "authority-bi" } };
  729. List<Authority> authorityBIList = await _azureStorage.FindListByDict<Authority>(dic);
  730. var cosmosClient = _azureCosmos.GetCosmosClient();
  731. List<PowerStics> powerStics = new List<PowerStics>();
  732. foreach (var temp in authorityBIList)
  733. {
  734. PowerStics tempPowerStics = new PowerStics();
  735. tempPowerStics.powerName = temp.Discription;
  736. //查询学校权限
  737. string sqlTxt = $"SELECT DISTINCT REPLACE(c.code, 'Teacher-', '') AS schoolId, c.id, c.name FROM c WHERE ARRAY_CONTAINS(c.permissions, '{temp.RowKey}', true) AND c.pk = 'Teacher' AND c.status = 'join'";
  738. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  739. {
  740. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  741. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  742. {
  743. tempPowerStics.powerCount += 1;
  744. }
  745. }
  746. powerStics.Add(tempPowerStics);
  747. }
  748. return Ok(new { state = 200, powerStics });
  749. }
  750. catch (Exception ex)
  751. {
  752. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-sticsbipower {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  753. return BadRequest();
  754. }
  755. }
  756. /// <summary>
  757. /// 统计学校和教师空间类型
  758. /// </summary>
  759. /// <returns></returns>
  760. [HttpPost("get-datatypestics")]
  761. public async Task<IActionResult> GetDataTypeStics()
  762. {
  763. try
  764. {
  765. long totalSize = 0; //总空间
  766. long useSize = 0; //已使用空间
  767. long teach = 0; //学校已经分配给所有教师的空间大小GB。
  768. Dictionary<string, long> typeStics = new Dictionary<string, long>(); //所有类型
  769. Dictionary<string, double?> typeStics1 = new Dictionary<string, double?>(); //所有类型
  770. List<string> schoolId = new List<string>();
  771. var cosmosClient = _azureCosmos.GetCosmosClient();
  772. //查询学校空间和学校Id
  773. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"School").GetItemQueryStreamIterator(queryText:$"select c.id,c.size from c",requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("Base")}))
  774. {
  775. var json = await JsonDocument.ParseAsync(item.ContentStream);
  776. foreach (var tempItem in json.RootElement.GetProperty("Documents").EnumerateArray())
  777. {
  778. totalSize += tempItem.GetProperty("size").GetInt64();
  779. schoolId.Add(tempItem.GetProperty("id").GetString());
  780. }
  781. }
  782. //查询学校已使用空间大小
  783. foreach (var itemId in schoolId)
  784. {
  785. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
  786. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{itemId}") }))
  787. {
  788. var json = await JsonDocument.ParseAsync(item.ContentStream);
  789. foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
  790. {
  791. if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
  792. {
  793. teach += _size.GetInt32();
  794. break;
  795. }
  796. }
  797. }
  798. Dictionary<string, double?> schoolStics = new Dictionary<string, double?>(); //学校空间
  799. long blobsize = 0;
  800. RedisValue value = default;
  801. value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemId);
  802. if (value != default && !value.IsNullOrEmpty)
  803. {
  804. JsonElement record = value.ToString().ToObject<JsonElement>();
  805. if (record.TryGetInt64(out blobsize))
  806. {
  807. }
  808. }
  809. else
  810. {
  811. var client = _azureStorage.GetBlobContainerClient(itemId);
  812. var size = await client.GetBlobsCatalogSize();
  813. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  814. foreach (var key in size.Item2.Keys)
  815. {
  816. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", key);
  817. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  818. }
  819. useSize += (long)size.Item1;
  820. schoolStics = size.Item2;
  821. typeStics1 = (from e in typeStics1.Concat(schoolStics) group e by e.Key into g select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) }).ToDictionary(item => item.Name, item => item.Count);
  822. continue;
  823. }
  824. SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{itemId}");
  825. if (Scores != null)
  826. {
  827. foreach (var score in Scores)
  828. {
  829. double val = score.Score;
  830. string key = score.Element.ToString();
  831. schoolStics.Add(key, val);
  832. }
  833. useSize += blobsize;
  834. typeStics1 = (from e in typeStics1.Concat(schoolStics) group e by e.Key into g select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) }).ToDictionary(item => item.Name, item => item.Count);
  835. continue;
  836. //return Ok(new { size = blobsize, catalog = catalog, teach });
  837. }
  838. else
  839. {
  840. var client = _azureStorage.GetBlobContainerClient(itemId);
  841. var size = await client.GetBlobsCatalogSize();
  842. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  843. foreach (var key in size.Item2.Keys)
  844. {
  845. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", key);
  846. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  847. }
  848. useSize += (long)size.Item1;
  849. schoolStics = size.Item2;
  850. typeStics1 = (from e in typeStics1.Concat(schoolStics) group e by e.Key into g select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) }).ToDictionary(item => item.Name, item => item.Count);
  851. continue;
  852. }
  853. ////RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemId);
  854. //if (!value.IsNullOrEmpty)
  855. //{
  856. // useSize += Convert.ToInt64(value);
  857. // //JsonElement record = value.ToString().ToObject<JsonElement>();
  858. // //long tempSize = 0;
  859. // //if (record.TryGetInt64(out tempSize))
  860. // //{
  861. // // sizeS += tempSize;
  862. // //}
  863. //}
  864. //else
  865. //{
  866. // var client = _azureStorage.GetBlobContainerClient(itemId);
  867. // var size = await client.GetBlobsCatalogSize();
  868. // var temp = await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  869. // foreach (var itemKey in size.Item2.Keys)
  870. // {
  871. // var temp1 = await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", itemKey);
  872. // var temp2 = await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", itemKey, size.Item2[itemKey].HasValue ? size.Item2[itemKey].Value : 0);
  873. // useSize += Convert.ToInt64(temp2);
  874. // }
  875. //}
  876. //SortedSetEntry[] sortedSetEntries = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemId}");
  877. //if (sortedSetEntries != null)
  878. //{
  879. // foreach (var tempSorted in sortedSetEntries)
  880. // {
  881. // if (typeStics.TryGetValue($"{tempSorted.Element}", out long val))
  882. // typeStics[$"{tempSorted.Element}"] = Convert.ToInt64(tempSorted.Score) != 0 ? val + Convert.ToInt64(tempSorted.Score) : 0;
  883. // else
  884. // typeStics.Add($"{tempSorted.Element}", Convert.ToInt64(tempSorted.Score));
  885. // }
  886. //}
  887. }
  888. ////教师数据
  889. //List<string> teacherId = new List<string>(); //教师Id集合
  890. ////查询教师的大小和教师集合信息
  891. //await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id,c.size from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  892. //{
  893. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  894. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  895. // {
  896. // totalSize += obj.GetProperty("size").GetInt64();
  897. // teacherId.Add(obj.GetProperty("id").GetString());
  898. // }
  899. //}
  900. ////查询教师已使用空间大小
  901. //foreach (var itemTeach in teacherId)
  902. //{
  903. // Dictionary<string, double?> teachStics = new Dictionary<string, double?>(); //学校空间
  904. // long blobsize = 0;
  905. // RedisValue value = default;
  906. // value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  907. // if (value != default && !value.IsNullOrEmpty)
  908. // {
  909. // JsonElement record = value.ToString().ToObject<JsonElement>();
  910. // if (record.TryGetInt64(out blobsize))
  911. // {
  912. // }
  913. // }
  914. // else
  915. // {
  916. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  917. // var size = await client.GetBlobsCatalogSize();
  918. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  919. // foreach (var key in size.Item2.Keys)
  920. // {
  921. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  922. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  923. // }
  924. // useSize += (long)size.Item1;
  925. // teachStics = size.Item2;
  926. // typeStics1 = (from e in typeStics1.Concat(teachStics) group e by e.Key into g select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) }).ToDictionary(item => item.Name, item => item.Count);
  927. // continue;
  928. // }
  929. // SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{itemTeach}");
  930. // if (Scores != null)
  931. // {
  932. // foreach (var score in Scores)
  933. // {
  934. // double val = score.Score;
  935. // string key = score.Element.ToString();
  936. // teachStics.Add(key, val);
  937. // }
  938. // useSize += blobsize;
  939. // typeStics1 = (from e in typeStics1.Concat(teachStics) group e by e.Key into g select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) }).ToDictionary(item => item.Name, item => item.Count);
  940. // continue;
  941. // }
  942. // else
  943. // {
  944. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  945. // var size = await client.GetBlobsCatalogSize();
  946. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  947. // foreach (var key in size.Item2.Keys)
  948. // {
  949. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  950. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  951. // }
  952. // useSize += (long)size.Item1;
  953. // teachStics = size.Item2;
  954. // typeStics1 = (from e in typeStics1.Concat(teachStics) group e by e.Key into g select new { Name = g.Key, Count = g.Sum(kvp => kvp.Value) }).ToDictionary(item => item.Name, item => item.Count);
  955. // continue;
  956. // }
  957. // //RedisValue tempValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  958. // //if (!tempValue.IsNullOrEmpty)
  959. // //{
  960. // // useSize += Convert.ToInt64(tempValue);
  961. // // //JsonElement record = tempValue.ToString().ToObject<JsonElement>();
  962. // // //long tempSize = 0;
  963. // // //if (record.TryGetInt64(out tempSize))
  964. // // //{
  965. // // // sizeT += tempSize;
  966. // // //}
  967. // //}
  968. // //SortedSetEntry[] tempSorted = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemTeach}");
  969. // //if (tempSorted != null)
  970. // //{
  971. // // foreach (var itemSorted in tempSorted)
  972. // // {
  973. // // if (typeStics.TryGetValue($"{itemSorted.Element}", out long val))
  974. // // typeStics[$"{itemSorted.Element}"] = Convert.ToInt64(itemSorted.Score) != 0 ? val + Convert.ToInt64(itemSorted.Score) : 0;
  975. // // else
  976. // // typeStics.Add($"{itemSorted.Element}", Convert.ToInt64(itemSorted.Score));
  977. // // }
  978. // //}
  979. //}
  980. return Ok(new { state = 200, totalSize, teach, useSize, stics = typeStics1.ToList() });
  981. }
  982. catch (Exception ex)
  983. {
  984. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-datatypestics \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  985. return BadRequest();
  986. }
  987. }
  988. /// <summary>
  989. /// 统计教师的空间类型
  990. /// </summary>
  991. /// <returns></returns>
  992. [HttpPost("get-teachetypestics")]
  993. public async Task<IActionResult> GetTeachDataTypeStics()
  994. {
  995. try
  996. {
  997. long teacheSize = 0; //教师空间
  998. long sizeT = 0; //教师空间
  999. List<string> teacherId = new List<string>(); //教师Id集合
  1000. Dictionary<string, long> typeStics = new Dictionary<string, long>(); //所有类型
  1001. var cosmosClient = _azureCosmos.GetCosmosClient();
  1002. //查询教师的大小和教师集合信息
  1003. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id,c.size from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  1004. {
  1005. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1006. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1007. {
  1008. teacheSize += obj.GetProperty("size").GetInt64();
  1009. teacherId.Add(obj.GetProperty("id").GetString());
  1010. }
  1011. }
  1012. //查询教师已使用空间大小
  1013. foreach (var itemTeach in teacherId)
  1014. {
  1015. RedisValue tempValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  1016. if (!tempValue.IsNullOrEmpty)
  1017. {
  1018. sizeT += Convert.ToInt64(tempValue);
  1019. //JsonElement record = tempValue.ToString().ToObject<JsonElement>();
  1020. //long tempSize = 0;
  1021. //if (record.TryGetInt64(out tempSize))
  1022. //{
  1023. // sizeT += tempSize;
  1024. //}
  1025. }
  1026. SortedSetEntry[] tempSorted = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemTeach}");
  1027. if (tempSorted != null)
  1028. {
  1029. foreach (var itemSorted in tempSorted)
  1030. {
  1031. if (typeStics.TryGetValue($"{itemSorted.Element}", out long val))
  1032. typeStics[$"{itemSorted.Element}"] = Convert.ToInt64(itemSorted.Score) != 0 ? val + Convert.ToInt64(itemSorted.Score) : 0;
  1033. else
  1034. typeStics.Add($"{itemSorted.Element}", Convert.ToInt64(itemSorted.Score));
  1035. }
  1036. }
  1037. }
  1038. return Ok(new { state = 200, teacheSize, sizeT, typeStics });
  1039. }
  1040. catch (Exception ex)
  1041. {
  1042. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-datatypestics \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  1043. return BadRequest();
  1044. }
  1045. }
  1046. /// <summary>
  1047. /// 存放的类型
  1048. /// </summary>
  1049. public record AllDataType
  1050. {
  1051. public long vote { get; set; }/// 投票josn
  1052. public long thum { get; set; }//视频和封面缩略图
  1053. public long avatar { get; set; }//学生头像
  1054. public long test { get; set; }//
  1055. public long survey { get; set; }//问卷调查 josn
  1056. public long image { get; set; }// PNG
  1057. public long other { get; set; }//其他
  1058. public long doc { get; set; }//文档
  1059. public long homework { get; set; }//作业 json
  1060. public long item { get; set; }// 题库 json
  1061. public long paper { get; set; }//试卷 json
  1062. public long train { get; set; }// 研修
  1063. public long res { get; set; }// 教程 htx
  1064. public long exam { get; set; }//评测 json
  1065. public long syllabus { get; set; }// 课纲 json
  1066. public long video { get; set; }// 视频
  1067. public long yxpt { get; set; }//研修平台
  1068. public long jyzx { get; set; }// 教研中心
  1069. public long records { get; set; } //HiTeach上传的数据
  1070. }
  1071. /// <summary>
  1072. /// 省的数据标准
  1073. /// </summary>
  1074. public record ProvinceStics
  1075. {
  1076. /// <summary>
  1077. /// 省份ID
  1078. /// </summary>
  1079. public string provCode { get; set; }
  1080. /// <summary>
  1081. /// 省份名称
  1082. /// </summary>
  1083. public string provName { get; set; }
  1084. /// <summary>
  1085. /// 省份下的方案数量
  1086. /// </summary>
  1087. public int standardCount { get; set; }
  1088. /// <summary>
  1089. /// 省份下的学校
  1090. /// </summary>
  1091. public int schoolCount { get; set; }
  1092. /// <summary>
  1093. /// 省份下的教师数量
  1094. /// </summary>
  1095. public int teacherCount { get; set; }
  1096. /// <summary>
  1097. /// 省份下的学生数量
  1098. /// </summary>
  1099. public int studentCount { get; set; }
  1100. }
  1101. /// <summary>
  1102. /// 省以下的区域标准
  1103. /// </summary>
  1104. public record ProvinceStandard
  1105. {
  1106. /// <summary>
  1107. /// 城市Code
  1108. /// </summary>
  1109. public string provCode { get; set; }
  1110. /// <summary>
  1111. /// 城市名称
  1112. /// </summary>
  1113. public string provName { get; set; }
  1114. /// <summary>
  1115. /// 区域标准
  1116. /// </summary>
  1117. public string standard { get; set; }
  1118. }
  1119. public record PowerStics
  1120. {
  1121. public string powerName { get; set; }
  1122. public int powerCount { get; set; }
  1123. }
  1124. /// <summary>
  1125. /// 市的学校数据,及以下数据
  1126. /// </summary>
  1127. public record SticsCitys
  1128. {
  1129. /// <summary>
  1130. /// 市级Code
  1131. /// </summary>
  1132. public string cityCode { get; set; }
  1133. /// <summary>
  1134. /// 市级名称
  1135. /// </summary>
  1136. public string cityName { get; set; }
  1137. /// <summary>
  1138. /// 学校数量
  1139. /// </summary>
  1140. public int schoolCount { get; set; }
  1141. /// <summary>
  1142. /// 市级下的区域,学校、
  1143. /// </summary>
  1144. public List<DistrictStics> districtSticss { get; set; }
  1145. }
  1146. /// <summary>
  1147. /// 所有区域标准
  1148. /// </summary>
  1149. public record AllCityStics
  1150. {
  1151. /// <summary>
  1152. /// 城市Code
  1153. /// </summary>
  1154. public string cityCode { get; set; }
  1155. /// <summary>
  1156. /// 城市名称
  1157. /// </summary>
  1158. public string cityName { get; set; }
  1159. /// <summary>
  1160. /// 区名称
  1161. /// </summary>
  1162. public string distName { get; set; }
  1163. /// <summary>
  1164. /// 区域标准
  1165. /// </summary>
  1166. public string standard { get; set; }
  1167. }
  1168. /// <summary>
  1169. /// 统计区域数量
  1170. /// </summary>
  1171. public record DistrictStics
  1172. {
  1173. /// <summary>
  1174. /// 市级名称
  1175. /// </summary>
  1176. public string cityName { get; set; }
  1177. /// <summary>
  1178. /// 地区名称
  1179. /// </summary>
  1180. public string distName { get; set; }
  1181. /// <summary>
  1182. /// 学校数量
  1183. /// </summary>
  1184. public int schoolCount { get; set; }
  1185. /// <summary>
  1186. /// 教师数量
  1187. /// </summary>
  1188. public int teacherCount { get; set; }
  1189. /// <summary>
  1190. /// 学生数量
  1191. /// </summary>
  1192. public int studentCount { get; set; }
  1193. }
  1194. /// <summary>
  1195. /// 区域标准
  1196. /// </summary>
  1197. public record DistrictStandard()
  1198. {
  1199. /// <summary>
  1200. /// 市名称
  1201. /// </summary>
  1202. public string cityName { get; set; }
  1203. /// <summary>
  1204. /// 区域名称
  1205. /// </summary>
  1206. public string distName { get; set; }
  1207. /// <summary>
  1208. /// 区域标准
  1209. /// </summary>
  1210. public string standard { get; set; }
  1211. }
  1212. /// <summary>
  1213. /// 市级学校数量
  1214. /// </summary>
  1215. public record CitySchool
  1216. {
  1217. /// <summary>
  1218. /// 市级ID
  1219. /// </summary>
  1220. public string cityCode { get; set; }
  1221. /// <summary>
  1222. /// 市级名称
  1223. /// </summary>
  1224. public string cityName { get; set; }
  1225. /// <summary>
  1226. /// 市级学校数量
  1227. /// </summary>
  1228. public int schoolCount { get; set;}
  1229. }
  1230. /// <summary>
  1231. /// 市级信息标准
  1232. /// </summary>
  1233. public record CityStandard
  1234. {
  1235. /// <summary>
  1236. /// 城市Code
  1237. /// </summary>
  1238. public string cityCode { get; set; }
  1239. /// <summary>
  1240. /// 城市名称
  1241. /// </summary>
  1242. public string cityName { get; set; }
  1243. /// <summary>
  1244. /// 区域标准
  1245. /// </summary>
  1246. public string standard { get; set; }
  1247. }
  1248. /// <summary>
  1249. /// 区域
  1250. /// </summary>
  1251. public record SticsCity
  1252. {
  1253. /// <summary>
  1254. /// 省份
  1255. /// </summary>
  1256. public string provName { get; set; }
  1257. /// <summary>
  1258. /// 市名称
  1259. /// </summary>
  1260. public string cityName { get; set; }
  1261. /// <summary>
  1262. /// 区域名称
  1263. /// </summary>
  1264. public string distName { get; set; }
  1265. /// <summary>
  1266. /// 区域标准
  1267. /// </summary>
  1268. public string standard { get; set; }
  1269. }
  1270. /// <summary>
  1271. /// 返回统计数据
  1272. /// </summary>
  1273. public record SticsDist
  1274. {
  1275. /// <summary>
  1276. /// 省份
  1277. /// </summary>
  1278. public string provName { get; set; }
  1279. /// <summary>
  1280. /// 市名
  1281. /// </summary>
  1282. public string cityName { get; set; }
  1283. /// <summary>
  1284. /// 区域标准名称
  1285. /// </summary>
  1286. public string distName { get; set; }
  1287. /// <summary>
  1288. /// 区域标准
  1289. /// </summary>
  1290. public string standard { get; set; }
  1291. /// <summary>
  1292. /// 学校数量
  1293. /// </summary>
  1294. public int schoolCount { get; set; }
  1295. /// <summary>
  1296. /// 教师人数
  1297. /// </summary>
  1298. public int teacherCount { get; set; }
  1299. /// <summary>
  1300. /// 学生人数
  1301. /// </summary>
  1302. public int studentCount { get; set; }
  1303. }
  1304. }
  1305. }