AbilityStatisticsController.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.Models;
  12. using TEAMModelOS.SDK.DI;
  13. using TEAMModelOS.SDK;
  14. using TEAMModelOS.SDK.Models;
  15. using TEAMModelOS.SDK.Extension;
  16. using static TEAMModelOS.Controllers.SchoolTeacherController;
  17. using HTEXLib.COMM.Helpers;
  18. using TEAMModelOS.Filter;
  19. using static TEAMModelOS.Controllers.AbilitySubController;
  20. using Microsoft.AspNetCore.Authorization;
  21. using TEAMModelOS.SDK.Models.Service;
  22. using ScTeacher = TEAMModelOS.SDK.Models.ScTeacher;
  23. namespace TEAMModelOS.Controllers
  24. {
  25. [ProducesResponseType(StatusCodes.Status200OK)]
  26. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  27. [Route("research")]
  28. [ApiController]
  29. public class AbilityStatisticsController : ControllerBase
  30. {
  31. private readonly AzureCosmosFactory _azureCosmos;
  32. private readonly DingDing _dingDing;
  33. private readonly Option _option;
  34. private readonly CoreAPIHttpService _coreAPIHttpService;
  35. private readonly HttpTrigger _httpTrigger;
  36. private readonly AzureStorageFactory _azureStorage;
  37. public AbilityStatisticsController(AzureStorageFactory azureStorage,HttpTrigger httpTrigger,CoreAPIHttpService coreAPIHttpService, AzureCosmosFactory azureCosmos, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option)
  38. {
  39. _azureCosmos = azureCosmos;
  40. _dingDing = dingDing;
  41. _option = option?.Value;
  42. _coreAPIHttpService=coreAPIHttpService;
  43. _httpTrigger = httpTrigger;
  44. _azureStorage = azureStorage;
  45. }
  46. /// <summary>
  47. /// 更新教师最终学习分数。
  48. /// </summary>
  49. /// <param name="request"></param>
  50. /// <returns></returns>
  51. [ProducesDefaultResponseType]
  52. [Authorize(Roles = "IES")]
  53. [HttpPost("upsert-teacher-finalScore")]
  54. [AuthToken(Roles = "admin,teacher", Permissions = "train-appraise")]
  55. public async Task<IActionResult> UpsertFinalScore(JsonElement request)
  56. {
  57. var (userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
  58. var client = _azureCosmos.GetCosmosClient();
  59. if (!request.TryGetProperty("finalScore", out JsonElement _finalScore)) return BadRequest();
  60. if (!request.TryGetProperty("tmdids", out JsonElement _tmdids)) return BadRequest();
  61. int finalScore = -999;
  62. int.TryParse($"{_finalScore}", out finalScore);
  63. List<string> tmdids = _tmdids.ToObject<List<string>>();
  64. if (tmdids.IsNotEmpty() && finalScore >= -1 && finalScore <= 2)
  65. {
  66. List<TeacherTrain> trains = new List<TeacherTrain>();
  67. string insql = $"select value(c) from c where c.id in ({string.Join(",", tmdids.Select(x => $"'{x}'"))})";
  68. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher")
  69. .GetItemQueryIterator<TeacherTrain>(queryText: insql, requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TeacherTrain-{school}") }))
  70. {
  71. trains.Add(item);
  72. }
  73. List<Task<ItemResponse<TeacherTrain>>> teacherTrains = new List<Task<ItemResponse<TeacherTrain>>>();
  74. trains.ForEach(x => {
  75. x.finalScore = finalScore;
  76. teacherTrains.Add(client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync(x, x.id, new PartitionKey(x.code)));
  77. });
  78. int pagesize = 50;
  79. if (teacherTrains.Count <= pagesize)
  80. {
  81. await Task.WhenAll(teacherTrains);
  82. }
  83. else
  84. {
  85. int pages = (teacherTrains.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
  86. for (int i = 0; i < pages; i++)
  87. {
  88. var listssb = teacherTrains.Skip((i) * pagesize).Take(pagesize).ToList();
  89. await Task.WhenAll(listssb);
  90. }
  91. }
  92. return Ok(new { status = 200 });
  93. }
  94. else
  95. {
  96. return Ok(new { });
  97. }
  98. }
  99. /// <summary>
  100. /// 提交学习总结、。
  101. /// </summary>
  102. /// <param name="request"></param>
  103. /// <returns></returns>
  104. [ProducesDefaultResponseType]
  105. [HttpPost("upsert-teacher-summary")]
  106. [Authorize(Roles = "IES")]
  107. [AuthToken(Roles = "teacher,admin,area")]
  108. public async Task<IActionResult> UpsertSummary(JsonElement request)
  109. {
  110. var client = _azureCosmos.GetCosmosClient();
  111. request.TryGetProperty("summary", out JsonElement _summary);
  112. if (string.IsNullOrEmpty($"{_summary}"))
  113. {
  114. return BadRequest();
  115. }
  116. var (userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
  117. if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  118. try
  119. {
  120. TeacherTrain teacherTrain = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherTrain>(userid, new PartitionKey($"TeacherTrain-{school}"));
  121. teacherTrain.nickname= name;
  122. teacherTrain.name = name;
  123. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  124. List<ScTeacher> teachers = await table.FindListByDict<ScTeacher>(new Dictionary<string, object>() { { "PartitionKey", "ScTeacher" }, { "schoolCode", school }, { "tmdid", $"{teacherTrain.id}" } });
  125. if (teachers.Any())
  126. {
  127. teacherTrain.name = teachers[0].TeacherName;
  128. }
  129. teacherTrain.picture = picture;
  130. teacherTrain.summary = $"{_summary}";
  131. await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<TeacherTrain>(teacherTrain, userid, new PartitionKey($"TeacherTrain-{school}"));
  132. return Ok(new { status = 200 });
  133. }
  134. catch (CosmosException ex)
  135. {
  136. return Ok(new { error = 1, msg = "你没有学习记录" });
  137. }
  138. }
  139. /// <summary>
  140. /// 区级数据统计
  141. /// </summary>
  142. /// <param name="request"></param>
  143. /// <returns></returns>
  144. [ProducesDefaultResponseType]
  145. [HttpPost("statistics-area-simple")]
  146. [AuthToken(Roles = "teacher,admin,area")]
  147. [Authorize(Roles = "IES")]
  148. public async Task<IActionResult> StatisticsAreaSimple(JsonElement request)
  149. {
  150. var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  151. request.TryGetProperty("standard", out JsonElement _standard);
  152. string standard = $"{_standard}";
  153. if (string.IsNullOrEmpty(standard))
  154. {
  155. return BadRequest();
  156. }
  157. var client = _azureCosmos.GetCosmosClient();
  158. Area area = null;
  159. string sql = $"select value(c) from c where c.standard='{standard}'";
  160. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
  161. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  162. {
  163. area = item;
  164. }
  165. AreaSetting setting = null;
  166. if (area != null)
  167. {
  168. try
  169. {
  170. setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
  171. }
  172. catch (CosmosException)
  173. {
  174. setting = null;
  175. }
  176. }
  177. if (setting == null)
  178. {
  179. setting = new AreaSetting
  180. {
  181. allTime = 50,
  182. classTime = 5,
  183. submitTime = 15,
  184. onlineTime = 20,
  185. offlineTime = 10,
  186. lessonMinutes = 45,
  187. };
  188. }
  189. try
  190. {
  191. List<TeacherTrain> teacherTrains = new List<TeacherTrain>();
  192. List<School> schools = new List<School>();
  193. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: $"select value(c) from c where c.areaId='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  194. {
  195. schools.Add(item);
  196. }
  197. List<Task<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>> trains = new List<Task<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>>();
  198. int countArea = 0;
  199. int appraiseArea = 0;
  200. List<SchoolInfos> schoolInfos = new List<SchoolInfos>();
  201. foreach (var school in schools)
  202. {
  203. schoolInfos.Add(new SchoolInfos { schoolId = school.id, schoolName = school.name, picture = school.picture });
  204. //增加评审人员总人数,学习总人数。
  205. trains.Add(StatisticsService.StatisticsSchool(_coreAPIHttpService, school.id, setting, area, client, _dingDing, null));
  206. }
  207. int pagesize = 100;
  208. if (trains.Count <= pagesize)
  209. {
  210. (List<TeacherTrain> trains, List<RGroupList> yxtrain)[] tasks = await Task.WhenAll(trains);
  211. tasks.ToList().ForEach(x => {
  212. teacherTrains.AddRange(x.trains);
  213. schoolInfos.ForEach(y => {
  214. var list = x.yxtrain.Find(z => z.school.Equals(y.schoolId));
  215. if (list != null)
  216. {
  217. y.trainCount = list.members.Count;
  218. }
  219. });
  220. });
  221. }
  222. else
  223. {
  224. int pages = (trains.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
  225. for (int i = 0; i < pages; i++)
  226. {
  227. var listssb = trains.Skip((i) * pagesize).Take(pagesize).ToList();
  228. (List<TeacherTrain> trains, List<RGroupList> yxtrain)[] tasks = await Task.WhenAll(listssb);
  229. tasks.ToList().ForEach(x => {
  230. teacherTrains.AddRange(x.trains);
  231. schoolInfos.ForEach(y => {
  232. var list = x.yxtrain.Find(z => z.school.Equals(y.schoolId));
  233. if (list != null)
  234. {
  235. y.trainCount = list.members.Count;
  236. }
  237. });
  238. });
  239. }
  240. }
  241. long totalTime = teacherTrains.Select(x => x.totalTime).Sum();
  242. int hgcount = teacherTrains.Where(x => x.finalScore == 1 || x.finalScore == 2).Count();
  243. setting.accessConfig = null;
  244. List<dynamic> dynamics = new List<dynamic>();
  245. teacherTrains.ForEach(x => {
  246. x.schoolName = schools.Find(s => s.id.Equals(x.school))?.name;
  247. x.currency.videoTime = (int)x.currency.videoTime;
  248. x.currency.teacherAilities.ForEach(y => {
  249. y.videoTime = (int)y.videoTime;
  250. y.onlineTime = (int)y.onlineTime;
  251. y.debateOrther = y.debateOrther > 0 ? y.debateOrther : 0;
  252. });
  253. dynamic dynamic = new {
  254. x.schoolName,
  255. x.classTime,
  256. x.finalScore,
  257. x.name,
  258. x.offlineTime,
  259. x.onlineTime,
  260. x.school,
  261. x.id,
  262. x.totalTime,
  263. currency = new {
  264. x.currency.submitTime,
  265. x.currency.exerciseAbility,
  266. x.currency.learnAbility,
  267. x.currency.uploadDone,
  268. x.currency.uploadTotal,
  269. x.currency.videoTime,
  270. teacherAilities = x.currency.teacherAilities.Select(t => new {
  271. t.onlineTime,
  272. t.videoTime,
  273. t.no,
  274. t.name,
  275. t.dimension,
  276. t.zpscore, t.xzscore, t.hpscore
  277. }),
  278. },
  279. offlineCountNo = x.offlineRecords.Where(o => o.upload == 0).Count(),
  280. offlineCountOk = x.offlineRecords.Where(o => o.upload == 1).Count(),
  281. teacherClasseCount = x.teacherClasses.Count
  282. };
  283. dynamics.Add(dynamic);
  284. });
  285. //var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  286. //var ScTeachers = await table.FindListByDict<ScTeacher>(new Dictionary<string, object>() { { "PartitionKey", "ScTeacher" }, { "areaId", $"{setting.id}" } });
  287. //var ttt = ScTeachers.Select(x => x.tmdid).Except(teacherTrains.Select(x => x.id));
  288. return Ok(new { teacherTrains= dynamics, setting, schools = schoolInfos, totalTime, hgcount, teacherCount = countArea, appraiseCount = appraiseArea });
  289. }
  290. catch (Exception ex)
  291. {
  292. await _dingDing.SendBotMsg($"OS,{_option.Location},UpsertSubmitScore/UpsertSubmitScore()\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  293. return BadRequest(new { ex.Message, ex.StackTrace });
  294. }
  295. }
  296. /// <summary>
  297. /// 区级数据统计
  298. /// </summary>
  299. /// <param name="request"></param>
  300. /// <returns></returns>
  301. [ProducesDefaultResponseType]
  302. [HttpPost("statistics-area")]
  303. [AuthToken(Roles = "teacher,admin,area")]
  304. [Authorize(Roles = "IES")]
  305. public async Task<IActionResult> StatisticsArea(JsonElement request)
  306. {
  307. var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  308. request.TryGetProperty("standard", out JsonElement _standard);
  309. string standard = $"{_standard}";
  310. if (string.IsNullOrEmpty(standard))
  311. {
  312. return BadRequest();
  313. }
  314. var client = _azureCosmos.GetCosmosClient();
  315. Area area = null;
  316. string sql = $"select value(c) from c where c.standard='{standard}'";
  317. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
  318. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  319. {
  320. area = item;
  321. }
  322. AreaSetting setting = null;
  323. if (area != null)
  324. {
  325. try
  326. {
  327. setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
  328. }
  329. catch (CosmosException)
  330. {
  331. setting = null;
  332. }
  333. }
  334. if (setting == null)
  335. {
  336. setting = new AreaSetting
  337. {
  338. allTime = 50,
  339. classTime = 5,
  340. submitTime = 15,
  341. onlineTime = 20,
  342. offlineTime = 10,
  343. lessonMinutes = 45,
  344. };
  345. }
  346. try
  347. {
  348. List<TeacherTrain> teacherTrains = new List<TeacherTrain>();
  349. List<School> schools = new List<School>();
  350. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<School>(queryText: $"select value(c) from c where c.areaId='{area.id}'", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  351. {
  352. schools.Add(item);
  353. }
  354. List<Task<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>> trains = new List<Task<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>>();
  355. int countArea = 0;
  356. int appraiseArea = 0;
  357. List<SchoolInfos> schoolInfos = new List<SchoolInfos>();
  358. foreach (var school in schools)
  359. {
  360. int count = 0;
  361. int appraise = 0;
  362. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<bool>(queryText: $"select value(ARRAY_CONTAINS( c.permissions,'train-appraise')) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school.id}") }))
  363. {
  364. if (item)
  365. {
  366. appraise += 1;
  367. appraiseArea += 1;
  368. }
  369. countArea += 1;
  370. count += 1;
  371. }
  372. schoolInfos.Add(new SchoolInfos { schoolId = school.id, schoolName = school.name, picture = school.picture, teacherCount = count, appraiseCount = appraise });
  373. //增加评审人员总人数,学习总人数。
  374. trains.Add(StatisticsService.StatisticsSchool(_coreAPIHttpService, school.id, setting, area, client, _dingDing, null));
  375. }
  376. int pagesize = 100;
  377. if (trains.Count <= pagesize)
  378. {
  379. (List<TeacherTrain> trains, List<RGroupList> yxtrain)[] tasks = await Task.WhenAll(trains);
  380. tasks.ToList().ForEach(x => {
  381. teacherTrains.AddRange(x.trains);
  382. schoolInfos.ForEach(y => {
  383. var list = x.yxtrain.Find(z => z.school.Equals(y.schoolId));
  384. if (list != null)
  385. {
  386. y.trainCount = list.members.Count;
  387. }
  388. });
  389. });
  390. }
  391. else
  392. {
  393. int pages = (trains.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
  394. for (int i = 0; i < pages; i++)
  395. {
  396. var listssb = trains.Skip((i) * pagesize).Take(pagesize).ToList();
  397. (List<TeacherTrain> trains, List<RGroupList> yxtrain)[] tasks = await Task.WhenAll(listssb);
  398. tasks.ToList().ForEach(x => {
  399. teacherTrains.AddRange(x.trains);
  400. schoolInfos.ForEach(y => {
  401. var list = x.yxtrain.Find(z => z.school.Equals(y.schoolId));
  402. if (list != null)
  403. {
  404. y.trainCount = list.members.Count;
  405. }
  406. });
  407. });
  408. }
  409. }
  410. long totalTime = teacherTrains.Select(x => x.totalTime).Sum();
  411. int hgcount = teacherTrains.Where(x => x.finalScore == 1 || x.finalScore == 2).Count();
  412. var teacherAilities = teacherTrains.SelectMany(x => x.currency.teacherAilities);
  413. setting.accessConfig = null;
  414. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  415. teacherTrains.ForEach(x => {
  416. x.currency.videoTime = (int)x.currency.videoTime;
  417. x.currency.teacherAilities.ForEach(y => {
  418. y.videoTime = (int)y.videoTime;
  419. y.onlineTime = (int)y.onlineTime;
  420. y.debateOrther = y.debateOrther > 0 ? y.debateOrther : 0;
  421. });
  422. });
  423. return Ok(new { teacherTrains, setting, schools = schoolInfos, totalTime, hgcount, teacherAilities, teacherCount = countArea, appraiseCount = appraiseArea });
  424. }
  425. catch (Exception ex)
  426. {
  427. await _dingDing.SendBotMsg($"OS,{_option.Location},UpsertSubmitScore/UpsertSubmitScore()\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  428. return BadRequest(new { ex.Message, ex.StackTrace });
  429. }
  430. }
  431. public class SchoolInfos
  432. {
  433. public string schoolId { get; set; }
  434. public string schoolName { get; set; }
  435. public string picture { get; set; }
  436. public int teacherCount { get; set; }
  437. public int appraiseCount { get; set; }
  438. public int trainCount { get; set; }
  439. }
  440. /// <summary>
  441. /// 校级数据统计
  442. /// </summary>
  443. /// <param name="request"></param>
  444. /// <returns></returns>
  445. [ProducesDefaultResponseType]
  446. [HttpPost("push-school-data")]
  447. //[AuthToken(Roles = "teacher,admin,area")]
  448. //[Authorize(Roles = "IES")]
  449. public async Task<IActionResult> SchoolDataPush(JsonElement request) {
  450. var client = _azureCosmos.GetCosmosClient();
  451. request.TryGetProperty("standard", out JsonElement standard);
  452. request.TryGetProperty("school", out JsonElement _school);
  453. request.TryGetProperty("pushTeachers", out JsonElement _pushTeachers);
  454. List<string> pushTeachers = _pushTeachers.ToObject<List<string>>();
  455. (string accessConfig, Area area, AreaSetting setting) = await ThirdService.GetAccessConfig(client, $"{standard}");
  456. School school= await client.GetContainer(Constant.TEAMModelOS, Constant.School).ReadItemAsync<School>($"{_school}",new PartitionKey("Base"));
  457. if (setting.id.Equals($"{school.areaId}"))
  458. {
  459. string json = await ThirdService.SchoolDataPush(setting, _httpTrigger, pushTeachers, _option, $"{_school}");
  460. if (!string.IsNullOrWhiteSpace(json)) {
  461. JsonElement element = json.ToObject<JsonElement>();
  462. return Ok(new { element });
  463. }
  464. }
  465. else {
  466. return BadRequest("学校不属于该区");
  467. }
  468. return Ok(pushTeachers);
  469. }
  470. /// <summary>
  471. /// 校级数据统计
  472. /// </summary>
  473. /// <param name="request"></param>
  474. /// <returns></returns>
  475. [ProducesDefaultResponseType]
  476. [HttpPost("get-school-appraise")]
  477. [AuthToken(Roles = "teacher,admin,area")]
  478. [Authorize(Roles = "IES")]
  479. public async Task<IActionResult> GetSchoolAppraise(JsonElement request)
  480. {
  481. request.TryGetProperty("school", out JsonElement _school);
  482. string school = "";
  483. var (userid, _, _, __school) = HttpContext.GetAuthTokenInfo();
  484. if (!string.IsNullOrEmpty($"{_school}"))
  485. {
  486. school = $"{_school}";
  487. }
  488. else
  489. {
  490. school = __school;
  491. }
  492. request.TryGetProperty("update", out JsonElement _update);
  493. HashSet<string> update = null;
  494. if (_update.ValueKind.Equals(JsonValueKind.Array))
  495. {
  496. update = _update.ToObject<HashSet<string>>();
  497. }
  498. if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  499. var client = _azureCosmos.GetCosmosClient();
  500. Area area = null;
  501. string sql = $"select value(c) from c where c.standard='{standard}'";
  502. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
  503. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  504. {
  505. area = item;
  506. }
  507. AreaSetting setting = null;
  508. if (area != null)
  509. {
  510. try
  511. {
  512. //优先找校级
  513. setting = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<AreaSetting>(school, new PartitionKey("AreaSetting"));
  514. }
  515. catch (CosmosException)
  516. {
  517. try
  518. {
  519. setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
  520. }
  521. catch (CosmosException)
  522. {
  523. setting = null;
  524. }
  525. }
  526. }
  527. if (setting == null)
  528. {
  529. setting = new AreaSetting
  530. {
  531. allTime = 50,
  532. classTime = 5,
  533. submitTime = 15,
  534. onlineTime = 20,
  535. offlineTime = 10,
  536. lessonMinutes = 45,
  537. };
  538. }
  539. try
  540. {
  541. int count = 0;
  542. int appraise = 0;
  543. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School").GetItemQueryIterator<bool>(queryText: $" select value(ARRAY_CONTAINS( c.permissions,'train-appraise') ) from c", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Teacher-{school}") }))
  544. {
  545. if (item)
  546. {
  547. appraise += 1;
  548. }
  549. count += 1;
  550. }
  551. //增加评审人员总人数,学习总人数。
  552. (List<TeacherTrain> teacherTrains, List<RGroupList> yxtrain) = await StatisticsService.StatisticsSchool(_coreAPIHttpService, school, setting, area, client, _dingDing, update);
  553. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  554. teacherTrains.ForEach(x => {
  555. x.currency.videoTime = (int)x.currency.videoTime;
  556. x.currency.teacherAilities.ForEach(y => {
  557. y.videoTime = (int)y.videoTime;
  558. y.onlineTime = (int)y.onlineTime;
  559. y.debateOrther= y.debateOrther>0? y.debateOrther: 0;
  560. });
  561. });
  562. //try
  563. //{
  564. // teacherTrain = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherTrain>(userid, new PartitionKey($"TeacherTrain-{school}"));
  565. //}
  566. //catch (CosmosException)
  567. //{
  568. // await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<TeacherTrain>(teacherTrain, new PartitionKey($"TeacherTrain-{school}"));
  569. //}
  570. setting.accessConfig = null;
  571. return Ok(new { teacherTrains, setting, teacherCount = count, appraiseCount = appraise, yxtrain });
  572. }
  573. catch (Exception ex)
  574. {
  575. return BadRequest(new { ex.Message, ex.StackTrace });
  576. }
  577. }
  578. /// <summary>
  579. /// 教师个人数据统计
  580. /// </summary>
  581. /// <param name="request"></param>
  582. /// <returns></returns>
  583. [ProducesDefaultResponseType]
  584. [HttpPost("statistics-teacher")]
  585. [AuthToken(Roles = "teacher,admin,area")]
  586. [Authorize(Roles = "IES")]
  587. public async Task<IActionResult> StatisticsTeacher(JsonElement request)
  588. {
  589. //{"tmdid":"1595321354","school":"hbcn"}
  590. var (_userid, name, picture, school) = HttpContext.GetAuthTokenInfo();
  591. if (!HttpContext.Items.TryGetValue("Standard", out object standard)) return BadRequest();
  592. var client = _azureCosmos.GetCosmosClient();
  593. request.TryGetProperty("update", out JsonElement _update);
  594. HashSet<string> update = null;
  595. if (_update.ValueKind.Equals(JsonValueKind.Array))
  596. {
  597. update = _update.ToObject<HashSet<string>>();
  598. }
  599. request.TryGetProperty("tmdid", out JsonElement _tmdid);
  600. string tmdid = "";
  601. if (_tmdid.ValueKind.Equals(JsonValueKind.String))
  602. {
  603. tmdid = $"{_tmdid}";
  604. }
  605. else
  606. {
  607. tmdid = $"{_userid}";
  608. }
  609. Area area = null;
  610. string sql = $"select value(c) from c where c.standard='{standard}'";
  611. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Normal").GetItemQueryIterator<Area>(queryText: sql,
  612. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base-Area") }))
  613. {
  614. area = item;
  615. }
  616. AreaSetting setting = null;
  617. if (area != null)
  618. {
  619. try
  620. {
  621. //优先找校级
  622. setting = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<AreaSetting>(school, new PartitionKey("AreaSetting"));
  623. }
  624. catch (CosmosException)
  625. {
  626. try
  627. {
  628. setting = await client.GetContainer(Constant.TEAMModelOS, "Normal").ReadItemAsync<AreaSetting>(area.id, new PartitionKey("AreaSetting"));
  629. }
  630. catch (CosmosException)
  631. {
  632. setting = null;
  633. }
  634. }
  635. }
  636. if (setting == null)
  637. {
  638. setting = new AreaSetting
  639. {
  640. allTime = 50,
  641. classTime = 5,
  642. submitTime = 15,
  643. onlineTime = 20,
  644. offlineTime = 10,
  645. lessonMinutes = 45,
  646. };
  647. }
  648. try
  649. {
  650. TeacherTrain teacherTrain = null;
  651. try
  652. {
  653. teacherTrain = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherTrain>(tmdid, new PartitionKey($"TeacherTrain-{school}"));
  654. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  655. teacherTrain.nickname = name;
  656. teacherTrain.name = name;
  657. if (!string.IsNullOrWhiteSpace(setting.accessConfig))
  658. {
  659. List<ScTeacher> teachers = await table.FindListByDict<ScTeacher>(new Dictionary<string, object>() { { "PartitionKey", "ScTeacher" }, { "areaId", setting.id }, { "schoolCode", school }, { "tmdid", $"{_tmdid}" } });
  660. if (teachers.Any())
  661. {
  662. teacherTrain.name = teachers[0].TeacherName;
  663. }
  664. }
  665. teacherTrain.picture = picture;
  666. if (update != null && update.Count > 0)
  667. {
  668. foreach (var up in update)
  669. {
  670. teacherTrain.update.Add(up);
  671. }
  672. }
  673. ///强制教师个人信息统计的时候 都再去统计一次。
  674. teacherTrain.update.Add(StatisticsService.TeacherAbility);
  675. if (teacherTrain.update.Count > 0)
  676. {
  677. await StatisticsService.StatisticsTeacher(teacherTrain, setting, area, client, null);
  678. }
  679. }
  680. catch (CosmosException)
  681. {
  682. string tname = name;
  683. if (!string.IsNullOrWhiteSpace(setting.accessConfig)) {
  684. var table = _azureStorage.GetCloudTableClient().GetTableReference("ScYxpt");
  685. List<ScTeacher> teachers = await table.FindListByDict<ScTeacher>(new Dictionary<string, object>() { { "PartitionKey", "ScTeacher" }, { "areaId", setting.id }, { "schoolCode", school }, { "tmdid", $"{_tmdid}" } });
  686. if (teachers.Any())
  687. {
  688. tname = teachers[0].TeacherName;
  689. }
  690. }
  691. teacherTrain = await StatisticsService.StatisticsTeacher(new TeacherTrain
  692. {
  693. pk = "TeacherTrain",
  694. id = tmdid,
  695. code = $"TeacherTrain-{school}",
  696. tmdid = tmdid,
  697. nickname= name,
  698. school = school,
  699. name = tname,
  700. picture = picture,
  701. update = new HashSet<string> { StatisticsService.TeacherAbility,
  702. StatisticsService.TeacherClass, StatisticsService.OfflineRecord }
  703. }, setting, area, client, null);
  704. }
  705. setting.accessConfig = null;
  706. teacherTrain.currency.videoTime = (int)teacherTrain.currency.videoTime;
  707. teacherTrain.currency.teacherAilities.ForEach(y => {
  708. y.videoTime = (int)y.videoTime;
  709. y.onlineTime = (int)y.onlineTime;
  710. y.debateOrther = y.debateOrther > 0 ? y.debateOrther : 0;
  711. });
  712. return Ok(new { teacherTrain, setting });
  713. }
  714. catch (Exception ex)
  715. {
  716. return BadRequest(new { ex.Message, ex.StackTrace });
  717. }
  718. }
  719. }
  720. }