|
@@ -10,6 +10,8 @@ using TEAMModelOS.SDK.DI;
|
|
|
using HTEXLib.COMM.Helpers;
|
|
|
using System.Text;
|
|
|
using TEAMModelOS.SDK.Models;
|
|
|
+using TEAMModelOS.SDK.Models.Cosmos.Common.Inner;
|
|
|
+using StackExchange.Redis;
|
|
|
|
|
|
namespace TEAMModelOS.Services.Common
|
|
|
{
|
|
@@ -21,8 +23,179 @@ namespace TEAMModelOS.Services.Common
|
|
|
/// </summary>
|
|
|
/// <param name="data"></param>
|
|
|
/// <returns></returns>
|
|
|
- delegate dynamic DoActivityTips(ActivityData data, AzureCosmosFactory _azureCosmos,string id);
|
|
|
- public static async Task<(List<ActivityData> datas, string continuationToken)> FindByRole(string containerId, JsonElement requert, string id, AzureCosmosFactory _azureCosmos)
|
|
|
+ delegate dynamic DoActivityTips(ActivityData data, AzureCosmosFactory _azureCosmos,string id, AzureRedisFactory _azureRedis);
|
|
|
+
|
|
|
+ public static async Task<int> Decide(JsonElement request,AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis,string userid ) {
|
|
|
+
|
|
|
+ DateTimeOffset now = DateTimeOffset.UtcNow;
|
|
|
+ long curr = now.ToUnixTimeMilliseconds();
|
|
|
+ byte msgid = 0;//0投票失败,1投票成功,2不在时间范围内,3不在发布范围内,4投票周期内重复投票,5周期内的可投票数不足,6未设置投票项
|
|
|
+ //活动id
|
|
|
+ if (!request.TryGetProperty("id", out JsonElement id)) {
|
|
|
+ return msgid;
|
|
|
+ }
|
|
|
+ //活动分区
|
|
|
+ if (!request.TryGetProperty("code", out JsonElement code)) {
|
|
|
+ return msgid;
|
|
|
+ }
|
|
|
+ List<string> option = new List<string>();
|
|
|
+ if (request.TryGetProperty("option", out JsonElement joption))
|
|
|
+ {
|
|
|
+ option = joption.ToObject<List<string>>();
|
|
|
+ if (option.IsEmpty())
|
|
|
+ {
|
|
|
+ msgid = 6;
|
|
|
+ return msgid;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ return msgid;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
+ {
|
|
|
+ string Field = "";
|
|
|
+ RedisValue value;
|
|
|
+ switch (vote.times) {
|
|
|
+ case "once":
|
|
|
+ // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
|
|
|
+ Field = $"{userid}-once";
|
|
|
+ RedisValue[] values = _azureRedis.GetRedisClient(8).HashValues($"Vote:Record:{vote.id}_{vote.code}");
|
|
|
+ if (values != null && values.Length > 0)
|
|
|
+ {
|
|
|
+ msgid = await VoteIng(vote, values[0], msgid, option, Field, curr, _azureRedis);
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ msgid = 1;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "day": //周期内每天
|
|
|
+ Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+ msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis);
|
|
|
+ break;
|
|
|
+ case "week": //自然周
|
|
|
+ Field = $"{userid}-week-{now.ToString("yyyy")}{GetWeek(now)}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+ msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis);
|
|
|
+ break;
|
|
|
+ case "month": //月份
|
|
|
+ Field = $"{userid}-month-{now.ToString("yyyyMM")}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+ msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis);
|
|
|
+ break;
|
|
|
+ case "year"://年份
|
|
|
+ Field = $"{userid}-year-{now.ToString("yyyy")}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+ msgid = await VoteIng(vote, value, msgid, option, Field, curr, _azureRedis);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ msgid = 2;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ }
|
|
|
+ return msgid;
|
|
|
+ }
|
|
|
+ public static async Task<byte> VoteIng(Vote vote, RedisValue value, byte msgid, List<string> option, string Field, long curr, AzureRedisFactory _azureRedis)
|
|
|
+ {
|
|
|
+ if (!value.IsNullOrEmpty)
|
|
|
+ {
|
|
|
+ VoteRecord record=value.ToString().ToObject<VoteRecord>();
|
|
|
+ //处理记录投票+当前设置的投票是否小于等于周期内最大投票数
|
|
|
+ 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}_{vote.code}", Field, record.ToJsonString());
|
|
|
+ //当前投票分组计数存入活动的Redis
|
|
|
+ var group_opt= option.GroupBy(x => x);
|
|
|
+ foreach (var opt in group_opt) {
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}_{vote.code}", opt.Key, opt.Count());
|
|
|
+ }
|
|
|
+ if (status)
|
|
|
+ {
|
|
|
+ msgid = 1;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ msgid = 5;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //保存投票记录
|
|
|
+ VoteRecord record = new VoteRecord { opt = option, time = curr };
|
|
|
+ bool status = await _azureRedis.GetRedisClient(8).HashSetAsync($"Vote:Record:{vote.id}_{vote.code}", Field, record.ToJsonString());
|
|
|
+ //当前投票分组计数存入活动的Redis
|
|
|
+ var group_opt = option.GroupBy(x => x);
|
|
|
+ foreach (var opt in group_opt)
|
|
|
+ {
|
|
|
+ await _azureRedis.GetRedisClient(8).SortedSetIncrementAsync($"Vote:Count:{vote.id}_{vote.code}", opt.Key, opt.Count());
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ ///
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="containerId">容器</param>
|
|
|
+ /// <param name="requert"></param>
|
|
|
+ /// <param name="id">登录者ID</param>
|
|
|
+ /// <param name="_azureCosmos"></param>
|
|
|
+ /// <returns></returns>
|
|
|
+ public static async Task<(List<ActivityData> datas, string continuationToken)> FindByRole(string containerId, JsonElement requert, string id, AzureCosmosFactory _azureCosmos,AzureRedisFactory _azureRedis)
|
|
|
{
|
|
|
//开始时间,默认最近三十天
|
|
|
var stimestamp = DateTimeOffset.UtcNow.AddDays(-30).ToUnixTimeMilliseconds();
|
|
@@ -35,10 +208,10 @@ namespace TEAMModelOS.Services.Common
|
|
|
}
|
|
|
if (!requert.TryGetProperty("type", out JsonElement type))
|
|
|
{
|
|
|
- if (stime.TryGetInt64(out long data))
|
|
|
- {
|
|
|
- stimestamp = data;
|
|
|
- }
|
|
|
+ //if (stime.TryGetInt64(out long data))
|
|
|
+ //{
|
|
|
+ // stimestamp = data;
|
|
|
+ //}
|
|
|
}
|
|
|
//默认当前时间, 未开始的不能查询
|
|
|
var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
|
|
@@ -48,7 +221,6 @@ namespace TEAMModelOS.Services.Common
|
|
|
// {
|
|
|
// etimestamp = data;
|
|
|
// };
|
|
|
-
|
|
|
//};
|
|
|
string continuationToken = null;
|
|
|
//默认不指定返回大小
|
|
@@ -94,8 +266,6 @@ namespace TEAMModelOS.Services.Common
|
|
|
}
|
|
|
|
|
|
List<ActivityData> datas = new List<ActivityData>();
|
|
|
-
|
|
|
-
|
|
|
var client = _azureCosmos.GetCosmosClient();
|
|
|
await foreach (var item in client.GetContainer("TEAMModelOS", containerId).GetItemQueryStreamIterator(query, continuationToken: continuationToken, requestOptions: new QueryRequestOptions() { MaxItemCount = topcout, PartitionKey = new PartitionKey($"Common-{id}") }))
|
|
|
{
|
|
@@ -123,69 +293,159 @@ namespace TEAMModelOS.Services.Common
|
|
|
tips = jtips.GetBoolean();
|
|
|
}
|
|
|
}
|
|
|
- DoActivityTips activityTips;
|
|
|
- dynamic res = default;
|
|
|
- foreach (var data in datas)
|
|
|
+ if (tips)
|
|
|
{
|
|
|
- switch (data.type)
|
|
|
+ DoActivityTips activityTips;
|
|
|
+ dynamic res = default;
|
|
|
+ foreach (var data in datas)
|
|
|
{
|
|
|
+ switch (data.type)
|
|
|
+ {
|
|
|
//投票
|
|
|
- case "vote":
|
|
|
- activityTips = DoVoteTips;
|
|
|
- res = activityTips(data,_azureCosmos, id );
|
|
|
- break;
|
|
|
+ case "vote":
|
|
|
+ activityTips = DoVoteTips;
|
|
|
+ //msgid, //0不能投票,1可以投票,2不在时间范围内,3周期内的可投票数不足
|
|
|
+ //voteCount 可用投票数
|
|
|
+ res = activityTips(data, _azureCosmos, id, _azureRedis);
|
|
|
+ break;
|
|
|
//问卷
|
|
|
- case "survey":
|
|
|
- activityTips = DoSurveyTips;
|
|
|
- res = activityTips(data, _azureCosmos,id);
|
|
|
- break;
|
|
|
+ case "survey":
|
|
|
+ //msgid 0 已作答, 1未作答,2,未完成
|
|
|
+ activityTips = DoSurveyTips;
|
|
|
+ res = activityTips(data, _azureCosmos, id, _azureRedis);
|
|
|
+ break;
|
|
|
//评测
|
|
|
- case "exam":
|
|
|
- activityTips = DoExamTips;
|
|
|
- res = activityTips(data, _azureCosmos, id);
|
|
|
- break;
|
|
|
+ case "exam":
|
|
|
+ //msgid 0 已作答, 1未作答,2,未完成, 用时间控制 相关发布状态,并且展示相应的结果
|
|
|
+ activityTips = DoExamTips;
|
|
|
+ res = activityTips(data, _azureCosmos, id, _azureRedis);
|
|
|
+ break;
|
|
|
//学习活动
|
|
|
- case "learn":
|
|
|
- activityTips = DoLearnTips;
|
|
|
- res = activityTips(data, _azureCosmos, id);
|
|
|
- break;
|
|
|
+ case "learn":
|
|
|
+ //msgid 0 已完成, 1未开始,2,未完成
|
|
|
+ activityTips = DoLearnTips;
|
|
|
+ res = activityTips(data, _azureCosmos, id, _azureRedis);
|
|
|
+ break;
|
|
|
//作业活动
|
|
|
- case "homework":
|
|
|
- activityTips = DoHomeworkTips;
|
|
|
- res = activityTips(data, _azureCosmos, id);
|
|
|
- break;
|
|
|
- default: break;
|
|
|
+ case "homework":
|
|
|
+ //msgid 0 已作答, 1未作答,2,未完成,3已批改,且有错误,4已批改,已完成
|
|
|
+ //index:0,1,5 错误题序
|
|
|
+ activityTips = DoHomeworkTips;
|
|
|
+ res = activityTips(data, _azureCosmos, id, _azureRedis);
|
|
|
+ break;
|
|
|
+ default: break;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
return (datas, continuationToken);
|
|
|
}
|
|
|
- private async static Task<dynamic> DoVoteTips(ActivityData commonData , AzureCosmosFactory _azureCosmos,string id )
|
|
|
+ private async static Task<dynamic> DoVoteTips(ActivityData commonData , AzureCosmosFactory _azureCosmos,string userid,AzureRedisFactory _azureRedis)
|
|
|
{
|
|
|
Vote vote=null;
|
|
|
var client = _azureCosmos.GetCosmosClient();
|
|
|
- RedisHelper.HGet($"Vote:{id}","");
|
|
|
await foreach (var item in client.GetContainer("TEAMModelOS", "Common").GetItemQueryIterator<Vote>($"select value c from c where c.id='{commonData.id}'",
|
|
|
requestOptions: new QueryRequestOptions() { PartitionKey = new PartitionKey(commonData.scode) }))
|
|
|
{
|
|
|
vote = item;
|
|
|
break;
|
|
|
}
|
|
|
-
|
|
|
- return null;
|
|
|
+ byte msgid = 0;
|
|
|
+ int voteCount = 0;
|
|
|
+ if (vote != null) {
|
|
|
+ DateTimeOffset now = DateTimeOffset.UtcNow;
|
|
|
+ long curr = now.ToUnixTimeMilliseconds();
|
|
|
+ //判断投票时间是否在起止时间内
|
|
|
+ if (curr >= vote.startTime && curr <= vote.endTime)
|
|
|
+ {
|
|
|
+ string Field = "";
|
|
|
+ RedisValue value = default;
|
|
|
+ switch (vote.times)
|
|
|
+ {
|
|
|
+ case "once":
|
|
|
+ // //如果是只能投票一次的活动则直接获取Redis的第一条 只能投一次
|
|
|
+ Field = $"{userid}-once";
|
|
|
+ RedisValue[] values = _azureRedis.GetRedisClient(8).HashValues($"Vote:Record:{vote.id}_{vote.code}");
|
|
|
+ if (values != null && values.Length>0)
|
|
|
+ {
|
|
|
+ value = values[0];
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "day": //周期内每天
|
|
|
+ Field = $"{userid}-day-{now.ToString("yyyyMMdd")}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+ break;
|
|
|
+ case "week": //自然周
|
|
|
+ Field = $"{userid}-week-{now.ToString("yyyy")}{GetWeek(now)}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+
|
|
|
+ break;
|
|
|
+ case "month": //月份
|
|
|
+ Field = $"{userid}-month-{now.ToString("yyyyMM")}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+
|
|
|
+ break;
|
|
|
+ case "year"://年份
|
|
|
+ Field = $"{userid}-year-{now.ToString("yyyy")}";
|
|
|
+ value = _azureRedis.GetRedisClient(8).HashGet($"Vote:Record:{vote.id}_{vote.code}", Field);
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (value != default && value.IsNullOrEmpty)
|
|
|
+ {
|
|
|
+ List<string> opt = null;
|
|
|
+ JsonElement record = value.ToString().ToObject<JsonElement>();
|
|
|
+ if (record.TryGetProperty("opt", out JsonElement jopt)) {
|
|
|
+ opt = jopt.ToObject<List<string>>();
|
|
|
+ }
|
|
|
+ if (opt != null)
|
|
|
+ {
|
|
|
+ //处理记录投票是否小于等于周期内最大投票数
|
|
|
+ if (opt.Count <= vote.voteNum)
|
|
|
+ {
|
|
|
+ voteCount = vote.voteNum - opt.Count;
|
|
|
+ msgid = 1;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ //3周期内的可投票数不足
|
|
|
+ msgid = 3;
|
|
|
+ voteCount = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ msgid = 1;
|
|
|
+ voteCount = vote.voteNum;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ //未投票,可以投票
|
|
|
+ msgid = 1;
|
|
|
+ voteCount = vote.voteNum;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ msgid = 2;
|
|
|
+ voteCount = 0;
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return new {msgid,voteCount };
|
|
|
}
|
|
|
- private static dynamic DoHomeworkTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
|
|
|
+
|
|
|
+ private static dynamic DoHomeworkTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
- private static dynamic DoLearnTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
|
|
|
+ private static dynamic DoLearnTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
- private static dynamic DoExamTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
|
|
|
+ private static dynamic DoExamTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
- private static dynamic DoSurveyTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id)
|
|
|
+ private static dynamic DoSurveyTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
|
|
|
{
|
|
|
return null;
|
|
|
}
|