VoteController.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. using Azure.Cosmos;
  2. using Microsoft.AspNetCore.Http;
  3. using Microsoft.AspNetCore.Mvc;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IdentityModel.Tokens.Jwt;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Text.Json;
  10. using System.Threading.Tasks;
  11. using TEAMModelOS.Models.Dto;
  12. using TEAMModelOS.SDK.Models;
  13. using TEAMModelOS.SDK;
  14. using TEAMModelOS.SDK.Context.Constant.Common;
  15. using TEAMModelOS.SDK.DI;
  16. using TEAMModelOS.SDK.DI.AzureCosmos.Inner;
  17. using TEAMModelOS.SDK.Extension;
  18. using TEAMModelOS.SDK;
  19. using TEAMModelOS.SDK.Helper.Common.StringHelper;
  20. using TEAMModelOS.Models;
  21. using Microsoft.Extensions.Options;
  22. using TEAMModelOS.SDK.Models.Cosmos;
  23. using Microsoft.AspNetCore.Authorization;
  24. using TEAMModelOS.Filter;
  25. using StackExchange.Redis;
  26. using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
  27. using TEAMModelOS.Services.Common;
  28. using Azure.Messaging.ServiceBus;
  29. using Microsoft.Extensions.Configuration;
  30. namespace TEAMModelOS.Controllers.Learn
  31. {
  32. /// <summary>
  33. /// 投票活动
  34. /// </summary>
  35. [ProducesResponseType(StatusCodes.Status200OK)]
  36. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  37. //[Authorize(Roles = "IES5")]
  38. [Route("common/vote")]
  39. [ApiController]
  40. public class VoteController : ControllerBase
  41. {
  42. private readonly AzureRedisFactory _azureRedis;
  43. private readonly AzureCosmosFactory _azureCosmos;
  44. private readonly SnowflakeId _snowflakeId;
  45. private readonly AzureServiceBusFactory _serviceBus;
  46. private readonly DingDing _dingDing;
  47. private readonly Option _option;
  48. private readonly AzureStorageFactory _azureStorage;
  49. public IConfiguration _configuration { get; set; }
  50. public VoteController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
  51. AzureRedisFactory azureRedis, AzureStorageFactory azureStorage, IConfiguration configuration)
  52. {
  53. _azureCosmos = azureCosmos;
  54. _serviceBus = serviceBus;
  55. _snowflakeId = snowflakeId;
  56. _dingDing = dingDing;
  57. _option = option?.Value;
  58. _azureRedis = azureRedis;
  59. _azureStorage = azureStorage;
  60. _configuration = configuration;
  61. }
  62. /// <summary>
  63. /// 新增 或 修改投票活动
  64. /// </summary>
  65. /// <param name="request"></param>
  66. /// <returns></returns>
  67. [ProducesDefaultResponseType]
  68. [HttpPost("upsert")]
  69. [AuthToken(Roles = "teacher,admin")]
  70. public async Task<IActionResult> Upsert(Vote request)
  71. {
  72. try
  73. {
  74. //新增Vote
  75. var client = _azureCosmos.GetCosmosClient();
  76. request.code = request.pk + "-" + request.code;
  77. request.ttl = -1;
  78. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  79. request.createTime = now;
  80. //如果设置的时间是小于当前时间则立即发布
  81. if (request.startTime <= 0) {
  82. request.startTime = now;
  83. }
  84. if (string.IsNullOrEmpty(request.id))
  85. {
  86. request.id = Guid.NewGuid().ToString();
  87. if (request.startTime > now)
  88. {
  89. request.progress = "pending";
  90. }
  91. else {
  92. request.progress = "going";
  93. }
  94. var messageBlob = new ServiceBusMessage();
  95. if (request.scope.Equals("school"))
  96. {
  97. request.size = await _azureStorage.GetBlobContainerClient(request.school).GetBlobsSize($"vote/{request.id}");
  98. messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "insert", root = $"vote/{request.id}", name = $"{request.school}" }.ToJsonString());
  99. }
  100. else
  101. {
  102. request.size = await _azureStorage.GetBlobContainerClient(request.creatorId).GetBlobsSize($"vote/{request.id}");
  103. messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "insert", root = $"vote/{request.id}", name = $"{request.creatorId}" }.ToJsonString());
  104. }
  105. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  106. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  107. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  108. request = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request, new PartitionKey($"{request.code}"));
  109. }
  110. else
  111. {
  112. var response = await client.GetContainer("TEAMModelOS", "Common").ReadItemStreamAsync(request.id, new PartitionKey($"{request.code}"));
  113. var messageBlob = new ServiceBusMessage();
  114. if (request.scope.Equals("school"))
  115. {
  116. request.size = await _azureStorage.GetBlobContainerClient(request.school).GetBlobsSize($"vote/{request.id}");
  117. messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = $"vote/{request.id}", name = $"{request.school}" }.ToJsonString());
  118. }
  119. else
  120. {
  121. request.size = await _azureStorage.GetBlobContainerClient(request.creatorId).GetBlobsSize($"vote/{request.id}");
  122. messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "update", root = $"vote/{request.id}", name = $"{request.creatorId}" }.ToJsonString());
  123. }
  124. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  125. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  126. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  127. if (response.Status == 200)
  128. {
  129. using var json = await JsonDocument.ParseAsync(response.ContentStream);
  130. var info = json.ToObject<Vote>();
  131. if (info.progress.Equals("going"))
  132. {
  133. return Ok(new { v = "活动正在进行中" });
  134. }
  135. if (request.startTime > now)
  136. {
  137. request.progress = "pending";
  138. }
  139. else
  140. {
  141. request.progress = "going";
  142. }
  143. request.progress = info.progress;
  144. request = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(request, info.id, new PartitionKey($"{info.code}"));
  145. }
  146. else
  147. {
  148. if (request.startTime > now)
  149. {
  150. request.progress = "pending";
  151. }
  152. else
  153. {
  154. request.progress = "going";
  155. }
  156. request = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request, new PartitionKey($"{request.code}"));
  157. }
  158. }
  159. return Ok(new { vote = request });
  160. }
  161. catch (Exception e)
  162. {
  163. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/save()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  164. return BadRequest(e.StackTrace);
  165. }
  166. }
  167. /// <summary>
  168. /// 查询投票活动,用于列表,编辑,查看
  169. /// </summary>
  170. /// <data>
  171. ///Vote-学校/教师编码 活动分区 !"code":"hbcn"/1606285227
  172. ///时间筛选范围开始时间 默认30天之前 ?"stime":1608274766154
  173. ///时间筛选范围结束时间 默认当前时间 ?"etime":1608274766666
  174. ///每页大小 ?"count":10/null/Undefined
  175. ///分页Token ?"continuationToken":Undefined/null/"[{\"token\":\"+RID:~omxMAP3ipcSEEwAAAAAAAA==#RT:2#TRC:20#ISV:2#IEO:65551#QCF:1#FPC:AYQTAAAAAAAAiRMAAAAAAAA=\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}]"
  176. /// 当前状态 ?"progress":Undefined/null/"" 表示两种状态都要查询/ "going"/"finish" 表示查询进行中/ 或者已完成 学生端只能查询正在进行或已经结束 going 已发布|finish 已结束
  177. /// </data>
  178. /// <param name="request"></param>
  179. /// <returns></returns>
  180. [ProducesDefaultResponseType]
  181. [HttpPost("find")]
  182. [AuthToken(Roles = "teacher,admin")]
  183. public async Task<IActionResult> Find(JsonElement requert)
  184. {
  185. try
  186. {
  187. var (userid, _, _, school) = HttpContext.GetAuthTokenInfo();
  188. //必须有学校或者教师编码
  189. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  190. //开始时间,默认最近三十天
  191. var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
  192. if (requert.TryGetProperty("stime", out JsonElement stime)) {
  193. if (long.TryParse($"{stime}", out long data))
  194. {
  195. stimestamp = data;
  196. };
  197. };
  198. //默认当前时间
  199. var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  200. if (requert.TryGetProperty("etime", out JsonElement etime))
  201. {
  202. if (long.TryParse($"{etime}", out long data))
  203. {
  204. etimestamp = data;
  205. };
  206. };
  207. var progresssql = "";
  208. if (!requert.TryGetProperty("progress", out JsonElement progress))
  209. {
  210. if (!progress.ValueKind.Equals(JsonValueKind.Undefined) && !progress.ValueKind.Equals(JsonValueKind.Null) && progress.ValueKind.Equals(JsonValueKind.String))
  211. {
  212. progresssql = $" and c.progress='{progresssql}' ";
  213. }
  214. }
  215. string continuationToken = null;
  216. //默认不指定返回大小
  217. int? topcout=null;
  218. if (requert.TryGetProperty("count", out JsonElement jcount)) {
  219. if (int.TryParse($"{jcount}", out int data))
  220. {
  221. topcout = data;
  222. }
  223. };
  224. //是否需要进行分页查询,默认不分页
  225. bool iscontinuation = false;
  226. //如果指定了返回大小
  227. if (requert.TryGetProperty("continuationToken", out JsonElement continuation))
  228. {
  229. //指定了cancellationToken 表示需要进行分页
  230. if (!continuation.ValueKind.Equals(JsonValueKind.Null) && !continuation.ValueKind.Equals(JsonValueKind.Undefined))
  231. {
  232. continuationToken = continuation.GetString();
  233. iscontinuation = true;
  234. }
  235. };
  236. List<Vote> votes = new List<Vote>();
  237. var client = _azureCosmos.GetCosmosClient();
  238. var query = $"select c.id,c.name,c.code,c.startTime,c.endTime,c.progress ,c.scope,c.school from c where c.createTime >= {stimestamp} and c.createTime <= {etimestamp} {progresssql } and c.ttl=-1 ";
  239. if (string.IsNullOrEmpty(school))
  240. {
  241. query = $"{query} and c.scope='private' ";
  242. }
  243. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Vote>(queryText: query,
  244. requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Vote-{code}") }))
  245. {
  246. if (!string.IsNullOrEmpty(school))
  247. {
  248. //只能查出相关学校的
  249. if (!item.scope.Equals("private") && !string.IsNullOrEmpty(item.school) && item.school.Equals(school))
  250. {
  251. votes.Add(item);
  252. }
  253. //和自己私人发布的
  254. if (item.scope.Equals("private"))
  255. {
  256. votes.Add(item);
  257. }
  258. }
  259. else
  260. {
  261. votes.Add(item);
  262. }
  263. }
  264. //List<object> votes = new List<object>();
  265. //var client = _azureCosmos.GetCosmosClient();
  266. //var query =$"select c.id,c.name,c.code,c.startTime,c.endTime,c.progress from c where c.createTime >= {stimestamp} and c.createTime <= {etimestamp} {progresssql } ";
  267. //await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query,
  268. // requestOptions: new QueryRequestOptions() {MaxItemCount = topcout, PartitionKey = new PartitionKey($"Vote-{code}") }))
  269. //{
  270. // using var json = await JsonDocument.ParseAsync(item.ContentStream);
  271. // if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  272. // {
  273. // foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  274. // {
  275. // votes.Add(obj.ToObject<JsonElement>());
  276. // }
  277. // //如果需要分页则跳出
  278. // if (iscontinuation) {
  279. // continuationToken = item.GetContinuationToken();
  280. // break;
  281. // }
  282. // }
  283. //}
  284. return Ok(new { votes, continuationToken });
  285. }
  286. catch (Exception ex)
  287. {
  288. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find()\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  289. return BadRequest(ex.StackTrace);
  290. }
  291. }
  292. ///<summary>
  293. /// 查询投票活动,用于创建者列表,编辑,查看,投票人员查看
  294. /// </summary>
  295. /// <data>
  296. /// ! "id":"3c075347-75ef-4bcb-ae03-68678d02d5ef",
  297. /// ! "code":"Vote-hbcn"/"code":"Vote-1606285227"
  298. /// </data>
  299. /// <param name="request"></param>
  300. /// <returns></returns>
  301. [ProducesDefaultResponseType]
  302. [HttpPost("find-id")]
  303. [AuthToken(Roles = "teacher,admin,student")]
  304. public async Task<IActionResult> FindById(JsonElement requert)
  305. {
  306. Vote vote = null;
  307. //活动id
  308. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  309. //活动分区
  310. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  311. try
  312. {
  313. var client = _azureCosmos.GetCosmosClient();
  314. vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}"));
  315. if (vote != null)
  316. {
  317. return Ok(new { vote, status = 200 });
  318. }
  319. else
  320. {
  321. return Ok(new { vote, status = 404 });
  322. }
  323. }
  324. catch (Exception ex)
  325. {
  326. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find-id()\n{ex.Message}\n{id}\n{code}", GroupNames.醍摩豆服務運維群組);
  327. return Ok(new { vote,status=404 });
  328. }
  329. }
  330. /// <summary>
  331. /// 删除投票活动 TODO 使用ttl删除,并处理相关事务逻辑
  332. /// </summary>
  333. /// <param name="request"></param>
  334. /// <returns></returns>
  335. [ProducesDefaultResponseType]
  336. [HttpPost("delete")]
  337. [AuthToken(Roles = "admin,teacher")]
  338. public async Task<IActionResult> Delete(JsonElement request)
  339. {
  340. try
  341. {
  342. var (userid, _, _,school) = HttpContext.GetAuthTokenInfo();
  343. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  344. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  345. var client = _azureCosmos.GetCosmosClient();
  346. Vote vote =await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}") );
  347. bool flag = false;
  348. //必须是本人或者这个学校的管理者才能删除
  349. if (vote.creatorId == userid)
  350. {
  351. flag = true;
  352. }
  353. else {
  354. if (vote.scope == "school"&& vote.school.Equals(school)) {
  355. flag = true;
  356. }
  357. }
  358. if (flag)
  359. {
  360. //使用ttl删除,并处理相关事务逻辑
  361. vote.ttl = 1;
  362. vote.status = 404;
  363. vote = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote,vote.id, new PartitionKey($"{vote.code}"));
  364. await _dingDing.SendBotMsg($"{_option.Location}-投票活动【{vote.name}-{vote.id}-ttl={vote.ttl}】正在操作", GroupNames.成都开发測試群組);
  365. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Record:{vote.id}");
  366. _azureRedis.GetRedisClient(8).KeyDelete($"Vote:Count:{vote.id}");
  367. await _dingDing.SendBotMsg($"{_option.Location}-投票活动【{vote.name}-{vote.id}】被删除", GroupNames.成都开发測試群組);
  368. //删除blob 相关资料
  369. await _azureStorage.GetBlobServiceClient().DeleteBlobs(_dingDing, $"{code}".Replace("Vote-",""), new List<string> { $"vote/{vote.id}" });
  370. //通知删除信息
  371. var messageBlob = new ServiceBusMessage(new { id = Guid.NewGuid().ToString(), progress = "delete", root = $"vote/{vote.id}", name = $"{code}" }.ToJsonString());
  372. messageBlob.ApplicationProperties.Add("name", "BlobRoot");
  373. var ActiveTask = _configuration.GetValue<string>("Azure:ServiceBus:ActiveTask");
  374. await _serviceBus.GetServiceBusClient().SendMessageAsync(ActiveTask, messageBlob);
  375. return Ok(new { flag });
  376. }
  377. else {
  378. return Ok(new { flag });
  379. }
  380. }
  381. catch (Exception e)
  382. {
  383. return BadRequest(e.StackTrace);
  384. }
  385. }
  386. /// <summary>
  387. /// 投票记录 当活动没结算且没有BlobUrl时则调用此接口
  388. /// </summary>
  389. /// <redis>
  390. /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
  391. /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
  392. /// </redis>
  393. /// <param name="request">
  394. /// !"id":"aaaa"
  395. /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
  396. /// </param>
  397. /// <returns>
  398. /// </returns>
  399. [ProducesDefaultResponseType]
  400. [HttpPost("record")]
  401. [AuthToken(Roles = "teacher,admin,student")]
  402. public async Task<IActionResult> Record(JsonElement request)
  403. {
  404. if (!request.TryGetProperty("id", out JsonElement id))
  405. {
  406. return BadRequest();
  407. }
  408. //活动分区
  409. if (!request.TryGetProperty("code", out JsonElement code))
  410. {
  411. return BadRequest();
  412. }
  413. //获取投票活动的所有投票记录
  414. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{id}");
  415. //获取投票活动的选项及投票数
  416. var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{id}");
  417. List<dynamic> options = new List<dynamic>();
  418. if (counts != null && counts.Length > 0)
  419. {
  420. foreach (var count in counts)
  421. {
  422. options.Add(new { code = count.Element.ToString(), count = (int)count.Score });
  423. }
  424. }
  425. List<JsonElement> res = new List<JsonElement>();
  426. foreach (var rcd in records)
  427. {
  428. var value = rcd.Value.ToString().ToObject<JsonElement>();
  429. res.Add(value);
  430. }
  431. return Ok(new { options, records= res});
  432. }
  433. /// <summary>
  434. /// 个人已投票记录查询
  435. /// </summary>
  436. /// <param name="request"></param>
  437. /// <returns></returns>
  438. [ProducesDefaultResponseType]
  439. [HttpPost("decided")]
  440. [AuthToken(Roles = "teacher,student")]
  441. public async Task<IActionResult> Decided(JsonElement request)
  442. {
  443. var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  444. if (!request.TryGetProperty("id", out JsonElement id))
  445. {
  446. return BadRequest();
  447. }
  448. //活动分区
  449. if (!request.TryGetProperty("code", out JsonElement code))
  450. {
  451. return BadRequest();
  452. }
  453. //获取投票活动的所有投票记录
  454. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{id}:{userid}");
  455. List<JsonElement> res = new List<JsonElement>();
  456. foreach (var rcd in records)
  457. {
  458. var value = rcd.Value.ToString().ToObject<JsonElement>();
  459. res.Add(value);
  460. }
  461. return Ok(new { records= res });
  462. }
  463. /// <summary>
  464. /// 投票
  465. /// </summary>
  466. /// <redis>
  467. /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
  468. /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
  469. /// </redis>
  470. /// <param name="request">
  471. /// !"id":"aaaa"
  472. /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
  473. /// !"option":{"A":5,"B":6}/{"1":5,"2":8}
  474. /// </param>
  475. /// <returns>
  476. /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足
  477. ///
  478. /// </returns>
  479. [ProducesDefaultResponseType]
  480. [HttpPost("decide")]
  481. [AuthToken(Roles = "teacher,student")]
  482. public async Task<IActionResult> Decide(JsonElement request)
  483. {
  484. var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  485. int msgid = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis, userid);
  486. return Ok(new { msgid });
  487. }
  488. /// <summary>
  489. /// 投票
  490. /// </summary>
  491. /// <redis>
  492. /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
  493. /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
  494. /// </redis>
  495. /// <param name="request">
  496. /// !"id":"aaaa"
  497. /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
  498. /// !"option":{"A":5,"B":6}/{"1":5,"2":8}
  499. /// !"userid":"15283771540"
  500. /// </param>
  501. /// <returns>
  502. /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
  503. /// </returns>
  504. [ProducesDefaultResponseType]
  505. [HttpPost("decide-mock")]
  506. public async Task<IActionResult> DecideMock(JsonElement request)
  507. {
  508. //var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  509. //活动id
  510. if (request.TryGetProperty("userid", out JsonElement userid))
  511. {
  512. int msgid = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis, $"{userid}");
  513. return Ok(new { msgid });
  514. }
  515. else { return Ok(new { msgid = 0 }); }
  516. }
  517. }
  518. }