HomeStatisController.cs 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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. namespace TEAMModeBI.Controllers.BIHome
  15. {
  16. [Route("homestatis")]
  17. [ApiController]
  18. public class HomeStatisController : ControllerBase
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly AzureStorageFactory _azureStorage;
  22. private readonly DingDing _dingDing;
  23. private readonly Option _option;
  24. public HomeStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option, AzureStorageFactory azureStorage)
  25. {
  26. _azureCosmos = azureCosmos;
  27. _dingDing = dingDing;
  28. _option = option?.Value;
  29. _azureStorage = azureStorage;
  30. }
  31. /// <summary>
  32. /// 获取全部教师数量、学生数量、已创校数量、全部学校总空间大小
  33. /// </summary>
  34. /// <param name="jsonElement"></param>
  35. /// <returns></returns>
  36. [ProducesDefaultResponseType]
  37. [HttpPost("get-allnumber")]
  38. public async Task<IActionResult> GetAllNumber()
  39. {
  40. try
  41. {
  42. var client = _azureCosmos.GetCosmosClient();
  43. //依据学校查询教师人数
  44. List<string> teacherCount_list = new();
  45. //依据学校查询学生信息
  46. List<string> studentCount_List = new();
  47. //学校数
  48. List<string> schoolCount_List = new();
  49. //学校空间大小
  50. int schoolsize = 0;
  51. //查询全部教师人数
  52. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select * from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  53. {
  54. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  55. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  56. {
  57. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  58. while (accounts.MoveNext())
  59. {
  60. JsonElement account = accounts.Current;
  61. teacherCount_list.Add(account.GetProperty("id").GetString());
  62. }
  63. }
  64. }
  65. //查询全部学生人数
  66. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.pk='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. studentCount_List.Add(account.GetProperty("id").GetString());
  76. }
  77. }
  78. }
  79. //查询已创建多少学校
  80. 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") }))
  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. schoolCount_List.Add(account.GetProperty("id").GetString());
  90. schoolsize += int.Parse(account.GetProperty("size").ToString());
  91. }
  92. }
  93. }
  94. return Ok(new { state = 200, teacherCount = teacherCount_list.Count, studentCount = studentCount_List.Count, schoolCount = schoolCount_List.Count, schoolSize = schoolsize });
  95. }
  96. catch (Exception ex)
  97. {
  98. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-numberpeople \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  99. return BadRequest();
  100. }
  101. }
  102. /// <summary>
  103. /// 获取其它类型的统计:问卷调查、评量检测、投票活动
  104. /// </summary>
  105. /// <param name="jsonElement"></param>
  106. /// <returns></returns>
  107. [ProducesDefaultResponseType]
  108. [HttpPost("get-othertypes")]
  109. public async Task<IActionResult> GetOtherTypes(JsonElement jsonElement)
  110. {
  111. try
  112. {
  113. if (!jsonElement.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  114. int surveyJoinCount = 0; //调查参加数量
  115. int surveyDoneCount = 0; //调查完成数量
  116. int surveyAreaJoinCount = 0; //调查区域参加数量
  117. int surveyAreaDoneCount = 0; //调查区域完成数量
  118. int examJoinCount = 0; //评量检测参加数量
  119. int examDoneCount = 0; //评量检测完成数量
  120. int examAreaJoinCount = 0; //评量检测区域参加数量
  121. int examAreaDoneCount = 0; //评量检测区域完成数量
  122. int voteJoinCount = 0; //投票参加数量
  123. int voteDoneCount = 0; //投票完成数量
  124. int voteAreaJoinCount = 0; //投票区域参加数量
  125. int voteAreaDoneCount = 0; //投票区域完成数量
  126. //问卷调查
  127. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  128. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Survey' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  129. {
  130. if (!string.IsNullOrEmpty(item.owner))
  131. {
  132. if (item.owner.Equals("school"))
  133. {
  134. surveyJoinCount += 1;
  135. if (item.taskStatus > 0)
  136. {
  137. surveyDoneCount += 1;
  138. }
  139. }
  140. else if (item.owner.Equals("area"))
  141. {
  142. surveyAreaJoinCount += 1;
  143. if (item.taskStatus > 0)
  144. {
  145. surveyAreaDoneCount += 1;
  146. }
  147. }
  148. }
  149. }
  150. //评量检测
  151. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  152. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'ExamLite' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  153. {
  154. if (!string.IsNullOrEmpty(item.owner))
  155. {
  156. if (item.owner.Equals("school"))
  157. {
  158. examJoinCount += 1;
  159. if (item.taskStatus > 0)
  160. {
  161. examDoneCount += 1;
  162. }
  163. }
  164. else if (item.owner.Equals("area"))
  165. {
  166. examAreaJoinCount += 1;
  167. if (item.taskStatus > 0)
  168. {
  169. examAreaDoneCount += 1;
  170. }
  171. }
  172. }
  173. }
  174. //投票活动
  175. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  176. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Vote' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  177. {
  178. if (!string.IsNullOrEmpty(item.owner))
  179. {
  180. if (item.owner.Equals("school"))
  181. {
  182. voteJoinCount += 1;
  183. if (item.taskStatus > 0)
  184. {
  185. voteDoneCount += 1;
  186. }
  187. }
  188. else if (item.owner.Equals("area"))
  189. {
  190. voteAreaJoinCount += 1;
  191. if (item.taskStatus > 0)
  192. {
  193. voteAreaDoneCount += 1;
  194. }
  195. }
  196. }
  197. }
  198. return Ok(new { state = 200, surveyJoinCount, surveyDoneCount, surveyAreaJoinCount, surveyAreaDoneCount, examJoinCount, examDoneCount, examAreaJoinCount, examAreaDoneCount, voteJoinCount, voteDoneCount, voteAreaJoinCount, voteAreaDoneCount });
  199. }
  200. catch (Exception ex)
  201. {
  202. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-othertypes \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  203. return BadRequest();
  204. }
  205. }
  206. /// <summary>
  207. /// 查询所有区级信息统计数据
  208. /// </summary>
  209. /// <param name="jsonElement"></param>
  210. /// <returns></returns>
  211. [ProducesDefaultResponseType]
  212. [HttpPost("get-alldiststics")]
  213. public async Task<IActionResult> GetAllDistStics()
  214. {
  215. try
  216. {
  217. var cosmosClient = _azureCosmos.GetCosmosClient();
  218. List<SticsCity> standards = new List<SticsCity>();
  219. StringBuilder stringBuder = new StringBuilder("select c.name,c.provName,c.cityName,c.standard from c");
  220. //查询省份区域
  221. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: stringBuder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  222. {
  223. SticsCity sticsCity = new SticsCity
  224. {
  225. provName = item.provName,
  226. cityName = item.cityName,
  227. distName = item.name,
  228. standard = item.standard
  229. };
  230. standards.Add(sticsCity);
  231. }
  232. List<SticsDist> sticsDists = new List<SticsDist>();
  233. //查询所有下面的学校数量
  234. foreach (var itemStandrds in standards)
  235. {
  236. SticsDist sticsDist = new SticsDist();
  237. sticsDist.provName = itemStandrds.provName;
  238. sticsDist.cityName = itemStandrds.cityName;
  239. sticsDist.distName = itemStandrds.distName;
  240. sticsDist.standard = itemStandrds.standard;
  241. List<string> schoolIds = new List<string>();
  242. int schoolCount = 0;
  243. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.standard='{itemStandrds.standard}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  244. {
  245. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  246. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  247. {
  248. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  249. while (accounts.MoveNext())
  250. {
  251. JsonElement account = accounts.Current;
  252. schoolIds.Add(account.GetProperty("id").GetString());
  253. schoolCount += 1;
  254. }
  255. }
  256. }
  257. sticsDist.schoolCount = schoolCount;
  258. int teacherCount = 0; //教师数量
  259. int studentCount = 0; //学生数量
  260. List<string> teacherIds = new List<string>();
  261. List<string> studentIds = new List<string>();
  262. //查询学校下面的教师人数
  263. foreach (var itemSchool in schoolIds)
  264. {
  265. string sqlTeacherTxt = $"select distinct value(c) from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  266. //查询全部教师人数
  267. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: sqlTeacherTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  268. {
  269. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  270. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  271. {
  272. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  273. while (accounts.MoveNext())
  274. {
  275. //JsonElement account = accounts.Current;
  276. //teacherIds.Add(account.GetProperty("id").GetString());
  277. teacherCount += 1;
  278. }
  279. }
  280. }
  281. //查询全部学生人数
  282. 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}") }))
  283. {
  284. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  285. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  286. {
  287. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  288. while (accounts.MoveNext())
  289. {
  290. //JsonElement account = accounts.Current;
  291. //studentIds.Add(account.GetProperty("id").GetString());
  292. studentCount += 1;
  293. }
  294. }
  295. }
  296. }
  297. sticsDist.teacherCount = teacherCount;
  298. sticsDist.studentCount = studentCount;
  299. sticsDists.Add(sticsDist);
  300. }
  301. return Ok(new { state = 200, sticsDists });
  302. }
  303. catch (Exception ex)
  304. {
  305. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-alldiststics \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  306. return BadRequest();
  307. }
  308. }
  309. /// <summary>
  310. /// 依据城市Code查询学校,教师数量,学生数量
  311. /// </summary>
  312. /// <param name="jsonElement"></param>
  313. /// <returns></returns>
  314. [ProducesDefaultResponseType]
  315. [HttpPost("get-citystics")]
  316. public async Task<IActionResult> GetCityStics(JsonElement jsonElement)
  317. {
  318. try
  319. {
  320. if(!jsonElement.TryGetProperty("cityCode", out JsonElement _cityCode)) return BadRequest();
  321. var cosmosClient = _azureCosmos.GetCosmosClient();
  322. List<string> areaStandard = new List<string>();
  323. //查询省份区域
  324. 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") }))
  325. {
  326. areaStandard.Add(item.standard);
  327. }
  328. int citySchoolCount = 0;
  329. List<string> teacherIds = new List<string>();
  330. List<string> studentIds = new List<string>();
  331. foreach (var tempSatndard in areaStandard)
  332. {
  333. List<string> schoolIds = new List<string>();
  334. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.standard='{tempSatndard}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  335. {
  336. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  337. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  338. {
  339. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  340. while (accounts.MoveNext())
  341. {
  342. JsonElement account = accounts.Current;
  343. schoolIds.Add(account.GetProperty("id").GetString());
  344. citySchoolCount += 1;
  345. }
  346. }
  347. }
  348. //查询学校下面的教师人数
  349. foreach (var itemSchool in schoolIds)
  350. {
  351. //查询全部教师人数
  352. 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") }))
  353. {
  354. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  355. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  356. {
  357. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  358. while (accounts.MoveNext())
  359. {
  360. JsonElement account = accounts.Current;
  361. teacherIds.Add(account.GetProperty("id").GetString());
  362. }
  363. }
  364. }
  365. //查询全部学生人数
  366. 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}") }))
  367. {
  368. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  369. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  370. {
  371. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  372. while (accounts.MoveNext())
  373. {
  374. JsonElement account = accounts.Current;
  375. studentIds.Add(account.GetProperty("id").GetString());
  376. }
  377. }
  378. }
  379. }
  380. }
  381. return Ok(new { state = 200, schoolCount = citySchoolCount, teacherCount = teacherIds.Count, tudentCount = studentIds.Count });
  382. }
  383. catch (Exception ex)
  384. {
  385. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-citystics \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  386. return BadRequest();
  387. }
  388. }
  389. /// <summary>
  390. /// 查询所有市级下的学校数量
  391. /// </summary>
  392. /// <returns></returns>
  393. [ProducesDefaultResponseType]
  394. [HttpPost("get-cityschool")]
  395. public async Task<IActionResult> GetCitySchool()
  396. {
  397. try
  398. {
  399. var cosmosClient = _azureCosmos.GetCosmosClient();
  400. List<CityStandard> standards = new List<CityStandard>();
  401. //查询省份区域
  402. 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") }))
  403. {
  404. CityStandard Citystics = new CityStandard
  405. {
  406. cityCode = item.cityCode,
  407. cityName = item.cityName,
  408. standard = item.standard
  409. };
  410. standards.Add(Citystics);
  411. }
  412. List<CitySchool> citySchools = new List<CitySchool>(); //返回数量
  413. foreach (var itemStandrd in standards)
  414. {
  415. CitySchool citySchool = new CitySchool();
  416. var tempCode = citySchools.Find(x => x.cityCode == itemStandrd.cityCode);
  417. if (tempCode != null)
  418. {
  419. 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") }))
  420. {
  421. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  422. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  423. {
  424. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  425. while (accounts.MoveNext())
  426. {
  427. JsonElement account = accounts.Current;
  428. tempCode.schoolCount += 1;
  429. }
  430. }
  431. }
  432. var tempModel = citySchools.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  433. if (tempModel != null)
  434. {
  435. tempModel.schoolCount = tempCode.schoolCount;
  436. }
  437. }
  438. else
  439. {
  440. citySchool.cityCode = itemStandrd.cityCode;
  441. citySchool.cityName = itemStandrd.cityName;
  442. 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") }))
  443. {
  444. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  445. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  446. {
  447. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  448. while (accounts.MoveNext())
  449. {
  450. JsonElement account = accounts.Current;
  451. citySchool.schoolCount += 1;
  452. }
  453. }
  454. }
  455. citySchools.Add(citySchool);
  456. }
  457. }
  458. return Ok(new { state = 200, citySchools });
  459. }
  460. catch (Exception ex)
  461. {
  462. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityschool \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  463. return BadRequest();
  464. }
  465. }
  466. /// <summary>
  467. /// 依据市级ID查询区域(区、县、郡)的学校、教师、学生数量
  468. /// </summary>
  469. /// <param name="jsonElement"></param>
  470. /// <returns></returns>
  471. [ProducesDefaultResponseType]
  472. [HttpPost("get-districtstics")]
  473. public async Task<IActionResult> GetDistrictStics(JsonElement jsonElement)
  474. {
  475. try
  476. {
  477. if (!jsonElement.TryGetProperty("cityCode", out JsonElement _cityCode)) return BadRequest();
  478. var cosmosClient = _azureCosmos.GetCosmosClient();
  479. List<DistrictStandard> districtStandards = new List<DistrictStandard>();
  480. //查询省份区域
  481. 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") }))
  482. {
  483. DistrictStandard districtStandard = new DistrictStandard
  484. {
  485. cityName = item.cityName,
  486. distName = item.name,
  487. standard = item.standard
  488. };
  489. districtStandards.Add(districtStandard);
  490. }
  491. List<DistrictStics> districtSticss = new List<DistrictStics>(); //返回数据
  492. foreach (var itemStandrd in districtStandards)
  493. {
  494. DistrictStics districtStics = new DistrictStics();
  495. districtStics.cityName = itemStandrd.cityName;
  496. districtStics.distName = itemStandrd.distName;
  497. List<string> schoolIds = new List<string>();
  498. //查询区域学校数量
  499. 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") }))
  500. {
  501. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  502. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  503. {
  504. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  505. while (accounts.MoveNext())
  506. {
  507. JsonElement account = accounts.Current;
  508. schoolIds.Add(account.GetProperty("id").GetString());
  509. districtStics.schoolCount += 1;
  510. }
  511. }
  512. }
  513. if (schoolIds.Count > 0)
  514. {
  515. foreach (var itemSchool in schoolIds)
  516. {
  517. //查询学校教师人数
  518. 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") }))
  519. {
  520. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  521. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  522. {
  523. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  524. while (accounts.MoveNext())
  525. {
  526. JsonElement account = accounts.Current;
  527. districtStics.teacherCount += 1;
  528. }
  529. }
  530. }
  531. //查询学校学生人数
  532. 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}") }))
  533. {
  534. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  535. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  536. {
  537. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  538. while (accounts.MoveNext())
  539. {
  540. JsonElement account = accounts.Current;
  541. districtStics.studentCount += 1;
  542. }
  543. }
  544. }
  545. }
  546. }
  547. districtSticss.Add(districtStics);
  548. }
  549. return Ok(new { state = 200, districtSticss });
  550. }
  551. catch (Exception ex)
  552. {
  553. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-districtstics \n {ex.Message} {ex.StackTrace} ", GroupNames.醍摩豆服務運維群組);
  554. return BadRequest();
  555. }
  556. }
  557. /// <summary>
  558. /// 查询市的学校数据,以及市级下的区中的学校,教师、学生数量
  559. /// </summary>
  560. /// <param name="jsonElement"></param>
  561. /// <returns></returns>
  562. [ProducesDefaultResponseType]
  563. [HttpPost("get-cityallstics")]
  564. public async Task<IActionResult> GetCityAllStics(JsonElement jsonElement)
  565. {
  566. try
  567. {
  568. var cosmosClient = _azureCosmos.GetCosmosClient();
  569. List<AllCityStics> tempAllCityStics = new List<AllCityStics>();
  570. //查询省份区域
  571. 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") }))
  572. {
  573. AllCityStics Citystics = new AllCityStics
  574. {
  575. cityCode = item.cityCode,
  576. cityName = item.cityName,
  577. distName = item.name,
  578. standard = item.standard
  579. };
  580. tempAllCityStics.Add(Citystics);
  581. }
  582. List<SticsCitys> sticsCitys = new List<SticsCitys>();
  583. foreach(var itemStandrd in tempAllCityStics)
  584. {
  585. SticsCitys citySchool = new SticsCitys();
  586. var tempCode = sticsCitys.Find(x => (x.cityCode) == (itemStandrd.cityCode));
  587. if (tempCode != null)
  588. {
  589. List<string> schoolIds = new List<string>();
  590. try
  591. {
  592. 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") }))
  593. {
  594. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  595. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  596. {
  597. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  598. while (accounts.MoveNext())
  599. {
  600. JsonElement account = accounts.Current;
  601. schoolIds.Add(account.GetProperty("id").GetString());
  602. tempCode.schoolCount += 1;
  603. }
  604. }
  605. }
  606. }
  607. catch
  608. {
  609. }
  610. var tempModel = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  611. if (tempModel != null)
  612. {
  613. tempModel.schoolCount = tempCode.schoolCount;
  614. }
  615. DistrictStics districtStics = new DistrictStics();
  616. districtStics.cityName = itemStandrd.cityName;
  617. districtStics.distName = itemStandrd.distName;
  618. districtStics.schoolCount = schoolIds.Count;
  619. if (schoolIds.Count > 0)
  620. {
  621. foreach (var itemSchool in schoolIds)
  622. {
  623. //查询学校教师人数
  624. 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") }))
  625. {
  626. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  627. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  628. {
  629. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  630. while (accounts.MoveNext())
  631. {
  632. JsonElement account = accounts.Current;
  633. districtStics.teacherCount += 1;
  634. }
  635. }
  636. }
  637. //查询学校学生人数
  638. 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}") }))
  639. {
  640. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  641. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  642. {
  643. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  644. while (accounts.MoveNext())
  645. {
  646. JsonElement account = accounts.Current;
  647. districtStics.studentCount += 1;
  648. }
  649. }
  650. }
  651. }
  652. }
  653. var tempDistrict = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  654. if (tempDistrict != null)
  655. {
  656. tempDistrict.districtSticss.Add(districtStics);
  657. }
  658. }
  659. else
  660. {
  661. citySchool.cityCode = itemStandrd.cityCode;
  662. citySchool.cityName = itemStandrd.cityName;
  663. List<string> schoolIds = new List<string>();
  664. 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") }))
  665. {
  666. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  667. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  668. {
  669. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  670. while (accounts.MoveNext())
  671. {
  672. JsonElement account = accounts.Current;
  673. schoolIds.Add(account.GetProperty("id").GetString());
  674. citySchool.schoolCount += 1;
  675. }
  676. }
  677. }
  678. List<DistrictStics> tempDistrictStics = new List<DistrictStics>();
  679. if (schoolIds.Count > 0)
  680. {
  681. DistrictStics districtStics = new DistrictStics();
  682. districtStics.cityName = itemStandrd.cityName;
  683. districtStics.distName= itemStandrd.distName;
  684. districtStics.schoolCount = schoolIds.Count;
  685. foreach (var itemSchool in schoolIds)
  686. {
  687. //查询学校教师人数
  688. 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") }))
  689. {
  690. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  691. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  692. {
  693. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  694. while (accounts.MoveNext())
  695. {
  696. JsonElement account = accounts.Current;
  697. districtStics.teacherCount += 1;
  698. }
  699. }
  700. }
  701. //查询学校学生人数
  702. 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}") }))
  703. {
  704. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  705. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  706. {
  707. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  708. while (accounts.MoveNext())
  709. {
  710. JsonElement account = accounts.Current;
  711. districtStics.studentCount += 1;
  712. }
  713. }
  714. }
  715. }
  716. tempDistrictStics.Add(districtStics);
  717. }
  718. citySchool.districtSticss = tempDistrictStics;
  719. sticsCitys.Add(citySchool);
  720. }
  721. }
  722. return Ok(new { state = 200, sticsCitys });
  723. }
  724. catch (Exception ex)
  725. {
  726. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityallstics \n {ex.Message} {ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  727. return BadRequest();
  728. }
  729. }
  730. /// <summary>
  731. /// 查询BI权限开放人数
  732. /// </summary>
  733. /// <returns></returns>
  734. [ProducesDefaultResponseType]
  735. [HttpPost("get-sticsbipower")]
  736. public async Task<IActionResult> GetSticsBIPower()
  737. {
  738. try
  739. {
  740. Dictionary<string, object> dic = new Dictionary<string, object> { { "PartitionKey", "authority-bi" } };
  741. List<Authority> authorityBIList = await _azureStorage.FindListByDict<Authority>(dic);
  742. var cosmosClient = _azureCosmos.GetCosmosClient();
  743. List<PowerStics> powerStics = new List<PowerStics>();
  744. foreach (var temp in authorityBIList)
  745. {
  746. PowerStics tempPowerStics = new PowerStics();
  747. tempPowerStics.powerName = temp.Discription;
  748. //查询学校权限
  749. 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'";
  750. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { }))
  751. {
  752. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  753. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  754. {
  755. tempPowerStics.powerCount += 1;
  756. }
  757. }
  758. powerStics.Add(tempPowerStics);
  759. }
  760. return Ok(new { state = 200, powerStics });
  761. }
  762. catch (Exception ex)
  763. {
  764. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-sticsbipower {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  765. return BadRequest();
  766. }
  767. }
  768. public record PowerStics
  769. {
  770. public string powerName { get; set; }
  771. public int powerCount { get; set; }
  772. }
  773. /// <summary>
  774. /// 市的学校数据,及以下数据
  775. /// </summary>
  776. public record SticsCitys
  777. {
  778. /// <summary>
  779. /// 市级Code
  780. /// </summary>
  781. public string cityCode { get; set; }
  782. /// <summary>
  783. /// 市级名称
  784. /// </summary>
  785. public string cityName { get; set; }
  786. /// <summary>
  787. /// 学校数量
  788. /// </summary>
  789. public int schoolCount { get; set; }
  790. /// <summary>
  791. /// 市级下的区域,学校、
  792. /// </summary>
  793. public List<DistrictStics> districtSticss { get; set; }
  794. }
  795. /// <summary>
  796. /// 所有区域标准
  797. /// </summary>
  798. public record AllCityStics
  799. {
  800. /// <summary>
  801. /// 城市Code
  802. /// </summary>
  803. public string cityCode { get; set; }
  804. /// <summary>
  805. /// 城市名称
  806. /// </summary>
  807. public string cityName { get; set; }
  808. /// <summary>
  809. /// 区名称
  810. /// </summary>
  811. public string distName { get; set; }
  812. /// <summary>
  813. /// 区域标准
  814. /// </summary>
  815. public string standard { get; set; }
  816. }
  817. /// <summary>
  818. /// 统计区域数量
  819. /// </summary>
  820. public record DistrictStics
  821. {
  822. /// <summary>
  823. /// 市级名称
  824. /// </summary>
  825. public string cityName { get; set; }
  826. /// <summary>
  827. /// 地区名称
  828. /// </summary>
  829. public string distName { get; set; }
  830. /// <summary>
  831. /// 学校数量
  832. /// </summary>
  833. public int schoolCount { get; set; }
  834. /// <summary>
  835. /// 教师数量
  836. /// </summary>
  837. public int teacherCount { get; set; }
  838. /// <summary>
  839. /// 学生数量
  840. /// </summary>
  841. public int studentCount { get; set; }
  842. }
  843. /// <summary>
  844. /// 区域标准
  845. /// </summary>
  846. public record DistrictStandard()
  847. {
  848. /// <summary>
  849. /// 市名称
  850. /// </summary>
  851. public string cityName { get; set; }
  852. /// <summary>
  853. /// 区域名称
  854. /// </summary>
  855. public string distName { get; set; }
  856. /// <summary>
  857. /// 区域标准
  858. /// </summary>
  859. public string standard { get; set; }
  860. }
  861. /// <summary>
  862. /// 市级学校数量
  863. /// </summary>
  864. public record CitySchool
  865. {
  866. /// <summary>
  867. /// 市级ID
  868. /// </summary>
  869. public string cityCode { get; set; }
  870. /// <summary>
  871. /// 市级名称
  872. /// </summary>
  873. public string cityName { get; set; }
  874. /// <summary>
  875. /// 市级学校数量
  876. /// </summary>
  877. public int schoolCount { get; set;}
  878. }
  879. /// <summary>
  880. /// 市级信息标准
  881. /// </summary>
  882. public record CityStandard
  883. {
  884. /// <summary>
  885. /// 城市Code
  886. /// </summary>
  887. public string cityCode { get; set; }
  888. /// <summary>
  889. /// 城市名称
  890. /// </summary>
  891. public string cityName { get; set; }
  892. /// <summary>
  893. /// 区域标准
  894. /// </summary>
  895. public string standard { get; set; }
  896. }
  897. /// <summary>
  898. /// 区域
  899. /// </summary>
  900. public record SticsCity
  901. {
  902. /// <summary>
  903. /// 省份
  904. /// </summary>
  905. public string provName { get; set; }
  906. /// <summary>
  907. /// 市名称
  908. /// </summary>
  909. public string cityName { get; set; }
  910. /// <summary>
  911. /// 区域名称
  912. /// </summary>
  913. public string distName { get; set; }
  914. /// <summary>
  915. /// 区域标准
  916. /// </summary>
  917. public string standard { get; set; }
  918. }
  919. /// <summary>
  920. /// 返回统计数据
  921. /// </summary>
  922. public record SticsDist
  923. {
  924. /// <summary>
  925. /// 省份
  926. /// </summary>
  927. public string provName { get; set; }
  928. /// <summary>
  929. /// 市名
  930. /// </summary>
  931. public string cityName { get; set; }
  932. /// <summary>
  933. /// 区域标准名称
  934. /// </summary>
  935. public string distName { get; set; }
  936. /// <summary>
  937. /// 区域标准
  938. /// </summary>
  939. public string standard { get; set; }
  940. /// <summary>
  941. /// 学校数量
  942. /// </summary>
  943. public int schoolCount { get; set; }
  944. /// <summary>
  945. /// 教师人数
  946. /// </summary>
  947. public int teacherCount { get; set; }
  948. /// <summary>
  949. /// 学生人数
  950. /// </summary>
  951. public int studentCount { get; set; }
  952. }
  953. }
  954. }