StatisticsService.cs 45 KB

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