ActivitySticsController.cs 33 KB

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