ActivitySticsController.cs 31 KB

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