StatisticsService.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. using Azure.Cosmos;
  2. using Azure.Messaging.ServiceBus;
  3. using HTEXLib.COMM.Helpers;
  4. using Microsoft.AspNetCore.Http;
  5. using Microsoft.Extensions.Configuration;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.SDK.DI;
  12. using TEAMModelOS.SDK.Extension;
  13. using TEAMModelOS.SDK.Models;
  14. namespace TEAMModelOS.SDK
  15. {
  16. public static class StatisticsService
  17. {
  18. /// <summary>
  19. /// 教师能力点操作
  20. /// </summary>
  21. public const string TeacherAility = "TeacherAility";
  22. /// <summary>
  23. /// 课堂实录
  24. /// </summary>
  25. public const string TeacherClass = "TeacherClass";
  26. /// <summary>
  27. /// 线下研修
  28. /// </summary>
  29. public const string OfflineRecord = "OfflineRecord";
  30. /// <summary>
  31. /// 教师投票活动
  32. /// </summary>
  33. public const string TeacherVote = "TeacherVote";
  34. /// <summary>
  35. /// 教师作业活动
  36. /// </summary>
  37. public const string TeacherHomework = "TeacherHomework";
  38. /// <summary>
  39. /// 教师问卷活动
  40. /// </summary>
  41. public const string TeacherSurvey = "TeacherSurvey";
  42. /// <summary>
  43. /// 教师评测活动
  44. /// </summary>
  45. public const string TeacherExamLite = "TeacherExamLite";
  46. public static async Task SendServiceBus(string standard,string tmdid,string school,string update,int statistics, IConfiguration _configuration, AzureServiceBusFactory _serviceBus) {
  47. TeacherTrainChange change = new TeacherTrainChange
  48. {
  49. standard = standard,
  50. tmdid =tmdid,
  51. school =school,
  52. update = new HashSet<string>(new List<string> { update }),
  53. statistics = statistics
  54. };
  55. var messageChange = new ServiceBusMessage(change.ToJsonString());
  56. messageChange.ApplicationProperties.Add("name", "TeacherTrainChange");
  57. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  58. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageChange);
  59. }
  60. public static async Task GetAreaAndAreaSetting( string schoolId, string _standard, CosmosClient client, HttpContext httpContext)
  61. {
  62. School school = null;
  63. AreaSetting setting = null;
  64. string standard = "";
  65. if (string.IsNullOrEmpty(_standard))
  66. {
  67. standard = _standard;
  68. }
  69. else if(!string.IsNullOrEmpty(schoolId)) {
  70. //优先找校级
  71. setting = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<AreaSetting>(schoolId, new PartitionKey("AreaSetting"));
  72. //优先找校级
  73. school = await client.GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(schoolId, new PartitionKey("Base"));
  74. }
  75. }
  76. public static async Task<List<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>> StatisticsArea(AreaSetting setting, Area area, CosmosClient client, DingDing _dingDing, HashSet<string> updates)
  77. {
  78. List<(List<TeacherTrain> trains, List<RGroupList> yxtrain)> teacherTrains = new List<(List<TeacherTrain> trains, List<RGroupList> yxtrain)>() ;
  79. List<School> schools = new List<School>();
  80. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "School")
  81. .GetItemQueryIterator<School>(queryText: $"select value(c) from c where c.areaId='{area.id}' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Base") }))
  82. {
  83. schools.Add(item);
  84. }
  85. await foreach ((List<TeacherTrain> trains, List<RGroupList> yxtrain) tarain in GetStatisticsSchool(schools, setting, area, client,_dingDing,updates))
  86. {
  87. teacherTrains.Add(tarain);
  88. }
  89. return teacherTrains;
  90. }
  91. private static async IAsyncEnumerable<(List<TeacherTrain> trains, List<RGroupList> yxtrain)> GetStatisticsSchool(List<School> schools, AreaSetting setting, Area area, CosmosClient client, DingDing _dingDing, HashSet<string> updates)
  92. {
  93. foreach (var school in schools)
  94. {
  95. yield return await StatisticsSchool(school.id, setting, area, client,_dingDing,updates);
  96. }
  97. }
  98. public static async Task<(List<TeacherTrain> trains, List<RGroupList> yxtrain)> StatisticsSchool(string school, AreaSetting setting, Area area, CosmosClient client,DingDing _dingDing,HashSet<string> updates) {
  99. List<RGroupList> yxtrain = await GroupListService.GetGroupListMemberByType(client, "yxtrain", new List<string> { "school" }, $"{school}", _dingDing);
  100. List<TeacherTrain> trains = new List<TeacherTrain>();
  101. var members = yxtrain.SelectMany(x => x.members).ToList();
  102. if (members.Count <= 0)
  103. {
  104. return (trains, yxtrain);
  105. }
  106. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher")
  107. .GetItemQueryIterator<TeacherTrain>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"TeacherTrain-{school}") })) {
  108. trains.Add(item);
  109. }
  110. if (updates != null) {
  111. foreach (var up in updates)
  112. {
  113. trains.ForEach(x => x.updateProperty.Add(up));
  114. }
  115. }
  116. var update = trains.FindAll(x => x.updateProperty.Count() > 0);
  117. var noupdate = trains.FindAll(x => x.updateProperty.Count() <=0);
  118. var unStatistics = members.Select(x => x.id).Except(trains.Select(x => x.id));
  119. List<TeacherTrain> teacherTrains = new List<TeacherTrain>();
  120. List<TeacherTrain> returnTrains = new List<TeacherTrain>();
  121. if (update.IsNotEmpty()) {
  122. teacherTrains.AddRange(update);
  123. }
  124. if (unStatistics != null) {
  125. foreach (string x in unStatistics) {
  126. var member = members.Find(y => y.id.Equals(x));
  127. teacherTrains.Add(new TeacherTrain
  128. {
  129. pk = "TeacherTrain",
  130. id = x,
  131. code = $"TeacherTrain-{school}",
  132. tmdid = x,
  133. name = member.name,
  134. picture=member.picture,
  135. school = school,
  136. updateProperty = new HashSet<string> { TeacherAility,TeacherClass,OfflineRecord }
  137. });
  138. }
  139. }
  140. List<Study> studies = new List<Study>();
  141. await foreach (var item in client.GetContainer("TEAMModelOS", "Common")
  142. .GetItemQueryIterator<Study>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Study-{school}") }))
  143. {
  144. studies.Add(item);
  145. }
  146. returnTrains = await GetStatisticsTeacher(teacherTrains, setting, area, client,studies);
  147. //await foreach (var tarain in GetStatisticsTeacher(teacherTrains, setting, area, client))
  148. //{
  149. // returnTrains.Add(tarain);
  150. //}
  151. if (noupdate.IsNotEmpty())
  152. {
  153. returnTrains.AddRange(noupdate);
  154. }
  155. returnTrains.ForEach(x => {
  156. var mbm= members.Find(y => y.id.Equals(x.id));
  157. x.groupName = mbm?.groupName;
  158. x.name = mbm?.name;
  159. x.picture = mbm?.picture;
  160. });
  161. return (returnTrains, yxtrain);
  162. }
  163. private static async Task<List<TeacherTrain>> GetStatisticsTeacher(List<TeacherTrain> trains, AreaSetting setting, Area area, CosmosClient client, List<Study> studies)
  164. {
  165. List<Task<TeacherTrain>> teachers = new List<Task<TeacherTrain>>();
  166. foreach (var train in trains)
  167. {
  168. teachers.Add(StatisticsTeacher(train, setting, area, client,studies)); //yield return await StatisticsTeacher( train, setting, area, client);
  169. }
  170. int pagesize = 50;
  171. if (teachers.Count <= pagesize)
  172. {
  173. await Task.WhenAll(teachers);
  174. }
  175. else
  176. {
  177. int pages = (teachers.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
  178. for (int i = 0; i < pages; i++)
  179. {
  180. var lists = teachers.Skip((i) * pagesize).Take(pagesize).ToList();
  181. await Task.WhenAll(lists);
  182. }
  183. }
  184. return trains;
  185. }
  186. public static async Task<TeacherTrain> StatisticsTeacher(TeacherTrain train, AreaSetting setting, Area area, CosmosClient client,List<Study> studies) {
  187. string _school = train.school;
  188. string _tmdid = train.tmdid;
  189. // TeacherTrain teacher_train = null;
  190. List<Task<TeacherTrain>> teachers = new List<Task<TeacherTrain>>();
  191. if (train.updateProperty.Count > 0) {
  192. foreach (string property in train.updateProperty) {
  193. teachers.Add(DoProperty(train.updateProperty, property, setting, area, client, train,studies));
  194. }
  195. int pagesize = 50;
  196. if (teachers.Count <= pagesize)
  197. {
  198. await Task.WhenAll(teachers);
  199. }
  200. else
  201. {
  202. int pages = (teachers.Count + pagesize) / pagesize; //256是批量操作最大值,pages = (total + max -1) / max;
  203. for (int i = 0; i < pages; i++)
  204. {
  205. var lists = teachers.Skip((i) * pagesize).Take(pagesize).ToList();
  206. await Task.WhenAll(lists);
  207. }
  208. }
  209. }
  210. //每次都统计活动相关的数据。
  211. // train= await DoActivity(train, setting, area, client, _school, _tmdid);
  212. train.totalTime = train.onlineTime + train.classTime + train.currency.submitTime + train.offlineTime;
  213. if (train.totalTime >= setting.allTime)
  214. {
  215. //如果总学生超过50 且不是优秀则至少是合格。
  216. if (train.finalScore != 2)
  217. {
  218. train.finalScore = 1;
  219. }
  220. }
  221. // 50> 学时>0 是不合格
  222. else if (train.totalTime < setting.allTime && train.totalTime > 0)
  223. {
  224. train.finalScore = 0;
  225. }
  226. else {
  227. //学时<=0 则是为
  228. train.finalScore = -1;
  229. }
  230. await client.GetContainer(Constant.TEAMModelOS, "Teacher").UpsertItemAsync<TeacherTrain>(train, new PartitionKey($"TeacherTrain-{_school}"));
  231. return train;
  232. }
  233. private static async Task<TeacherTrain> DoProperty(HashSet<string> updateProperty,string property, AreaSetting setting, Area area, CosmosClient client, TeacherTrain train ,List<Study> studies )
  234. {
  235. string _school = train.school;
  236. string _tmdid = train.tmdid;
  237. switch (property) {
  238. case TeacherAility:
  239. train = await DoTeacherAility(train, setting, area, client, _school, _tmdid);
  240. train.updateProperty.Remove(TeacherAility);
  241. break;
  242. //课堂实录更新
  243. case TeacherClass:
  244. train = await DoTeacherClass(train, setting, area, client, _school, _tmdid);
  245. train.updateProperty.Remove(TeacherClass);
  246. break;
  247. //线下研修
  248. case OfflineRecord:
  249. train = await DoOfflineRecord(train, setting, area, client, _school, _tmdid,studies);
  250. train.updateProperty.Remove(OfflineRecord);
  251. break;
  252. //投票
  253. case TeacherVote:
  254. train = await DoTeacherVote(train, setting, area, client, _school, _tmdid);
  255. train.updateProperty.Remove(TeacherVote);
  256. break;
  257. //问卷
  258. case TeacherSurvey:
  259. train = await DoTeacherSurvey(train, setting, area, client, _school, _tmdid);
  260. train.updateProperty.Remove(TeacherSurvey);
  261. break;
  262. //作业
  263. //case TeacherHomework:
  264. // train = await DoTeacherHomework(train, setting, area, client, _school, _tmdid);
  265. // train.updateProperty.Remove(TeacherHomework);
  266. // break;
  267. //评测
  268. case TeacherExamLite:
  269. train = await DoTeacherExamLite(train, setting, area, client, _school, _tmdid);
  270. train.updateProperty.Remove(TeacherExamLite);
  271. break;
  272. default:
  273. train.updateProperty.Remove(property);
  274. break;
  275. }
  276. return train;
  277. }
  278. public static async Task<TeacherTrain> DoTeacherVote(TeacherTrain train, AreaSetting setting, Area area, CosmosClient client, string _school, string _tmdid) {
  279. int voteJoin = 0;
  280. int voteDone = 0;
  281. int voteAreaJoin = 0;
  282. int voteAreaDone = 0;
  283. //投票活动
  284. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher")
  285. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Vote' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  286. {
  287. if (!string.IsNullOrEmpty(item.owner))
  288. {
  289. if (item.owner.Equals("school"))
  290. {
  291. voteJoin += 1;
  292. if (item.taskStatus > 0)
  293. {
  294. voteDone += 1;
  295. }
  296. }
  297. else if (item.owner.Equals("area"))
  298. {
  299. voteAreaJoin += 1;
  300. if (item.taskStatus > 0)
  301. {
  302. voteAreaDone += 1;
  303. }
  304. }
  305. }
  306. }
  307. train.voteJoin = voteJoin;
  308. train.voteDone = voteDone;
  309. train.voteAreaJoin = voteAreaJoin;
  310. train.voteAreaDone = voteAreaDone;
  311. return train;
  312. }
  313. public static async Task<TeacherTrain> DoTeacherSurvey(TeacherTrain train, AreaSetting setting, Area area, CosmosClient client, string _school, string _tmdid)
  314. {
  315. //问卷调查
  316. int surveyJoin = 0;
  317. int surveyDone = 0;
  318. int surveyAreaJoin = 0;
  319. int surveyAreaDone = 0;
  320. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Teacher")
  321. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'Survey' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  322. {
  323. if (!string.IsNullOrEmpty(item.owner))
  324. {
  325. if (item.owner.Equals("school"))
  326. {
  327. surveyJoin += 1;
  328. if (item.taskStatus > 0)
  329. {
  330. surveyDone += 1;
  331. }
  332. }
  333. else if (item.owner.Equals("area"))
  334. {
  335. surveyAreaJoin += 1;
  336. if (item.taskStatus > 0)
  337. {
  338. surveyAreaDone += 1;
  339. }
  340. }
  341. }
  342. }
  343. train.surveyJoin = surveyJoin;
  344. train.surveyDone = surveyDone;
  345. train.surveyAreaJoin = surveyAreaJoin;
  346. train.surveyAreaDone = surveyAreaDone;
  347. return train;
  348. }
  349. public static async Task<TeacherTrain> DoTeacherExamLite(TeacherTrain train, AreaSetting setting, Area area, CosmosClient client, string _school, string _tmdid)
  350. {
  351. //问卷调查
  352. int examJoin = 0;
  353. int examDone = 0;
  354. int examAreaJoin = 0;
  355. int examAreaDone = 0;
  356. //评量检测
  357. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher")
  358. .GetItemQueryIterator<StuActivity>(queryText: $"select c.owner, c.taskStatus from c where c.type = 'ExamLite' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  359. {
  360. if (!string.IsNullOrEmpty(item.owner))
  361. {
  362. if (item.owner.Equals("school"))
  363. {
  364. examJoin += 1;
  365. if (item.taskStatus > 0)
  366. {
  367. examDone += 1;
  368. }
  369. }
  370. else if (item.owner.Equals("area"))
  371. {
  372. examAreaJoin += 1;
  373. if (item.taskStatus > 0)
  374. {
  375. examAreaDone += 1;
  376. }
  377. }
  378. }
  379. }
  380. train.examJoin = examJoin;
  381. train.examDone = examDone;
  382. train.examAreaJoin = examAreaJoin;
  383. train.examAreaDone = examAreaDone;
  384. return train;
  385. }
  386. /// <summary>
  387. /// 课堂实录更新
  388. /// </summary>
  389. /// <param name="train"></param>
  390. /// <param name="setting"></param>
  391. /// <param name="area"></param>
  392. /// <param name="client"></param>
  393. /// <param name="_school"></param>
  394. /// <param name="_tmdid"></param>
  395. /// <returns></returns>
  396. public static async Task<TeacherTrain> DoOfflineRecord(TeacherTrain train, AreaSetting setting, Area area, CosmosClient client, string _school, string _tmdid, List<Study> studies) {
  397. //owner: school area
  398. //线下 学校研修活动
  399. List<StuActivity> activities = new List<StuActivity>();
  400. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher")
  401. .GetItemQueryIterator<StuActivity>(queryText: $"select value(c) from c where c.type = 'Study' ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Activity-{_tmdid}") }))
  402. {
  403. activities.Add(item);
  404. }
  405. string insql = "";
  406. if (studies.IsEmpty()) {
  407. studies = new List<Study>();
  408. if (activities.IsNotEmpty())
  409. {
  410. insql = $" where c.id in ({string.Join(",", activities.Select(o => $"'{o.id}'"))})";
  411. }
  412. await foreach (var item in client.GetContainer("TEAMModelOS", "Common")
  413. .GetItemQueryIterator<Study>(queryText: $"select value(c) from c {insql} ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Study-{_school}") }))
  414. {
  415. studies.Add(item);
  416. }
  417. }
  418. List<StudyRecord> studyRecords = new List<StudyRecord>();
  419. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher")
  420. .GetItemQueryIterator<StudyRecord>(queryText: $"select value(c) from c {insql} ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"StudyRecord-{_tmdid}") }))
  421. {
  422. studyRecords.Add(item);
  423. }
  424. List<HomeworkRecord> homeworkRecords = new List<HomeworkRecord>();
  425. List<Study> workids= studies.FindAll(x => !string.IsNullOrEmpty(x.workId));
  426. if (workids.IsNotEmpty()) {
  427. string rcdsql = $" where c.id in ({string.Join(",", workids.Select(o => $"'{o.workId}'"))})";
  428. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher")
  429. .GetItemQueryIterator<HomeworkRecord>(queryText: $"select value(c) from c {rcdsql} ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"HomeworkRecord-{_tmdid}") }))
  430. {
  431. homeworkRecords.Add(item);
  432. }
  433. }
  434. List<OfflineRecord> offlines = new List<OfflineRecord>();
  435. activities.ForEach(item => {
  436. Study study = studies.Find(y=>y.id.Equals(item.id));
  437. if (!string.IsNullOrEmpty(item.owner) && study != null
  438. //&& !string.IsNullOrEmpty(study.workId)
  439. && !item.owner.Equals("area") )
  440. {
  441. StudyRecord studyRecord = studyRecords.Find(y => y.id.Equals(item.id));
  442. OfflineRecord record = new OfflineRecord
  443. {
  444. id = item.id,
  445. name = item.name,
  446. done = item.taskStatus,
  447. owner = item.owner
  448. };
  449. record.sethour = study.hour;
  450. //
  451. if (!string.IsNullOrEmpty(study.workId))
  452. {
  453. HomeworkRecord homeworkRecord = homeworkRecords.Find(y => y.id.Equals(study.workId));
  454. Attachment attachment = homeworkRecord != null ? homeworkRecord.content.Find(x => x.prime) : null;
  455. record.haswork = 1;
  456. //有作业的必须检查有没有提交作业,只有提交作业通过才能统计获得学时。
  457. if (null != attachment)
  458. {
  459. record.url = attachment.url;
  460. record.upload = 1;
  461. record.hash = attachment.hash;
  462. record.size = attachment.size;
  463. if (null != studyRecord)
  464. {
  465. //通过获得学时
  466. record.hour = studyRecord.status == 1 ? study.hour : 0;
  467. record.score = studyRecord.status;
  468. if (record.score >= 0)
  469. {
  470. record.done = 1;
  471. }
  472. else
  473. {
  474. record.score = -1;
  475. }
  476. }
  477. }
  478. }
  479. else {
  480. //没有作业的 可以直接统计通过获得学时
  481. if (null != studyRecord)
  482. {
  483. //通过获得学时
  484. record.hour = studyRecord.status == 1 ? study.hour : 0;
  485. record.score = studyRecord.status;
  486. if (record.score >= 0)
  487. {
  488. record.done = 1;
  489. }
  490. else
  491. {
  492. record.score = -1;
  493. }
  494. }
  495. }
  496. offlines.Add(record);
  497. }
  498. });
  499. int sum = offlines.Select(x => x.hour).Sum();
  500. train.offlineTime =sum >setting.offlineTime?setting.offlineTime: sum;
  501. train.offlineRecords= offlines;
  502. return train;
  503. }
  504. /// <summary>
  505. /// 课堂实录更新
  506. /// </summary>
  507. /// <param name="train"></param>
  508. /// <param name="setting"></param>
  509. /// <param name="area"></param>
  510. /// <param name="client"></param>
  511. /// <param name="_school"></param>
  512. /// <param name="_tmdid"></param>
  513. /// <returns></returns>
  514. public static async Task<TeacherTrain> DoTeacherClass(TeacherTrain train, AreaSetting setting, Area area, CosmosClient client, string _school, string _tmdid)
  515. {
  516. string code = $"ClassVideo-{_school}";
  517. ClassVideo classVideo = null;
  518. try { classVideo = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ClassVideo>($"{_tmdid}", new PartitionKey(code));
  519. } catch (Exception ex) {
  520. classVideo = null;
  521. }
  522. if (classVideo != null && classVideo.files.IsNotEmpty())
  523. {
  524. //2021.11.17 15:05,与J哥确认,取课堂实录第一个。前端也只show第一个视频。
  525. var files = classVideo.files[0];
  526. if (files.score > 0)
  527. {
  528. train.classTime = setting.classTime;
  529. }
  530. else
  531. {
  532. train.classTime = 0;
  533. }
  534. train.teacherClasses = new List<TeacherClass> { new Models.TeacherClass { url = files.url, score = files.score, hash = files.hash, name = files.name, size = files.size } };
  535. }
  536. else {
  537. train.classTime = 0;
  538. }
  539. return train;
  540. }
  541. public static async Task<TeacherTrain> DoTeacherAility(TeacherTrain train, AreaSetting setting,Area area , CosmosClient client,string _school,string _tmdid) {
  542. //视频播放
  543. List<string> abilityIds = new List<string> ();
  544. TeacherFile file = null;
  545. try
  546. {
  547. file = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherFile>(_tmdid, new PartitionKey($"TeacherFile-{_school}"));
  548. }
  549. catch (CosmosException )
  550. {
  551. file = new TeacherFile
  552. {
  553. id = _tmdid,
  554. code = $"TeacherFile-{_school}",
  555. pk = "TeacherFile",
  556. ttl = -1,
  557. };
  558. await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<TeacherFile>(file, new PartitionKey($"TeacherFile-{_school}"));
  559. }
  560. List<AbilitySub> abilitySubs= new List<AbilitySub> ();
  561. //认证材料
  562. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher")
  563. .GetItemQueryIterator<AbilitySub>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilitySub-{_school}-{_tmdid}") }))
  564. {
  565. abilitySubs.Add(item);
  566. }
  567. List<Ability> abilities = new List<Ability>();
  568. string insql = "";
  569. if (abilitySubs.IsNotEmpty()) {
  570. insql =$" where c.id in ({string.Join(",", abilitySubs.Select(o => $"'{o.id}'"))})";
  571. }
  572. await foreach (var item in client.GetContainer("TEAMModelOS", "Normal")
  573. .GetItemQueryIterator<Ability>(queryText: $"select c.id,c.name,c.currency,c.no,c.dimension,c.hour,c.stds,c.abilityCount from c {insql} ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"Ability-{area.standard}") }))
  574. {
  575. abilities.Add(item);
  576. }
  577. Currency currency= new Currency();
  578. Currency currencyAll = new Currency();
  579. abilitySubs.ForEach(item => {
  580. int currencyInt = item.from == 1 ? 1 : 0;
  581. Ability ability = abilities.Find(x=>x.id.Equals(item.id));
  582. if (ability != null) {
  583. if (ability != null)
  584. {
  585. currencyInt = item.from == 0 ? ability.currency : 1;
  586. }
  587. else
  588. {
  589. currencyInt = 0;
  590. }
  591. if (item.uploads.IsNotEmpty())
  592. {
  593. if (currencyInt == 1)
  594. {
  595. currency.uploadDone += item.uploads.Count;
  596. }
  597. currencyAll.uploadDone += item.uploads.Count;
  598. }
  599. //通过能力点自测
  600. if (item.exerciseScore > 0)
  601. {
  602. if (currencyInt == 1)
  603. {// 与J哥 ,郭杰确认。只计算通过能力点自测就能获得的成长值。 并取消已学能力点 learnAbility
  604. currency.exerciseAbility += ability.abilityCount;
  605. //并且完全看完视频和文档。
  606. if (item.allDone)
  607. {
  608. currency.learnAbility += 1;
  609. }
  610. }
  611. //并且完全看完视频和文档。
  612. if (item.allDone)
  613. {
  614. currencyAll.learnAbility += 1;
  615. }
  616. currencyAll.exerciseAbility += ability.abilityCount;
  617. }
  618. List<TeacherHprecord> hprecords = new List<TeacherHprecord>();
  619. TeacherAility teacherAility = new Models.TeacherAility
  620. {
  621. id = ability.id,
  622. currency = currencyInt,
  623. no = ability.no,
  624. name = ability.name,
  625. dimension = ability.dimension,
  626. zpscore = item.self,
  627. hprecord = hprecords,
  628. uploadHas = item.uploads.Count
  629. };
  630. if (file != null)
  631. {
  632. long view = 0;
  633. file.fileRecords.ForEach(record => {
  634. var abilityVideo = record.files.FindAll(x => x.abilityId.Equals(item.id));
  635. if (abilityVideo.IsNotEmpty())
  636. {
  637. view += record.view;
  638. }
  639. });
  640. //能力点学时限制
  641. int limit = ability.hour * setting.lessonMinutes;
  642. //如果超过 8* 45分钟学时,则直接赋值360(limit)分钟。
  643. view = view / 60;
  644. view = view > limit ? limit : view;
  645. teacherAility.videoTime = view;
  646. teacherAility.limitTime = ability.hour;
  647. teacherAility.onlineTime = view / setting.lessonMinutes;
  648. }
  649. if (item.otherScore.IsNotEmpty())
  650. {
  651. var schoolScore = item.otherScore.Where(x => x.roleType.Equals("school")).FirstOrDefault();
  652. if (schoolScore != null && schoolScore.score >= 0)
  653. {
  654. teacherAility.xzscore = schoolScore.score;
  655. teacherAility.xztime = schoolScore.time;
  656. teacherAility.xztmdid = schoolScore.tmdid;
  657. teacherAility.xztmdname = schoolScore.tmdname;
  658. }
  659. var hprecord = item.otherScore.FindAll(x => x.roleType.Equals("member")).Select(y => new TeacherHprecord { tmdid = y.tmdid, tmdname = y.tmdname, score = y.score });
  660. if (hprecord != null)
  661. {
  662. var no = hprecord.Where(x => x.score == 0) != null ? hprecord.Where(x => x.score == 0).Count() : 0;
  663. var hg = hprecord.Where(x => x.score == 1) != null ? hprecord.Where(x => x.score == 1).Count() : 0;
  664. var yx = hprecord.Where(x => x.score == 2) != null ? hprecord.Where(x => x.score == 2).Count() : 0;
  665. if (no == hg && hg == yx && no == 0)
  666. {
  667. teacherAility.hpscore = -1;
  668. }
  669. else if (no == hg && hg == yx && no != 0)
  670. {
  671. teacherAility.hpscore = 2;
  672. }
  673. else
  674. {
  675. bool ok = false;
  676. List<int> arr = new List<int>() { yx, hg, no };
  677. int max = arr.Max();
  678. if (max == yx && !ok)
  679. {
  680. teacherAility.hpscore = 2;
  681. ok = true;
  682. }
  683. if (max == hg && !ok)
  684. {
  685. teacherAility.hpscore = 1;
  686. ok = true;
  687. }
  688. if (max == no && !ok)
  689. {
  690. teacherAility.hpscore = 0;
  691. ok = true;
  692. }
  693. }
  694. teacherAility.hprecord.AddRange(hprecord);
  695. }
  696. }
  697. if (currencyInt == 1)
  698. {
  699. currency.subCount += 1;
  700. currency.uploadTotal += ability.stds.FindAll(x => x.task.IsNotEmpty()).Select(y => y.task).Count();
  701. currency.teacherAilities.Add(teacherAility);
  702. }
  703. currencyAll.subCount += 1;
  704. currencyAll.uploadTotal += ability.stds.FindAll(x => x.task.IsNotEmpty()).Select(y => y.task).Count();
  705. currencyAll.teacherAilities.Add(teacherAility);
  706. }
  707. });
  708. train.currency = currency;
  709. train.currencyAll = currencyAll;
  710. train.currency.videoTime = train.currency.teacherAilities.Select(x => x.videoTime).Sum();
  711. train.currencyAll.videoTime= train.currencyAll.teacherAilities.Select(x => x.videoTime).Sum();
  712. //如果总分钟数超过20学时,则直接复制20学时。
  713. var videoTime = (int)train.currency.videoTime / setting.lessonMinutes;
  714. train.onlineTime = videoTime > setting.onlineTime ? setting.onlineTime:videoTime;
  715. var bhg = train.currency.teacherAilities.FindAll(x => x.xzscore > 0);
  716. if (bhg.IsNotEmpty()&& bhg.Count == train.currency.subCount)
  717. {
  718. ///要全部合格才能获得学时。
  719. train.currency.submitTime = setting.submitTime;
  720. train.currencyAll.submitTime = setting.submitTime;
  721. }
  722. return train;
  723. }
  724. }
  725. }