ActivitySticsController.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811
  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("TEAMModelOS", "School").GetItemQueryIterator<string>(queryText: areaSc, requestOptions: new QueryRequestOptions() { }))
  125. {
  126. scIds.Add(item);
  127. }
  128. inSql = $" and {BICommonWay.ManyScSql("c.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. var (weekStart, weekEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "week");
  464. var (termStart, termEnd) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "term");
  465. var (monthS, monthE) = TimeHelper.GetStartOrEnd(DateTimeOffset.UtcNow, "month");
  466. int areaCount = 0; //区域数量
  467. int scCount = 0; //学校数量
  468. int tecCount = 0; //教师数量
  469. int stuCount = 0; //学生数量
  470. int allSize = 0; //空间总量
  471. int weekScCnt = 0; //本周学校
  472. int weekTchCnt = 0; //本周教师
  473. int monthScCnt = 0; //本月学校
  474. int monthTchCnt = 0; //本月教师
  475. int heCount = 0;//高教
  476. int geCount = 0;//普教
  477. int oeCount = 0; //其他教育
  478. int allActivity = 0; //活动累计
  479. int weekActivity = 0;//本周活动
  480. int termActivity = 0;//本学期活动
  481. int monthActCnt = 0; //本月活动
  482. int weekLess = 0; //本周课例
  483. int termLess = 0; //本学期课例
  484. int allLess = 0; //所有课例
  485. int monthLesCnt = 0; //本月课例
  486. int resourceCount = 0; //累计资源
  487. int totalTime = 0; //总学时
  488. string commSql = "select value(count(c.id)) from c";
  489. areaCount = await CommonFind.GetSqlValueCount(cosmosClient, "Normal", commSql, "Base-Area");
  490. scCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", commSql, "Base");
  491. tecCount = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", commSql,"Base");
  492. stuCount = await CommonFind.GetSqlValueCount(cosmosClient, "Student", commSql, "Base");
  493. allSize = await CommonFind.GetSqlValueCount(cosmosClient, "School", commSql, "Base");
  494. weekScCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"{commSql} where c.createTime >= {termStart} and c.createTime >= {termEnd}", "Base");
  495. monthScCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"{commSql} where c.createTime >= {monthS} and c.createTime >= {monthE}", "Base");
  496. weekTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", $"{commSql} where c.createTime >= {termStart} and c.createTime >= {termEnd}", "Base");
  497. monthTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", $"{commSql} where c.createTime >= {monthS} and c.createTime >= {monthE}", "Base");
  498. heCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"{commSql} where c.type = 1", "Base");
  499. geCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"{commSql} where c.type = 2", "Base");
  500. oeCount = await CommonFind.GetSqlValueCount(cosmosClient, "School", $"{commSql} where c.type != 1 and c.type != 2", "Base");
  501. //活动
  502. allActivity = await ActivityWay.GetCnt(cosmosClient);
  503. string weekSql = $" and c.createTime >= {weekStart} and c.createTime <={weekEnd}";
  504. weekActivity = await ActivityWay.GetCnt(cosmosClient, weekSql);
  505. string termSql = $" and c.createTime >= {termStart} and c.createTime <={termEnd}";
  506. termActivity = await ActivityWay.GetCnt(cosmosClient, termSql);
  507. string monthActSql = $" and c.createTime >= {monthS} and c.createTime <={monthE}";
  508. monthActCnt = await ActivityWay.GetCnt(cosmosClient, termSql);
  509. //课例
  510. string allLessSql = $"select value(count(c.id)) from c where c.pk='LessonRecord'";
  511. allLess = await CommonFind.GetSqlValueCount(cosmosClient, "School", allLessSql);
  512. string weekLSql = $"{allLessSql} and c.startTime >= {weekStart} and c.startTime <={weekEnd}";
  513. weekLess = await CommonFind.GetSqlValueCount(cosmosClient, "School", weekLSql);
  514. string termLSql = $"{allLessSql} and c.startTime >= {termStart} and c.startTime <={termEnd}";
  515. termLess = await CommonFind.GetSqlValueCount(cosmosClient, "School", termLSql);
  516. string monthLSql = $"{allLessSql} and c.startTime >= {monthS} and c.startTime <={monthE}";
  517. monthLesCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", termLSql);
  518. totalTime = await CommonFind.GetSqlValueCount(cosmosClient, "Teacher", "select value(sum(c.totalTime)) from c where c.pk='TeacherTrain'");
  519. resourceCount = await CommonFind.GetSqlValueCount(cosmosClient, new List<string>() { "School", "Teacher" }, "select value(count(c.id)) from c where c.pk='Bloblog'");
  520. return Ok(new { state = RespondCode.Ok, areaCount, scCount, tecCount, stuCount, allSize, weekScCnt, weekTchCnt, monthScCnt, monthTchCnt, heCount, geCount, oeCount, allActivity, weekActivity, termActivity, monthActCnt, weekLess, termLess, allLess, monthLesCnt, resourceCount, totalTime });
  521. }
  522. /// <summary>
  523. /// 分析所有区的人数和学校 学区情况
  524. /// </summary>
  525. /// <param name="jsonElement"></param>
  526. /// <returns></returns>
  527. [HttpPost("get-areasanls")]
  528. public async Task<IActionResult> GetAreasAnls(JsonElement jsonElement)
  529. {
  530. jsonElement.TryGetProperty("site", out JsonElement site);
  531. var cosmosClient = _azureCosmos.GetCosmosClient();
  532. if ($"{site}".Equals(BIConst.Global))
  533. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  534. List<AreaInfo> areaInfos = new();
  535. 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") }))
  536. {
  537. areaInfos.Add(item);
  538. }
  539. foreach (var area in areaInfos)
  540. {
  541. string scSql = $"select value(c.id) from c where c.areaId='{area.id}'";
  542. List<string> scIds = await CommonFind.GetValueSingle(cosmosClient, "School", scSql,"Base");
  543. //allSize += recSchools.Select(s => s.size).Sum();
  544. area.scCnt = scIds.Count;
  545. int tTchCnt = 0;
  546. int tStuCnt = 0;
  547. if (scIds.Count > 0)
  548. {
  549. scSql = BICommonWay.ManyScSql("REPLACE(c.code, 'Teacher-', '')", scIds);
  550. //查教师
  551. string tchSql = $"select value(count(c.id)) from c where c.pk='Teacher' and {scSql}";
  552. tTchCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", tchSql);
  553. scSql = BICommonWay.ManyScSql("REPLACE(c.code, 'Base-', '')", scIds);
  554. //查学生
  555. string stuSql = $"select value(count(c.id)) from c where c.pk='Base' and {scSql}";
  556. tStuCnt = await CommonFind.GetSqlValueCount(cosmosClient, "Student", stuSql);
  557. }
  558. area.tchCnt = tTchCnt;
  559. area.stuCnt = tStuCnt;
  560. }
  561. return Ok(new { state = RespondCode.Ok, areaInfos });
  562. }
  563. /// <summary>
  564. /// 所有活动分析
  565. /// </summary>
  566. /// <param name="jsonElement"></param>
  567. /// <returns></returns>
  568. [HttpPost("get-allanls")]
  569. public async Task<IActionResult> GetAllAnls(JsonElement jsonElement)
  570. {
  571. jsonElement.TryGetProperty("site", out JsonElement site);
  572. var cosmosClient = _azureCosmos.GetCosmosClient();
  573. if ($"{site}".Equals(BIConst.Global))
  574. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  575. DateTimeOffset dateTime = DateTimeOffset.UtcNow;
  576. var (weeks, weeke) = TimeHelper.GetStartOrEnd(dateTime, "week");
  577. var (terms, terme) = TimeHelper.GetStartOrEnd(dateTime, "term");
  578. var (months, monthe) = TimeHelper.GetStartOrEnd(dateTime, "month");
  579. int allLess = 0; //所有课例
  580. int weekLess = 0; //本周课例
  581. int termLess = 0; //本学期课例
  582. int monthLessCnt = 0; //本月课例
  583. int actAllCnt = 0; //所有活动
  584. int actWeekCnt = 0; //本周活动
  585. int actTermCnt = 0; //本学期活动
  586. int actMonthCnt = 0; //本月活动
  587. //课例
  588. string allLessSql = $"select value(count(c.id)) from c where c.pk='LessonRecord'";
  589. allLess = await CommonFind.GetSqlValueCount(cosmosClient,"School", allLessSql);
  590. string weekLSql = $"{allLessSql} and c.startTime >= {weeks} and c.startTime <={weeke}";
  591. weekLess = await CommonFind.GetSqlValueCount(cosmosClient, "School", weekLSql);
  592. string termLSql = $"{allLessSql} and c.startTime >= {terms} and c.startTime <={terme}";
  593. termLess = await CommonFind.GetSqlValueCount(cosmosClient, "School", termLSql);
  594. string monthLSql = $"{allLessSql} and c.startTime >= {months} and c.startTime <={monthe}";
  595. monthLessCnt = await CommonFind.GetSqlValueCount(cosmosClient, "School", monthLSql);
  596. //活动
  597. actAllCnt = await ActivityWay.GetCnt(cosmosClient);
  598. string weekSql = $" and c.createTime >= {weeks} and c.createTime <={weeke}";
  599. actWeekCnt = await ActivityWay.GetCnt(cosmosClient, weekSql);
  600. string termSql = $" and c.createTime >= {terms} and c.createTime <={terme}";
  601. actTermCnt = await ActivityWay.GetCnt(cosmosClient,termSql);
  602. string monthSql = $" and c.createTime >= {months} and c.createTime <={monthe}";
  603. actMonthCnt = await ActivityWay.GetCnt(cosmosClient, monthSql);
  604. return Ok(new {state = RespondCode.Ok, allLess, weekLess, termLess, monthLessCnt, actAllCnt, actWeekCnt, actTermCnt, actMonthCnt });
  605. }
  606. /// <summary>
  607. /// 依据活动Id查询活动详情信息 数据管理工具——查询工具
  608. /// </summary>
  609. /// <param name="jsonElement"></param>
  610. /// <returns></returns>
  611. [HttpPost("get-info")]
  612. public async Task<IActionResult> GetInfo(JsonElement jsonElement)
  613. {
  614. if (!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
  615. jsonElement.TryGetProperty("activity", out JsonElement activity);
  616. jsonElement.TryGetProperty("isPersonal", out JsonElement isPersonal);
  617. jsonElement.TryGetProperty("site", out JsonElement site);
  618. //if (jsonElement.TryGetProperty("", out JsonElement code)) return BadRequest();
  619. var cosmosClient = _azureCosmos.GetCosmosClient();
  620. if ($"{site}".Equals(BIConst.Global))
  621. cosmosClient = _azureCosmos.GetCosmosClient(name: BIConst.Global);
  622. List<object> infos = new List<object>();
  623. StringBuilder sqlTxt = new StringBuilder($"select * from c where c.id='{id}'");
  624. if (!string.IsNullOrEmpty($"{activity}"))
  625. {
  626. sqlTxt.Append($" and c.pk='{activity}'");
  627. }
  628. if (!string.IsNullOrEmpty($"{isPersonal}"))
  629. {
  630. if (bool.Parse($"{isPersonal}") == true)
  631. {
  632. sqlTxt.Append($" and c.scope='private'");
  633. }
  634. else
  635. {
  636. sqlTxt.Append($" and c.scope='school'");
  637. }
  638. }
  639. await foreach (var item in cosmosClient.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: sqlTxt.ToString(), requestOptions: new QueryRequestOptions() { }))
  640. {
  641. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  642. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  643. {
  644. infos.Add(obj.ToObject<object>());
  645. }
  646. }
  647. return Ok(new { state = 200, infos });
  648. }
  649. /// <summary>
  650. /// 区级信息
  651. /// </summary>
  652. public record AreaInfo
  653. {
  654. public string id { get; set; }
  655. public string name { get; set; }
  656. public string standard { get; set; }
  657. public string standardName { get; set; }
  658. public int scCnt { get; set; } = 0;
  659. public int tchCnt { get; set; } = 0;
  660. public int stuCnt { get; set; } = 0;
  661. }
  662. private class SchoolInfo
  663. {
  664. public string id { get; set; }
  665. public string name { get; set; }
  666. public string picture { get; set; }
  667. //教师人数
  668. public int teacherCount { get; set; }
  669. //学生人数
  670. public long studentCount { get; set; }
  671. //评审人数
  672. public int appraiseCount { get; set; }
  673. //参训人数
  674. public int trainCount { get; set; }
  675. public List<KeyValuePair<object, long>> census { get; set; } = new List<KeyValuePair<object, long>>();
  676. }
  677. private record SchoolLesson
  678. {
  679. public string id { get; set; }
  680. public string name { get; set; }
  681. public string picture { get; set; }
  682. public int count { get; set; }
  683. }
  684. public record ActivityCount
  685. {
  686. public string id { get; set; }
  687. public string name { get; set; }
  688. public List<KeyValuePair<object, long>> census { get; set; } = new List<KeyValuePair<object, long>>();
  689. }
  690. }
  691. }