ActivityStudentService.cs 40 KB

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