StatisticsService.cs 46 KB

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