ActivitySticsController.cs 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.Extensions.Options;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using TEAMModelOS.Models;
  10. using TEAMModelOS.SDK.DI;
  11. using TEAMModelOS.SDK.Models;
  12. using System.Text.Json;
  13. using TEAMModelBI.Tool;
  14. using System.Text;
  15. using TEAMModelOS.SDK.Extension;
  16. using TEAMModelBI.Models;
  17. using TEAMModelOS.SDK;
  18. using TEAMModelBI.Tool.CosmosBank;
  19. namespace TEAMModelBI.Controllers.Census
  20. {
  21. [Route("activity")]
  22. [ApiController]
  23. public class ActivitySticsController : ControllerBase
  24. {
  25. private readonly AzureCosmosFactory _azureCosmos;
  26. private readonly AzureStorageFactory _azureStorage;
  27. private readonly DingDing _dingDing;
  28. private readonly Option _option;
  29. private readonly CoreAPIHttpService _coreAPIHttpService;
  30. public ActivitySticsController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, DingDing dingDing, IOptionsSnapshot<Option> option, CoreAPIHttpService coreAPIHttpService)
  31. {
  32. _azureCosmos = azureCosmos;
  33. _dingDing = dingDing;
  34. _azureStorage = azureStorage;
  35. _option = option?.Value;
  36. _coreAPIHttpService = coreAPIHttpService;
  37. }
  38. /// <summary>
  39. /// 统计活动 传醍摩豆则查询相关的学校活动
  40. /// </summary>
  41. /// <param name="jsonElement"></param>
  42. /// <returns></returns>
  43. [ProducesDefaultResponseType]
  44. [HttpPost("get-count")]
  45. public async Task<IActionResult> GetCount(JsonElement jsonElement)
  46. {
  47. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  48. if(!jsonElement.TryGetProperty("term", out JsonElement term)) return BadRequest();
  49. long start = 0, end = 0;
  50. if (bool.Parse($"{term}") == true)
  51. {
  52. (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
  53. }
  54. var cosmosClient = _azureCosmos.GetCosmosClient();
  55. List<object> activityCount = new List<object>();
  56. if (!string.IsNullOrEmpty($"{tmdId}"))
  57. {
  58. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  59. foreach (var itemId in schoolIds)
  60. {
  61. School school = new();
  62. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  63. if (response.Status == 200)
  64. {
  65. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  66. school = json.ToObject<School>();
  67. }
  68. ActivityCount tempCount = new ActivityCount() { id = itemId, name = school.name != null ? school.name : itemId };
  69. foreach (var type in StaticValue.activityTypes)
  70. {
  71. StringBuilder sqlTxt = new StringBuilder($"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{itemId}' ");
  72. if (bool.Parse($"{term}") == true)
  73. {
  74. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  75. }
  76. long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), new List<string>() { "Common" });
  77. tempCount.census.Add(new KeyValuePair<object, long>(type, totals));
  78. }
  79. activityCount.Add(tempCount);
  80. }
  81. }
  82. else
  83. {
  84. foreach (var type in StaticValue.activityTypes)
  85. {
  86. StringBuilder sqlTxt = new StringBuilder($"SELECT COUNT(c.id) AS totals FROM c where c.pk='{type}' ");
  87. if (bool.Parse($"{term}") == true)
  88. {
  89. sqlTxt.Append($" and c.createTime >= {start} and c.createTime <= {end}");
  90. }
  91. long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), new List<string>() { "Common" });
  92. activityCount.Add(new KeyValuePair<string, object>(type, totals));
  93. }
  94. }
  95. return Ok(new { state = 200, activityCount });
  96. }
  97. /// <summary>
  98. /// 统计所有的评量活动,问卷调查,投票活动,作业
  99. /// </summary>
  100. /// <returns></returns>
  101. [HttpPost("get-allactivity")]
  102. public async Task<IActionResult> GetAllActivity()
  103. {
  104. try
  105. {
  106. var cosmosClient = _azureCosmos.GetCosmosClient();
  107. List<KeyValuePair<string, long>> typeCount = new List<KeyValuePair<string, long>>();
  108. foreach (var type in StaticValue.activityTypes)
  109. {
  110. string querySql = $"SELECT Count(c.id) as totals FROM c where c.pk='{type}' ";
  111. long totals = await CommonFind.FindTotals(cosmosClient, querySql, new List<string>() { "Common" });
  112. KeyValuePair<string, long> valuePair = new KeyValuePair<string, long>(type, totals);
  113. typeCount.Add(valuePair);
  114. }
  115. return Ok(new { state = 200, typeCount });
  116. }
  117. catch (Exception ex)
  118. {
  119. await _dingDing.SendBotMsg($"BI,{_option.Location} /activity/get-allactivity \n {ex.Message}\n{ex.StackTrace}", GroupNames.成都开发測試群組);
  120. return BadRequest();
  121. }
  122. }
  123. /// <summary>
  124. /// 统计顾问关联的学校活动数量:评测、问卷、投票、作业
  125. /// </summary>
  126. /// <param name="jsonElement"></param>
  127. /// <returns></returns>
  128. [ProducesDefaultResponseType]
  129. [HttpPost("get-assistactivity")]
  130. public async Task<IActionResult> GetAssistSchoolActivity(JsonElement jsonElement)
  131. {
  132. if (!jsonElement.TryGetProperty("tmdId", out JsonElement tmdId)) return BadRequest();
  133. var cosmosClient = _azureCosmos.GetCosmosClient();
  134. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  135. List<ActivityCount> activityCounts = new List<ActivityCount>();
  136. foreach (var itemId in schoolIds)
  137. {
  138. School school = new();
  139. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(itemId, new PartitionKey("Base"));
  140. if (response.Status == 200)
  141. {
  142. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  143. school = json.ToObject<School>();
  144. }
  145. ActivityCount activityCount = new ActivityCount() { id = itemId, name = school.name != null ? school.name : itemId };
  146. foreach (var type in StaticValue.activityTypes)
  147. {
  148. string activitySql = $"SELECT Count(c.id) as totals FROM c WHERE c.pk='{type}' AND c.school='{itemId}'";
  149. long totals = await CommonFind.FindTotals(cosmosClient, activitySql, new List<string>() { "Common" });
  150. activityCount.census.Add(new KeyValuePair<object, long>(type, totals));
  151. }
  152. activityCounts.Add(activityCount);
  153. }
  154. return Ok(new { state = 200, activityCounts });
  155. }
  156. /// <summary>
  157. /// 统计当前学期的活动,传醍摩豆账户则统计该账户当前学期
  158. /// </summary>
  159. /// <param name="jsonElement"></param>
  160. /// <returns></returns>
  161. [HttpPost("get-termactivity")]
  162. public async Task<IActionResult> GetTermActivity(JsonElement jsonElement)
  163. {
  164. jsonElement.TryGetProperty("tmdId", out JsonElement tmdId);
  165. var cosmosClient = _azureCosmos.GetCosmosClient();
  166. var (start, end) = TimeHelper.GetTermStartOrEnd(DateTime.Now);
  167. if (!string.IsNullOrEmpty($"{tmdId}"))
  168. {
  169. List<string> schoolIds = await CommonFind.FindSchoolIds(cosmosClient, $"{tmdId}");
  170. List<ActivityCount> activityCounts = new List<ActivityCount>();
  171. foreach (var schoolId in schoolIds)
  172. {
  173. School school = new();
  174. var response = await cosmosClient.GetContainer(Constant.TEAMModelOS, "School").ReadItemStreamAsync(schoolId, new PartitionKey("Base"));
  175. if (response.Status == 200)
  176. {
  177. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  178. school = json.ToObject<School>();
  179. }
  180. ActivityCount activityCount = new ActivityCount() { id = schoolId, name = school.name != null ? school.name : schoolId };
  181. foreach (var type in StaticValue.activityTypes)
  182. {
  183. string activitySql = $"SELECT COUNT(c.id) AS totals FROM c WHERE c.pk='{type}' AND c.school='{school}' and c.createTime >= {start} and c.createTime <= {end}";
  184. long totals = await CommonFind.FindTotals(cosmosClient, activitySql, new List<string>() { "Common" });
  185. activityCount.census.Add(new KeyValuePair<object, long>(type, totals));
  186. }
  187. activityCounts.Add(activityCount);
  188. }
  189. return Ok(new { state = 200, activityCounts });
  190. }
  191. else
  192. {
  193. List<KeyValuePair<string, object>> typeCount = new List<KeyValuePair<string, object>>();
  194. foreach (var type in StaticValue.activityTypes)
  195. {
  196. string querySql = $"SELECT COUNT(c.id) AS totals FROM c where c.pk='{type}' and c.createTime >= {start} and c.createTime <= {end}";
  197. long totals = await CommonFind.FindTotals(cosmosClient, querySql, new List<string>() { "Common" });
  198. KeyValuePair<string, object> valuePair = new KeyValuePair<string, object>(type, totals);
  199. typeCount.Add(valuePair);
  200. }
  201. return Ok(new { state = 200, typeCount });
  202. }
  203. }
  204. /// <summary>
  205. /// 统计区域的活动
  206. /// </summary>
  207. /// <param name="jsonElement"></param>
  208. /// <returns></returns>
  209. [HttpPost("get-area")]
  210. public async Task<IActionResult> GetAreaActovoty(JsonElement jsonElement)
  211. {
  212. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  213. var cosmosClient = _azureCosmos.GetCosmosClient();
  214. List<RecSchool> schools = new();
  215. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecSchool>(queryText: $"select c.id,c.name,c.picture,c.scale from c where c.areaId='{areaId}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  216. {
  217. schools.Add(item);
  218. }
  219. long exemAreaCount = 0; //评测活动
  220. long surveyAreaCount = 0; //问卷
  221. long voteAreaCount = 0; //投票
  222. long homeworkAreaCount = 0; //作业活动
  223. List<ActivityCount> activityCount = new();
  224. foreach (var school in schools)
  225. {
  226. ActivityCount tempCount = new ActivityCount() { id = school.id, name = school.name != null ? school.name : school.id };
  227. foreach (var type in StaticValue.activityTypes)
  228. {
  229. StringBuilder sqlTxt = new($"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{school.id}' ");
  230. long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt.ToString(), new List<string>() { "Common" });
  231. switch (type)
  232. {
  233. case "Exam":
  234. exemAreaCount += totals;
  235. break;
  236. case "Survey":
  237. surveyAreaCount += totals;
  238. break;
  239. case "Vote":
  240. voteAreaCount += totals;
  241. break;
  242. case "Homework":
  243. homeworkAreaCount += totals;
  244. break;
  245. }
  246. tempCount.census.Add(new KeyValuePair<object, long>(type, totals));
  247. }
  248. activityCount.Add(tempCount);
  249. }
  250. return Ok(new { state = 200, exemAreaCount, surveyAreaCount, voteAreaCount, homeworkAreaCount, activityCount });
  251. }
  252. /// <summary>
  253. /// 依据区统计区级相关信息包括活动统计
  254. /// </summary>
  255. /// <param name="jsonElement"></param>
  256. /// <returns></returns>
  257. [HttpPost("get-areastics")]
  258. public async Task<IActionResult> GetAreaStics(JsonElement jsonElement)
  259. {
  260. if (!jsonElement.TryGetProperty("areaId", out JsonElement areaId)) return BadRequest();
  261. var (dayStart, dayEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow);
  262. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
  263. var (monthStart, monthEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
  264. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
  265. var cosmosClient = _azureCosmos.GetCosmosClient();
  266. List<RecSchool> schools = new();
  267. StringBuilder scSqlTxt = new("select c.id,c.name,c.picture,c.size,c.scale,c.type from c");
  268. if (!string.IsNullOrEmpty($"{areaId}"))
  269. {
  270. scSqlTxt.Append($" where c.areaId='{areaId}'");
  271. }
  272. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecSchool>(queryText: scSqlTxt.ToString(), requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  273. {
  274. schools.Add(item);
  275. }
  276. int countArea = 0;//区级人员
  277. int appraiseArea = 0;//区级评审人员
  278. int areaSize = 0; //区级空间
  279. long examAreaCount = 0; //试卷活动
  280. long surveyAreaCount = 0; //问卷活动
  281. long voteAreaCount = 0; //投票活动
  282. long homeworkAreaCount = 0; //作业活动
  283. long weekActivity = 0; //周活动数量
  284. long termActivity=0; //学期活动
  285. int basics = 0; //基础版数
  286. int standard = 0; //标准版数
  287. int major = 0; //专业版数
  288. int geCount = 0; //普教
  289. int heCount = 0; //高教
  290. int oeCount = 0; //其他教育
  291. int allLess = 0; //所有课例
  292. int dayLess = 0; //当天课例
  293. int weekLess = 0; //周课例
  294. int monthLess = 0; //月课例
  295. int termLess = 0; //学期课例
  296. double teachCount = 0; //课例教师
  297. List<SchoolLesson> schoolLessons = new(); //学校课例集合
  298. List<SchoolInfo> schoolInfos = new();
  299. List<LessonRecord> records = new();//所有的课程记录
  300. List<string> scIds = schools.Select(x => x.id).ToList();
  301. int totalTime = 0;
  302. foreach (var school in schools)
  303. {
  304. if (school.type == 2)
  305. heCount += 1;
  306. else if (school.type == 1)
  307. geCount += 1;
  308. else oeCount += 1;
  309. areaSize += school.size;
  310. int count = 0;
  311. int appraise = 0;
  312. await foreach (var info in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<bool>(queryText: $"select value(array_contains(c.permissions,'train-appraise')) from c", requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey($"Teacher-{school.id}") }))
  313. {
  314. if (info)
  315. {
  316. appraise += 1;
  317. appraiseArea += 1;
  318. }
  319. countArea += 1;
  320. count += 1;
  321. }
  322. //查询学校的总学时
  323. totalTime += await CommonFind.FindTotals(cosmosClient, "SELECT sum(c.totalTime) as totals FROM c", "Teacher", $"TeacherTrain-{school.id}");
  324. //查询学校参训人数
  325. int tempCount = await CommonFind.FindTotals(cosmosClient, $"select array_length(c.members) as totals from c where c.type='yxtrain'", "School", $"GroupList-{school.id}");
  326. //学校学生人数
  327. long stuCount = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c", "Student", $"Base-{school.id}");
  328. //查询是否有服务
  329. int serCount = await CommonFind.FindTotals(cosmosClient, $"select array_length(c.service) as totals from c where c.id='{school.id}'", "School", "ProductSum");
  330. if (serCount > 0)
  331. major += 1;
  332. else if (school.size >= 300 && school.scale >= 500)
  333. standard += 1;
  334. else basics += 1;
  335. string sqlTxtSchool = $"select value(c) from c where c.code='LessonRecord-{school}'";
  336. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<LessonRecord>(queryText: sqlTxtSchool, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord-{school}") }))
  337. {
  338. records.Add(item);
  339. }
  340. //课例
  341. int lessCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) totals from c", "School", $"LessonRecord-{school.id}");
  342. SchoolLesson schoolLesson = new()
  343. {
  344. id = school.id,
  345. name = school.name,
  346. picture = school.picture,
  347. count = lessCount
  348. };
  349. SchoolInfo schoolInfo = new() {
  350. id = school.id,
  351. name = school.name,
  352. picture = school.picture,
  353. teacherCount = count,
  354. studentCount = stuCount,
  355. appraiseCount = appraise,
  356. trainCount = tempCount
  357. };
  358. ActivityCount tempActivity = new() { id = school.id, name = school.name != null ? school.name : school.id };
  359. foreach (var type in StaticValue.activityTypes)
  360. {
  361. long totals = await CommonFind.FindTotals(cosmosClient, $"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.school='{school.id}' ", new List<string>() { "Common" });
  362. string weekSql= $"select count(c.id) as totals from c where c.pk='{type}' and c.school='{school.id}' and c.createTime>={weekStart} and c.createTime<={weekEnd}";
  363. long weekActCount = await CommonFind.FindTotals(cosmosClient, weekSql, new List<string>() { "Common" });
  364. weekActivity += weekActCount;
  365. string termSql = $"select count(c.id) as totals from c where c.pk='{type}' and c.school='{school.id}' and c.createTime>={termStart} and c.createTime<={termEnd}";
  366. long termActCount = await CommonFind.FindTotals(cosmosClient, termSql, new List<string>() { "Common" });
  367. termActivity += termActCount;
  368. switch (type)
  369. {
  370. case "Exam":
  371. examAreaCount += totals;
  372. break;
  373. case "Survey":
  374. surveyAreaCount += totals;
  375. break;
  376. case "Vote":
  377. voteAreaCount += totals;
  378. break;
  379. case "Homework":
  380. homeworkAreaCount += totals;
  381. break;
  382. }
  383. schoolInfo.census.Add(new KeyValuePair<object, long>(type, totals));
  384. }
  385. schoolInfos.Add(schoolInfo);
  386. schoolLessons.Add(schoolLesson);
  387. }
  388. List<string> tecIds = await CommonFind.FindRolesId(cosmosClient, scIds);
  389. //查询去下面所有学校教师课例
  390. foreach (var tecId in tecIds)
  391. {
  392. string sqlTxt = $"select value(c) from c where c.id='{tecId}'";
  393. await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS, "Teacher").GetItemQueryIterator<LessonRecord>(queryText: sqlTxt, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"LessonRecord") }))
  394. {
  395. records.Add(item);
  396. }
  397. }
  398. //课例统计
  399. if (records.Count > 0)
  400. {
  401. records.ForEach(x => { if (x.startTime >= dayStart && x.startTime <= dayEnd) dayLess += 1; });
  402. records.ForEach(x => { if (x.startTime >= weekStart && x.startTime <= weekEnd) weekLess += 1; });
  403. records.ForEach(x => { if (x.startTime >= monthStart && x.startTime <= monthEnd) monthLess += 1; });
  404. records.ForEach(x => { if (x.startTime >= termStart && x.startTime <= termEnd) termLess += 1; });
  405. teachCount = records.Where(r => r.tmdid != null).Where((x, i) => records.FindIndex(z => z.tmdid == x.tmdid) == i).ToList().Count;
  406. }
  407. return Ok(new { state = 200, schoolCount = schools.Count, countArea, weekActivity, termActivity, totalTime, appraiseArea, examAreaCount, surveyAreaCount, voteAreaCount, homeworkAreaCount, major, standard, basics, oeCount, dayLess, weekLess, monthLess, termLess, teachCount, allLess = records.Count, schools = schoolInfos, schoolLessons });
  408. }
  409. /// <summary>
  410. /// 所有区的统计接口
  411. /// </summary>
  412. /// <returns></returns>
  413. [HttpPost("get-all")]
  414. public async Task<IActionResult> GetAll()
  415. {
  416. var cosmosClient = _azureCosmos.GetCosmosClient();
  417. List<AreaInfo> areaInfos = new();
  418. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Normal").GetItemQueryIterator<AreaInfo>(queryText: $"select c.id,c.name,c.standard,c.standardName from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base-Area") }))
  419. {
  420. areaInfos.Add(item);
  421. }
  422. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
  423. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
  424. int heCount = 0;//高教
  425. int geCount = 0;//普教
  426. int oeCount = 0; //其他教育
  427. int allSize = 0;//空间总量
  428. Dictionary<string, long> activitys = new(); //活动类型集合
  429. long allActivity = 0; //活动累计
  430. long weekActivity = 0;//本周活动
  431. long termActivity = 0;//本学期活动
  432. long weekLess = 0; //本周课例
  433. long termLess = 0; //本学期课例
  434. long allLess = 0; //所有课例
  435. int tecCount = 0; //教师数量
  436. int scCount = 0; //学校数量
  437. int stuCount = 0; //学生数量
  438. int basics = 0; //基础版数
  439. int standard = 0; //标准版数
  440. int major = 0; //专业版数
  441. long resourceCount = 0; //累计资源
  442. long totalTime = 0; //总学时
  443. scCount = await CommonFind.FindTotals(cosmosClient, $"SELECT count(c.id) as totals FROM c ", "School", "Base");
  444. tecCount = await CommonFind.FindTotals(cosmosClient, $"SELECT count(c.id) as totals FROM c ", "Teacher", "Base");
  445. stuCount = await CommonFind.FindTotals(cosmosClient, $"SELECT count(c.id) as totals FROM c ", "Student", "Base");
  446. allSize = await CommonFind.FindTotals(cosmosClient, $"SELECT sum(c.size) as totals FROM c ", "School", "Base");
  447. foreach (var area in areaInfos)
  448. {
  449. List<RecSchool> recSchools = new();
  450. await foreach (var school in cosmosClient.GetContainer("TEAMModelOS", "School").GetItemQueryIterator<RecSchool>(queryText: $"select c.id,c.name,c.picture,c.type,c.size,c.scale from c where c.areaId='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey("Base") }))
  451. {
  452. recSchools.Add(school);
  453. }
  454. //scCount += recSchools.Count;
  455. recSchools.ForEach(x => { if (x.type == 2) heCount += 1; else if (x.type == 1) geCount += 1; else oeCount += 1; });
  456. //allSize += recSchools.Select(s => s.size).Sum();
  457. area.schoolCount = recSchools.Count;
  458. //统计服务
  459. var (tempb, temps, tempm) = await ProductWay.GetVersionCount(cosmosClient, recSchools);
  460. basics += tempb;
  461. standard += temps;
  462. major += tempm;
  463. int tempCount = await CommonFind.GetPeopleNumber(cosmosClient, "School", recSchools?.Select(x => x.id).ToList(), "Teacher");
  464. //tecCount += tempCount;
  465. //查教师
  466. area.techCount = tempCount;
  467. //查询学生
  468. area.stuCount = await CommonFind.GetPeopleNumber(cosmosClient, "Student", recSchools?.Select(x => x.id).ToList(), "Base");
  469. }
  470. //统计活动
  471. foreach (var type in StaticValue.activityTypes)
  472. {
  473. string sqlTxt = $"select COUNT(c.id) AS totals from c where c.pk='{type}'";
  474. long totals = await CommonFind.FindTotals(cosmosClient, sqlTxt, new List<string>() { "Common" });
  475. string weekSql = $"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.createTime>={weekStart} and c.createTime<={weekStart} ";
  476. weekActivity += await CommonFind.FindTotals(cosmosClient, weekSql,new List<string> { "Common" });
  477. string termSql = $"select COUNT(c.id) AS totals from c where c.pk='{type}' and c.createTime>={termStart} and c.createTime<={termEnd} ";
  478. termActivity += await CommonFind.FindTotals(cosmosClient, termSql, new List<string> { "Common" });
  479. allActivity += totals;
  480. activitys.Add(type, totals);
  481. }
  482. allLess = await CommonFind.FindTotals(cosmosClient, "select count(c.id) as totals from c where c.pk='LessonRecord'", new List<string>() { "School", "Teacher" });
  483. weekLess = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={weekStart} and c.startTime <={weekEnd}", new List<string>() { "School", "Teacher" });
  484. termLess = await CommonFind.FindTotals(cosmosClient, $"select count(c.id) as totals from c where c.pk='LessonRecord' and c.startTime>={termStart} and c.startTime <={termEnd}", new List<string>() { "School","Teacher" });
  485. totalTime = await CommonFind.FindTotals(cosmosClient, "select sum(c.totalTime) as totals from c where c.pk='TeacherTrain'", new List<string>() { "Teacher" });
  486. resourceCount = await CommonFind.FindTotals(cosmosClient, "select count(c.id) as totals from c where c.pk='Bloblog'", new List<string>() { "School", "Teacher" });
  487. return Ok(new { state = 200, areaCount = areaInfos.Count, scCount, tecCount, stuCount, allSize, heCount, geCount, oeCount, allLess, termLess, weekLess, allActivity, termActivity, weekActivity, resourceCount, basics, standard, major, totalTime, activitys, areaInfos });
  488. }
  489. /// <summary>
  490. /// 依据活动Id查询活动详情信息 数据管理工具——查询工具
  491. /// </summary>
  492. /// <param name="jsonElement"></param>
  493. /// <returns></returns>
  494. [HttpPost("get-info")]
  495. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  496. {
  497. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  498. jsonElement.TryGetProperty("activity", out JsonElement activity);
  499. jsonElement.TryGetProperty("isPersonal", out JsonElement isPersonal);
  500. //if (jsonElement.TryGetProperty("", out JsonElement code)) return BadRequest();
  501. var cosmosClient = _azureCosmos.GetCosmosClient();
  502. List<object> infos = new List<object>();
  503. StringBuilder sqlTxt = new StringBuilder($"select * from c where c.id='{id}'");
  504. if (!string.IsNullOrEmpty($"{activity}"))
  505. {
  506. sqlTxt.Append($" and c.pk='{activity}'");
  507. }
  508. if (!string.IsNullOrEmpty($"{isPersonal}"))
  509. {
  510. if (bool.Parse($"{isPersonal}") == true)
  511. {
  512. sqlTxt.Append($" and c.scope='private'");
  513. }
  514. else
  515. {
  516. sqlTxt.Append($" and c.scope='school'");
  517. }
  518. }
  519. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { }))
  520. {
  521. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  522. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  523. {
  524. infos.Add(obj.ToObject<object>());
  525. }
  526. }
  527. return Ok(new { state = 200, infos });
  528. }
  529. /// <summary>
  530. /// 区级信息
  531. /// </summary>
  532. public record AreaInfo
  533. {
  534. public string id { get; set; }
  535. public string name { get; set; }
  536. public string standard { get; set; }
  537. public string standardName { get; set; }
  538. public int schoolCount { get; set; }
  539. public int techCount { get; set;}
  540. public int stuCount { get; set; }
  541. }
  542. private class SchoolInfo
  543. {
  544. public string id { get; set; }
  545. public string name { get; set; }
  546. public string picture { get; set; }
  547. //教师人数
  548. public int teacherCount { get; set; }
  549. //学生人数
  550. public long studentCount { get; set; }
  551. //评审人数
  552. public int appraiseCount { get; set; }
  553. //参训人数
  554. public int trainCount { get; set; }
  555. public List<KeyValuePair<object, long>> census { get; set; } = new List<KeyValuePair<object, long>>();
  556. }
  557. private record SchoolLesson
  558. {
  559. public string id { get; set; }
  560. public string name { get; set; }
  561. public string picture { get; set; }
  562. public int count { get; set; }
  563. }
  564. public record ActivityCount
  565. {
  566. public string id { get; set; }
  567. public string name { get; set; }
  568. public List<KeyValuePair<object, long>> census { get; set; } = new List<KeyValuePair<object, long>>();
  569. }
  570. }
  571. }