HomeStatisController.cs 42 KB

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