HomeStatisController.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 TEAMModelOS.Models;
  10. using Azure.Cosmos;
  11. using Microsoft.Extensions.Options;
  12. using TEAMModelOS.SDK.Models;
  13. namespace TEAMModeBI.Controllers.BIHome
  14. {
  15. [Route("homestatis")]
  16. [ApiController]
  17. public class HomeStatisController : ControllerBase
  18. {
  19. private readonly AzureCosmosFactory _azureCosmos;
  20. private readonly DingDing _dingDing;
  21. private readonly Option _option;
  22. public HomeStatisController(AzureCosmosFactory azureCosmos, DingDing dingDing, IOptionsSnapshot<Option> option)
  23. {
  24. _azureCosmos = azureCosmos;
  25. _dingDing = dingDing;
  26. _option = option?.Value;
  27. }
  28. /// <summary>
  29. /// 获取人数
  30. /// </summary>
  31. /// <param name="jsonElement"></param>
  32. /// <returns></returns>
  33. [ProducesDefaultResponseType]
  34. [HttpPost("get-numberpeople")]
  35. public async Task<IActionResult> GetNumberPeople(JsonElement jsonElement)
  36. {
  37. try
  38. {
  39. jsonElement.TryGetProperty("schooolId", out JsonElement schoolId);
  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. if (!string.IsNullOrEmpty($"{schoolId}"))
  50. {
  51. //查询学校教师人数
  52. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select c.id from c join S1 in c.schools where S1.schoolId='{schoolId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  53. {
  54. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  55. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  56. {
  57. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  58. while (accounts.MoveNext())
  59. {
  60. JsonElement account = accounts.Current;
  61. teacherCount_list.Add(account.GetProperty("id").GetString());
  62. }
  63. }
  64. }
  65. //查询学校学生人数
  66. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-{schoolId}") }))
  67. {
  68. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  69. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  70. {
  71. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  72. while (accounts.MoveNext())
  73. {
  74. JsonElement account = accounts.Current;
  75. studentCount_List.Add(account.GetProperty("id").GetString());
  76. }
  77. }
  78. }
  79. return Ok(new { TeacherCount = teacherCount_list.Count, StudentCount = studentCount_List.Count });
  80. }
  81. else
  82. {
  83. //查询全部教师人数
  84. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryStreamIterator(queryText: $"select * from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  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. teacherCount_list.Add(account.GetProperty("id").GetString());
  94. }
  95. }
  96. }
  97. //查询全部学生人数
  98. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Student").GetItemQueryStreamIterator(queryText: $"select c.id from c where c.pk='Base'"))
  99. {
  100. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  101. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  102. {
  103. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  104. while (accounts.MoveNext())
  105. {
  106. JsonElement account = accounts.Current;
  107. studentCount_List.Add(account.GetProperty("id").GetString());
  108. }
  109. }
  110. }
  111. //查询已创建多少学校
  112. 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") }))
  113. {
  114. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  115. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  116. {
  117. var accounts = json.RootElement.GetProperty("Documents").EnumerateArray();
  118. while (accounts.MoveNext())
  119. {
  120. JsonElement account = accounts.Current;
  121. schoolCount_List.Add(account.GetProperty("id").GetString());
  122. schoolsize += int.Parse(account.GetProperty("size").ToString());
  123. }
  124. }
  125. }
  126. return Ok(new { TeacherCount = teacherCount_list.Count, StudentCount = studentCount_List.Count, schoolCount = schoolCount_List.Count, schoolsize = schoolsize });
  127. }
  128. }
  129. catch (Exception ex)
  130. {
  131. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-numberpeople \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  132. return BadRequest();
  133. }
  134. }
  135. /// <summary>
  136. /// 其它类型的统计
  137. /// </summary>
  138. /// <param name="jsonElement"></param>
  139. /// <returns></returns>
  140. [ProducesDefaultResponseType]
  141. [HttpPost("get-othertypes")]
  142. public async Task<IActionResult> GetOtherTypes(JsonElement jsonElement)
  143. {
  144. try
  145. {
  146. if (!jsonElement.TryGetProperty("tmdid", out JsonElement _tmdid)) return BadRequest();
  147. int surveyJoinCount = 0;
  148. int surveyDoneCount = 0;
  149. int surveyAreaJoinCount = 0;
  150. int surveyAreaDoneCount = 0;
  151. int examJoinCount = 0;
  152. int examDoneCount = 0;
  153. int examAreaJoinCount = 0;
  154. int examAreaDoneCount = 0;
  155. int voteJoinCount = 0;
  156. int voteDoneCount = 0;
  157. int voteAreaJoinCount = 0;
  158. int voteAreaDoneCount = 0;
  159. //问卷调查
  160. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  161. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Survey' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  162. {
  163. if (!string.IsNullOrEmpty(item.owner))
  164. {
  165. if (item.owner.Equals("school"))
  166. {
  167. surveyJoinCount += 1;
  168. if (item.taskStatus > 0)
  169. {
  170. surveyDoneCount += 1;
  171. }
  172. }
  173. else if (item.owner.Equals("area"))
  174. {
  175. surveyAreaJoinCount += 1;
  176. if (item.taskStatus > 0)
  177. {
  178. surveyAreaDoneCount += 1;
  179. }
  180. }
  181. }
  182. }
  183. //评量检测
  184. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  185. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'ExamLite' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  186. {
  187. if (!string.IsNullOrEmpty(item.owner))
  188. {
  189. if (item.owner.Equals("school"))
  190. {
  191. examJoinCount += 1;
  192. if (item.taskStatus > 0)
  193. {
  194. examDoneCount += 1;
  195. }
  196. }
  197. else if (item.owner.Equals("area"))
  198. {
  199. examAreaJoinCount += 1;
  200. if (item.taskStatus > 0)
  201. {
  202. examAreaDoneCount += 1;
  203. }
  204. }
  205. }
  206. }
  207. //投票活动
  208. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer("TEAMModelOS", "Teacher")
  209. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Vote' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  210. {
  211. if (!string.IsNullOrEmpty(item.owner))
  212. {
  213. if (item.owner.Equals("school"))
  214. {
  215. voteJoinCount += 1;
  216. if (item.taskStatus > 0)
  217. {
  218. voteDoneCount += 1;
  219. }
  220. }
  221. else if (item.owner.Equals("area"))
  222. {
  223. voteAreaJoinCount += 1;
  224. if (item.taskStatus > 0)
  225. {
  226. voteAreaDoneCount += 1;
  227. }
  228. }
  229. }
  230. }
  231. return Ok(new { surveyJoinCount, surveyDoneCount, surveyAreaJoinCount, surveyAreaDoneCount, examJoinCount, examDoneCount, examAreaJoinCount, examAreaDoneCount, voteJoinCount, voteDoneCount, voteAreaJoinCount, voteAreaDoneCount, });
  232. }
  233. catch (Exception ex)
  234. {
  235. await _dingDing.SendBotMsg($"BI,{_option.Location} /homestatis/get-othertypes \n {ex.Message}{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  236. return BadRequest();
  237. }
  238. }
  239. }
  240. }