ActivityStudentService.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. namespace TEAMModelOS.Services.Common
  17. {
  18. public static class ActivityStudentService
  19. {
  20. public static async Task<int> Decide(JsonElement request,AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis,string userid ) {
  21. DateTimeOffset now = DateTimeOffset.UtcNow;
  22. long curr = now.ToUnixTimeMilliseconds();
  23. byte msgid = 0;//0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
  24. //活动id
  25. if (!request.TryGetProperty("id", out JsonElement id)) {
  26. return msgid;
  27. }
  28. //活动分区
  29. if (!request.TryGetProperty("code", out JsonElement code)) {
  30. return msgid;
  31. }
  32. Dictionary<string, int> option = new Dictionary<string, int>();
  33. if (request.TryGetProperty("option", out JsonElement joption))
  34. {
  35. option = joption.ToObject<Dictionary<string, int>>();
  36. if (option.IsEmpty())
  37. {
  38. msgid = 6;
  39. return msgid;
  40. }
  41. }
  42. else
  43. {
  44. return msgid;
  45. }
  46. try
  47. {
  48. //1.再次检查投票
  49. var client = _azureCosmos.GetCosmosClient();
  50. Vote vote = null;
  51. ///TODO 检查是否在投票范围内,包括在tmdids 及班级 但是需要处理认证金钥中的班级问题
  52. 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}'",
  53. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  54. {
  55. vote = item;
  56. break;
  57. }
  58. if (vote != null)
  59. {
  60. //判断投票时间是否在起止时间内
  61. if (curr >= vote.startTime && curr <= vote.endTime)
  62. {
  63. string Field = "";
  64. RedisValue value;
  65. switch (vote.times) {
  66. case "once":
  67. // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
  68. Field = $"{userid}-once";
  69. HashEntry[] values = _azureRedis.GetRedisClient(8).HashGetAll($"Vote:Record:{vote.id}");
  70. if (values != null && values.Length > 0)
  71. {
  72. value = new RedisValue();
  73. foreach (var val in values) {
  74. if (val.Name.ToString() == Field) {
  75. value = val.Value;
  76. break;
  77. }
  78. }
  79. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid,vote.times, "once");
  80. }
  81. else
  82. {
  83. msgid = await VoteIng(vote, new RedisValue(), msgid, option, Field, curr, _azureRedis, userid, vote.times, "once");
  84. }
  85. break;
  86. case "day": //周期内每天
  87. Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
  88. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  89. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis,userid, vote.times, now.ToString("yyyyMMdd"));
  90. break;
  91. case "week": //自然周
  92. int week = GetWeek(now);
  93. Field = $"{userid}-week-{now.ToString("yyyy")}{week}";
  94. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  95. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times,$"{now.ToString("yyyy")}{week}");
  96. break;
  97. case "month": //月份
  98. Field = $"{userid}-month-{now.ToString("yyyyMM")}";
  99. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  100. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyyMM"));
  101. break;
  102. case "year"://年份
  103. Field = $"{userid}-year-{now.ToString("yyyy")}";
  104. value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
  105. msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis, userid, vote.times, now.ToString("yyyy"));
  106. break;
  107. }
  108. }
  109. else
  110. {
  111. msgid = 2;
  112. }
  113. }
  114. }
  115. catch (Exception e)
  116. {
  117. throw new Exception(e.StackTrace);
  118. }
  119. return msgid;
  120. }
  121. 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)
  122. {
  123. if (!value.IsNullOrEmpty)
  124. {
  125. VoteRecord record=value.ToString().ToObject<VoteRecord>();
  126. int addCount = 0;
  127. foreach (var op in option) {
  128. addCount +=op.Value;
  129. }
  130. int crdCount = 0;
  131. foreach (var op in record.opt)
  132. {
  133. crdCount += op.Value;
  134. }
  135. //处理记录投票+当前设置的投票是否小于等于周期内最大投票数
  136. if (addCount + crdCount <= vote.voteNum)
  137. {
  138. foreach (var op in option)
  139. {
  140. if (record.opt.ContainsKey(op.Key))
  141. {
  142. record.opt[op.Key] = record.opt[op.Key] + op.Value;
  143. }
  144. else {
  145. record.opt.Add(op.Key, op.Value);
  146. }
  147. }
  148. record.time = curr;
  149. record.userid = userid;
  150. record.times = times;
  151. record.endpoint = endpoint;
  152. //保存投票记录
  153. bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
  154. //单独保存每个人方便查询的记录
  155. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}:{userid}", Field, record.ToJsonString());
  156. //当前投票分组计数存入活动的Redis
  157. foreach (var opt in option)
  158. {
  159. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}", opt.Key, opt.Value);
  160. }
  161. msgid = 1;
  162. }
  163. else
  164. {
  165. msgid = 5;
  166. }
  167. }
  168. else
  169. {
  170. if (option.Count <= vote.voteNum)
  171. {
  172. //保存投票记录
  173. VoteRecord record = new VoteRecord { opt = option, time = curr, userid = userid ,times=times,endpoint=endpoint};
  174. bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
  175. //单独保存每个人方便查询的记录
  176. bool stuallstatus = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}:{userid}", Field, record.ToJsonString());
  177. //当前投票分组计数存入活动的Redis
  178. foreach (var opt in option)
  179. {
  180. await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}", opt.Key, opt.Value);
  181. }
  182. if (status)
  183. {
  184. msgid = 1;
  185. }
  186. }
  187. else {
  188. msgid = 5;
  189. }
  190. }
  191. return msgid;
  192. }
  193. /// <summary>
  194. /// 获取时间的在当年的第几周
  195. /// </summary>
  196. /// <param name="dt"></param>
  197. /// <returns></returns>
  198. public static int GetWeek(DateTimeOffset dt)
  199. {
  200. DateTimeOffset time = Convert.ToDateTime(dt.ToString("yyyy") + "-01-01");
  201. TimeSpan ts = dt - time;
  202. int iii = (int)time.DayOfWeek;
  203. int day = int.Parse(ts.TotalDays.ToString("F0"));
  204. if (iii == 0)
  205. {
  206. day--;
  207. }
  208. else
  209. {
  210. day = day - (7 - iii) - 1;
  211. }
  212. int week = ((day + 7) / 7) + 1;
  213. return week;
  214. }
  215. public static async Task<(List<StuActivity> datas, string continuationToken)> FindActivity(JsonElement request, string id, string school, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis)
  216. {
  217. if (string.IsNullOrWhiteSpace(id))
  218. {
  219. if (request.TryGetProperty("userid", out JsonElement userid))
  220. {
  221. if (!userid.ValueKind.Equals(JsonValueKind.Undefined) && !userid.ValueKind.Equals(JsonValueKind.Null) && userid.ValueKind.Equals(JsonValueKind.String))
  222. {
  223. id = userid.GetString();
  224. }
  225. }
  226. }
  227. if (string.IsNullOrWhiteSpace(school))
  228. {
  229. if (request.TryGetProperty("school", out JsonElement schooljson))
  230. {
  231. if (!schooljson.ValueKind.Equals(JsonValueKind.Undefined) && !schooljson.ValueKind.Equals(JsonValueKind.Null) && schooljson.ValueKind.Equals(JsonValueKind.String))
  232. {
  233. school = schooljson.GetString();
  234. }
  235. }
  236. }
  237. /// tmdid, schoolid
  238. var userType = "tmdid";
  239. if (request.TryGetProperty("userType", out JsonElement usertype))
  240. {
  241. if (!usertype.ValueKind.Equals(JsonValueKind.Undefined) && !usertype.ValueKind.Equals(JsonValueKind.Null) && usertype.ValueKind.Equals(JsonValueKind.String))
  242. {
  243. userType = usertype.GetString();
  244. }
  245. }
  246. //开始时间,默认最近三十天
  247. var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
  248. if (request.TryGetProperty("stime", out JsonElement stime))
  249. {
  250. if (!stime.ValueKind.Equals(JsonValueKind.Undefined) && !stime.ValueKind.Equals(JsonValueKind.Null) && stime.TryGetInt64(out long data))
  251. {
  252. stimestamp = data;
  253. }
  254. }
  255. string stimesql = $" c.startTime >= {stimestamp} ";
  256. //默认当前时间, 未开始的不能查询
  257. var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  258. string etimesql = $" and c.startTime <= {etimestamp} ";
  259. var typesql = "";
  260. if (request.TryGetProperty("type", out JsonElement type))
  261. {
  262. if (!type.ValueKind.Equals(JsonValueKind.Undefined) && !type.ValueKind.Equals(JsonValueKind.Null) && type.ValueKind.Equals(JsonValueKind.String))
  263. {
  264. typesql = $" and c.type='{type}' ";
  265. }
  266. }
  267. string continuationToken = null;
  268. //默认不指定返回大小
  269. int? topcout = null;
  270. if (request.TryGetProperty("count", out JsonElement jcount))
  271. {
  272. if (!jcount.ValueKind.Equals(JsonValueKind.Undefined) && !jcount.ValueKind.Equals(JsonValueKind.Null) && jcount.TryGetInt32(out int data))
  273. {
  274. topcout = data;
  275. }
  276. }
  277. //是否需要进行分页查询,默认不分页
  278. bool iscontinuation = false;
  279. if (topcout != null && topcout.Value > 0)
  280. {
  281. iscontinuation = true;
  282. }
  283. //如果指定了返回大小
  284. if (request.TryGetProperty("continuationToken", out JsonElement token))
  285. {
  286. //指定了cancellationToken continuationSchool
  287. if (!token.ValueKind.Equals(JsonValueKind.Null) && token.ValueKind.Equals(JsonValueKind.String))
  288. {
  289. continuationToken = token.GetString();
  290. }
  291. }
  292. ////个人tmdid
  293. //string joinSqlTmdids = $"join A0 in c.tmdids";
  294. //string andSqlTmdids = $" A0 in('{id}')";
  295. ////班级
  296. //string joinSqlClasses = "";
  297. //string andSqlClasses = "";
  298. //List<string> classes = null;
  299. //if (request.TryGetProperty("classes", out JsonElement jclasses))
  300. //{
  301. // if (jclasses.ValueKind is JsonValueKind.Array)
  302. // {
  303. // classes = jclasses.ToObject<List<string>>();
  304. // if (classes.IsNotEmpty())
  305. // {
  306. // joinSqlClasses = " join A1 in c.classes ";
  307. // List<string> sqlList = new List<string>();
  308. // classes.ForEach(x => { sqlList.Add($" '{x}' "); });
  309. // string sql = string.Join(" , ", sqlList);
  310. // andSqlClasses = $" A1 in ({sql}) ";
  311. // }
  312. // }
  313. //}
  314. //string tgSql = "";
  315. //if (!string.IsNullOrWhiteSpace(joinSqlClasses))
  316. //{
  317. // tgSql = $" and ({andSqlTmdids} or {andSqlClasses } )";
  318. //}
  319. //else
  320. //{
  321. // tgSql = $" and {andSqlTmdids}";
  322. //}
  323. //科目
  324. string joinSqlSubjects = "";
  325. string andSqlSubjects = "";
  326. if (request.TryGetProperty("subjects", out JsonElement jsubjects))
  327. {
  328. if (jsubjects.ValueKind is JsonValueKind.Array)
  329. {
  330. List<string> subjects = jsubjects.ToObject<List<string>>();
  331. if (subjects.IsNotEmpty())
  332. {
  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<StuActivity> datas = new List<StuActivity>();
  342. var client = _azureCosmos.GetCosmosClient();
  343. if (!string.IsNullOrWhiteSpace(school))
  344. {
  345. string containerId = "Student";
  346. string PartitionKey = $"Activity-{school}-{id}";
  347. /// tmdid, schoolid
  348. if (userType.Equals("tmdid")) {
  349. containerId = "Teacher";
  350. PartitionKey = $"Activity-{id}";
  351. }
  352. string querySchool = $" SELECT distinct value c FROM c {joinSqlSubjects} where {stimesql} {etimesql} and c.pk='Activity' {typesql} {andSqlSubjects} ";
  353. //查询数据归属学校的
  354. await foreach (var item in client.GetContainer("TEAMModelOS", containerId).GetItemQueryStreamIterator(querySchool, continuationToken: continuationToken,
  355. requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey(PartitionKey) }))
  356. {
  357. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  358. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  359. {
  360. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  361. {
  362. datas.Add(obj.ToObject<StuActivity>());
  363. }
  364. //如果需要分页则跳出
  365. if (iscontinuation)
  366. {
  367. continuationToken = item.GetContinuationToken();
  368. break;
  369. }
  370. }
  371. }
  372. }
  373. return (datas, continuationToken);
  374. }
  375. public static async Task<int> Answer(JsonElement request, AzureCosmosFactory _azureCosmos, AzureRedisFactory azureRedis, string userid, AzureStorageFactory _azureStorage)
  376. {
  377. DateTimeOffset now = DateTimeOffset.UtcNow;
  378. long curr = now.ToUnixTimeMilliseconds();
  379. byte msgid = 0;//
  380. //活动id
  381. if (!request.TryGetProperty("id", out JsonElement id))
  382. {
  383. return msgid;
  384. }
  385. //活动分区
  386. if (!request.TryGetProperty("code", out JsonElement code))
  387. {
  388. return msgid;
  389. }
  390. try
  391. {
  392. //1.再次检查投票
  393. var client = _azureCosmos.GetCosmosClient();
  394. Survey survey = null;
  395. ///TODO 检查是否在投票范围内,包括在tmdids 及班级 但是需要处理认证金钥中的班级问题
  396. await foreach (var item in client.GetContainer("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}'",
  397. requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
  398. {
  399. survey = item;
  400. break;
  401. }
  402. if (survey != null)
  403. {
  404. //判断投票时间是否在起止时间内
  405. if (curr >= survey.startTime && curr <= survey.endTime)
  406. {
  407. if (request.TryGetProperty("record", out JsonElement record))
  408. {
  409. var recs = record.ToObject<List<List<string>>>();
  410. if (recs.IsNotEmpty() && recs.Count == survey.answers.Count)
  411. {
  412. //处理问卷调查表的每一题选项数
  413. // List<Task<string>> tasks = new List<Task<string>>();
  414. for (int index = 0; index < recs.Count; index++) {
  415. Dictionary<string, int> dict = new Dictionary<string, int>();
  416. if (recs[index].IsNotEmpty()) {
  417. recs[index].ForEach(x => {
  418. if (survey.answers[index].Contains(x))
  419. {
  420. if (dict.ContainsKey(x))
  421. {
  422. dict[x] = dict[x] + 1;
  423. }
  424. else {
  425. dict[x] = 1;
  426. }
  427. }
  428. else {
  429. if (dict.ContainsKey("other"))
  430. {
  431. dict["other"] = dict["other"] + 1;
  432. }
  433. else
  434. {
  435. dict["other"] = 1;
  436. }
  437. //这里暂不处理, 结算再处理other
  438. // tasks.Add(_azureStorage.UploadFileByContainer(survey.owner,new { other=x, userid, time =curr }.ToJsonString(), "survey", $"{survey.id}/other/{index}/{userid}.json", false));
  439. }
  440. });
  441. }
  442. var value= azureRedis.GetRedisClient(8).HashGet($"Survey:Record:{survey.id}",index);
  443. if (value != default && !value.IsNullOrEmpty)
  444. {
  445. Dictionary<string, int> dt = value.ToString().ToObject<Dictionary<string, int>>();
  446. foreach (var kp in dict)
  447. { //不建议放在reids
  448. if (dt.ContainsKey(kp.Key))
  449. {
  450. dt[kp.Key] = dt[kp.Key] + kp.Value;
  451. }
  452. else
  453. {
  454. dt.Add(kp.Key, kp.Value);
  455. }
  456. }
  457. await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dt.ToJsonString());
  458. }
  459. else {
  460. await azureRedis.GetRedisClient(8).HashSetAsync($"Survey:Record:{survey.id}", index, dict.ToJsonString());
  461. }
  462. }
  463. //处理other ,这里暂不处理, 结算再处理other
  464. //await Task.WhenAll(tasks);
  465. //保存当前提交人的记录
  466. string blobcntr = null;
  467. if (survey.scope == "school")
  468. {
  469. blobcntr = survey.school;
  470. }
  471. else if (survey.scope == "private")
  472. {
  473. blobcntr = survey.creatorId;
  474. }
  475. await _azureStorage.UploadFileByContainer(blobcntr, new SurveyRecord { ans= recs, userid=userid, time = curr }.ToJsonString(), "survey", $"{survey.id}/urecord/{userid}.json");
  476. await azureRedis.GetRedisClient(8).SetAddAsync($"Survey:Submit:{survey.id}", userid);
  477. msgid = 1;
  478. }
  479. else {
  480. //提交的作答不符合问卷的答案长度。
  481. msgid = 3;
  482. }
  483. }
  484. }
  485. else
  486. {
  487. msgid = 2;
  488. }
  489. }
  490. }
  491. catch (Exception e)
  492. {
  493. throw new Exception(e.StackTrace);
  494. }
  495. return msgid;
  496. }
  497. public class RdsRecord {
  498. public Dictionary<string, int> srecord { get; set; } = new Dictionary<string, int>();
  499. public Dictionary<string, string[]> urecord { get; set; } = new Dictionary<string, string[]>();
  500. }
  501. }
  502. }