HomeStatisController.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  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. using TEAMModelOS.SDK.Context.BI;
  18. namespace TEAMModelBI.Controllers.BIHome
  19. {
  20. [Route("homestatis")]
  21. [ApiController]
  22. public class HomeStatisController : ControllerBase
  23. {
  24. private readonly AzureCosmosFactory _azureCosmos;
  25. private readonly AzureStorageFactory _azureStorage;
  26. private readonly DingDing _dingDing;
  27. private readonly Option _option;
  28. private readonly AzureRedisFactory _azureRedis;
  29. public HomeStatisController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option)
  30. {
  31. _azureCosmos = azureCosmos;
  32. _dingDing = dingDing;
  33. _option = option?.Value;
  34. _azureStorage = azureStorage;
  35. _azureRedis = azureRedis;
  36. }
  37. /// <summary>
  38. /// 获取全部教师数量、学生数量、已创校数量、全部学校总空间大小 //已对接
  39. /// </summary>
  40. /// <param name="jsonElement"></param>
  41. /// <returns></returns>
  42. [ProducesDefaultResponseType]
  43. [HttpPost("get-allnumber")]
  44. public async Task<IActionResult> GetAllNumber(JsonElement jsonElement)
  45. {
  46. try
  47. {
  48. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  49. var client = _azureCosmos.GetCosmosClient();
  50. ////分开部署,就不需要,一站多用时,取消注释
  51. //if ($"{site}".Equals(BIConst.Global))
  52. // client = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  53. //查询全部教师人数
  54. long teacherCount = await CommonFind.GetSqlValueCount(client, "Teacher", "select value(count(c.id)) from c ", "Base");
  55. //查询全部学生人数
  56. long studentCount = await CommonFind.GetSqlValueCount(client, "Student", "select value(count(c.id)) from c", "Base");
  57. //查询已创建多少学校
  58. long schoolCount = await CommonFind.GetSqlValueCount(client, "School", "select value(count(c.id)) from c ", "Base");
  59. //空间
  60. long schoolSize = await CommonFind.GetSqlValueCount(client, "School", "select value(sum(c.size)) from c ", "Base");
  61. return Ok(new { state = 200, teacherCount, studentCount, schoolCount, schoolSize });
  62. }
  63. catch (Exception ex)
  64. {
  65. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-numberpeople \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  66. return BadRequest();
  67. }
  68. }
  69. /// <summary>
  70. /// 统计所有省份下的数据 //已对接
  71. /// </summary>
  72. /// <returns></returns>
  73. [ProducesDefaultResponseType]
  74. [HttpPost("get-provincestics")]
  75. public async Task<IActionResult> GetProvinceStics(JsonElement jsonElement)
  76. {
  77. try
  78. {
  79. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  80. var cosmosClient = _azureCosmos.GetCosmosClient();
  81. //if ($"{site}".Equals(BIConst.Global))
  82. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  83. List<ProvinceStandard> standards = new();
  84. 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") }))
  85. {
  86. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  87. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  88. {
  89. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  90. while (accounts.MoveNext())
  91. {
  92. JsonElement account = accounts.Current;
  93. ProvinceStandard provinceStandard = new()
  94. {
  95. provCode = account.GetProperty("provCode").GetString(),
  96. provName = account.GetProperty("provName").GetString(),
  97. standard = account.GetProperty("standard").GetString()
  98. };
  99. standards.Add(provinceStandard);
  100. }
  101. }
  102. }
  103. List<ProvinceStics> provinceStics = new();
  104. foreach (var itemStandard in standards)
  105. {
  106. ProvinceStics tempProvinceStics = new();
  107. //ProvinceStics tempCode = new ProvinceStics();
  108. var tempCode = provinceStics.Find(x => x.provCode == itemStandard.provCode);
  109. if (tempCode != null)
  110. {
  111. string sqlSc = $"select value(c.id) from c where c.standard='{itemStandard.standard}'";
  112. List<string> schoolIds = await CommonFind.FindScIds(cosmosClient, sqlSc, "Base");
  113. tempCode.schoolCount += schoolIds.Count;
  114. if (schoolIds.Count > 0)
  115. {
  116. foreach (var itemSchool in schoolIds)
  117. {
  118. //查询学校教师人数
  119. string sqlT = $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  120. tempCode.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", sqlT, "Base");
  121. //查询学校学生人数
  122. string sqlStu = $"select value(count(c.id)) from c";
  123. tempCode.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", sqlStu, $"Base-{itemSchool}");
  124. }
  125. }
  126. var tempModel = provinceStics.Where(x => x.provCode == tempCode.provCode).FirstOrDefault();
  127. if (tempModel != null)
  128. {
  129. tempModel.schoolCount = tempCode.schoolCount;
  130. tempModel.teacherCount = tempCode.teacherCount;
  131. tempModel.studentCount = tempCode.studentCount;
  132. tempModel.standardCount +=1;
  133. }
  134. }
  135. else
  136. {
  137. tempProvinceStics.provCode = itemStandard.provCode;
  138. tempProvinceStics.provName = itemStandard.provName;
  139. tempProvinceStics.standardCount += 1;
  140. string sqlSc = $"select value(c.id) from c where c.standard='{itemStandard.standard}'";
  141. List<string> schoolIds = await CommonFind.FindScIds(cosmosClient, sqlSc, "Base");
  142. tempProvinceStics.schoolCount += schoolIds.Count;
  143. if (schoolIds.Count > 0)
  144. {
  145. foreach (var itemSchool in schoolIds)
  146. {
  147. //查询学校教师人数
  148. string sqlT = $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  149. tempProvinceStics.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", sqlT, "Base");
  150. //查询学校学生人数
  151. string sqlStu = "select value(count(c.id)) from c";
  152. tempProvinceStics.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", sqlStu, $"Base-{itemSchool}");
  153. }
  154. }
  155. provinceStics.Add(tempProvinceStics);
  156. }
  157. }
  158. return Ok(new { state = 200, provinceStics });
  159. }
  160. catch (Exception ex)
  161. {
  162. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-provincestics \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  163. return BadRequest();
  164. }
  165. }
  166. /// <summary>
  167. /// 查询所有市级下的学校数量 //已对接
  168. /// </summary>
  169. /// <returns></returns>
  170. [ProducesDefaultResponseType]
  171. [HttpPost("get-cityschool")]
  172. public async Task<IActionResult> GetCitySchool(JsonElement jsonElement)
  173. {
  174. try
  175. {
  176. //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
  177. var cosmosClient = _azureCosmos.GetCosmosClient();
  178. ////分开部署,就不需要,一站多用时,取消注释
  179. //if ($"{site}".Equals(BIConst.Global))
  180. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  181. List<CityStandard> standards = new();
  182. //查询省份区域
  183. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select c.id,c.cityCode,c.cityName,c.standard from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  184. {
  185. CityStandard Citystics = new CityStandard
  186. {
  187. areaId = item.id,
  188. cityCode = item.cityCode,
  189. cityName = item.cityName,
  190. standard = item.standard
  191. };
  192. standards.Add(Citystics);
  193. }
  194. List<CitySchool> citySchools = new(); //返回数量
  195. foreach (var itemStandrd in standards)
  196. {
  197. CitySchool citySchool = new();
  198. var tempCode = citySchools.Find(x => x.cityCode == itemStandrd.cityCode);
  199. if (tempCode != null)
  200. {
  201. //string sqlTxt = $"select count(c.id) totals from c where c.standard='{itemStandrd.standard}'";
  202. string sqlTxt = $"select value(count(c.id)) from c where c.areaId='{itemStandrd.areaId}' and c.standard='{itemStandrd.standard}'";
  203. tempCode.schoolCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlTxt, "Base");
  204. var tempModel = citySchools.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  205. if (tempModel != null)
  206. {
  207. tempModel.schoolCount = tempCode.schoolCount;
  208. }
  209. }
  210. else
  211. {
  212. citySchool.cityCode = itemStandrd.cityCode;
  213. citySchool.cityName = itemStandrd.cityName;
  214. string sqlTxt = $"select value(count(c.id)) from c where c.areaId='{itemStandrd.areaId}' and c.standard='{itemStandrd.standard}'";
  215. citySchool.schoolCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlTxt, "Base");
  216. citySchools.Add(citySchool);
  217. }
  218. }
  219. return Ok(new { state = 200, citySchools });
  220. }
  221. catch (Exception ex)
  222. {
  223. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityschool \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  224. return BadRequest();
  225. }
  226. }
  227. /// <summary>
  228. /// 依据市级ID查询区域(区、县、郡)的学校、教师、学生数量 //已对接
  229. /// </summary>
  230. /// <param name="jsonElement"></param>
  231. /// <returns></returns>
  232. [ProducesDefaultResponseType]
  233. [HttpPost("get-districtstics")]
  234. public async Task<IActionResult> GetDistrictStics(JsonElement jsonElement)
  235. {
  236. try
  237. {
  238. if (!jsonElement.TryGetProperty("cityCode", out JsonElement _cityCode)) return BadRequest();
  239. //jsonElement.TryGetProperty("site", out JsonElement site);
  240. var cosmosClient = _azureCosmos.GetCosmosClient();
  241. ////分开部署,就不需要,一站多用时,取消注释
  242. //if ($"{site}".Equals(BIConst.Global))
  243. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  244. List<DistrictStandard> districtStandards = new();
  245. //查询省份区域
  246. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select c.id,c.name,c.cityName,c.standard from c where c.cityCode='{_cityCode}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  247. {
  248. DistrictStandard districtStandard = new()
  249. {
  250. id = item.id,
  251. cityName = item.cityName,
  252. distName = item.name,
  253. standard = item.standard
  254. };
  255. districtStandards.Add(districtStandard);
  256. }
  257. List<DistrictStics> districtSticss = new(); //返回数据
  258. foreach (var itemStandrd in districtStandards)
  259. {
  260. DistrictStics districtStics = new()
  261. {
  262. cityName = itemStandrd.cityName,
  263. distName = itemStandrd.distName
  264. };
  265. //string sqlTxt = $"select c.id from c where c.standard='{itemStandrd.standard}'";
  266. string sqlTxt = $"select value(c.id) from c where c.areaId='{itemStandrd.id}' and c.standard='{itemStandrd.standard}'";
  267. List<string> schoolIds = await CommonFind.FindScIds(cosmosClient,sqlTxt, "Base");
  268. districtStics.schoolCount += schoolIds.Count;
  269. if (schoolIds.Count > 0)
  270. {
  271. foreach (var itemSchool in schoolIds)
  272. {
  273. //查询学校教师人数
  274. string sqlT = $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  275. districtStics.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", sqlT, "Base");
  276. //查询学校学生人数
  277. string sqlS = $"select value(count(c.id)) from c ";
  278. districtStics.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", sqlS, $"Base-{itemSchool}");
  279. }
  280. }
  281. districtSticss.Add(districtStics);
  282. }
  283. return Ok(new { state = 200, districtSticss });
  284. }
  285. catch (Exception ex)
  286. {
  287. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-districtstics \n {ex.Message}\n{ex.StackTrace} ", GroupNames.成都开发測試群組);
  288. return BadRequest();
  289. }
  290. }
  291. /// <summary>
  292. /// 查询市的学校数据,以及市级下的区中的学校,教师、学生数量
  293. /// </summary>
  294. /// <param name="jsonElement"></param>
  295. /// <returns></returns>
  296. [ProducesDefaultResponseType]
  297. [HttpPost("get-cityallstics")]
  298. public async Task<IActionResult> GetCityAllStics(JsonElement jsonElement)
  299. {
  300. try
  301. {
  302. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  303. var cosmosClient = _azureCosmos.GetCosmosClient();
  304. ////分开部署,就不需要,一站多用时,取消注释
  305. //if ($"{site}".Equals(BIConst.Global))
  306. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  307. List<AllCityStics> tempAllCityStics = new();
  308. //查询省份区域
  309. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: $"select c.id,c.name,c.cityCode,c.cityName,c.standard from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  310. {
  311. AllCityStics Citystics = new()
  312. {
  313. id = item.id,
  314. cityCode = item.cityCode,
  315. cityName = item.cityName,
  316. distName = item.name,
  317. standard = item.standard
  318. };
  319. tempAllCityStics.Add(Citystics);
  320. }
  321. List<SticsCitys> sticsCitys = new();
  322. foreach(var itemStandrd in tempAllCityStics)
  323. {
  324. SticsCitys citySchool = new();
  325. var tempCode = sticsCitys.Find(x => (x.cityCode) == (itemStandrd.cityCode));
  326. if (tempCode != null)
  327. {
  328. //List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.standard='{itemStandrd.standard}'", "Base");
  329. List<string> schoolIds = await CommonFind.FindScIds(cosmosClient, $"select value(c.id) from c where c.areaId='{itemStandrd.id}' and c.standard='{itemStandrd.standard}'", "Base");
  330. tempCode.schoolCount += schoolIds.Count;
  331. var tempModel = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  332. if (tempModel != null)
  333. {
  334. tempModel.schoolCount = tempCode.schoolCount;
  335. }
  336. DistrictStics districtStics = new()
  337. {
  338. cityName = itemStandrd.cityName,
  339. distName = itemStandrd.distName,
  340. schoolCount = schoolIds.Count
  341. };
  342. if (schoolIds.Count > 0)
  343. {
  344. foreach (var itemSchool in schoolIds)
  345. {
  346. //查询学校教师人数
  347. districtStics.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'", "Base");
  348. //查询学校学生人数
  349. districtStics.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", "select value( count(c.id)) from c", $"Base-{itemSchool}");
  350. }
  351. }
  352. var tempDistrict = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  353. if (tempDistrict != null)
  354. {
  355. tempDistrict.districtSticss.Add(districtStics);
  356. }
  357. }
  358. else
  359. {
  360. citySchool.cityCode = itemStandrd.cityCode;
  361. citySchool.cityName = itemStandrd.cityName;
  362. List<string> schoolIds = await CommonFind.FindScIds(cosmosClient, $"select value(c.id) from c where c.standard='{itemStandrd.standard}'","Base");
  363. citySchool.schoolCount += schoolIds.Count;
  364. List<DistrictStics> tempDistrictStics = new();
  365. if (schoolIds.Count > 0)
  366. {
  367. DistrictStics districtStics = new()
  368. {
  369. cityName = itemStandrd.cityName,
  370. distName = itemStandrd.distName,
  371. schoolCount = schoolIds.Count
  372. };
  373. foreach (var itemSchool in schoolIds)
  374. {
  375. //查询学校教师人数
  376. districtStics.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'", "Base");
  377. //查询学校学生人数
  378. districtStics.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", $"select value(count(c.id)) from c", "Base");
  379. }
  380. tempDistrictStics.Add(districtStics);
  381. }
  382. citySchool.districtSticss = tempDistrictStics;
  383. sticsCitys.Add(citySchool);
  384. }
  385. }
  386. return Ok(new { state = 200, sticsCitys });
  387. }
  388. catch (Exception ex)
  389. {
  390. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityallstics \n {ex.Message}\n{ex.StackTrace} ", GroupNames.成都开发測試群組);
  391. return BadRequest();
  392. }
  393. }
  394. /// <summary>
  395. /// 统计学校和教师空间类型 //已对接
  396. /// </summary>
  397. /// <returns></returns>
  398. [ProducesDefaultResponseType]
  399. [HttpPost("get-datatypestics")]
  400. public async Task<IActionResult> GetDataTypeStics(JsonElement jsonElement)
  401. {
  402. try
  403. {
  404. //jsonElement.TryGetProperty("site", out JsonElement site); //分开部署,就不需要,一站多用时,取消注释
  405. long totalSize = 0; //总空间
  406. long useSize = 0; //已使用空间
  407. long teach = 0; //学校已经分配给所有教师的空间大小GB。
  408. Dictionary<string, long> typeStics = new(); //所有类型
  409. Dictionary<string, double?> typeStics1 = new(); //所有类型
  410. List<string> schoolId = new();
  411. var cosmosClient = _azureCosmos.GetCosmosClient();
  412. var redisClinet = _azureRedis.GetRedisClient(8);
  413. ////分开部署,就不需要,一站多用时,取消注释
  414. //if ($"{site}".Equals(BIConst.Global))
  415. //{
  416. // cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  417. // redisClinet = _azureRedis.GetRedisClient(dbnum: 8, name: BIConst.Global);
  418. //}
  419. //查询学校空间和学校Id
  420. totalSize = await CommonFind.GetSqlValueCount(cosmosClient, "School", "select value(sum(c.size)) from c", "Base");
  421. schoolId = await CommonFind.FindScIds(cosmosClient, "select value(c.id) from c", "Base");
  422. //查询学校已使用空间大小
  423. foreach (var itemId in schoolId)
  424. {
  425. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"SELECT sum(c.size) as size FROM c ",
  426. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{itemId}") }))
  427. {
  428. var json = await JsonDocument.ParseAsync(item.ContentStream);
  429. foreach (var elmt in json.RootElement.GetProperty("Documents").EnumerateArray())
  430. {
  431. if (elmt.TryGetProperty("size", out JsonElement _size) && _size.ValueKind.Equals(JsonValueKind.Number))
  432. {
  433. teach += _size.GetInt32();
  434. break;
  435. }
  436. }
  437. }
  438. long blobsize = 0;
  439. RedisValue value = default;
  440. value = redisClinet.HashGet($"Blob:Record", itemId);
  441. if (value != default && !value.IsNullOrEmpty)
  442. {
  443. JsonElement record = value.ToString().ToObject<JsonElement>();
  444. if (record.TryGetInt64(out blobsize)){}
  445. }
  446. else
  447. {
  448. var client = _azureStorage.GetBlobContainerClient(itemId);
  449. ////分开部署,就不需要,一站多用时,取消注释
  450. //if ($"{site}".Equals(BIConst.Global))
  451. // client = _azureStorage.GetBlobContainerClient(containerName: itemId, name: BIConst.Global);
  452. var size = await client.GetBlobsCatalogSize();
  453. await redisClinet.HashSetAsync($"Blob:Record", itemId, size.Item1);
  454. foreach (var key in size.Item2.Keys)
  455. {
  456. await redisClinet.SortedSetRemoveAsync($"Blob:Catalog:{itemId}", key);
  457. await redisClinet.SortedSetIncrementAsync($"Blob:Catalog:{itemId}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  458. }
  459. useSize += size.Item1.Value;
  460. typeStics1 = typeStics1.Concat(size.Item2).GroupBy(g => g.Key).ToDictionary(k => k.Key, k => k.Sum(kvp => kvp.Value)); //lamebda表达式
  461. //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);
  462. continue;
  463. }
  464. SortedSetEntry[] Scores = redisClinet.SortedSetRangeByScoreWithScores($"Blob:Catalog:{itemId}");
  465. if (Scores != null)
  466. {
  467. Dictionary<string, double?> schoolStics = new(); //学校空间
  468. foreach (var score in Scores)
  469. {
  470. double val = score.Score;
  471. string key = score.Element.ToString();
  472. schoolStics.Add(key, val);
  473. }
  474. useSize += blobsize;
  475. typeStics1 = typeStics1.Concat(schoolStics).GroupBy(g => g.Key).ToDictionary(k => k.Key, k => k.Sum(kvp => kvp.Value)); //lamebda表达式
  476. continue;
  477. }
  478. else
  479. {
  480. var client = _azureStorage.GetBlobContainerClient(itemId);
  481. ////分开部署,就不需要,一站多用时,取消注释
  482. //if ($"{site}".Equals(BIConst.Global))
  483. // client = _azureStorage.GetBlobContainerClient(containerName: itemId, name: BIConst.Global);
  484. var size = await client.GetBlobsCatalogSize();
  485. await redisClinet.HashSetAsync($"Blob:Record", itemId, size.Item1);
  486. foreach (var key in size.Item2.Keys)
  487. {
  488. await redisClinet.SortedSetRemoveAsync($"Blob:Catalog:{itemId}", key);
  489. await redisClinet.SortedSetIncrementAsync($"Blob:Catalog:{itemId}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  490. }
  491. useSize += size.Item1.Value;
  492. typeStics1 = typeStics1.Concat(size.Item2).GroupBy(g => g.Key).ToDictionary(k => k.Key, k => k.Sum(kvp => kvp.Value)); //lamebda表达式
  493. //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);
  494. continue;
  495. }
  496. ////RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemId);
  497. //if (!value.IsNullOrEmpty)
  498. //{
  499. // useSize += Convert.ToInt64(value);
  500. // //JsonElement record = value.ToString().ToObject<JsonElement>();
  501. // //long tempSize = 0;
  502. // //if (record.TryGetInt64(out tempSize))
  503. // //{
  504. // // sizeS += tempSize;
  505. // //}
  506. //}
  507. //else
  508. //{
  509. // var client = _azureStorage.GetBlobContainerClient(itemId);
  510. // var size = await client.GetBlobsCatalogSize();
  511. // var temp = await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  512. // foreach (var itemKey in size.Item2.Keys)
  513. // {
  514. // var temp1 = await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", itemKey);
  515. // var temp2 = await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", itemKey, size.Item2[itemKey].HasValue ? size.Item2[itemKey].Value : 0);
  516. // useSize += Convert.ToInt64(temp2);
  517. // }
  518. //}
  519. //SortedSetEntry[] sortedSetEntries = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemId}");
  520. //if (sortedSetEntries != null)
  521. //{
  522. // foreach (var tempSorted in sortedSetEntries)
  523. // {
  524. // if (typeStics.TryGetValue($"{tempSorted.Element}", out long val))
  525. // typeStics[$"{tempSorted.Element}"] = Convert.ToInt64(tempSorted.Score) != 0 ? val + Convert.ToInt64(tempSorted.Score) : 0;
  526. // else
  527. // typeStics.Add($"{tempSorted.Element}", Convert.ToInt64(tempSorted.Score));
  528. // }
  529. //}
  530. }
  531. ////教师数据
  532. //List<string> teacherId = new List<string>(); //教师Id集合
  533. ////查询教师的大小和教师集合信息
  534. //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") }))
  535. //{
  536. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  537. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  538. // {
  539. // totalSize += obj.GetProperty("size").GetInt64();
  540. // teacherId.Add(obj.GetProperty("id").GetString());
  541. // }
  542. //}
  543. ////查询教师已使用空间大小
  544. //foreach (var itemTeach in teacherId)
  545. //{
  546. // Dictionary<string, double?> teachStics = new Dictionary<string, double?>(); //学校空间
  547. // long blobsize = 0;
  548. // RedisValue value = default;
  549. // value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  550. // if (value != default && !value.IsNullOrEmpty)
  551. // {
  552. // JsonElement record = value.ToString().ToObject<JsonElement>();
  553. // if (record.TryGetInt64(out blobsize))
  554. // {
  555. // }
  556. // }
  557. // else
  558. // {
  559. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  560. // var size = await client.GetBlobsCatalogSize();
  561. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  562. // foreach (var key in size.Item2.Keys)
  563. // {
  564. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  565. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  566. // }
  567. // useSize += (long)size.Item1;
  568. // teachStics = size.Item2;
  569. // 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);
  570. // continue;
  571. // }
  572. // SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{itemTeach}");
  573. // if (Scores != null)
  574. // {
  575. // foreach (var score in Scores)
  576. // {
  577. // double val = score.Score;
  578. // string key = score.Element.ToString();
  579. // teachStics.Add(key, val);
  580. // }
  581. // useSize += blobsize;
  582. // 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);
  583. // continue;
  584. // }
  585. // else
  586. // {
  587. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  588. // var size = await client.GetBlobsCatalogSize();
  589. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  590. // foreach (var key in size.Item2.Keys)
  591. // {
  592. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  593. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  594. // }
  595. // useSize += (long)size.Item1;
  596. // teachStics = size.Item2;
  597. // 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);
  598. // continue;
  599. // }
  600. // //RedisValue tempValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  601. // //if (!tempValue.IsNullOrEmpty)
  602. // //{
  603. // // useSize += Convert.ToInt64(tempValue);
  604. // // //JsonElement record = tempValue.ToString().ToObject<JsonElement>();
  605. // // //long tempSize = 0;
  606. // // //if (record.TryGetInt64(out tempSize))
  607. // // //{
  608. // // // sizeT += tempSize;
  609. // // //}
  610. // //}
  611. // //SortedSetEntry[] tempSorted = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemTeach}");
  612. // //if (tempSorted != null)
  613. // //{
  614. // // foreach (var itemSorted in tempSorted)
  615. // // {
  616. // // if (typeStics.TryGetValue($"{itemSorted.Element}", out long val))
  617. // // typeStics[$"{itemSorted.Element}"] = Convert.ToInt64(itemSorted.Score) != 0 ? val + Convert.ToInt64(itemSorted.Score) : 0;
  618. // // else
  619. // // typeStics.Add($"{itemSorted.Element}", Convert.ToInt64(itemSorted.Score));
  620. // // }
  621. // //}
  622. //}
  623. return Ok(new { state = 200, totalSize, teach, useSize, stics = typeStics1.ToList() });
  624. }
  625. catch (Exception ex)
  626. {
  627. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-datatypestics \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  628. return BadRequest();
  629. }
  630. }
  631. /// <summary>
  632. /// 省的数据标准
  633. /// </summary>
  634. public record ProvinceStics
  635. {
  636. /// <summary>
  637. /// 省份ID
  638. /// </summary>
  639. public string provCode { get; set; }
  640. /// <summary>
  641. /// 省份名称
  642. /// </summary>
  643. public string provName { get; set; }
  644. /// <summary>
  645. /// 省份下的方案数量
  646. /// </summary>
  647. public int standardCount { get; set; }
  648. /// <summary>
  649. /// 省份下的学校
  650. /// </summary>
  651. public int schoolCount { get; set; }
  652. /// <summary>
  653. /// 省份下的教师数量
  654. /// </summary>
  655. public int teacherCount { get; set; }
  656. /// <summary>
  657. /// 省份下的学生数量
  658. /// </summary>
  659. public int studentCount { get; set; }
  660. }
  661. /// <summary>
  662. /// 省以下的区域标准
  663. /// </summary>
  664. public record ProvinceStandard
  665. {
  666. /// <summary>
  667. /// 城市Code
  668. /// </summary>
  669. public string provCode { get; set; }
  670. /// <summary>
  671. /// 城市名称
  672. /// </summary>
  673. public string provName { get; set; }
  674. /// <summary>
  675. /// 区域标准
  676. /// </summary>
  677. public string standard { get; set; }
  678. }
  679. /// <summary>
  680. /// 市的学校数据,及以下数据
  681. /// </summary>
  682. public record SticsCitys
  683. {
  684. /// <summary>
  685. /// 市级Code
  686. /// </summary>
  687. public string cityCode { get; set; }
  688. /// <summary>
  689. /// 市级名称
  690. /// </summary>
  691. public string cityName { get; set; }
  692. /// <summary>
  693. /// 学校数量
  694. /// </summary>
  695. public int schoolCount { get; set; }
  696. /// <summary>
  697. /// 市级下的区域,学校、
  698. /// </summary>
  699. public List<DistrictStics> districtSticss { get; set; }
  700. }
  701. /// <summary>
  702. /// 所有区域标准
  703. /// </summary>
  704. public record AllCityStics
  705. {
  706. /// <summary>
  707. /// 区域ID
  708. /// </summary>
  709. public string id { get; set; }
  710. /// <summary>
  711. /// 城市Code
  712. /// </summary>
  713. public string cityCode { get; set; }
  714. /// <summary>
  715. /// 城市名称
  716. /// </summary>
  717. public string cityName { get; set; }
  718. /// <summary>
  719. /// 区名称
  720. /// </summary>
  721. public string distName { get; set; }
  722. /// <summary>
  723. /// 区域标准
  724. /// </summary>
  725. public string standard { get; set; }
  726. }
  727. /// <summary>
  728. /// 统计区域数量
  729. /// </summary>
  730. public record DistrictStics
  731. {
  732. /// <summary>
  733. /// 市级名称
  734. /// </summary>
  735. public string cityName { get; set; }
  736. /// <summary>
  737. /// 地区名称
  738. /// </summary>
  739. public string distName { get; set; }
  740. /// <summary>
  741. /// 学校数量
  742. /// </summary>
  743. public int schoolCount { get; set; }
  744. /// <summary>
  745. /// 教师数量
  746. /// </summary>
  747. public int teacherCount { get; set; }
  748. /// <summary>
  749. /// 学生数量
  750. /// </summary>
  751. public int studentCount { get; set; }
  752. }
  753. /// <summary>
  754. /// 区域标准
  755. /// </summary>
  756. public record DistrictStandard()
  757. {
  758. /// <summary>
  759. /// 区域标准
  760. /// </summary>
  761. public string id { get; set; }
  762. /// <summary>
  763. /// 市名称
  764. /// </summary>
  765. public string cityName { get; set; }
  766. /// <summary>
  767. /// 区域名称
  768. /// </summary>
  769. public string distName { get; set; }
  770. /// <summary>
  771. /// 区域标准
  772. /// </summary>
  773. public string standard { get; set; }
  774. }
  775. /// <summary>
  776. /// 市级学校数量
  777. /// </summary>
  778. public record CitySchool
  779. {
  780. /// <summary>
  781. /// 市级ID
  782. /// </summary>
  783. public string cityCode { get; set; }
  784. /// <summary>
  785. /// 市级名称
  786. /// </summary>
  787. public string cityName { get; set; }
  788. /// <summary>
  789. /// 市级学校数量
  790. /// </summary>
  791. public int schoolCount { get; set;}
  792. }
  793. /// <summary>
  794. /// 市级信息标准
  795. /// </summary>
  796. public record CityStandard
  797. {
  798. public string areaId { get; set; }
  799. /// <summary>
  800. /// 城市Code
  801. /// </summary>
  802. public string cityCode { get; set; }
  803. /// <summary>
  804. /// 城市名称
  805. /// </summary>
  806. public string cityName { get; set; }
  807. /// <summary>
  808. /// 区域标准
  809. /// </summary>
  810. public string standard { get; set; }
  811. }
  812. }
  813. }