HomeStatisController.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. using Microsoft.AspNetCore.Http;
  2. using Microsoft.AspNetCore.Mvc;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using System.Text.Json;
  8. using TEAMModelOS.SDK.DI;
  9. using Azure.Cosmos;
  10. using Microsoft.Extensions.Options;
  11. using TEAMModelOS.SDK.Models;
  12. using TEAMModelOS.Models;
  13. using System.Text;
  14. namespace TEAMModeBI.Controllers.BIHome
  15. {
  16. [Route("homestatis")]
  17. [ApiController]
  18. public class HomeStatisController : ControllerBase
  19. {
  20. private readonly AzureCosmosFactory _azureCosmos;
  21. private readonly DingDing _dingDing;
  22. private readonly Option _option;
  23. public HomeStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
  24. {
  25. _azureCosmos = azureCosmos;
  26. _dingDing = dingDing;
  27. _option = option?.Value;
  28. }
  29. /// <summary>
  30. /// 获取全部教师数量、学生数量、已创校数量、全部学校总空间大小
  31. /// </summary>
  32. /// <param name="jsonElement"></param>
  33. /// <returns></returns>
  34. [ProducesDefaultResponseType]
  35. [HttpPost("get-allnumber")]
  36. public async Task<IActionResult> GetAllNumber()
  37. {
  38. try
  39. {
  40. var client = _azureCosmos.GetCosmosClient();
  41. //依据学校查询教师人数
  42. List<string> teacherCount_list = new();
  43. //依据学校查询学生信息
  44. List<string> studentCount_List = new();
  45. //学校数
  46. List<string> schoolCount_List = new();
  47. //学校空间大小
  48. int schoolsize = 0;
  49. //查询全部教师人数
  50. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select * from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  51. {
  52. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  53. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  54. {
  55. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  56. while (accounts.MoveNext())
  57. {
  58. JsonElement account = accounts.Current;
  59. teacherCount_list.Add(account.GetProperty("id").GetString());
  60. }
  61. }
  62. }
  63. //查询全部学生人数
  64. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.pk='Base'"))
  65. {
  66. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  67. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  68. {
  69. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  70. while (accounts.MoveNext())
  71. {
  72. JsonElement account = accounts.Current;
  73. studentCount_List.Add(account.GetProperty("id").GetString());
  74. }
  75. }
  76. }
  77. //查询已创建多少学校
  78. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id,c.size from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  79. {
  80. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  81. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  82. {
  83. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  84. while (accounts.MoveNext())
  85. {
  86. JsonElement account = accounts.Current;
  87. schoolCount_List.Add(account.GetProperty("id").GetString());
  88. schoolsize += int.Parse(account.GetProperty("size").ToString());
  89. }
  90. }
  91. }
  92. return Ok(new { teacherCount = teacherCount_list.Count, studentCount = studentCount_List.Count, schoolCount = schoolCount_List.Count, schoolSize = schoolsize });
  93. }
  94. catch (Exception ex)
  95. {
  96. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-numberpeople \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  97. return BadRequest();
  98. }
  99. }
  100. /// <summary>
  101. /// 获取其它类型的统计:问卷调查、评量检测、投票活动
  102. /// </summary>
  103. /// <param name="jsonElement"></param>
  104. /// <returns></returns>
  105. [ProducesDefaultResponseType]
  106. [HttpPost("get-othertypes")]
  107. public async Task<IActionResult> GetOtherTypes(JsonElement jsonElement)
  108. {
  109. try
  110. {
  111. if (!jsonElement.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  112. int surveyJoinCount = 0; //调查参加数量
  113. int surveyDoneCount = 0; //调查完成数量
  114. int surveyAreaJoinCount = 0; //调查区域参加数量
  115. int surveyAreaDoneCount = 0; //调查区域完成数量
  116. int examJoinCount = 0; //评量检测参加数量
  117. int examDoneCount = 0; //评量检测完成数量
  118. int examAreaJoinCount = 0; //评量检测区域参加数量
  119. int examAreaDoneCount = 0; //评量检测区域完成数量
  120. int voteJoinCount = 0; //投票参加数量
  121. int voteDoneCount = 0; //投票完成数量
  122. int voteAreaJoinCount = 0; //投票区域参加数量
  123. int voteAreaDoneCount = 0; //投票区域完成数量
  124. //问卷调查
  125. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  126. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Survey' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  127. {
  128. if (!string.IsNullOrEmpty(item.owner))
  129. {
  130. if (item.owner.Equals("school"))
  131. {
  132. surveyJoinCount += 1;
  133. if (item.taskStatus > 0)
  134. {
  135. surveyDoneCount += 1;
  136. }
  137. }
  138. else if (item.owner.Equals("area"))
  139. {
  140. surveyAreaJoinCount += 1;
  141. if (item.taskStatus > 0)
  142. {
  143. surveyAreaDoneCount += 1;
  144. }
  145. }
  146. }
  147. }
  148. //评量检测
  149. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  150. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'ExamLite' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  151. {
  152. if (!string.IsNullOrEmpty(item.owner))
  153. {
  154. if (item.owner.Equals("school"))
  155. {
  156. examJoinCount += 1;
  157. if (item.taskStatus > 0)
  158. {
  159. examDoneCount += 1;
  160. }
  161. }
  162. else if (item.owner.Equals("area"))
  163. {
  164. examAreaJoinCount += 1;
  165. if (item.taskStatus > 0)
  166. {
  167. examAreaDoneCount += 1;
  168. }
  169. }
  170. }
  171. }
  172. //投票活动
  173. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  174. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Vote' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  175. {
  176. if (!string.IsNullOrEmpty(item.owner))
  177. {
  178. if (item.owner.Equals("school"))
  179. {
  180. voteJoinCount += 1;
  181. if (item.taskStatus > 0)
  182. {
  183. voteDoneCount += 1;
  184. }
  185. }
  186. else if (item.owner.Equals("area"))
  187. {
  188. voteAreaJoinCount += 1;
  189. if (item.taskStatus > 0)
  190. {
  191. voteAreaDoneCount += 1;
  192. }
  193. }
  194. }
  195. }
  196. return Ok(new { surveyJoinCount, surveyDoneCount, surveyAreaJoinCount, surveyAreaDoneCount, examJoinCount, examDoneCount, examAreaJoinCount, examAreaDoneCount, voteJoinCount, voteDoneCount, voteAreaJoinCount, voteAreaDoneCount });
  197. }
  198. catch (Exception ex)
  199. {
  200. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-othertypes \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  201. return BadRequest();
  202. }
  203. }
  204. /// <summary>
  205. /// 查询所有区级信息统计数据
  206. /// </summary>
  207. /// <param name="jsonElement"></param>
  208. /// <returns></returns>
  209. [ProducesDefaultResponseType]
  210. [HttpPost("get-alldiststics")]
  211. public async Task<IActionResult> GetAllDistStics(JsonElement jsonElement)
  212. {
  213. try
  214. {
  215. var cosmosClient = _azureCosmos.GetCosmosClient();
  216. List<SticsCity> standards = new List<SticsCity>();
  217. StringBuilder stringBuder = new StringBuilder("select c.name,c.provName,c.cityName,c.standard from c");
  218. //查询省份区域
  219. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: stringBuder.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  220. {
  221. SticsCity sticsCity = new SticsCity
  222. {
  223. provName = item.provName,
  224. cityName = item.cityName,
  225. distName = item.name,
  226. standard = item.standard
  227. };
  228. standards.Add(sticsCity);
  229. }
  230. List<SticsDist> sticsDists = new List<SticsDist>();
  231. //查询所有下面的学校数量
  232. foreach (var itemStandrds in standards)
  233. {
  234. SticsDist sticsDist = new SticsDist();
  235. sticsDist.provName = itemStandrds.provName;
  236. sticsDist.cityName = itemStandrds.cityName;
  237. sticsDist.distName = itemStandrds.distName;
  238. sticsDist.standard = itemStandrds.standard;
  239. List<string> schoolIds = new List<string>();
  240. int schoolCount = 0;
  241. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.standard='{itemStandrds.standard}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  242. {
  243. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  244. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  245. {
  246. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  247. while (accounts.MoveNext())
  248. {
  249. JsonElement account = accounts.Current;
  250. schoolIds.Add(account.GetProperty("id").GetString());
  251. schoolCount += 1;
  252. }
  253. }
  254. }
  255. sticsDist.schoolCount = schoolCount;
  256. int teacherCount = 0; //教师数量
  257. int studentCount = 0; //学生数量
  258. List<string> teacherIds = new List<string>();
  259. List<string> studentIds = new List<string>();
  260. //查询学校下面的教师人数
  261. foreach (var itemSchool in schoolIds)
  262. {
  263. string sqlTeacherTxt = $"select distinct value(c) from c join a1 in c.schools where a1.schoolId='{itemSchool}'";
  264. //查询全部教师人数
  265. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: sqlTeacherTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  266. {
  267. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  268. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  269. {
  270. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  271. while (accounts.MoveNext())
  272. {
  273. //JsonElement account = accounts.Current;
  274. //teacherIds.Add(account.GetProperty("id").GetString());
  275. teacherCount += 1;
  276. }
  277. }
  278. }
  279. //查询全部学生人数
  280. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{itemSchool}") }))
  281. {
  282. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  283. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  284. {
  285. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  286. while (accounts.MoveNext())
  287. {
  288. //JsonElement account = accounts.Current;
  289. //studentIds.Add(account.GetProperty("id").GetString());
  290. studentCount += 1;
  291. }
  292. }
  293. }
  294. }
  295. sticsDist.teacherCount = teacherCount;
  296. sticsDist.studentCount = studentCount;
  297. sticsDists.Add(sticsDist);
  298. }
  299. return Ok(new { state = 200, sticsDists });
  300. }
  301. catch (Exception ex)
  302. {
  303. await _dingDing.SendBotMsg($"BI,{_option.Location} homestatis/get-alldiststics \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  304. return BadRequest();
  305. }
  306. }
  307. /// <summary>
  308. /// 区域
  309. /// </summary>
  310. public record SticsCity
  311. {
  312. /// <summary>
  313. /// 省份
  314. /// </summary>
  315. public string provName { get; set; }
  316. /// <summary>
  317. /// 市名称
  318. /// </summary>
  319. public string cityName { get; set; }
  320. /// <summary>
  321. /// 区域名称
  322. /// </summary>
  323. public string distName { get; set; }
  324. /// <summary>
  325. /// 区域标准
  326. /// </summary>
  327. public string standard { get; set; }
  328. }
  329. /// <summary>
  330. /// 返回统计数据
  331. /// </summary>
  332. public record SticsDist
  333. {
  334. /// <summary>
  335. /// 省份
  336. /// </summary>
  337. public string provName { get; set; }
  338. /// <summary>
  339. /// 市名
  340. /// </summary>
  341. public string cityName { get; set; }
  342. /// <summary>
  343. /// 区域标准名称
  344. /// </summary>
  345. public string distName { get; set; }
  346. /// <summary>
  347. /// 区域标准
  348. /// </summary>
  349. public string standard { get; set; }
  350. /// <summary>
  351. /// 学校数量
  352. /// </summary>
  353. public int schoolCount { get; set; }
  354. /// <summary>
  355. /// 教师人数
  356. /// </summary>
  357. public int teacherCount { get; set; }
  358. /// <summary>
  359. /// 学生人数
  360. /// </summary>
  361. public int studentCount { get; set; }
  362. }
  363. }
  364. }