StatisticsService.cs 41 KB

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