ActivityStudentService.cs 47 KB

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