ActivitySticsController.cs 33 KB

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