StatisticsService.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  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 && !string.IsNullOrEmpty(study.workId) && !item.owner.Equals("area") )
  438. {
  439. StudyRecord studyRecord = studyRecords.Find(y => y.id.Equals(item.id));
  440. HomeworkRecord homeworkRecord = homeworkRecords.Find(y => y.id.Equals(study.workId));
  441. Attachment attachment = homeworkRecord != null ? homeworkRecord.content.Find(x => x.prime) : null;
  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. if (!string.IsNullOrEmpty(study.workId) ) {
  451. record.haswork = 1;
  452. }
  453. if (null != studyRecord)
  454. {
  455. //通过获得学时
  456. record.hour = studyRecord.status == 1 ? study.hour : 0;
  457. record.score = studyRecord.status;
  458. if (record.score >= 0)
  459. {
  460. record.done = 1;
  461. }
  462. else {
  463. record.score = -1;
  464. }
  465. }
  466. if (null != attachment)
  467. {
  468. record.url = attachment.url;
  469. record.upload = 1;
  470. record.hash = attachment.hash;
  471. record.size = attachment.size;
  472. }
  473. offlines.Add(record);
  474. }
  475. });
  476. int sum = offlines.Select(x => x.hour).Sum();
  477. train.offlineTime =sum >setting.offlineTime?setting.offlineTime: sum;
  478. train.offlineRecords= offlines;
  479. return train;
  480. }
  481. /// <summary>
  482. /// 课堂实录更新
  483. /// </summary>
  484. /// <param name="train"></param>
  485. /// <param name="setting"></param>
  486. /// <param name="area"></param>
  487. /// <param name="client"></param>
  488. /// <param name="_school"></param>
  489. /// <param name="_tmdid"></param>
  490. /// <returns></returns>
  491. public static async Task<TeacherTrain> DoTeacherClass(TeacherTrain train, AreaSetting setting, Area area, CosmosClient client, string _school, string _tmdid)
  492. {
  493. string code = $"ClassVideo-{_school}";
  494. ClassVideo classVideo = null;
  495. try { classVideo = await client.GetContainer("TEAMModelOS", "Teacher").ReadItemAsync<ClassVideo>($"{_tmdid}", new PartitionKey(code));
  496. } catch (Exception ex) {
  497. classVideo = null;
  498. }
  499. if (classVideo != null && classVideo.files.IsNotEmpty())
  500. {
  501. //2021.11.17 15:05,与J哥确认,取课堂实录第一个。前端也只show第一个视频。
  502. var files = classVideo.files[0];
  503. if (files.score > 0)
  504. {
  505. train.classTime = setting.classTime;
  506. }
  507. else
  508. {
  509. train.classTime = 0;
  510. }
  511. train.teacherClasses = new List<TeacherClass> { new Models.TeacherClass { url = files.url, score = files.score, hash = files.hash, name = files.name, size = files.size } };
  512. }
  513. else {
  514. train.classTime = 0;
  515. }
  516. return train;
  517. }
  518. public static async Task<TeacherTrain> DoTeacherAility(TeacherTrain train, AreaSetting setting,Area area , CosmosClient client,string _school,string _tmdid) {
  519. //视频播放
  520. List<string> abilityIds = new List<string> ();
  521. TeacherFile file = null;
  522. try
  523. {
  524. file = await client.GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<TeacherFile>(_tmdid, new PartitionKey($"TeacherFile-{_school}"));
  525. }
  526. catch (CosmosException )
  527. {
  528. file = new TeacherFile
  529. {
  530. id = _tmdid,
  531. code = $"TeacherFile-{_school}",
  532. pk = "TeacherFile",
  533. ttl = -1,
  534. };
  535. await client.GetContainer(Constant.TEAMModelOS, "Teacher").CreateItemAsync<TeacherFile>(file, new PartitionKey($"TeacherFile-{_school}"));
  536. }
  537. List<AbilitySub> abilitySubs= new List<AbilitySub> ();
  538. //认证材料
  539. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher")
  540. .GetItemQueryIterator<AbilitySub>(queryText: $"select value(c) from c ", requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"AbilitySub-{_school}-{_tmdid}") }))
  541. {
  542. abilitySubs.Add(item);
  543. }
  544. List<Ability> abilities = new List<Ability>();
  545. string insql = "";
  546. if (abilitySubs.IsNotEmpty()) {
  547. insql =$" where c.id in ({string.Join(",", abilitySubs.Select(o => $"'{o.id}'"))})";
  548. }
  549. await foreach (var item in client.GetContainer("TEAMModelOS", "Normal")
  550. .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}") }))
  551. {
  552. abilities.Add(item);
  553. }
  554. Currency currency= new Currency();
  555. Currency currencyAll = new Currency();
  556. abilitySubs.ForEach(item => {
  557. int currencyInt = item.from == 1 ? 1 : 0;
  558. Ability ability = abilities.Find(x=>x.id.Equals(item.id));
  559. if (ability != null) {
  560. if (ability != null)
  561. {
  562. currencyInt = item.from == 0 ? ability.currency : 1;
  563. }
  564. else
  565. {
  566. currencyInt = 0;
  567. }
  568. if (item.uploads.IsNotEmpty())
  569. {
  570. if (currencyInt == 1)
  571. {
  572. currency.uploadDone += item.uploads.Count;
  573. }
  574. currencyAll.uploadDone += item.uploads.Count;
  575. }
  576. //通过能力点自测
  577. if (item.exerciseScore > 0)
  578. {
  579. if (currencyInt == 1)
  580. {// 与J哥 ,郭杰确认。只计算通过能力点自测就能获得的成长值。 并取消已学能力点 learnAbility
  581. currency.exerciseAbility += ability.abilityCount;
  582. //并且完全看完视频和文档。
  583. if (item.allDone)
  584. {
  585. currency.learnAbility += 1;
  586. }
  587. }
  588. //并且完全看完视频和文档。
  589. if (item.allDone)
  590. {
  591. currencyAll.learnAbility += 1;
  592. }
  593. currencyAll.exerciseAbility += ability.abilityCount;
  594. }
  595. List<TeacherHprecord> hprecords = new List<TeacherHprecord>();
  596. TeacherAility teacherAility = new Models.TeacherAility
  597. {
  598. id = ability.id,
  599. currency = currencyInt,
  600. no = ability.no,
  601. name = ability.name,
  602. dimension = ability.dimension,
  603. zpscore = item.self,
  604. hprecord = hprecords,
  605. uploadHas = item.uploads.Count
  606. };
  607. if (file != null)
  608. {
  609. long view = 0;
  610. file.fileRecords.ForEach(record => {
  611. var abilityVideo = record.files.FindAll(x => x.abilityId.Equals(item.id));
  612. if (abilityVideo.IsNotEmpty())
  613. {
  614. view += record.view;
  615. }
  616. });
  617. //能力点学时限制
  618. int limit = ability.hour * setting.lessonMinutes;
  619. //如果超过 8* 45分钟学时,则直接赋值360(limit)分钟。
  620. view = view / 60;
  621. view = view > limit ? limit : view;
  622. teacherAility.videoTime = view;
  623. teacherAility.limitTime = ability.hour;
  624. teacherAility.onlineTime = view / setting.lessonMinutes;
  625. }
  626. if (item.otherScore.IsNotEmpty())
  627. {
  628. var schoolScore = item.otherScore.Where(x => x.roleType.Equals("school")).FirstOrDefault();
  629. if (schoolScore != null && schoolScore.score >= 0)
  630. {
  631. teacherAility.xzscore = schoolScore.score;
  632. teacherAility.xztime = schoolScore.time;
  633. teacherAility.xztmdid = schoolScore.tmdid;
  634. teacherAility.xztmdname = schoolScore.tmdname;
  635. }
  636. var hprecord = item.otherScore.FindAll(x => x.roleType.Equals("member")).Select(y => new TeacherHprecord { tmdid = y.tmdid, tmdname = y.tmdname, score = y.score });
  637. if (hprecord != null)
  638. {
  639. var no = hprecord.Where(x => x.score == 0) != null ? hprecord.Where(x => x.score == 0).Count() : 0;
  640. var hg = hprecord.Where(x => x.score == 1) != null ? hprecord.Where(x => x.score == 1).Count() : 0;
  641. var yx = hprecord.Where(x => x.score == 2) != null ? hprecord.Where(x => x.score == 2).Count() : 0;
  642. if (no == hg && hg == yx && no == 0)
  643. {
  644. teacherAility.hpscore = -1;
  645. }
  646. else if (no == hg && hg == yx && no != 0)
  647. {
  648. teacherAility.hpscore = 2;
  649. }
  650. else
  651. {
  652. bool ok = false;
  653. List<int> arr = new List<int>() { yx, hg, no };
  654. int max = arr.Max();
  655. if (max == yx && !ok)
  656. {
  657. teacherAility.hpscore = 2;
  658. ok = true;
  659. }
  660. if (max == hg && !ok)
  661. {
  662. teacherAility.hpscore = 1;
  663. ok = true;
  664. }
  665. if (max == no && !ok)
  666. {
  667. teacherAility.hpscore = 0;
  668. ok = true;
  669. }
  670. }
  671. teacherAility.hprecord.AddRange(hprecord);
  672. }
  673. }
  674. if (currencyInt == 1)
  675. {
  676. currency.subCount += 1;
  677. currency.uploadTotal += ability.stds.FindAll(x => x.task.IsNotEmpty()).Select(y => y.task).Count();
  678. currency.teacherAilities.Add(teacherAility);
  679. }
  680. currencyAll.subCount += 1;
  681. currencyAll.uploadTotal += ability.stds.FindAll(x => x.task.IsNotEmpty()).Select(y => y.task).Count();
  682. currencyAll.teacherAilities.Add(teacherAility);
  683. }
  684. });
  685. train.currency = currency;
  686. train.currencyAll = currencyAll;
  687. train.currency.videoTime = train.currency.teacherAilities.Select(x => x.videoTime).Sum();
  688. train.currencyAll.videoTime= train.currencyAll.teacherAilities.Select(x => x.videoTime).Sum();
  689. //如果总分钟数超过20学时,则直接复制20学时。
  690. var videoTime = (int)train.currency.videoTime / setting.lessonMinutes;
  691. train.onlineTime = videoTime > setting.onlineTime ? setting.onlineTime:videoTime;
  692. var bhg = train.currency.teacherAilities.FindAll(x => x.xzscore > 0);
  693. if (bhg.IsNotEmpty()&& bhg.Count == train.currency.subCount)
  694. {
  695. ///要全部合格才能获得学时。
  696. train.currency.submitTime = setting.submitTime;
  697. train.currencyAll.submitTime = setting.submitTime;
  698. }
  699. return train;
  700. }
  701. }
  702. }