ActivitySticsController.cs 37 KB

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