|
@@ -21,6 +21,9 @@ using TEAMModelOS.Models;
|
|
using Microsoft.Extensions.Options;
|
|
using Microsoft.Extensions.Options;
|
|
using TEAMModelOS.SDK.Models.Cosmos;
|
|
using TEAMModelOS.SDK.Models.Cosmos;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
+using TEAMModelOS.Filter;
|
|
|
|
+using StackExchange.Redis;
|
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
|
|
|
|
|
|
namespace TEAMModelOS.Controllers.Learn
|
|
namespace TEAMModelOS.Controllers.Learn
|
|
{
|
|
{
|
|
@@ -197,7 +200,7 @@ namespace TEAMModelOS.Controllers.Learn
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
[ProducesDefaultResponseType]
|
|
[ProducesDefaultResponseType]
|
|
[HttpPost("find-tch")]
|
|
[HttpPost("find-tch")]
|
|
- [Authorize(Roles = "teacher")]
|
|
|
|
|
|
+ [AuthToken(Roles = "teacher")]
|
|
public async Task<IActionResult> FindTch(JsonElement requert)
|
|
public async Task<IActionResult> FindTch(JsonElement requert)
|
|
{
|
|
{
|
|
(List<ActivityData> datas, string continuationToken) = await FindByRole("Teacher", requert);
|
|
(List<ActivityData> datas, string continuationToken) = await FindByRole("Teacher", requert);
|
|
@@ -220,7 +223,7 @@ namespace TEAMModelOS.Controllers.Learn
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
[ProducesDefaultResponseType]
|
|
[ProducesDefaultResponseType]
|
|
[HttpPost("find-stu")]
|
|
[HttpPost("find-stu")]
|
|
- [Authorize(Roles = "student")]
|
|
|
|
|
|
+ [AuthToken(Roles = "student")]
|
|
public async Task<IActionResult> FindStu(JsonElement requert)
|
|
public async Task<IActionResult> FindStu(JsonElement requert)
|
|
{
|
|
{
|
|
(List<ActivityData> datas, string continuationToken) =await FindByRole("Student", requert);
|
|
(List<ActivityData> datas, string continuationToken) =await FindByRole("Student", requert);
|
|
@@ -376,10 +379,11 @@ namespace TEAMModelOS.Controllers.Learn
|
|
/// <data>
|
|
/// <data>
|
|
/// !"id":"aaaa"
|
|
/// !"id":"aaaa"
|
|
/// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
|
|
/// !"code":"Vote-hbcn"/"code":"Vote-1606285227"
|
|
- /// !"option":"A/B/C..1/2/3..."
|
|
|
|
|
|
+ /// !"option":["A","B","B"]/["1","2","3"]
|
|
/// </data>
|
|
/// </data>
|
|
/// <redis>
|
|
/// <redis>
|
|
- /// 投票活动选项计数器 数据集合 key:"Vote:Count:AAA",value:"A",score:1
|
|
|
|
|
|
+ /// 投票活动选项计数器 使用SortedSet(有序集合)ZSET 数据集合 使用命令 ZINCRBY key:"Vote:Count:AAA",value:"A",score:1
|
|
|
|
+ /// 投票活动 投票记录 使用Hash(哈希表)使用命令 key:"Vote:Record:AAA",feild:15283771540-20210105,value:"{"opt":["A","C","A"],"time":1608274766154}"
|
|
/// </redis>
|
|
/// </redis>
|
|
/// <param name="request"></param>
|
|
/// <param name="request"></param>
|
|
/// <returns></returns>
|
|
/// <returns></returns>
|
|
@@ -387,11 +391,154 @@ namespace TEAMModelOS.Controllers.Learn
|
|
[HttpPost("decide")]
|
|
[HttpPost("decide")]
|
|
public async Task<IActionResult> UpsertRecord(JsonElement request)
|
|
public async Task<IActionResult> UpsertRecord(JsonElement request)
|
|
{
|
|
{
|
|
- //1.再次检查投票
|
|
|
|
-
|
|
|
|
- //2.处理投票
|
|
|
|
|
|
+ DateTimeOffset now = DateTimeOffset.UtcNow;
|
|
|
|
|
|
- return Ok(request);
|
|
|
|
|
|
+ long curr= now.ToUnixTimeMilliseconds();
|
|
|
|
+ var (userid, _, _, _) = HttpContext.GetAuthTokenInfo();
|
|
|
|
+ byte msgid =0;//投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
|
|
|
|
+ //活动id
|
|
|
|
+ if (!request.TryGetProperty("id", out JsonElement id)) return BadRequest();
|
|
|
|
+ //活动分区
|
|
|
|
+ if (!request.TryGetProperty("code", out JsonElement code)) return BadRequest();
|
|
|
|
+ List<string> option = new List<string>() ;
|
|
|
|
+ if (request.TryGetProperty("option", out JsonElement joption)) {
|
|
|
|
+ option= joption.ToObject<List<string>>();
|
|
|
|
+ if (option.IsEmpty()) {
|
|
|
|
+ msgid = 6;
|
|
|
|
+ return Ok( new { msgid});
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ return BadRequest();
|
|
|
|
+ }
|
|
|
|
+ VoteRecord record = null;
|
|
|
|
+ try {
|
|
|
|
+ //1.再次检查投票
|
|
|
|
+ var client = _azureCosmos.GetCosmosClient();
|
|
|
|
+ Vote vote = null;
|
|
|
|
+ ///TODO 检查是否在投票范围内,包括在tmdids 及班级 但是需要处理认证金钥中的班级问题
|
|
|
|
+ await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Vote>(queryText: $"select c.progress,c.times,c.voteNum,c.startTime,c.endTime from c where c.id = '{id}'",
|
|
|
|
+ requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey($"{code}") }))
|
|
|
|
+ {
|
|
|
|
+ vote = item;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ if (vote != null)
|
|
|
|
+ {
|
|
|
|
+ //判断投票时间是否在起止时间内
|
|
|
|
+ if (curr >= vote.startTime && curr <= vote.endTime)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ //如果是只能投票一次的活动则直接获取Redis的第一条
|
|
|
|
+ if (vote.times.Equals("once"))
|
|
|
|
+ {
|
|
|
|
+ //只能投一次
|
|
|
|
+ string Field = $"{userid}-once";
|
|
|
|
+ RedisValue[] values = _azureRedis.GetRedisClient(8).HashValues($"Vote:Record:{vote.id}");
|
|
|
|
+ if (values != null)
|
|
|
|
+ {
|
|
|
|
+ record = values[0].ToString().ToObject<VoteRecord>();
|
|
|
|
+ msgid=await VoteIng(vote, record, msgid, option, Field, curr);
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ msgid = 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else if (vote.times.Equals("day"))
|
|
|
|
+ {
|
|
|
|
+ //周期内每天
|
|
|
|
+ string Field= $"{userid}-day-{now.ToString("yyyyMMdd")}";
|
|
|
|
+ RedisValue value= _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
|
|
|
|
+ record = value.ToString().ToObject<VoteRecord>();
|
|
|
|
+ msgid = await VoteIng(vote, record, msgid, option, Field, curr);
|
|
|
|
+ }
|
|
|
|
+ else if (vote.times.Equals("week"))
|
|
|
|
+ {
|
|
|
|
+ //自然周
|
|
|
|
+ string Field = $"{userid}-week-{now.ToString("yyyy")}{GetWeek(now)}";
|
|
|
|
+ RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
|
|
|
|
+ record = value.ToString().ToObject<VoteRecord>();
|
|
|
|
+ msgid = await VoteIng(vote, record, msgid, option, Field, curr);
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ else if (vote.times.Equals("month"))
|
|
|
|
+ {
|
|
|
|
+ //月份
|
|
|
|
+ string Field = $"{userid}-month-{now.ToString("yyyyMM")}";
|
|
|
|
+ RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
|
|
|
|
+ record = value.ToString().ToObject<VoteRecord>();
|
|
|
|
+ msgid = await VoteIng(vote, record, msgid, option, Field, curr);
|
|
|
|
+ }
|
|
|
|
+ else if (vote.times.Equals("year"))
|
|
|
|
+ {
|
|
|
|
+ //年份
|
|
|
|
+ string Field = $"{userid}-year-{now.ToString("yyyy")}";
|
|
|
|
+ RedisValue value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}", Field);
|
|
|
|
+ record = value.ToString().ToObject<VoteRecord>();
|
|
|
|
+ msgid = await VoteIng(vote, record, msgid, option, Field,curr);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ msgid = 2;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ catch (Exception e) {
|
|
|
|
+ }
|
|
|
|
+ return Ok(new { msgid });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task<byte> VoteIng(Vote vote, VoteRecord record,byte msgid,List<string> option,string Field ,long curr) {
|
|
|
|
+ if (record != null)
|
|
|
|
+ {
|
|
|
|
+ //处理记录投票+当前设置的投票是否小于等于周期内最大投票数
|
|
|
|
+ if (record.opt.Count + option.Count <= vote.voteNum)
|
|
|
|
+ {
|
|
|
|
+ record.opt.AddRange(option);
|
|
|
|
+ record.time = curr;
|
|
|
|
+ bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
|
|
|
|
+ if (status)
|
|
|
|
+ {
|
|
|
|
+ msgid = 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ msgid = 5;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ else {
|
|
|
|
+ record = new VoteRecord { opt = option, time = curr };
|
|
|
|
+ bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}", Field, record.ToJsonString());
|
|
|
|
+ if (status)
|
|
|
|
+ {
|
|
|
|
+ msgid = 1;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return msgid;
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取时间的在当年的第几周
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="dt"></param>
|
|
|
|
+ /// <returns></returns>
|
|
|
|
+ public static int GetWeek(DateTimeOffset dt)
|
|
|
|
+ {
|
|
|
|
+ DateTimeOffset time = Convert.ToDateTime(dt.ToString("yyyy") + "-01-01");
|
|
|
|
+ TimeSpan ts = dt - time;
|
|
|
|
+ int iii = (int)time.DayOfWeek;
|
|
|
|
+ int day = int.Parse(ts.TotalDays.ToString("F0"));
|
|
|
|
+ if (iii == 0)
|
|
|
|
+ {
|
|
|
|
+ day--;
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ day = day - (7 - iii) - 1;
|
|
|
|
+ }
|
|
|
|
+ int week = ((day + 7) / 7) + 1;
|
|
|
|
+ return week;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|