ActivityStudentService.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text.Json;
  5. using System.Threading.Tasks;
  6. using TEAMModelOS.SDK.Models.Cosmos;
  7. using TEAMModelOS.SDK.Extension;
  8. using Azure.Cosmos;
  9. using TEAMModelOS.SDK.DI;
  10. using HTEXLib.COMM.Helpers;
  11. using System.Text;
  12. using TEAMModelOS.SDK.Models;
  13. using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
  14. using StackExchange.Redis;
  15. using TEAMModelOS.SDK.Models.Cosmos.Common;
  16. using TEAMModelOS.Models;
  17. using TEAMModelOS.SDK;
  18. namespace TEAMModelOS.SDK.Services
  19. {
  20. public static class ActivityStudentService
  21. {
  22. public static async Task<(int msgid, int taskStatus)> Decide(DingDing _dingDing, Option _option, JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory _azureRedis,
  23. AzureStorageFactory _azureStorage, string userid, string school, string standard, AzureServiceBusFactory _serviceBus, Microsoft.Extensions.Configuration.IConfiguration _configuration)
  24. {
  25. Vote vote = null;
  26. DateTimeOffset now = DateTimeOffset.UtcNow;
  27. long curr = now.ToUnixTimeMilliseconds();
  28. byte msgid = 0;//0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
  29. int taskStatus = 0;
  30. //活动id
  31. if (!request.TryGetProperty("id", out JsonElement id))
  32. {
  33. return (msgid, -1);
  34. }
  35. //活动分区
  36. if (!request.TryGetProperty("code", out JsonElement code))
  37. {
  38. return (msgid, taskStatus);
  39. }
  40. Dictionary<string, int> option = new Dictionary<string, int>();
  41. if (request.TryGetProperty("option", out JsonElement joption))
  42. {
  43. option = joption.ToObject<Dictionary<string, int>>();
  44. if (option.IsEmpty())
  45. {
  46. msgid = 6;
  47. return (msgid, -1);
  48. }
  49. }
  50. else
  51. {
  52. return (msgid, -1);
  53. }
  54. try
  55. {
  56. //1.再次检查投票
  57. var client = _azureCosmos.GetCosmosClient();
  58. ///TODO 检查是否在投票范围内,包括在tmdids 及班级 但是需要处理认证金钥中的班级问题
  59. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<Vote>(queryText: $"select c.id,c.code ,c.school,c.creatorId,c.scope , c.progress,c.times,c.voteNum,c.startTime,c.endTime from c where c.id = '{id}'",
  60. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  61. {
  62. vote = item;
  63. break;
  64. }
  65. if (vote != null)
  66. {
  67. //判断投票时间是否在起止时间内
  68. string optfrom = "";
  69. ///操作来源,如果是研修的,不限制,否则现在活动结束后不能再投票或者提交问卷作答。
  70. if (request.TryGetProperty("optfrom", out JsonElement _optFrom))
  71. {
  72. optfrom = $"{_optFrom}";
  73. }
  74. // if (curr >= vote.startTime && curr <= vote.endTime)
  75. bool intime=true;//默认有效期内
  76. var endDtae = DateTimeOffset.FromUnixTimeMilliseconds(vote.endTime);
  77. if (!string.IsNullOrWhiteSpace(optfrom) && optfrom.Equals("train"))
  78. { //"optfrom":"train" 代表是研修的
  79. if (curr >= vote.startTime)
  80. {
  81. intime = true;
  82. }
  83. else { intime = false; }
  84. endDtae = DateTimeOffset.UtcNow;
  85. }
  86. else {
  87. if (curr >= vote.startTime && curr <= vote.endTime)
  88. {
  89. intime = true;
  90. endDtae = DateTimeOffset.FromUnixTimeMilliseconds(vote.endTime);
  91. }
  92. else { intime = false; }
  93. }
  94. if (intime)
  95. {
  96. string endField = null;
  97. string Field = "";
  98. RedisValue value;
  99. switch (vote.times)
  100. {
  101. case "once":
  102. // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
  103. Field = $"{userid}-once";
  104. HashEntry[] values = _azureRedis.GetRedisClient(8).HashGetAll($"Vote:Record:{vote.id}");
  105. if (values != null && values.Length > 0)
  106. {
  107. value = new RedisValue();
  108. foreach (var val in values)
  109. {
  110. if (val.Name.ToString() == Field)
  111. {
  112. value = val.Value;
  113. break;
  114. }
  115. }
  116. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, "once");
  117. }
  118. else
  119. {
  120. msgid = await VoteIng(vote, new RedisValue(), msgid, option, Field, curr, _azureRedis, userid, vote.times, "once");
  121. }
  122. if (msgid == 1) { taskStatus = 1; }
  123. break;
  124. case "day": //周期内每天
  125. Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
  126. endField = $"{userid}-day-{endDtae.ToString("yyyyMMdd")}";
  127. if (Field.Equals(endField))
  128. {
  129. taskStatus = 1;
  130. }
  131. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  132. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyyMMdd"));
  133. break;
  134. case "week": //自然周
  135. int week = GetWeek(now);
  136. int endweek = GetWeek(endDtae);
  137. Field = $"{userid}-week-{now.ToString("yyyy")}{week}";
  138. endField = $"{userid}-week-{endDtae.ToString("yyyy")}{endweek}";
  139. if (Field.Equals(endField))
  140. {
  141. taskStatus = 1;
  142. }
  143. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  144. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, $"{now.ToString("yyyy")}{week}");
  145. break;
  146. case "month": //月份
  147. Field = $"{userid}-month-{now.ToString("yyyyMM")}";
  148. endField = $"{userid}-month-{endDtae.ToString("yyyyMM")}";
  149. if (Field.Equals(endField))
  150. {
  151. taskStatus = 1;
  152. }
  153. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  154. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyyMM"));
  155. break;
  156. case "year"://年份
  157. Field = $"{userid}-year-{now.ToString("yyyy")}";
  158. endField = $"{userid}-year-{endDtae.ToString("yyyy")}";
  159. if (Field.Equals(endField))
  160. {
  161. taskStatus = 1;
  162. }
  163. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  164. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyy"));
  165. break;
  166. }
  167. }
  168. else
  169. {
  170. msgid = 2;
  171. }
  172. try
  173. {
  174. if (!string.IsNullOrEmpty(school))
  175. {
  176. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuActivity>(vote.id, new PartitionKey($"Activity-{school}-{userid}"));
  177. activity.taskStatus = taskStatus;
  178. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuActivity>(activity, vote.id, new PartitionKey($"Activity-{school}-{userid}"));
  179. }
  180. else
  181. {
  182. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuActivity>(vote.id, new PartitionKey($"Activity-{userid}"));
  183. activity.taskStatus = taskStatus;
  184. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuActivity>(activity, vote.id, new PartitionKey($"Activity-{userid}"));
  185. }
  186. }
  187. catch (CosmosException ex)
  188. {
  189. if (ex.Status == 404)
  190. {
  191. try
  192. {
  193. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuActivity>(vote.id, new PartitionKey($"Activity-{userid}"));
  194. activity.taskStatus = taskStatus;
  195. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuActivity>(activity, vote.id, new PartitionKey($"Activity-{userid}"));
  196. }
  197. catch (CosmosException cex)
  198. {
  199. try
  200. {
  201. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<StuActivity>(vote.id, new PartitionKey($"Activity-{userid}"));
  202. activity.taskStatus = taskStatus;
  203. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<StuActivity>(activity, vote.id, new PartitionKey($"Activity-{userid}"));
  204. if (!string.IsNullOrEmpty(standard) && !string.IsNullOrEmpty(school))
  205. {
  206. await StatisticsService.SendServiceBus(($"{standard}", new List<string> { $"{userid}" }, $"{school}", new List<string> { StatisticsService.TeacherVote }, 0), _configuration, _serviceBus, client);
  207. }
  208. }
  209. catch (CosmosException cosex)
  210. {
  211. taskStatus = -1;
  212. }
  213. }
  214. }
  215. }
  216. }
  217. else
  218. {
  219. return (msgid, -1);
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. await _dingDing.SendBotMsg($"OS,{_option.Location},common/delete-activity\n{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  225. throw new Exception(ex.StackTrace);
  226. }
  227. if (msgid == 1 && vote != null)
  228. {
  229. string blobcntr = null;
  230. if (vote.scope.Equals("school"))
  231. {
  232. blobcntr = vote.school;
  233. }
  234. else
  235. {
  236. blobcntr = vote.creatorId;
  237. }
  238. //获取投票活动的所有投票记录
  239. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{vote.id}");
  240. //获取投票活动的选项及投票数
  241. var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{vote.id}");
  242. List<dynamic> countcds = new List<dynamic>();
  243. if (counts != null && counts.Length > 0)
  244. {
  245. foreach (var count in counts)
  246. {
  247. countcds.Add(new { code = count.Element.ToString(), count = (int)count.Score });
  248. }
  249. }
  250. List<Task<string>> tasks = new List<Task<string>>();
  251. List<VoteRecord> recordsBlob = new List<VoteRecord>();
  252. foreach (var rcd in records)
  253. {
  254. var value = rcd.Value.ToString().ToObject<VoteRecord>();
  255. recordsBlob.Add(value);
  256. }
  257. //分组每个人的
  258. var gp = recordsBlob.GroupBy(x => x.userid).Select(x => new { key = x.Key, list = x.ToList() });
  259. var userdata = gp.Where(x => x.key.Equals(userid)).FirstOrDefault();
  260. if (userdata != null)
  261. {
  262. tasks.Add(_azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(userdata.list.ToJsonString(), "vote", $"{vote.id}/urecord/{userdata.key}.json"));
  263. }
  264. //处理活动方的记录,
  265. string url = $"/vote/{vote.id}/record.json";
  266. tasks.Add(_azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(new { options = countcds, records = recordsBlob }.ToJsonString(), "vote", $"{vote.id}/record.json"));
  267. }
  268. return (msgid, taskStatus);
  269. }
  270. public static async Task<byte> VoteIng(Vote vote, RedisValue value, byte msgid, Dictionary<string, int> option, string Field, long curr, AzureRedisFactory _azureRedis, string userid, string times, string endpoint)
  271. {
  272. if (!value.IsNullOrEmpty)
  273. {
  274. VoteRecord record = value.ToString().ToObject<VoteRecord>();
  275. int addCount = 0;
  276. foreach (var op in option)
  277. {
  278. addCount += op.Value;
  279. }
  280. int crdCount = 0;
  281. foreach (var op in record.opt)
  282. {
  283. crdCount += op.Value;
  284. }
  285. //处理记录投票+当前设置的投票是否小于等于周期内最大投票数
  286. if (addCount + crdCount <= vote.voteNum)
  287. {
  288. foreach (var op in option)
  289. {
  290. if (record.opt.ContainsKey(op.Key))
  291. {
  292. record.opt[op.Key] = record.opt[op.Key] + op.Value;
  293. }
  294. else
  295. {
  296. record.opt.Add(op.Key, op.Value);
  297. }
  298. }
  299. record.time = curr;
  300. record.userid = userid;
  301. record.times = times;
  302. record.endpoint = endpoint;
  303. //保存投票记录
  304. bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
  305. //单独保存每个人方便查询的记录
  306. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}:{userid}", Field, record.ToJsonString());
  307. //当前投票分组计数存入活动的Redis
  308. foreach (var opt in option)
  309. {
  310. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}", opt.Key, opt.Value);
  311. }
  312. msgid = 1;
  313. }
  314. else
  315. {
  316. msgid = 5;
  317. }
  318. }
  319. else
  320. {
  321. if (option.Count <= vote.voteNum)
  322. {
  323. //保存投票记录
  324. VoteRecord record = new VoteRecord { opt = option, time = curr, userid = userid, times = times, endpoint = endpoint };
  325. bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
  326. //单独保存每个人方便查询的记录
  327. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}:{userid}", Field, record.ToJsonString());
  328. //当前投票分组计数存入活动的Redis
  329. foreach (var opt in option)
  330. {
  331. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}", opt.Key, opt.Value);
  332. }
  333. if (status)
  334. {
  335. msgid = 1;
  336. }
  337. }
  338. else
  339. {
  340. msgid = 5;
  341. }
  342. }
  343. return msgid;
  344. }
  345. /// <summary>
  346. /// 获取时间的在当年的第几周
  347. /// </summary>
  348. /// <param name="dt"></param>
  349. /// <returns></returns>
  350. public static int GetWeek(DateTimeOffset dt)
  351. {
  352. DateTimeOffset time = Convert.ToDateTime(dt.ToString("yyyy") + "-01-01");
  353. TimeSpan ts = dt - time;
  354. int iii = (int)time.DayOfWeek;
  355. int day = int.Parse(ts.TotalDays.ToString("F0"));
  356. if (iii == 0)
  357. {
  358. day--;
  359. }
  360. else
  361. {
  362. day = day - (7 - iii) - 1;
  363. }
  364. int week = ((day + 7) / 7) + 1;
  365. return week;
  366. }
  367. public static async Task<(List<StuActivity> datas, string continuationToken)> FindActivity(JsonElement request, string id, string school, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis)
  368. {
  369. if (string.IsNullOrWhiteSpace(id))
  370. {
  371. if (request.TryGetProperty("userid", out JsonElement userid))
  372. {
  373. if (!userid.ValueKind.Equals(JsonValueKind.Undefined) && !userid.ValueKind.Equals(JsonValueKind.Null) && userid.ValueKind.Equals(JsonValueKind.String))
  374. {
  375. id = userid.GetString();
  376. }
  377. }
  378. }
  379. if (string.IsNullOrWhiteSpace(school))
  380. {
  381. if (request.TryGetProperty("school", out JsonElement schooljson))
  382. {
  383. if (!schooljson.ValueKind.Equals(JsonValueKind.Undefined) && !schooljson.ValueKind.Equals(JsonValueKind.Null) && schooljson.ValueKind.Equals(JsonValueKind.String))
  384. {
  385. school = schooljson.GetString();
  386. }
  387. }
  388. }
  389. /// tmdid, schoolid
  390. var userType = "tmdid";
  391. if (request.TryGetProperty("userType", out JsonElement usertype))
  392. {
  393. if (!usertype.ValueKind.Equals(JsonValueKind.Undefined) && !usertype.ValueKind.Equals(JsonValueKind.Null) && usertype.ValueKind.Equals(JsonValueKind.String))
  394. {
  395. userType = usertype.GetString();
  396. }
  397. }
  398. string stimesql = "";
  399. //开始时间,默认最近三十天
  400. if (request.TryGetProperty("stime", out JsonElement stime))
  401. {
  402. if (!stime.ValueKind.Equals(JsonValueKind.Undefined) && !stime.ValueKind.Equals(JsonValueKind.Null) && stime.TryGetInt64(out long data))
  403. {
  404. stimesql = $" and c.startTime >= {data} ";
  405. }
  406. }
  407. string source = "";
  408. //评测类型
  409. if (request.TryGetProperty("source", out JsonElement sc))
  410. {
  411. source = $" and c.source = '{sc}' ";
  412. }
  413. string owner = "";
  414. //评测来源
  415. if (request.TryGetProperty("owner", out JsonElement element))
  416. {
  417. owner = $" and c.owner = '{element}' ";
  418. }
  419. string name = "";
  420. //评测来源
  421. if (request.TryGetProperty("name", out JsonElement jsonElement))
  422. {
  423. name = $" and Contains( c.name , '{jsonElement}') = true ";
  424. }
  425. //默认当前时间, 未开始的不能查询
  426. var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  427. string etimesql = $" and c.startTime <= {etimestamp} ";
  428. var typesql = "";
  429. if (request.TryGetProperty("type", out JsonElement type))
  430. {
  431. if (!type.ValueKind.Equals(JsonValueKind.Undefined) && !type.ValueKind.Equals(JsonValueKind.Null) && type.ValueKind.Equals(JsonValueKind.String))
  432. {
  433. typesql = $" and c.type='{type}' ";
  434. }
  435. }
  436. string token = default;
  437. //默认不指定返回大小
  438. int? topcout = null;
  439. if (request.TryGetProperty("count", out JsonElement jcount))
  440. {
  441. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  442. {
  443. topcout = data;
  444. }
  445. }
  446. //是否需要进行分页查询,默认不分页
  447. bool iscontinuation = false;
  448. if (topcout != null && topcout.Value > 0)
  449. {
  450. iscontinuation = true;
  451. }
  452. //如果指定了返回大小
  453. if (request.TryGetProperty("continuationToken", out JsonElement token_1))
  454. {
  455. //指定了cancellationToken continuationSchool
  456. if (!token_1.ValueKind.Equals(JsonValueKind.Null) && token_1.ValueKind.Equals(JsonValueKind.String))
  457. {
  458. token = token_1.GetString();
  459. }
  460. }
  461. //科目
  462. string joinSqlSubjects = "";
  463. string andSqlSubjects = "";
  464. if (request.TryGetProperty("subjects", out JsonElement jsubjects))
  465. {
  466. if (jsubjects.ValueKind is JsonValueKind.Array)
  467. {
  468. List<string> subjects = jsubjects.ToObject<List<string>>();
  469. if (subjects.IsNotEmpty())
  470. {
  471. joinSqlSubjects = " join A2 in c.subjects ";
  472. List<string> sqlList = new List<string>();
  473. subjects.ForEach(x => { sqlList.Add($" '{x}' "); });
  474. string sql = string.Join(" , ", sqlList);
  475. andSqlSubjects = $" and A2 in ({sql}) ";
  476. }
  477. }
  478. }
  479. List<StuActivity> datas = new List<StuActivity>();
  480. var client = _azureCosmos.GetCosmosClient();
  481. string containerId = "Student";
  482. string PartitionKey = "";
  483. if (!string.IsNullOrWhiteSpace(school) && userType.Equals("schoolid"))
  484. {
  485. containerId = "Student";
  486. PartitionKey = $"Activity-{school}-{id}";
  487. }
  488. else
  489. {
  490. containerId = "Student";
  491. PartitionKey = $"Activity-{id}";
  492. }
  493. string querySchool = $" SELECT value c FROM c {joinSqlSubjects} where c.pk='Activity' and c.qamode <> 2 {stimesql} {etimesql} {typesql} {andSqlSubjects} {source} {owner} {name} order by c.createTime desc";
  494. //查询数据归属学校的
  495. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, containerId).GetItemQueryStreamIterator(querySchool, continuationToken: token,
  496. requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey(PartitionKey) }))
  497. {
  498. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  499. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  500. {
  501. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  502. {
  503. datas.Add(obj.ToObject<StuActivity>());
  504. }
  505. }
  506. //如果需要分页则跳出
  507. if (iscontinuation)
  508. {
  509. token = item.GetContinuationToken();
  510. break;
  511. }
  512. }
  513. List<StuActivity> results = datas.Where((x, i) => datas.FindIndex(z => z.id == x.id && z.code == x.code) == i).ToList();
  514. return (results, token);
  515. }
  516. public static async Task<(int msgid, int taskStatus)> Answer(DingDing _dingDing, Option _option, JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid, string school, AzureStorageFactory _azureStorage, string standard, AzureServiceBusFactory _serviceBus, Microsoft.Extensions.Configuration.IConfiguration _configuration)
  517. {
  518. DateTimeOffset now = DateTimeOffset.UtcNow;
  519. long curr = now.ToUnixTimeMilliseconds();
  520. int msgid = 0;//
  521. int taskStatus = 0;
  522. //活动id
  523. if (!request.TryGetProperty("id", out JsonElement id))
  524. {
  525. return (msgid, -1);
  526. }
  527. //活动分区
  528. if (!request.TryGetProperty("code", out JsonElement code))
  529. {
  530. return (msgid, -1);
  531. }
  532. try
  533. {
  534. //1.再次检查投票
  535. var client = _azureCosmos.GetCosmosClient();
  536. Survey survey = null;
  537. ///TODO 检查是否在投票范围内,包括在tmdids 及班级 但是需要处理认证金钥中的班级问题
  538. await foreach (var item in client.GetContainer(Constant.TEAMModelOS, "Common").GetItemQueryIterator<Survey>(queryText: $"select c.id,c.owner,c.scope, c.code ,c.creatorId,c.answers ,c.school , c.progress,c.times,c.startTime,c.endTime from c where c.id = '{id}'",
  539. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  540. {
  541. survey = item;
  542. break;
  543. }
  544. if (survey != null)
  545. {
  546. string optfrom = "";
  547. ///操作来源,如果是研修的,不限制,否则现在活动结束后不能再投票或者提交问卷作答。
  548. if (request.TryGetProperty("optfrom", out JsonElement _optFrom))
  549. {
  550. optfrom = $"{_optFrom}";
  551. }
  552. // if (curr >= vote.startTime && curr <= vote.endTime)
  553. bool intime = true;//默认有效期内
  554. var endDtae = DateTimeOffset.FromUnixTimeMilliseconds(survey.endTime);
  555. if (!string.IsNullOrWhiteSpace(optfrom) && optfrom.Equals("train"))
  556. { //"optfrom":"train" 代表是研修的
  557. if (curr >= survey.startTime)
  558. {
  559. intime = true;
  560. }
  561. else { intime = false; }
  562. endDtae = DateTimeOffset.UtcNow;
  563. }
  564. else
  565. {
  566. if (curr >= survey.startTime && curr <= survey.endTime)
  567. {
  568. intime = true;
  569. endDtae = DateTimeOffset.FromUnixTimeMilliseconds(survey.endTime);
  570. }
  571. else { intime = false; }
  572. }
  573. //判断投票时间是否在起止时间内
  574. // if (curr >= survey.startTime && curr <= survey.endTime)
  575. if (intime)
  576. {
  577. if (request.TryGetProperty("record", out JsonElement _record))
  578. {
  579. var recs = _record.ToObject<List<List<string>>>();
  580. if (recs.IsNotEmpty() && recs.Count == survey.answers.Count)
  581. {
  582. //处理问卷调查表的每一题选项数
  583. // List<Task<string>> tasks = new List<Task<string>>();
  584. for (int index = 0; index < recs.Count; index++)
  585. {
  586. Dictionary<string, int> dict = new Dictionary<string, int>();
  587. if (recs[index].IsNotEmpty())
  588. {
  589. recs[index].ForEach(x =>
  590. {
  591. if (survey.answers[index].Contains(x))
  592. {
  593. if (dict.ContainsKey(x))
  594. {
  595. dict[x] = dict[x] + 1;
  596. }
  597. else
  598. {
  599. dict[x] = 1;
  600. }
  601. }
  602. else
  603. {
  604. if (dict.ContainsKey("other"))
  605. {
  606. dict["other"] = dict["other"] + 1;
  607. }
  608. else
  609. {
  610. dict["other"] = 1;
  611. }
  612. //这里暂不处理, 结算再处理other
  613. // tasks.Add(_azureStorage.UploadFileByContainer(survey.owner,new { other=x, userid, time =curr }.ToJsonString(), "survey", $"{survey.id}/other/{index}/{userid}.json", false));
  614. }
  615. });
  616. }
  617. var value = azureRedis.GetRedisClient(8).HashGet($"Survey:Record:{survey.id}", index);
  618. if (value != default && !value.IsNullOrEmpty)
  619. {
  620. Dictionary<string, int> dt = value.ToString().ToObject<Dictionary<string, int>>();
  621. foreach (var kp in dict)
  622. { //不建议放在reids
  623. if (dt.ContainsKey(kp.Key))
  624. {
  625. dt[kp.Key] = dt[kp.Key] + kp.Value;
  626. }
  627. else
  628. {
  629. dt.Add(kp.Key, kp.Value);
  630. }
  631. }
  632. await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dt.ToJsonString());
  633. }
  634. else
  635. {
  636. await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dict.ToJsonString());
  637. }
  638. }
  639. //处理other ,这里暂不处理, 结算再处理other
  640. //await Task.WhenAll(tasks);
  641. //保存当前提交人的记录
  642. string blobcntr = null;
  643. if (survey.scope.Equals("school"))
  644. {
  645. blobcntr = survey.school;
  646. }
  647. else
  648. {
  649. blobcntr = survey.creatorId;
  650. }
  651. await _azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(new SurveyRecord { ans = recs, userid = userid, time = curr }.ToJsonString(), "survey", $"{survey.id}/urecord/{userid}.json");
  652. ///bgn 20210805 huanghb 实时结算
  653. await azureRedis.GetRedisClient(8).SetAddAsync($"Survey:Submit:{survey.id}", userid);
  654. var submits = await azureRedis.GetRedisClient(8).SetMembersAsync($"Survey:Submit:{survey.id}");
  655. List<dynamic> userids = new List<dynamic>();
  656. foreach (var submit in submits)
  657. {
  658. var value = submit.ToString();
  659. userids.Add(value);
  660. }
  661. List<QuestionRecord> questionRecords = new List<QuestionRecord>();
  662. //结算每道题的答题情况
  663. var ContainerClient = _azureStorage.GetBlobContainerClient(blobcntr);
  664. List<Task<string>> tasks = new List<Task<string>>();
  665. List<string> items = await ContainerClient.List($"survey/{survey.id}/urecord");
  666. List<SurveyRecord> surveyRecords = new List<SurveyRecord>();
  667. foreach (string item in items)
  668. {
  669. var Download = await _azureStorage.GetBlobContainerClient(blobcntr).GetBlobClient(item).DownloadAsync();
  670. var json = await JsonDocument.ParseAsync(Download.Value.Content);
  671. var Record = json.RootElement.ToObject<SurveyRecord>();
  672. surveyRecords.Add(Record);
  673. }
  674. for (int index = 0; index < survey.answers.Count; index++)
  675. {
  676. string url = $"{survey.id}/qrecord/{index}.json";
  677. QuestionRecord question = new QuestionRecord() { index = index };
  678. foreach (SurveyRecord record in surveyRecords)
  679. {
  680. if (record.ans.Count == survey.answers.Count)
  681. {
  682. foreach (var an in record.ans[index])
  683. {
  684. //
  685. if (question.opt.ContainsKey(an))
  686. {
  687. if (question.opt[an] != null)
  688. {
  689. question.opt[an].Add(record.userid);
  690. }
  691. else
  692. {
  693. question.opt[an] = new HashSet<string>() { record.userid };
  694. }
  695. }
  696. else
  697. {
  698. if (survey.answers[index].Contains(an))
  699. {
  700. //如果是客观题code
  701. question.opt.Add(an, new HashSet<string> { record.userid });
  702. }
  703. else
  704. {
  705. //如果不是客观code
  706. question.other[record.userid] = an;
  707. }
  708. }
  709. }
  710. }
  711. }
  712. questionRecords.Add(question);
  713. tasks.Add(_azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(question.ToJsonString(), "survey", url));
  714. }
  715. var records = await azureRedis.GetRedisClient(8).HashGetAllAsync($"Survey:Record:{survey.id}");
  716. List<dynamic> recds = new List<dynamic>();
  717. foreach (var rcd in records)
  718. {
  719. var value = rcd.Value.ToString().ToObject<JsonElement>();
  720. recds.Add(new { index = rcd.Name.ToString(), ans = value });
  721. }
  722. await Task.WhenAll(tasks);
  723. var cods = new { records = recds, userids, question = questionRecords, urecord = surveyRecords };
  724. //问卷整体情况
  725. await _azureStorage.GetBlobContainerClient(blobcntr).UploadFileByContainer(cods.ToJsonString(), "survey", $"{survey.id}/record.json");
  726. ///end 20210805 huanghb 实时结算
  727. taskStatus = 1;
  728. msgid = 1;
  729. }
  730. else
  731. {
  732. //提交的作答不符合问卷的答案长度。
  733. msgid = 3;
  734. }
  735. }
  736. }
  737. else
  738. {
  739. msgid = 2;
  740. }
  741. try
  742. {
  743. if (!string.IsNullOrEmpty(school))
  744. {
  745. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuActivity>(survey.id, new PartitionKey($"Activity-{school}-{userid}"));
  746. activity.taskStatus = taskStatus;
  747. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuActivity>(activity, survey.id, new PartitionKey($"Activity-{school}-{userid}"));
  748. }
  749. else
  750. {
  751. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuActivity>(survey.id, new PartitionKey($"Activity-{userid}"));
  752. activity.taskStatus = taskStatus;
  753. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuActivity>(activity, survey.id, new PartitionKey($"Activity-{userid}"));
  754. }
  755. }
  756. catch (CosmosException ex)
  757. {
  758. if (ex.Status == 404)
  759. {
  760. try
  761. {
  762. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReadItemAsync<StuActivity>(survey.id, new PartitionKey($"Activity-{userid}"));
  763. activity.taskStatus = taskStatus;
  764. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Student").ReplaceItemAsync<StuActivity>(activity, survey.id, new PartitionKey($"Activity-{userid}"));
  765. }
  766. catch (CosmosException cex)
  767. {
  768. if (cex.Status == 404)
  769. {
  770. try
  771. {
  772. StuActivity activity = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReadItemAsync<StuActivity>(survey.id, new PartitionKey($"Activity-{userid}"));
  773. activity.taskStatus = taskStatus;
  774. await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "Teacher").ReplaceItemAsync<StuActivity>(activity, survey.id, new PartitionKey($"Activity-{userid}"));
  775. if (!string.IsNullOrEmpty(standard) && !string.IsNullOrEmpty(school))
  776. {
  777. await StatisticsService.SendServiceBus(($"{standard}", new List<string> { $"{userid}" }, $"{school}", new List<string> { StatisticsService.TeacherSurvey }, 0), _configuration, _serviceBus, client);
  778. }
  779. }
  780. catch (CosmosException cosex)
  781. {
  782. if (cosex.Status == 404)
  783. {
  784. taskStatus = -1;
  785. }
  786. }
  787. }
  788. }
  789. }
  790. }
  791. }
  792. else
  793. {
  794. return (msgid, -1);
  795. }
  796. }
  797. catch (Exception e)
  798. {
  799. throw new Exception(e.StackTrace);
  800. }
  801. return (msgid, taskStatus);
  802. }
  803. public class RdsRecord
  804. {
  805. public Dictionary<string, int> srecord { get; set; } = new Dictionary<string, int>();
  806. public Dictionary<string, string[]> urecord { get; set; } = new Dictionary<string, string[]>();
  807. }
  808. }
  809. }