HomeStatisController.cs 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915
  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 count(c.id) totals from c where c.standard='{itemStandrd.standard}'";
  213. string sqlTxt = $"select value(count(c.id)) from c where c.areaId='{itemStandrd.areaId}' and c.standard='{itemStandrd.standard}'";
  214. citySchool.schoolCount += await CommonFind.GetSqlValueCount(cosmosClient, "School", sqlTxt, "Base");
  215. citySchools.Add(citySchool);
  216. }
  217. }
  218. return Ok(new { state = 200, citySchools });
  219. }
  220. catch (Exception ex)
  221. {
  222. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityschool \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  223. return BadRequest();
  224. }
  225. }
  226. /// <summary>
  227. /// 依据市级ID查询区域(区、县、郡)的学校、教师、学生数量 //已对接
  228. /// </summary>
  229. /// <param name="jsonElement"></param>
  230. /// <returns></returns>
  231. [ProducesDefaultResponseType]
  232. [HttpPost("get-districtstics")]
  233. public async Task<IActionResult> GetDistrictStics(JsonElement jsonElement)
  234. {
  235. try
  236. {
  237. if (!jsonElement.TryGetProperty("cityCode", out JsonElement _cityCode)) return BadRequest();
  238. jsonElement.TryGetProperty("site", out JsonElement site);
  239. var cosmosClient = _azureCosmos.GetCosmosClient();
  240. if ($"{site}".Equals(BIConst.Global))
  241. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  242. List<DistrictStandard> districtStandards = new();
  243. //查询省份区域
  244. 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") }))
  245. {
  246. DistrictStandard districtStandard = new()
  247. {
  248. id = item.id,
  249. cityName = item.cityName,
  250. distName = item.name,
  251. standard = item.standard
  252. };
  253. districtStandards.Add(districtStandard);
  254. }
  255. List<DistrictStics> districtSticss = new(); //返回数据
  256. foreach (var itemStandrd in districtStandards)
  257. {
  258. DistrictStics districtStics = new()
  259. {
  260. cityName = itemStandrd.cityName,
  261. distName = itemStandrd.distName
  262. };
  263. //string sqlTxt = $"select c.id from c where c.standard='{itemStandrd.standard}'";
  264. string sqlTxt = $"select c.id from c where c.areaId='{itemStandrd.id}' and c.standard='{itemStandrd.standard}'";
  265. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient,sqlTxt, "Base");
  266. districtStics.schoolCount += schoolIds.Count;
  267. if (schoolIds.Count > 0)
  268. {
  269. foreach (var itemSchool in schoolIds)
  270. {
  271. //查询学校教师人数
  272. string sqlT = $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  273. districtStics.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", sqlT, "Base");
  274. //查询学校学生人数
  275. string sqlS = $"select value(count(c.id)) from c ";
  276. districtStics.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", sqlS, $"Base-{itemSchool}");
  277. }
  278. }
  279. districtSticss.Add(districtStics);
  280. }
  281. return Ok(new { state = 200, districtSticss });
  282. }
  283. catch (Exception ex)
  284. {
  285. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-districtstics \n {ex.Message}\n{ex.StackTrace} ", GroupNames.成都开发測試群組);
  286. return BadRequest();
  287. }
  288. }
  289. /// <summary>
  290. /// 查询市的学校数据,以及市级下的区中的学校,教师、学生数量
  291. /// </summary>
  292. /// <param name="jsonElement"></param>
  293. /// <returns></returns>
  294. [ProducesDefaultResponseType]
  295. [HttpPost("get-cityallstics")]
  296. public async Task<IActionResult> GetCityAllStics(JsonElement jsonElement)
  297. {
  298. try
  299. {
  300. jsonElement.TryGetProperty("site", out JsonElement site);
  301. var cosmosClient = _azureCosmos.GetCosmosClient();
  302. if ($"{site}".Equals(BIConst.Global))
  303. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  304. List<AllCityStics> tempAllCityStics = new();
  305. //查询省份区域
  306. 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") }))
  307. {
  308. AllCityStics Citystics = new()
  309. {
  310. id = item.id,
  311. cityCode = item.cityCode,
  312. cityName = item.cityName,
  313. distName = item.name,
  314. standard = item.standard
  315. };
  316. tempAllCityStics.Add(Citystics);
  317. }
  318. List<SticsCitys> sticsCitys = new();
  319. foreach(var itemStandrd in tempAllCityStics)
  320. {
  321. SticsCitys citySchool = new();
  322. var tempCode = sticsCitys.Find(x => (x.cityCode) == (itemStandrd.cityCode));
  323. if (tempCode != null)
  324. {
  325. //List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.standard='{itemStandrd.standard}'", "Base");
  326. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.areaId='{itemStandrd.id}' and c.standard='{itemStandrd.standard}'", "Base");
  327. tempCode.schoolCount += schoolIds.Count;
  328. var tempModel = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  329. if (tempModel != null)
  330. {
  331. tempModel.schoolCount = tempCode.schoolCount;
  332. }
  333. DistrictStics districtStics = new()
  334. {
  335. cityName = itemStandrd.cityName,
  336. distName = itemStandrd.distName,
  337. schoolCount = schoolIds.Count
  338. };
  339. if (schoolIds.Count > 0)
  340. {
  341. foreach (var itemSchool in schoolIds)
  342. {
  343. //查询学校教师人数
  344. districtStics.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'", "Base");
  345. //查询学校学生人数
  346. districtStics.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", "select value( count(c.id)) from c", $"Base-{itemSchool}");
  347. }
  348. }
  349. var tempDistrict = sticsCitys.Where(x => x.cityCode == tempCode.cityCode).FirstOrDefault();
  350. if (tempDistrict != null)
  351. {
  352. tempDistrict.districtSticss.Add(districtStics);
  353. }
  354. }
  355. else
  356. {
  357. citySchool.cityCode = itemStandrd.cityCode;
  358. citySchool.cityName = itemStandrd.cityName;
  359. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"select c.id from c where c.standard='{itemStandrd.standard}'","Base");
  360. citySchool.schoolCount += schoolIds.Count;
  361. List<DistrictStics> tempDistrictStics = new();
  362. if (schoolIds.Count > 0)
  363. {
  364. DistrictStics districtStics = new()
  365. {
  366. cityName = itemStandrd.cityName,
  367. distName = itemStandrd.distName,
  368. schoolCount = schoolIds.Count
  369. };
  370. foreach (var itemSchool in schoolIds)
  371. {
  372. //查询学校教师人数
  373. districtStics.teacherCount += await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", $"select value(count(c.id)) from c join a1 in c.schools where a1.schoolId='{itemSchool}'", "Base");
  374. //查询学校学生人数
  375. districtStics.studentCount += await CommonFind.GetSqlValueCount(cosmosClient, "Student", $"select value(count(c.id)) from c", "Base");
  376. }
  377. tempDistrictStics.Add(districtStics);
  378. }
  379. citySchool.districtSticss = tempDistrictStics;
  380. sticsCitys.Add(citySchool);
  381. }
  382. }
  383. return Ok(new { state = 200, sticsCitys });
  384. }
  385. catch (Exception ex)
  386. {
  387. await _dingDing.SendBotMsg($"BI, {_option.Location} /homestatis/get-cityallstics \n {ex.Message}\n{ex.StackTrace} ", GroupNames.成都开发測試群組);
  388. return BadRequest();
  389. }
  390. }
  391. /// <summary>
  392. /// 统计学校和教师空间类型 //已对接
  393. /// </summary>
  394. /// <returns></returns>
  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. //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);
  473. continue;
  474. }
  475. else
  476. {
  477. var client = _azureStorage.GetBlobContainerClient(itemId);
  478. if ($"{site}".Equals(BIConst.Global))
  479. {
  480. client = _azureStorage.GetBlobContainerClient(containerName: itemId, name: BIConst.Global);
  481. }
  482. var size = await client.GetBlobsCatalogSize();
  483. await redisClinet.HashSetAsync($"Blob:Record", itemId, size.Item1);
  484. foreach (var key in size.Item2.Keys)
  485. {
  486. await redisClinet.SortedSetRemoveAsync($"Blob:Catalog:{itemId}", key);
  487. await redisClinet.SortedSetIncrementAsync($"Blob:Catalog:{itemId}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  488. }
  489. useSize += size.Item1.Value;
  490. typeStics1 = typeStics1.Concat(size.Item2).GroupBy(g => g.Key).ToDictionary(k => k.Key, k => k.Sum(kvp => kvp.Value)); //lamebda表达式
  491. //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);
  492. continue;
  493. }
  494. ////RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemId);
  495. //if (!value.IsNullOrEmpty)
  496. //{
  497. // useSize += Convert.ToInt64(value);
  498. // //JsonElement record = value.ToString().ToObject<JsonElement>();
  499. // //long tempSize = 0;
  500. // //if (record.TryGetInt64(out tempSize))
  501. // //{
  502. // // sizeS += tempSize;
  503. // //}
  504. //}
  505. //else
  506. //{
  507. // var client = _azureStorage.GetBlobContainerClient(itemId);
  508. // var size = await client.GetBlobsCatalogSize();
  509. // var temp = await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemId, size.Item1);
  510. // foreach (var itemKey in size.Item2.Keys)
  511. // {
  512. // var temp1 = await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemId}", itemKey);
  513. // var temp2 = await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemId}", itemKey, size.Item2[itemKey].HasValue ? size.Item2[itemKey].Value : 0);
  514. // useSize += Convert.ToInt64(temp2);
  515. // }
  516. //}
  517. //SortedSetEntry[] sortedSetEntries = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemId}");
  518. //if (sortedSetEntries != null)
  519. //{
  520. // foreach (var tempSorted in sortedSetEntries)
  521. // {
  522. // if (typeStics.TryGetValue($"{tempSorted.Element}", out long val))
  523. // typeStics[$"{tempSorted.Element}"] = Convert.ToInt64(tempSorted.Score) != 0 ? val + Convert.ToInt64(tempSorted.Score) : 0;
  524. // else
  525. // typeStics.Add($"{tempSorted.Element}", Convert.ToInt64(tempSorted.Score));
  526. // }
  527. //}
  528. }
  529. ////教师数据
  530. //List<string> teacherId = new List<string>(); //教师Id集合
  531. ////查询教师的大小和教师集合信息
  532. //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") }))
  533. //{
  534. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  535. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  536. // {
  537. // totalSize += obj.GetProperty("size").GetInt64();
  538. // teacherId.Add(obj.GetProperty("id").GetString());
  539. // }
  540. //}
  541. ////查询教师已使用空间大小
  542. //foreach (var itemTeach in teacherId)
  543. //{
  544. // Dictionary<string, double?> teachStics = new Dictionary<string, double?>(); //学校空间
  545. // long blobsize = 0;
  546. // RedisValue value = default;
  547. // value = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  548. // if (value != default && !value.IsNullOrEmpty)
  549. // {
  550. // JsonElement record = value.ToString().ToObject<JsonElement>();
  551. // if (record.TryGetInt64(out blobsize))
  552. // {
  553. // }
  554. // }
  555. // else
  556. // {
  557. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  558. // var size = await client.GetBlobsCatalogSize();
  559. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  560. // foreach (var key in size.Item2.Keys)
  561. // {
  562. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  563. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  564. // }
  565. // useSize += (long)size.Item1;
  566. // teachStics = size.Item2;
  567. // 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);
  568. // continue;
  569. // }
  570. // SortedSetEntry[] Scores = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Blob:Catalog:{itemTeach}");
  571. // if (Scores != null)
  572. // {
  573. // foreach (var score in Scores)
  574. // {
  575. // double val = score.Score;
  576. // string key = score.Element.ToString();
  577. // teachStics.Add(key, val);
  578. // }
  579. // useSize += blobsize;
  580. // 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);
  581. // continue;
  582. // }
  583. // else
  584. // {
  585. // var client = _azureStorage.GetBlobContainerClient(itemTeach);
  586. // var size = await client.GetBlobsCatalogSize();
  587. // await _azureRedis.GetRedisClient(8).HashSetAsync($"Blob:Record", itemTeach, size.Item1);
  588. // foreach (var key in size.Item2.Keys)
  589. // {
  590. // await _azureRedis.GetRedisClient(8).SortedSetRemoveAsync($"Blob:Catalog:{itemTeach}", key);
  591. // await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Blob:Catalog:{itemTeach}", key, size.Item2[key].HasValue ? size.Item2[key].Value : 0);
  592. // }
  593. // useSize += (long)size.Item1;
  594. // teachStics = size.Item2;
  595. // 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);
  596. // continue;
  597. // }
  598. // //RedisValue tempValue = _azureRedis.GetRedisClient(8).HashGet($"Blob:Record", itemTeach);
  599. // //if (!tempValue.IsNullOrEmpty)
  600. // //{
  601. // // useSize += Convert.ToInt64(tempValue);
  602. // // //JsonElement record = tempValue.ToString().ToObject<JsonElement>();
  603. // // //long tempSize = 0;
  604. // // //if (record.TryGetInt64(out tempSize))
  605. // // //{
  606. // // // sizeT += tempSize;
  607. // // //}
  608. // //}
  609. // //SortedSetEntry[] tempSorted = await _azureRedis.GetRedisClient(8).SortedSetRangeByRankWithScoresAsync($"Blob:Catalog:{itemTeach}");
  610. // //if (tempSorted != null)
  611. // //{
  612. // // foreach (var itemSorted in tempSorted)
  613. // // {
  614. // // if (typeStics.TryGetValue($"{itemSorted.Element}", out long val))
  615. // // typeStics[$"{itemSorted.Element}"] = Convert.ToInt64(itemSorted.Score) != 0 ? val + Convert.ToInt64(itemSorted.Score) : 0;
  616. // // else
  617. // // typeStics.Add($"{itemSorted.Element}", Convert.ToInt64(itemSorted.Score));
  618. // // }
  619. // //}
  620. //}
  621. return Ok(new { state = 200, totalSize, teach, useSize, stics = typeStics1.ToList() });
  622. }
  623. catch (Exception ex)
  624. {
  625. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-datatypestics \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  626. return BadRequest();
  627. }
  628. }
  629. /// <summary>
  630. /// 省的数据标准
  631. /// </summary>
  632. public record ProvinceStics
  633. {
  634. /// <summary>
  635. /// 省份ID
  636. /// </summary>
  637. public string provCode { get; set; }
  638. /// <summary>
  639. /// 省份名称
  640. /// </summary>
  641. public string provName { get; set; }
  642. /// <summary>
  643. /// 省份下的方案数量
  644. /// </summary>
  645. public int standardCount { get; set; }
  646. /// <summary>
  647. /// 省份下的学校
  648. /// </summary>
  649. public int schoolCount { get; set; }
  650. /// <summary>
  651. /// 省份下的教师数量
  652. /// </summary>
  653. public int teacherCount { get; set; }
  654. /// <summary>
  655. /// 省份下的学生数量
  656. /// </summary>
  657. public int studentCount { get; set; }
  658. }
  659. /// <summary>
  660. /// 省以下的区域标准
  661. /// </summary>
  662. public record ProvinceStandard
  663. {
  664. /// <summary>
  665. /// 城市Code
  666. /// </summary>
  667. public string provCode { get; set; }
  668. /// <summary>
  669. /// 城市名称
  670. /// </summary>
  671. public string provName { get; set; }
  672. /// <summary>
  673. /// 区域标准
  674. /// </summary>
  675. public string standard { get; set; }
  676. }
  677. /// <summary>
  678. /// 市的学校数据,及以下数据
  679. /// </summary>
  680. public record SticsCitys
  681. {
  682. /// <summary>
  683. /// 市级Code
  684. /// </summary>
  685. public string cityCode { get; set; }
  686. /// <summary>
  687. /// 市级名称
  688. /// </summary>
  689. public string cityName { get; set; }
  690. /// <summary>
  691. /// 学校数量
  692. /// </summary>
  693. public int schoolCount { get; set; }
  694. /// <summary>
  695. /// 市级下的区域,学校、
  696. /// </summary>
  697. public List<DistrictStics> districtSticss { get; set; }
  698. }
  699. /// <summary>
  700. /// 所有区域标准
  701. /// </summary>
  702. public record AllCityStics
  703. {
  704. /// <summary>
  705. /// 区域ID
  706. /// </summary>
  707. public string id { get; set; }
  708. /// <summary>
  709. /// 城市Code
  710. /// </summary>
  711. public string cityCode { get; set; }
  712. /// <summary>
  713. /// 城市名称
  714. /// </summary>
  715. public string cityName { get; set; }
  716. /// <summary>
  717. /// 区名称
  718. /// </summary>
  719. public string distName { get; set; }
  720. /// <summary>
  721. /// 区域标准
  722. /// </summary>
  723. public string standard { get; set; }
  724. }
  725. /// <summary>
  726. /// 统计区域数量
  727. /// </summary>
  728. public record DistrictStics
  729. {
  730. /// <summary>
  731. /// 市级名称
  732. /// </summary>
  733. public string cityName { get; set; }
  734. /// <summary>
  735. /// 地区名称
  736. /// </summary>
  737. public string distName { get; set; }
  738. /// <summary>
  739. /// 学校数量
  740. /// </summary>
  741. public int schoolCount { get; set; }
  742. /// <summary>
  743. /// 教师数量
  744. /// </summary>
  745. public int teacherCount { get; set; }
  746. /// <summary>
  747. /// 学生数量
  748. /// </summary>
  749. public int studentCount { get; set; }
  750. }
  751. /// <summary>
  752. /// 区域标准
  753. /// </summary>
  754. public record DistrictStandard()
  755. {
  756. /// <summary>
  757. /// 区域标准
  758. /// </summary>
  759. public string id { get; set; }
  760. /// <summary>
  761. /// 市名称
  762. /// </summary>
  763. public string cityName { get; set; }
  764. /// <summary>
  765. /// 区域名称
  766. /// </summary>
  767. public string distName { get; set; }
  768. /// <summary>
  769. /// 区域标准
  770. /// </summary>
  771. public string standard { get; set; }
  772. }
  773. /// <summary>
  774. /// 市级学校数量
  775. /// </summary>
  776. public record CitySchool
  777. {
  778. /// <summary>
  779. /// 市级ID
  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. }
  791. /// <summary>
  792. /// 市级信息标准
  793. /// </summary>
  794. public record CityStandard
  795. {
  796. public string areaId { get; set; }
  797. /// <summary>
  798. /// 城市Code
  799. /// </summary>
  800. public string cityCode { get; set; }
  801. /// <summary>
  802. /// 城市名称
  803. /// </summary>
  804. public string cityName { get; set; }
  805. /// <summary>
  806. /// 区域标准
  807. /// </summary>
  808. public string standard { get; set; }
  809. }
  810. }
  811. }