VoteController.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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.Helper.Common.CollectionHelper;
  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. namespace TEAMModelOS.Controllers.Learn
  29. {
  30. /// <summary>
  31. /// 投票活动
  32. /// </summary>
  33. [ProducesResponseType(StatusCodes.Status200OK)]
  34. [ProducesResponseType(StatusCodes.Status400BadRequest)]
  35. //[Authorize(Roles = "IES5")]
  36. [Route("common/vote")]
  37. [ApiController]
  38. public class VoteController : ControllerBase
  39. {
  40. private readonly AzureRedisFactory _azureRedis;
  41. private readonly AzureCosmosFactory _azureCosmos;
  42. private readonly SnowflakeId _snowflakeId;
  43. private readonly AzureServiceBusFactory _serviceBus;
  44. private readonly DingDing _dingDing;
  45. private readonly Option _option;
  46. public VoteController(AzureCosmosFactory azureCosmos, AzureServiceBusFactory serviceBus, SnowflakeId snowflakeId, DingDing dingDing, IOptionsSnapshot<Option> option,
  47. AzureRedisFactory azureRedis)
  48. {
  49. _azureCosmos = azureCosmos;
  50. _serviceBus = serviceBus;
  51. _snowflakeId = snowflakeId;
  52. _dingDing = dingDing;
  53. _option = option?.Value;
  54. _azureRedis = azureRedis;
  55. }
  56. /// <summary>
  57. /// 新增 或 修改投票活动
  58. /// </summary>
  59. /// <param name="request"></param>
  60. /// <returns></returns>
  61. [ProducesDefaultResponseType]
  62. [HttpPost("upsert")]
  63. public async Task<IActionResult> Upsert(Vote request)
  64. {
  65. try
  66. {
  67. //新增Vote
  68. var client = _azureCosmos.GetCosmosClient();
  69. request.code = request.pk + "-" + request.code;
  70. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  71. request.createTime = now;
  72. if (string.IsNullOrEmpty(request.id))
  73. {
  74. request.id = Guid.NewGuid().ToString();
  75. if (request.startTime < now)
  76. {
  77. request.progress = "pending";
  78. }
  79. else {
  80. request.progress = "going";
  81. }
  82. request = await client.GetContainer("TEAMModelOS", "Common").CreateItemAsync(request, new PartitionKey($"{request.code}"));
  83. }
  84. else
  85. {
  86. Vote info = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(request.id, new PartitionKey($"{request.code}"));
  87. if (info.progress.Equals("going"))
  88. {
  89. return Ok(new { v = "活动正在进行中" });
  90. }
  91. request.progress = info.progress;
  92. request = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(request, info.id, new PartitionKey($"{info.code}"));
  93. }
  94. return Ok(new { vote = request });
  95. }
  96. catch (Exception e)
  97. {
  98. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/save()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  99. return BadRequest(e.StackTrace);
  100. }
  101. }
  102. /// <summary>
  103. /// 查询投票活动,用于列表,编辑,查看
  104. /// </summary>
  105. /// <data>
  106. ///Vote-学校/教师编码 活动分区 !"code":"hbcn"/1606285227
  107. ///时间筛选范围开始时间 默认30天之前 ?"stime":1608274766154
  108. ///时间筛选范围结束时间 默认当前时间 ?"etime":1608274766666
  109. ///每页大小 ?"count":10/null/Undefined
  110. ///分页Token ?"continuationToken":Undefined/null/"[{\"token\":\"+RID:~omxMAP3ipcSEEwAAAAAAAA==#RT:2#TRC:20#ISV:2#IEO:65551#QCF:1#FPC:AYQTAAAAAAAAiRMAAAAAAAA=\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}]"
  111. /// 当前状态 ?"progress":Undefined/null/"" 表示两种状态都要查询/ "going"/"finish" 表示查询进行中/ 或者已完成 学生端只能查询正在进行或已经结束 going 已发布|finish 已结束
  112. /// </data>
  113. /// <param name="request"></param>
  114. /// <returns></returns>
  115. [ProducesDefaultResponseType]
  116. [HttpPost("find")]
  117. public async Task<IActionResult> Find(JsonElement requert)
  118. {
  119. try
  120. {
  121. //必须有学校或者教师编码
  122. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  123. //开始时间,默认最近三十天
  124. var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
  125. if (requert.TryGetProperty("stime", out JsonElement stime)) {
  126. if (!stime.ValueKind.Equals(JsonValueKind.Undefined)&&stime.TryGetInt64(out long data))
  127. {
  128. stimestamp = data;
  129. };
  130. };
  131. //默认当前时间
  132. var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  133. if (requert.TryGetProperty("etime", out JsonElement etime))
  134. {
  135. if (!etime.ValueKind.Equals(JsonValueKind.Undefined)&&etime.TryGetInt64(out long data))
  136. {
  137. etimestamp = data;
  138. };
  139. };
  140. var progresssql = "";
  141. if (!requert.TryGetProperty("progress", out JsonElement progress))
  142. {
  143. if (!progress.ValueKind.Equals(JsonValueKind.Undefined) && !progress.ValueKind.Equals(JsonValueKind.Null) && progress.ValueKind.Equals(JsonValueKind.String))
  144. {
  145. progresssql = $" and c.progress='{progresssql}' ";
  146. }
  147. }
  148. string continuationToken = null;
  149. //默认不指定返回大小
  150. int? topcout=null;
  151. if (requert.TryGetProperty("count", out JsonElement jcount)) {
  152. if(!jcount.ValueKind.Equals(JsonValueKind.Undefined) && jcount.TryGetInt32(out int data))
  153. {
  154. topcout = data;
  155. }
  156. };
  157. //是否需要进行分页查询,默认不分页
  158. bool iscontinuation = false;
  159. //如果指定了返回大小
  160. if (requert.TryGetProperty("continuationToken", out JsonElement continuation))
  161. {
  162. //指定了cancellationToken 表示需要进行分页
  163. if (!continuation.ValueKind.Equals(JsonValueKind.Null) && !continuation.ValueKind.Equals(JsonValueKind.Undefined))
  164. {
  165. continuationToken = continuation.GetString();
  166. iscontinuation = true;
  167. }
  168. };
  169. List<object> votes = new List<object>();
  170. var client = _azureCosmos.GetCosmosClient();
  171. 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 } ";
  172. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query,
  173. requestOptions: new QueryRequestOptions() {MaxItemCount = topcout, PartitionKey = new PartitionKey($"Vote-{code}") }))
  174. {
  175. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  176. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  177. {
  178. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  179. {
  180. votes.Add(obj.ToObject<JsonElement>());
  181. }
  182. //如果需要分页则跳出
  183. if (iscontinuation) {
  184. continuationToken = item.GetContinuationToken();
  185. break;
  186. }
  187. }
  188. }
  189. return Ok(new { votes, continuationToken });
  190. }
  191. catch (Exception ex)
  192. {
  193. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find()\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  194. return BadRequest(ex.StackTrace);
  195. }
  196. }
  197. ///<summary>
  198. /// 查询投票活动,用于创建者列表,编辑,查看,投票人员查看
  199. /// </summary>
  200. /// <data>
  201. /// ! "id":"3c075347-75ef-4bcb-ae03-68678d02d5ef",
  202. /// ! "code":"Vote-hbcn"/"code":"Vote-1606285227"
  203. /// </data>
  204. /// <param name="request"></param>
  205. /// <returns></returns>
  206. [ProducesDefaultResponseType]
  207. [HttpPost("find-id")]
  208. public async Task<IActionResult> FindById(JsonElement requert)
  209. {
  210. try
  211. {
  212. var client = _azureCosmos.GetCosmosClient();
  213. //活动id
  214. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  215. //活动分区
  216. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  217. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}"));
  218. if (vote != null)
  219. {
  220. return Ok(new { vote });
  221. }
  222. else
  223. {
  224. return BadRequest("id,code不存在!");
  225. }
  226. }
  227. catch (Exception ex)
  228. {
  229. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  230. return BadRequest(ex.StackTrace);
  231. }
  232. }
  233. /// <summary>
  234. /// 删除投票活动 TODO 使用ttl删除,并处理相关事务逻辑
  235. /// </summary>
  236. /// <param name="request"></param>
  237. /// <returns></returns>
  238. [ProducesDefaultResponseType]
  239. [HttpPost("delete")]
  240. [AuthToken(Roles = "admin,teacher")]
  241. public async Task<IActionResult> Delete(JsonElement request)
  242. {
  243. try
  244. {
  245. var (userid, _, _,school) = HttpContext.GetAuthTokenInfo();
  246. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  247. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  248. var client = _azureCosmos.GetCosmosClient();
  249. Vote vote =await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}") );
  250. bool flag = false;
  251. //必须是本人或者这个学校的管理者才能删除
  252. if (vote.creatorId == userid)
  253. {
  254. flag = true;
  255. }
  256. else {
  257. if (vote.scope == "school"&& vote.owner.Equals(school)) {
  258. flag = true;
  259. }
  260. }
  261. if (flag)
  262. {
  263. //使用ttl删除,并处理相关事务逻辑
  264. vote.ttl = 1;
  265. vote = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, vote.id, new PartitionKey($"{vote.code}"));
  266. return Ok(new { flag });
  267. }
  268. else {
  269. return Ok(new { flag });
  270. }
  271. //var response = await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  272. // return Ok(new { code = response.Status });
  273. }
  274. catch (Exception e)
  275. {
  276. return BadRequest(e.StackTrace);
  277. }
  278. }
  279. /// <summary>
  280. /// 投票
  281. /// </summary>
  282. /// <redis>
  283. /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
  284. /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
  285. /// </redis>
  286. /// <param name="request">
  287. /// !"id":"aaaa"
  288. /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
  289. /// !"option":["A","B","B"]/["1","2","3"]
  290. /// </param>
  291. /// <returns>
  292. /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
  293. /// </returns>
  294. [ProducesDefaultResponseType]
  295. [HttpPost("decide")]
  296. [AuthToken(Roles = "teacher,student")]
  297. public async Task<IActionResult> UpsertRecord(JsonElement request)
  298. {
  299. var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  300. int msgid = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis, userid);
  301. return Ok(new { msgid });
  302. }
  303. }
  304. }