VoteController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. if (request.startTime > now)
  92. {
  93. request.progress = "pending";
  94. }
  95. else
  96. {
  97. request.progress = "going";
  98. }
  99. request.progress = info.progress;
  100. request = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(request, info.id, new PartitionKey($"{info.code}"));
  101. }
  102. return Ok(new { vote = request });
  103. }
  104. catch (Exception e)
  105. {
  106. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/save()\n{e.Message}", GroupNames.醍摩豆服務運維群組);
  107. return BadRequest(e.StackTrace);
  108. }
  109. }
  110. /// <summary>
  111. /// 查询投票活动,用于列表,编辑,查看
  112. /// </summary>
  113. /// <data>
  114. ///Vote-学校/教师编码 活动分区 !"code":"hbcn"/1606285227
  115. ///时间筛选范围开始时间 默认30天之前 ?"stime":1608274766154
  116. ///时间筛选范围结束时间 默认当前时间 ?"etime":1608274766666
  117. ///每页大小 ?"count":10/null/Undefined
  118. ///分页Token ?"continuationToken":Undefined/null/"[{\"token\":\"+RID:~omxMAP3ipcSEEwAAAAAAAA==#RT:2#TRC:20#ISV:2#IEO:65551#QCF:1#FPC:AYQTAAAAAAAAiRMAAAAAAAA=\",\"range\":{\"min\":\"\",\"max\":\"FF\"}}]"
  119. /// 当前状态 ?"progress":Undefined/null/"" 表示两种状态都要查询/ "going"/"finish" 表示查询进行中/ 或者已完成 学生端只能查询正在进行或已经结束 going 已发布|finish 已结束
  120. /// </data>
  121. /// <param name="request"></param>
  122. /// <returns></returns>
  123. [ProducesDefaultResponseType]
  124. [HttpPost("find")]
  125. public async Task<IActionResult> Find(JsonElement requert)
  126. {
  127. try
  128. {
  129. //必须有学校或者教师编码
  130. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  131. //开始时间,默认最近三十天
  132. var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
  133. if (requert.TryGetProperty("stime", out JsonElement stime)) {
  134. if (!stime.ValueKind.Equals(JsonValueKind.Undefined)&&stime.TryGetInt64(out long data))
  135. {
  136. stimestamp = data;
  137. };
  138. };
  139. //默认当前时间
  140. var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  141. if (requert.TryGetProperty("etime", out JsonElement etime))
  142. {
  143. if (!etime.ValueKind.Equals(JsonValueKind.Undefined)&&etime.TryGetInt64(out long data))
  144. {
  145. etimestamp = data;
  146. };
  147. };
  148. var progresssql = "";
  149. if (!requert.TryGetProperty("progress", out JsonElement progress))
  150. {
  151. if (!progress.ValueKind.Equals(JsonValueKind.Undefined) && !progress.ValueKind.Equals(JsonValueKind.Null) && progress.ValueKind.Equals(JsonValueKind.String))
  152. {
  153. progresssql = $" and c.progress='{progresssql}' ";
  154. }
  155. }
  156. string continuationToken = null;
  157. //默认不指定返回大小
  158. int? topcout=null;
  159. if (requert.TryGetProperty("count", out JsonElement jcount)) {
  160. if(!jcount.ValueKind.Equals(JsonValueKind.Undefined) && jcount.TryGetInt32(out int data))
  161. {
  162. topcout = data;
  163. }
  164. };
  165. //是否需要进行分页查询,默认不分页
  166. bool iscontinuation = false;
  167. //如果指定了返回大小
  168. if (requert.TryGetProperty("continuationToken", out JsonElement continuation))
  169. {
  170. //指定了cancellationToken 表示需要进行分页
  171. if (!continuation.ValueKind.Equals(JsonValueKind.Null) && !continuation.ValueKind.Equals(JsonValueKind.Undefined))
  172. {
  173. continuationToken = continuation.GetString();
  174. iscontinuation = true;
  175. }
  176. };
  177. List<object> votes = new List<object>();
  178. var client = _azureCosmos.GetCosmosClient();
  179. 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 } ";
  180. await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryStreamIterator(queryText: query,
  181. requestOptions: new QueryRequestOptions() {MaxItemCount = topcout, PartitionKey = new PartitionKey($"Vote-{code}") }))
  182. {
  183. using var json = await JsonDocument.ParseAsync(item.ContentStream);
  184. if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
  185. {
  186. foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
  187. {
  188. votes.Add(obj.ToObject<JsonElement>());
  189. }
  190. //如果需要分页则跳出
  191. if (iscontinuation) {
  192. continuationToken = item.GetContinuationToken();
  193. break;
  194. }
  195. }
  196. }
  197. return Ok(new { votes, continuationToken });
  198. }
  199. catch (Exception ex)
  200. {
  201. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find()\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
  202. return BadRequest(ex.StackTrace);
  203. }
  204. }
  205. ///<summary>
  206. /// 查询投票活动,用于创建者列表,编辑,查看,投票人员查看
  207. /// </summary>
  208. /// <data>
  209. /// ! "id":"3c075347-75ef-4bcb-ae03-68678d02d5ef",
  210. /// ! "code":"Vote-hbcn"/"code":"Vote-1606285227"
  211. /// </data>
  212. /// <param name="request"></param>
  213. /// <returns></returns>
  214. [ProducesDefaultResponseType]
  215. [HttpPost("find-id")]
  216. public async Task<IActionResult> FindById(JsonElement requert)
  217. {
  218. try
  219. {
  220. var client = _azureCosmos.GetCosmosClient();
  221. //活动id
  222. if (!requert.TryGetProperty("id", out JsonElement id)) return BadRequest();
  223. //活动分区
  224. if (!requert.TryGetProperty("code", out JsonElement code)) return BadRequest();
  225. Vote vote = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}"));
  226. if (vote != null)
  227. {
  228. return Ok(new { vote });
  229. }
  230. else
  231. {
  232. return BadRequest("id,code不存在!");
  233. }
  234. }
  235. catch (Exception ex)
  236. {
  237. await _dingDing.SendBotMsg($"OS,{_option.Location},common/vote/find-id()\n{ex.Message}", GroupNames.醍摩豆服務運維群組);
  238. return BadRequest(ex.StackTrace);
  239. }
  240. }
  241. /// <summary>
  242. /// 删除投票活动 TODO 使用ttl删除,并处理相关事务逻辑
  243. /// </summary>
  244. /// <param name="request"></param>
  245. /// <returns></returns>
  246. [ProducesDefaultResponseType]
  247. [HttpPost("delete")]
  248. [AuthToken(Roles = "admin,teacher")]
  249. public async Task<IActionResult> Delete(JsonElement request)
  250. {
  251. try
  252. {
  253. var (userid, _, _,school) = HttpContext.GetAuthTokenInfo();
  254. if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
  255. if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
  256. var client = _azureCosmos.GetCosmosClient();
  257. Vote vote =await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<Vote>(id.GetString(), new PartitionKey($"{code}") );
  258. bool flag = false;
  259. //必须是本人或者这个学校的管理者才能删除
  260. if (vote.creatorId == userid)
  261. {
  262. flag = true;
  263. }
  264. else {
  265. if (vote.scope == "school"&& vote.owner.Equals(school)) {
  266. flag = true;
  267. }
  268. }
  269. if (flag)
  270. {
  271. //使用ttl删除,并处理相关事务逻辑
  272. vote.ttl = 1;
  273. vote = await client.GetContainer("TEAMModelOS", "Common").ReplaceItemAsync(vote, vote.id, new PartitionKey($"{vote.code}"));
  274. return Ok(new { flag });
  275. }
  276. else {
  277. return Ok(new { flag });
  278. }
  279. //var response = await client.GetContainer("TEAMModelOS", "Common").DeleteItemStreamAsync(id.ToString(), new PartitionKey($"{code}"));
  280. // return Ok(new { code = response.Status });
  281. }
  282. catch (Exception e)
  283. {
  284. return BadRequest(e.StackTrace);
  285. }
  286. }
  287. /// <summary>
  288. /// 投票记录 当活动没结算且没有BlobUrl时则调用此接口
  289. /// </summary>
  290. /// <redis>
  291. /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
  292. /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
  293. /// </redis>
  294. /// <param name="request">
  295. /// !"id":"aaaa"
  296. /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
  297. /// </param>
  298. /// <returns>
  299. /// </returns>
  300. [ProducesDefaultResponseType]
  301. [HttpPost("record")]
  302. //[AuthToken(Roles = "teacher,student")]
  303. public async Task<IActionResult> Record(JsonElement request)
  304. {
  305. string uid = null;
  306. if (request.TryGetProperty("userid", out JsonElement userid)) {
  307. if (userid.ValueKind.Equals(JsonValueKind.String)) {
  308. uid = userid.GetString();
  309. }
  310. }
  311. //if (string.IsNullOrWhiteSpace(uid)) {
  312. // var (auid, _, _, _) = HttpContext.GetAuthTokenInfo();
  313. // uid = auid;
  314. //}
  315. if (!request.TryGetProperty("id", out JsonElement id))
  316. {
  317. return BadRequest();
  318. }
  319. //活动分区
  320. if (!request.TryGetProperty("code", out JsonElement code))
  321. {
  322. return BadRequest();
  323. }
  324. //获取投票活动的所有投票记录
  325. var records = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"Vote:Record:{id}_{code}");
  326. //获取投票活动的选项及投票数
  327. var counts = _azureRedis.GetRedisClient(8).SortedSetRangeByScoreWithScores($"Vote:Count:{id}_{code}");
  328. List<OptionVote> options = new List<OptionVote>();
  329. if (counts != null && counts.Length > 0)
  330. {
  331. foreach (var count in counts)
  332. {
  333. options.Add(new OptionVote { code = count.Element.ToString(), count = (int)count.Score });
  334. }
  335. }
  336. List<JsonElement> res = new List<JsonElement>();
  337. foreach (var rcd in records)
  338. {
  339. var key = rcd.Name.ToString().Split("-")[0];
  340. if (!string.IsNullOrWhiteSpace(uid))
  341. {
  342. if (uid.Equals(key))
  343. {
  344. var value = rcd.Value.ToString().ToObject<JsonElement>();
  345. res.Add(value);
  346. }
  347. }
  348. else {
  349. var value = rcd.Value.ToString().ToObject<JsonElement>();
  350. res.Add(value);
  351. }
  352. }
  353. return Ok(new { options, records= res });
  354. }
  355. /// <summary>
  356. /// 投票
  357. /// </summary>
  358. /// <redis>
  359. /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
  360. /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
  361. /// </redis>
  362. /// <param name="request">
  363. /// !"id":"aaaa"
  364. /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
  365. /// !"option":["A","B","B"]/["1","2","3"]
  366. /// </param>
  367. /// <returns>
  368. /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
  369. /// </returns>
  370. [ProducesDefaultResponseType]
  371. [HttpPost("decide")]
  372. [AuthToken(Roles = "teacher,student")]
  373. public async Task<IActionResult> Decide(JsonElement request)
  374. {
  375. var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  376. int msgid = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis, userid);
  377. return Ok(new { msgid });
  378. }
  379. /// <summary>
  380. /// 投票
  381. /// </summary>
  382. /// <redis>
  383. /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
  384. /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
  385. /// </redis>
  386. /// <param name="request">
  387. /// !"id":"aaaa"
  388. /// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
  389. /// !"option":["A","B","B"]/["1","2","3"]
  390. /// !"userid":"15283771540"
  391. /// </param>
  392. /// <returns>
  393. /// msgid=0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
  394. /// </returns>
  395. [ProducesDefaultResponseType]
  396. [HttpPost("decide-mock")]
  397. public async Task<IActionResult> DecideMock(JsonElement request)
  398. {
  399. //var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
  400. //活动id
  401. if (request.TryGetProperty("userid", out JsonElement userid))
  402. {
  403. int msgid = await ActivityStudentService.Decide(request, _azureCosmos, _azureRedis, $"{userid}");
  404. return Ok(new { msgid });
  405. }
  406. else { return Ok(new { msgid = 0 }); }
  407. }
  408. }
  409. }