StatisticsService.cs 46 KB

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