HomeStatisController.cs 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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. var table = _azureStorage.GetCloudTableClient().GetTableReference("SchoolSetting");
  730. List<Authority> authorityBIList = await table.FindListByDict<Authority>(dic);
  731. var cosmosClient = _azureCosmos.GetCosmosClient();
  732. List<PowerStics> powerStics = new List<PowerStics>();
  733. foreach (var temp in authorityBIList)
  734. {
  735. PowerStics tempPowerStics = new PowerStics();
  736. tempPowerStics.powerName = temp.Discription;
  737. //查询学校权限
  738. 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'";
  739. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  740. {
  741. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  742. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  743. {
  744. tempPowerStics.powerCount += 1;
  745. }
  746. }
  747. powerStics.Add(tempPowerStics);
  748. }
  749. return Ok(new { state = 200, powerStics });
  750. }
  751. catch (Exception ex)
  752. {
  753. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-sticsbipower {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  754. return BadRequest();
  755. }
  756. }
  757. /// <summary>
  758. /// 统计学校和教师空间类型
  759. /// </summary>
  760. /// <returns></returns>
  761. [HttpPost("get-datatypestics")]
  762. public async Task<IActionResult> GetDataTypeStics()
  763. {
  764. try
  765. {
  766. long totalSize = 0; //总空间
  767. long useSize = 0; //已使用空间
  768. long teach = 0; //学校已经分配给所有教师的空间大小GB。
  769. Dictionary<string, long> typeStics = new Dictionary<string, long>(); //所有类型
  770. Dictionary<string, double?> typeStics1 = new Dictionary<string, double?>(); //所有类型
  771. List<string> schoolId = new List<string>();
  772. var cosmosClient = _azureCosmos.GetCosmosClient();
  773. //查询学校空间和学校Id
  774. 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")}))
  775. {
  776. var json = await JsonDocument.ParseAsync(item.ContentStream);
  777. foreach (var tempItem in json.RootElement.GetProperty("Documents").EnumerateArray())
  778. {
  779. totalSize += tempItem.GetProperty("size").GetInt64();
  780. schoolId.Add(tempItem.GetProperty("id").GetString());
  781. }
  782. }
  783. //查询学校已使用空间大小
  784. foreach (var itemId in schoolId)
  785. {
  786. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
  787. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{itemId}") }))
  788. {
  789. var json = await JsonDocument.ParseAsync(item.ContentStream);
  790. foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
  791. {
  792. if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
  793. {
  794. teach += _size.GetInt32();
  795. break;
  796. }
  797. }
  798. }
  799. Dictionary<string, double?> schoolStics = new Dictionary<string, double?>(); //学校空间
  800. long blobsize = 0;
  801. RedisValue value = default;
  802. value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemId);
  803. if (value != default && !value.IsNullOrEmpty)
  804. {
  805. JsonElement record = value.ToString().ToObject<JsonElement>();
  806. if (record.TryGetInt64(out blobsize))
  807. {
  808. }
  809. }
  810. else
  811. {
  812. var client = _azureStorage.GetBlobContainerClient(itemId);
  813. var size = await client.GetBlobsCatalogSize();
  814. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  815. foreach (var key in size.Item2.Keys)
  816. {
  817. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", key);
  818. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  819. }
  820. useSize += (long)size.Item1;
  821. schoolStics = size.Item2;
  822. 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);
  823. continue;
  824. }
  825. SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{itemId}");
  826. if (Scores != null)
  827. {
  828. foreach (var score in Scores)
  829. {
  830. double val = score.Score;
  831. string key = score.Element.ToString();
  832. schoolStics.Add(key, val);
  833. }
  834. useSize += blobsize;
  835. 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);
  836. continue;
  837. //return Ok(new { size = blobsize, catalog = catalog, teach });
  838. }
  839. else
  840. {
  841. var client = _azureStorage.GetBlobContainerClient(itemId);
  842. var size = await client.GetBlobsCatalogSize();
  843. await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  844. foreach (var key in size.Item2.Keys)
  845. {
  846. await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", key);
  847. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  848. }
  849. useSize += (long)size.Item1;
  850. schoolStics = size.Item2;
  851. 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);
  852. continue;
  853. }
  854. ////RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemId);
  855. //if (!value.IsNullOrEmpty)
  856. //{
  857. // useSize += Convert.ToInt64(value);
  858. // //JsonElement record = value.ToString().ToObject<JsonElement>();
  859. // //long tempSize = 0;
  860. // //if (record.TryGetInt64(out tempSize))
  861. // //{
  862. // // sizeS += tempSize;
  863. // //}
  864. //}
  865. //else
  866. //{
  867. // var client = _azureStorage.GetBlobContainerClient(itemId);
  868. // var size = await client.GetBlobsCatalogSize();
  869. // var temp = await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  870. // foreach (var itemKey in size.Item2.Keys)
  871. // {
  872. // var temp1 = await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", itemKey);
  873. // var temp2 = await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", itemKey, size.Item2[itemKey].HasValue ? size.Item2[itemKey].Value : 0);
  874. // useSize += Convert.ToInt64(temp2);
  875. // }
  876. //}
  877. //SortedSetEntry[] sortedSetEntries = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemId}");
  878. //if (sortedSetEntries != null)
  879. //{
  880. // foreach (var tempSorted in sortedSetEntries)
  881. // {
  882. // if (typeStics.TryGetValue($"{tempSorted.Element}", out long val))
  883. // typeStics[$"{tempSorted.Element}"] = Convert.ToInt64(tempSorted.Score) != 0 ? val + Convert.ToInt64(tempSorted.Score) : 0;
  884. // else
  885. // typeStics.Add($"{tempSorted.Element}", Convert.ToInt64(tempSorted.Score));
  886. // }
  887. //}
  888. }
  889. ////教师数据
  890. //List<string> teacherId = new List<string>(); //教师Id集合
  891. ////查询教师的大小和教师集合信息
  892. //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") }))
  893. //{
  894. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  895. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  896. // {
  897. // totalSize += obj.GetProperty("size").GetInt64();
  898. // teacherId.Add(obj.GetProperty("id").GetString());
  899. // }
  900. //}
  901. ////查询教师已使用空间大小
  902. //foreach (var itemTeach in teacherId)
  903. //{
  904. // Dictionary<string, double?> teachStics = new Dictionary<string, double?>(); //学校空间
  905. // long blobsize = 0;
  906. // RedisValue value = default;
  907. // value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  908. // if (value != default && !value.IsNullOrEmpty)
  909. // {
  910. // JsonElement record = value.ToString().ToObject<JsonElement>();
  911. // if (record.TryGetInt64(out blobsize))
  912. // {
  913. // }
  914. // }
  915. // else
  916. // {
  917. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  918. // var size = await client.GetBlobsCatalogSize();
  919. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  920. // foreach (var key in size.Item2.Keys)
  921. // {
  922. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  923. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  924. // }
  925. // useSize += (long)size.Item1;
  926. // teachStics = size.Item2;
  927. // 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);
  928. // continue;
  929. // }
  930. // SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{itemTeach}");
  931. // if (Scores != null)
  932. // {
  933. // foreach (var score in Scores)
  934. // {
  935. // double val = score.Score;
  936. // string key = score.Element.ToString();
  937. // teachStics.Add(key, val);
  938. // }
  939. // useSize += blobsize;
  940. // 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);
  941. // continue;
  942. // }
  943. // else
  944. // {
  945. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  946. // var size = await client.GetBlobsCatalogSize();
  947. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  948. // foreach (var key in size.Item2.Keys)
  949. // {
  950. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  951. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  952. // }
  953. // useSize += (long)size.Item1;
  954. // teachStics = size.Item2;
  955. // 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);
  956. // continue;
  957. // }
  958. // //RedisValue tempValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  959. // //if (!tempValue.IsNullOrEmpty)
  960. // //{
  961. // // useSize += Convert.ToInt64(tempValue);
  962. // // //JsonElement record = tempValue.ToString().ToObject<JsonElement>();
  963. // // //long tempSize = 0;
  964. // // //if (record.TryGetInt64(out tempSize))
  965. // // //{
  966. // // // sizeT += tempSize;
  967. // // //}
  968. // //}
  969. // //SortedSetEntry[] tempSorted = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemTeach}");
  970. // //if (tempSorted != null)
  971. // //{
  972. // // foreach (var itemSorted in tempSorted)
  973. // // {
  974. // // if (typeStics.TryGetValue($"{itemSorted.Element}", out long val))
  975. // // typeStics[$"{itemSorted.Element}"] = Convert.ToInt64(itemSorted.Score) != 0 ? val + Convert.ToInt64(itemSorted.Score) : 0;
  976. // // else
  977. // // typeStics.Add($"{itemSorted.Element}", Convert.ToInt64(itemSorted.Score));
  978. // // }
  979. // //}
  980. //}
  981. return Ok(new { state = 200, totalSize, teach, useSize, stics = typeStics1.ToList() });
  982. }
  983. catch (Exception ex)
  984. {
  985. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-datatypestics \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  986. return BadRequest();
  987. }
  988. }
  989. /// <summary>
  990. /// 统计教师的空间类型
  991. /// </summary>
  992. /// <returns></returns>
  993. [HttpPost("get-teachetypestics")]
  994. public async Task<IActionResult> GetTeachDataTypeStics()
  995. {
  996. try
  997. {
  998. long teacheSize = 0; //教师空间
  999. long sizeT = 0; //教师空间
  1000. List<string> teacherId = new List<string>(); //教师Id集合
  1001. Dictionary<string, long> typeStics = new Dictionary<string, long>(); //所有类型
  1002. var cosmosClient = _azureCosmos.GetCosmosClient();
  1003. //查询教师的大小和教师集合信息
  1004. 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") }))
  1005. {
  1006. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  1007. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  1008. {
  1009. teacheSize += obj.GetProperty("size").GetInt64();
  1010. teacherId.Add(obj.GetProperty("id").GetString());
  1011. }
  1012. }
  1013. //查询教师已使用空间大小
  1014. foreach (var itemTeach in teacherId)
  1015. {
  1016. RedisValue tempValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  1017. if (!tempValue.IsNullOrEmpty)
  1018. {
  1019. sizeT += Convert.ToInt64(tempValue);
  1020. //JsonElement record = tempValue.ToString().ToObject<JsonElement>();
  1021. //long tempSize = 0;
  1022. //if (record.TryGetInt64(out tempSize))
  1023. //{
  1024. // sizeT += tempSize;
  1025. //}
  1026. }
  1027. SortedSetEntry[] tempSorted = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemTeach}");
  1028. if (tempSorted != null)
  1029. {
  1030. foreach (var itemSorted in tempSorted)
  1031. {
  1032. if (typeStics.TryGetValue($"{itemSorted.Element}", out long val))
  1033. typeStics[$"{itemSorted.Element}"] = Convert.ToInt64(itemSorted.Score) != 0 ? val + Convert.ToInt64(itemSorted.Score) : 0;
  1034. else
  1035. typeStics.Add($"{itemSorted.Element}", Convert.ToInt64(itemSorted.Score));
  1036. }
  1037. }
  1038. }
  1039. return Ok(new { state = 200, teacheSize, sizeT, typeStics });
  1040. }
  1041. catch (Exception ex)
  1042. {
  1043. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-datatypestics \n {ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
  1044. return BadRequest();
  1045. }
  1046. }
  1047. /// <summary>
  1048. /// 存放的类型
  1049. /// </summary>
  1050. public record AllDataType
  1051. {
  1052. public long vote { get; set; }/// 投票josn
  1053. public long thum { get; set; }//视频和封面缩略图
  1054. public long avatar { get; set; }//学生头像
  1055. public long test { get; set; }//
  1056. public long survey { get; set; }//问卷调查 josn
  1057. public long image { get; set; }// PNG
  1058. public long other { get; set; }//其他
  1059. public long doc { get; set; }//文档
  1060. public long homework { get; set; }//作业 json
  1061. public long item { get; set; }// 题库 json
  1062. public long paper { get; set; }//试卷 json
  1063. public long train { get; set; }// 研修
  1064. public long res { get; set; }// 教程 htx
  1065. public long exam { get; set; }//评测 json
  1066. public long syllabus { get; set; }// 课纲 json
  1067. public long video { get; set; }// 视频
  1068. public long yxpt { get; set; }//研修平台
  1069. public long jyzx { get; set; }// 教研中心
  1070. public long records { get; set; } //HiTeach上传的数据
  1071. }
  1072. /// <summary>
  1073. /// 省的数据标准
  1074. /// </summary>
  1075. public record ProvinceStics
  1076. {
  1077. /// <summary>
  1078. /// 省份ID
  1079. /// </summary>
  1080. public string provCode { get; set; }
  1081. /// <summary>
  1082. /// 省份名称
  1083. /// </summary>
  1084. public string provName { get; set; }
  1085. /// <summary>
  1086. /// 省份下的方案数量
  1087. /// </summary>
  1088. public int standardCount { get; set; }
  1089. /// <summary>
  1090. /// 省份下的学校
  1091. /// </summary>
  1092. public int schoolCount { get; set; }
  1093. /// <summary>
  1094. /// 省份下的教师数量
  1095. /// </summary>
  1096. public int teacherCount { get; set; }
  1097. /// <summary>
  1098. /// 省份下的学生数量
  1099. /// </summary>
  1100. public int studentCount { get; set; }
  1101. }
  1102. /// <summary>
  1103. /// 省以下的区域标准
  1104. /// </summary>
  1105. public record ProvinceStandard
  1106. {
  1107. /// <summary>
  1108. /// 城市Code
  1109. /// </summary>
  1110. public string provCode { get; set; }
  1111. /// <summary>
  1112. /// 城市名称
  1113. /// </summary>
  1114. public string provName { get; set; }
  1115. /// <summary>
  1116. /// 区域标准
  1117. /// </summary>
  1118. public string standard { get; set; }
  1119. }
  1120. public record PowerStics
  1121. {
  1122. public string powerName { get; set; }
  1123. public int powerCount { get; set; }
  1124. }
  1125. /// <summary>
  1126. /// 市的学校数据,及以下数据
  1127. /// </summary>
  1128. public record SticsCitys
  1129. {
  1130. /// <summary>
  1131. /// 市级Code
  1132. /// </summary>
  1133. public string cityCode { get; set; }
  1134. /// <summary>
  1135. /// 市级名称
  1136. /// </summary>
  1137. public string cityName { get; set; }
  1138. /// <summary>
  1139. /// 学校数量
  1140. /// </summary>
  1141. public int schoolCount { get; set; }
  1142. /// <summary>
  1143. /// 市级下的区域,学校、
  1144. /// </summary>
  1145. public List<DistrictStics> districtSticss { get; set; }
  1146. }
  1147. /// <summary>
  1148. /// 所有区域标准
  1149. /// </summary>
  1150. public record AllCityStics
  1151. {
  1152. /// <summary>
  1153. /// 城市Code
  1154. /// </summary>
  1155. public string cityCode { get; set; }
  1156. /// <summary>
  1157. /// 城市名称
  1158. /// </summary>
  1159. public string cityName { get; set; }
  1160. /// <summary>
  1161. /// 区名称
  1162. /// </summary>
  1163. public string distName { get; set; }
  1164. /// <summary>
  1165. /// 区域标准
  1166. /// </summary>
  1167. public string standard { get; set; }
  1168. }
  1169. /// <summary>
  1170. /// 统计区域数量
  1171. /// </summary>
  1172. public record DistrictStics
  1173. {
  1174. /// <summary>
  1175. /// 市级名称
  1176. /// </summary>
  1177. public string cityName { get; set; }
  1178. /// <summary>
  1179. /// 地区名称
  1180. /// </summary>
  1181. public string distName { get; set; }
  1182. /// <summary>
  1183. /// 学校数量
  1184. /// </summary>
  1185. public int schoolCount { get; set; }
  1186. /// <summary>
  1187. /// 教师数量
  1188. /// </summary>
  1189. public int teacherCount { get; set; }
  1190. /// <summary>
  1191. /// 学生数量
  1192. /// </summary>
  1193. public int studentCount { get; set; }
  1194. }
  1195. /// <summary>
  1196. /// 区域标准
  1197. /// </summary>
  1198. public record DistrictStandard()
  1199. {
  1200. /// <summary>
  1201. /// 市名称
  1202. /// </summary>
  1203. public string cityName { get; set; }
  1204. /// <summary>
  1205. /// 区域名称
  1206. /// </summary>
  1207. public string distName { get; set; }
  1208. /// <summary>
  1209. /// 区域标准
  1210. /// </summary>
  1211. public string standard { get; set; }
  1212. }
  1213. /// <summary>
  1214. /// 市级学校数量
  1215. /// </summary>
  1216. public record CitySchool
  1217. {
  1218. /// <summary>
  1219. /// 市级ID
  1220. /// </summary>
  1221. public string cityCode { get; set; }
  1222. /// <summary>
  1223. /// 市级名称
  1224. /// </summary>
  1225. public string cityName { get; set; }
  1226. /// <summary>
  1227. /// 市级学校数量
  1228. /// </summary>
  1229. public int schoolCount { get; set;}
  1230. }
  1231. /// <summary>
  1232. /// 市级信息标准
  1233. /// </summary>
  1234. public record CityStandard
  1235. {
  1236. /// <summary>
  1237. /// 城市Code
  1238. /// </summary>
  1239. public string cityCode { get; set; }
  1240. /// <summary>
  1241. /// 城市名称
  1242. /// </summary>
  1243. public string cityName { get; set; }
  1244. /// <summary>
  1245. /// 区域标准
  1246. /// </summary>
  1247. public string standard { get; set; }
  1248. }
  1249. /// <summary>
  1250. /// 区域
  1251. /// </summary>
  1252. public record SticsCity
  1253. {
  1254. /// <summary>
  1255. /// 省份
  1256. /// </summary>
  1257. public string provName { get; set; }
  1258. /// <summary>
  1259. /// 市名称
  1260. /// </summary>
  1261. public string cityName { get; set; }
  1262. /// <summary>
  1263. /// 区域名称
  1264. /// </summary>
  1265. public string distName { get; set; }
  1266. /// <summary>
  1267. /// 区域标准
  1268. /// </summary>
  1269. public string standard { get; set; }
  1270. }
  1271. /// <summary>
  1272. /// 返回统计数据
  1273. /// </summary>
  1274. public record SticsDist
  1275. {
  1276. /// <summary>
  1277. /// 省份
  1278. /// </summary>
  1279. public string provName { get; set; }
  1280. /// <summary>
  1281. /// 市名
  1282. /// </summary>
  1283. public string cityName { get; set; }
  1284. /// <summary>
  1285. /// 区域标准名称
  1286. /// </summary>
  1287. public string distName { get; set; }
  1288. /// <summary>
  1289. /// 区域标准
  1290. /// </summary>
  1291. public string standard { get; set; }
  1292. /// <summary>
  1293. /// 学校数量
  1294. /// </summary>
  1295. public int schoolCount { get; set; }
  1296. /// <summary>
  1297. /// 教师人数
  1298. /// </summary>
  1299. public int teacherCount { get; set; }
  1300. /// <summary>
  1301. /// 学生人数
  1302. /// </summary>
  1303. public int studentCount { get; set; }
  1304. }
  1305. }
  1306. }