ActivityStudentService.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. namespace TEAMModelOS.Services.Common
  16. {
  17. public static class ActivityStudentService
  18. {
  19. /// <summary>
  20. /// 活动委托
  21. /// </summary>
  22. /// <param name="data"></param>
  23. /// <returns></returns>
  24. delegate dynamic DoActivityTips(ActivityData data, AzureCosmosFactory _azureCosmos,string id, AzureRedisFactory _azureRedis);
  25. public static async Task<int> Decide(JsonElement request,AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis,string userid ) {
  26. DateTimeOffset now = DateTimeOffset.UtcNow;
  27. long curr = now.ToUnixTimeMilliseconds();
  28. byte msgid = 0;//0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
  29. //活动id
  30. if (!request.TryGetProperty("id", out JsonElement id)) {
  31. return msgid;
  32. }
  33. //活动分区
  34. if (!request.TryGetProperty("code", out JsonElement code)) {
  35. return msgid;
  36. }
  37. Dictionary<string, int> option = new Dictionary<string, int>();
  38. if (request.TryGetProperty("option", out JsonElement joption))
  39. {
  40. option = joption.ToObject<Dictionary<string, int>>();
  41. if (option.IsEmpty())
  42. {
  43. msgid = 6;
  44. return msgid;
  45. }
  46. }
  47. else
  48. {
  49. return msgid;
  50. }
  51. try
  52. {
  53. //1.再次检查投票
  54. var client = _azureCosmos.GetCosmosClient();
  55. Vote vote = null;
  56. ///TODO 检查是否在投票范围内,包括在tmdids 及班级 但是需要处理认证金钥中的班级问题
  57. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Vote>(queryText: $"select c.id,c.code , c.progress,c.times,c.voteNum,c.startTime,c.endTime from c where c.id = '{id}'",
  58. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  59. {
  60. vote = item;
  61. break;
  62. }
  63. if (vote != null)
  64. {
  65. //判断投票时间是否在起止时间内
  66. if (curr >= vote.startTime && curr <= vote.endTime)
  67. {
  68. string Field = "";
  69. RedisValue value;
  70. switch (vote.times) {
  71. case "once":
  72. // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
  73. Field = $"{userid}-once";
  74. HashEntry[] values = _azureRedis.GetRedisClient(8).HashGetAll($"Vote:Record:{vote.id}");
  75. if (values != null && values.Length > 0)
  76. {
  77. value = new RedisValue();
  78. foreach (var val in values) {
  79. if (val.Name.ToString() == Field) {
  80. value = val.Value;
  81. break;
  82. }
  83. }
  84. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid);
  85. }
  86. else
  87. {
  88. msgid = await VoteIng(vote, new RedisValue(), msgid, option, Field, curr, _azureRedis, userid);
  89. }
  90. break;
  91. case "day": //周期内每天
  92. Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
  93. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  94. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis,userid);
  95. break;
  96. case "week": //自然周
  97. Field = $"{userid}-week-{now.ToString("yyyy")}{GetWeek(now)}";
  98. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  99. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid);
  100. break;
  101. case "month": //月份
  102. Field = $"{userid}-month-{now.ToString("yyyyMM")}";
  103. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  104. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid);
  105. break;
  106. case "year"://年份
  107. Field = $"{userid}-year-{now.ToString("yyyy")}";
  108. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  109. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid);
  110. break;
  111. }
  112. }
  113. else
  114. {
  115. msgid = 2;
  116. }
  117. }
  118. }
  119. catch (Exception e)
  120. {
  121. throw new Exception(e.StackTrace);
  122. }
  123. return msgid;
  124. }
  125. public static async Task<byte> VoteIng(Vote vote, RedisValue value, byte msgid, Dictionary<string, int> option, string Field, long curr, AzureRedisFactory _azureRedis,string userid)
  126. {
  127. if (!value.IsNullOrEmpty)
  128. {
  129. VoteRecord record=value.ToString().ToObject<VoteRecord>();
  130. int addCount = 0;
  131. foreach (var op in option) {
  132. addCount +=op.Value;
  133. }
  134. int crdCount = 0;
  135. foreach (var op in record.opt)
  136. {
  137. crdCount += op.Value;
  138. }
  139. //处理记录投票+当前设置的投票是否小于等于周期内最大投票数
  140. if (addCount + crdCount <= vote.voteNum)
  141. {
  142. foreach (var op in option)
  143. {
  144. if (record.opt.ContainsKey(op.Key))
  145. {
  146. record.opt[op.Key] = record.opt[op.Key] + op.Value;
  147. }
  148. else {
  149. record.opt.Add(op.Key, op.Value);
  150. }
  151. }
  152. record.time = curr;
  153. record.userid = userid;
  154. //保存投票记录
  155. bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
  156. //单独保存每个人方便查询的记录
  157. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}:{userid}", Field, record.ToJsonString());
  158. //当前投票分组计数存入活动的Redis
  159. foreach (var opt in option)
  160. {
  161. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}", opt.Key, opt.Value);
  162. }
  163. msgid = 1;
  164. }
  165. else
  166. {
  167. msgid = 5;
  168. }
  169. }
  170. else
  171. {
  172. if (option.Count <= vote.voteNum)
  173. {
  174. //保存投票记录
  175. VoteRecord record = new VoteRecord { opt = option, time = curr, userid = userid };
  176. bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
  177. //单独保存每个人方便查询的记录
  178. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}:{userid}", Field, record.ToJsonString());
  179. //当前投票分组计数存入活动的Redis
  180. foreach (var opt in option)
  181. {
  182. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}", opt.Key, opt.Value);
  183. }
  184. if (status)
  185. {
  186. msgid = 1;
  187. }
  188. }
  189. else {
  190. msgid = 5;
  191. }
  192. }
  193. return msgid;
  194. }
  195. /// <summary>
  196. /// 获取时间的在当年的第几周
  197. /// </summary>
  198. /// <param name="dt"></param>
  199. /// <returns></returns>
  200. public static int GetWeek(DateTimeOffset dt)
  201. {
  202. DateTimeOffset time = Convert.ToDateTime(dt.ToString("yyyy") + "-01-01");
  203. TimeSpan ts = dt - time;
  204. int iii = (int)time.DayOfWeek;
  205. int day = int.Parse(ts.TotalDays.ToString("F0"));
  206. if (iii == 0)
  207. {
  208. day--;
  209. }
  210. else
  211. {
  212. day = day - (7 - iii) - 1;
  213. }
  214. int week = ((day + 7) / 7) + 1;
  215. return week;
  216. }
  217. /// <summary>
  218. /// 学生端查询
  219. /// </summary>
  220. /// <param name="containerId">容器</param>
  221. /// <param name="requert"></param>
  222. /// <param name="id">登录者ID</param>
  223. /// <param name="_azureCosmos"></param>
  224. /// <returns></returns>
  225. public static async Task<(List<ActivityData> datas, string continuationTokenSchool,string continuationTokenTeacher)> FindAsStu( JsonElement requert, string id,string school, AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis)
  226. {
  227. if (string.IsNullOrWhiteSpace(id)) {
  228. id = requert.GetProperty("userid").GetString();
  229. }
  230. if (string.IsNullOrWhiteSpace(school))
  231. {
  232. school = requert.GetProperty("school").GetString();
  233. }
  234. //开始时间,默认最近三十天
  235. var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
  236. if (requert.TryGetProperty("stime", out JsonElement stime))
  237. {
  238. if (!stime.ValueKind.Equals(JsonValueKind.Undefined) && !stime.ValueKind.Equals(JsonValueKind.Null) &&stime.TryGetInt64(out long data))
  239. {
  240. stimestamp = data;
  241. }
  242. }
  243. string stimesql = $" c.startTime >= {stimestamp} ";
  244. //默认当前时间, 未开始的不能查询
  245. var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  246. string etimesql = $" and c.startTime <= {etimestamp} ";
  247. var progresssql = "";
  248. if (requert.TryGetProperty("progress", out JsonElement progress))
  249. {
  250. if (!progress.ValueKind.Equals(JsonValueKind.Undefined) && !progress.ValueKind.Equals(JsonValueKind.Null) && progress.ValueKind.Equals(JsonValueKind.String))
  251. {
  252. progresssql = $" and c.progress='{progress}' ";
  253. }
  254. }
  255. var typesql = "";
  256. if (requert.TryGetProperty("type", out JsonElement type))
  257. {
  258. if (!type.ValueKind.Equals(JsonValueKind.Undefined) && !type.ValueKind.Equals(JsonValueKind.Null) && type.ValueKind.Equals(JsonValueKind.String))
  259. {
  260. typesql = $" and c.type='{type}' ";
  261. }
  262. }
  263. string continuationTokenSchool = null;
  264. string continuationTokenTeacher = null;
  265. //默认不指定返回大小
  266. int? topcout = null;
  267. if (requert.TryGetProperty("count", out JsonElement jcount))
  268. {
  269. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  270. {
  271. topcout = data;
  272. }
  273. }
  274. //是否需要进行分页查询,默认不分页
  275. bool iscontinuation = false;
  276. //如果指定了返回大小
  277. if (requert.TryGetProperty("continuationTokenSchool", out JsonElement continuationSchool))
  278. {
  279. //指定了cancellationToken continuationSchool
  280. if (!continuationSchool.ValueKind.Equals(JsonValueKind.Null) && continuationSchool.ValueKind.Equals(JsonValueKind.String))
  281. {
  282. continuationTokenSchool = continuationSchool.GetString();
  283. iscontinuation = true;
  284. }
  285. }
  286. //如果指定了返回大小
  287. if (requert.TryGetProperty("continuationTokenTeacher", out JsonElement continuationTeacher))
  288. {
  289. //指定了cancellationToken 表示需要进行分页
  290. if (!continuationTeacher.ValueKind.Equals(JsonValueKind.Null) && continuationTeacher.ValueKind.Equals(JsonValueKind.String))
  291. {
  292. continuationTokenTeacher = continuationTeacher.GetString();
  293. iscontinuation = true;
  294. }
  295. }
  296. //个人tmdid
  297. string joinSqlTmdids = $"join A0 in c.tmdids";
  298. string andSqlTmdids = $" A0 in('{id}')";
  299. //班级
  300. string joinSqlClasses = "";
  301. string andSqlClasses = "";
  302. List<string> classes=null;
  303. if ( requert.TryGetProperty("classes", out JsonElement jclasses))
  304. {
  305. if (jclasses.ValueKind is JsonValueKind.Array ) {
  306. classes = jclasses.ToObject<List<string>>();
  307. if (classes.IsNotEmpty()) {
  308. joinSqlClasses = " join A1 in c.classes ";
  309. List<string> sqlList = new List<string>();
  310. classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  311. string sql = string.Join(" , ", sqlList);
  312. andSqlClasses = $" A1 in ({sql}) ";
  313. }
  314. }
  315. }
  316. string tgSql = "";
  317. if (!string.IsNullOrWhiteSpace(joinSqlClasses))
  318. {
  319. tgSql = $"and ({andSqlTmdids} or {andSqlClasses } )";
  320. }
  321. else {
  322. tgSql = $"and {andSqlTmdids}";
  323. }
  324. //科目
  325. string joinSqlSubjects = "";
  326. string andSqlSubjects = "";
  327. if ( requert.TryGetProperty("subjects", out JsonElement jsubjects))
  328. {
  329. if (jsubjects.ValueKind is JsonValueKind.Array)
  330. {
  331. List<string> subjects = jsubjects.ToObject<List<string>>();
  332. if (subjects.IsNotEmpty()) {
  333. joinSqlSubjects = " join A2 in c.subjects ";
  334. List<string> sqlList = new List<string>();
  335. subjects.ForEach(x => { sqlList.Add($" '{x}' "); });
  336. string sql = string.Join(" , ", sqlList);
  337. andSqlSubjects = $" and A2 in ({sql}) ";
  338. }
  339. }
  340. }
  341. List<ActivityData> datas = new List<ActivityData>();
  342. var client = _azureCosmos.GetCosmosClient();
  343. if (!string.IsNullOrWhiteSpace(school)) {
  344. string querySchool = $" SELECT distinct value c FROM c {joinSqlTmdids} {joinSqlClasses} {joinSqlSubjects} where {stimesql} {etimesql} and c.pk='Activity' {progresssql} {typesql} {andSqlSubjects} {tgSql}";
  345. //查询数据归属学校的
  346. await foreach (var item in client.GetContainer("TEAMModelOS", "School").GetItemQueryStreamIterator(querySchool, continuationToken: continuationTokenSchool, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Activity-{school}") }))
  347. {
  348. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  349. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  350. {
  351. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  352. {
  353. datas.Add(obj.ToObject<ActivityData>());
  354. }
  355. //如果需要分页则跳出
  356. if (iscontinuation)
  357. {
  358. continuationTokenSchool = item.GetContinuationToken();
  359. break;
  360. }
  361. }
  362. }
  363. }
  364. //TODO会处理掉科目相关的
  365. //查询数据归属Common 私人教室的。
  366. string queryTeacher = $" SELECT distinct value c FROM c {joinSqlTmdids} {joinSqlClasses} where {stimesql} {etimesql} and c.pk='Activity' {progresssql} {typesql} {tgSql} ";
  367. await foreach (var item in client.GetContainer("TEAMModelOS", "Teacher").GetItemQueryStreamIterator(queryTeacher, continuationToken: continuationTokenTeacher, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Activity-Common") }))
  368. {
  369. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  370. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  371. {
  372. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  373. {
  374. datas.Add(obj.ToObject<ActivityData>());
  375. }
  376. //如果需要分页则跳出
  377. if (iscontinuation)
  378. {
  379. continuationTokenTeacher = item.GetContinuationToken();
  380. break;
  381. }
  382. }
  383. }
  384. bool tips = false;
  385. if (requert.TryGetProperty("tips", out JsonElement jtips))
  386. {
  387. if (!jtips.ValueKind.Equals(JsonValueKind.Undefined) && !jtips.ValueKind.Equals(JsonValueKind.Null) && (jtips.ValueKind.Equals(JsonValueKind.True)|| jtips.ValueKind.Equals(JsonValueKind.False)))
  388. {
  389. tips = jtips.GetBoolean();
  390. }
  391. }
  392. if (tips)
  393. {
  394. DoActivityTips activityTips;
  395. //TODO 处理活动tips 的res
  396. dynamic res = default;
  397. foreach (var data in datas)
  398. {
  399. switch (data.type)
  400. {
  401. //投票
  402. case "vote":
  403. activityTips = DoVoteTips;
  404. //msgid, //0不能投票,1可以投票,2不在时间范围内,3周期内的可投票数不足
  405. //voteCount 可用投票数
  406. res =await activityTips(data, _azureCosmos, id, _azureRedis);
  407. break;
  408. //问卷
  409. case "survey":
  410. //msgid 0 已作答, 1未作答,2,未完成
  411. activityTips = DoSurveyTips;
  412. res = await activityTips(data, _azureCosmos, id, _azureRedis);
  413. break;
  414. //评测
  415. case "exam":
  416. //msgid 0 已作答, 1未作答,2,未完成, 用时间控制 相关发布状态,并且展示相应的结果
  417. activityTips = DoExamTips;
  418. res = await activityTips(data, _azureCosmos, id, _azureRedis);
  419. break;
  420. //学习活动
  421. case "learn":
  422. //msgid 0 已完成, 1未开始,2,未完成
  423. activityTips = DoLearnTips;
  424. res = await activityTips(data, _azureCosmos, id, _azureRedis);
  425. break;
  426. //作业活动
  427. case "homework":
  428. //msgid 0 已作答, 1未作答,2,未完成,3已批改,且有错误,4已批改,已完成
  429. //index:0,1,5 错误题序
  430. activityTips = DoHomeworkTips;
  431. res = await activityTips(data, _azureCosmos, id, _azureRedis);
  432. break;
  433. default: break;
  434. }
  435. }
  436. }
  437. return (datas, continuationTokenSchool,continuationTokenTeacher);
  438. }
  439. public static async Task<int> Answer(JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid, AzureStorageFactory _azureStorage)
  440. {
  441. DateTimeOffset now = DateTimeOffset.UtcNow;
  442. long curr = now.ToUnixTimeMilliseconds();
  443. byte msgid = 0;//
  444. //活动id
  445. if (!request.TryGetProperty("id", out JsonElement id))
  446. {
  447. return msgid;
  448. }
  449. //活动分区
  450. if (!request.TryGetProperty("code", out JsonElement code))
  451. {
  452. return msgid;
  453. }
  454. try
  455. {
  456. //1.再次检查投票
  457. var client = _azureCosmos.GetCosmosClient();
  458. Survey survey = null;
  459. ///TODO 检查是否在投票范围内,包括在tmdids 及班级 但是需要处理认证金钥中的班级问题
  460. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Survey>(queryText: $"select c.id,c.owner, c.code ,c.ans , c.progress,c.times,c.startTime,c.endTime from c where c.id = '{id}'",
  461. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  462. {
  463. survey = item;
  464. break;
  465. }
  466. if (survey != null)
  467. {
  468. //判断投票时间是否在起止时间内
  469. if (curr >= survey.startTime && curr <= survey.endTime)
  470. {
  471. if (request.TryGetProperty("record", out JsonElement record))
  472. {
  473. var recs = record.ToObject<List<List<string>>>();
  474. if (recs.IsNotEmpty() && recs.Count == survey.ans.Count)
  475. {
  476. //处理问卷调查表的每一题选项数
  477. // List<Task<string>> tasks = new List<Task<string>>();
  478. for (int index = 0; index < recs.Count; index++) {
  479. Dictionary<string, int> dict = new Dictionary<string, int>();
  480. if (recs[index].IsNotEmpty()) {
  481. recs[index].ForEach(x => {
  482. if (survey.ans[index].Contains(x))
  483. {
  484. if (dict.ContainsKey(x))
  485. {
  486. dict[x] = dict[x] + 1;
  487. }
  488. else {
  489. dict[x] = 1;
  490. }
  491. }
  492. else {
  493. if (dict.ContainsKey("other"))
  494. {
  495. dict["other"] = dict["other"] + 1;
  496. }
  497. else
  498. {
  499. dict["other"] = 1;
  500. }
  501. //这里暂不处理, 结算再处理other
  502. // tasks.Add(_azureStorage.UploadFileByContainer(survey.owner,new { other=x, userid, time =curr }.ToJsonString(), "survey", $"{survey.id}/other/{index}/{userid}.json", false));
  503. }
  504. });
  505. }
  506. var value= azureRedis.GetRedisClient(8).HashGet($"Survey:Record:{survey.id}",index);
  507. if (value != default && !value.IsNullOrEmpty)
  508. {
  509. Dictionary<string, int> dt = value.ToString().ToObject<Dictionary<string, int>>();
  510. foreach (var kp in dict)
  511. { //不建议放在reids
  512. if (dt.ContainsKey(kp.Key))
  513. {
  514. dt[kp.Key] = dt[kp.Key] + kp.Value;
  515. }
  516. else
  517. {
  518. dt.Add(kp.Key, kp.Value);
  519. }
  520. }
  521. await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dt.ToJsonString());
  522. }
  523. else {
  524. await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dict.ToJsonString());
  525. }
  526. }
  527. //处理other ,这里暂不处理, 结算再处理other
  528. //await Task.WhenAll(tasks);
  529. //保存当前提交人的记录
  530. await _azureStorage.UploadFileByContainer(survey.owner,new { record= record, userid, time = curr }.ToJsonString(), "survey", $"{survey.id}/urecord/{userid}.json");
  531. await azureRedis.GetRedisClient(8).SetAddAsync($"Survey:Submit:{survey.id}", userid);
  532. msgid = 1;
  533. }
  534. else {
  535. //提交的作答不符合问卷的答案长度。
  536. msgid = 3;
  537. }
  538. }
  539. }
  540. else
  541. {
  542. msgid = 2;
  543. }
  544. }
  545. }
  546. catch (Exception e)
  547. {
  548. throw new Exception(e.StackTrace);
  549. }
  550. return msgid;
  551. }
  552. public class RdsRecord {
  553. public Dictionary<string, int> srecord { get; set; } = new Dictionary<string, int>();
  554. public Dictionary<string, string[]> urecord { get; set; } = new Dictionary<string, string[]>();
  555. }
  556. private async static Task<dynamic> DoVoteTips(ActivityData commonData , AzureCosmosFactory _azureCosmos,string userid,AzureRedisFactory _azureRedis)
  557. {
  558. Vote vote=null;
  559. var client = _azureCosmos.GetCosmosClient();
  560. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Vote>($"select value c from c where c.id='{commonData.id}'",
  561. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(commonData.scode) }))
  562. {
  563. vote = item;
  564. break;
  565. }
  566. byte msgid = 0;
  567. int voteCount = 0;
  568. if (vote != null) {
  569. DateTimeOffset now = DateTimeOffset.UtcNow;
  570. long curr = now.ToUnixTimeMilliseconds();
  571. //判断投票时间是否在起止时间内
  572. if (curr >= vote.startTime && curr <= vote.endTime)
  573. {
  574. string Field = "";
  575. RedisValue value = default;
  576. switch (vote.times)
  577. {
  578. case "once":
  579. // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
  580. Field = $"{userid}-once";
  581. RedisValue[] values = _azureRedis.GetRedisClient(8).HashValues($"Vote:Record:{vote.id}");
  582. if (values != null && values.Length>0)
  583. {
  584. value = values[0];
  585. }
  586. break;
  587. case "day": //周期内每天
  588. Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
  589. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  590. break;
  591. case "week": //自然周
  592. Field = $"{userid}-week-{now.ToString("yyyy")}{GetWeek(now)}";
  593. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  594. break;
  595. case "month": //月份
  596. Field = $"{userid}-month-{now.ToString("yyyyMM")}";
  597. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  598. break;
  599. case "year"://年份
  600. Field = $"{userid}-year-{now.ToString("yyyy")}";
  601. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  602. break;
  603. }
  604. if (value != default && !value.IsNullOrEmpty)
  605. {
  606. List<string> opt = null;
  607. JsonElement record = value.ToString().ToObject<JsonElement>();
  608. if (record.TryGetProperty("opt", out JsonElement jopt)) {
  609. opt = jopt.ToObject<List<string>>();
  610. }
  611. if (opt != null)
  612. {
  613. //处理记录投票是否小于等于周期内最大投票数
  614. if (opt.Count <= vote.voteNum)
  615. {
  616. voteCount = vote.voteNum - opt.Count;
  617. msgid = 1;
  618. }
  619. else
  620. {
  621. //3周期内的可投票数不足
  622. msgid = 3;
  623. voteCount = 0;
  624. }
  625. }
  626. else {
  627. msgid = 1;
  628. voteCount = vote.voteNum;
  629. }
  630. }
  631. else {
  632. //未投票,可以投票
  633. msgid = 1;
  634. voteCount = vote.voteNum;
  635. }
  636. }
  637. else
  638. {
  639. msgid = 2;
  640. voteCount = 0;
  641. }
  642. }
  643. return new {msgid,voteCount };
  644. }
  645. private static dynamic DoHomeworkTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
  646. {
  647. return null;
  648. }
  649. private static dynamic DoLearnTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
  650. {
  651. return null;
  652. }
  653. private static dynamic DoExamTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
  654. {
  655. return null;
  656. }
  657. private static dynamic DoSurveyTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
  658. {
  659. return null;
  660. }
  661. }
  662. }