123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.Models.Cosmos;
- using TEAMModelOS.SDK.Extension;
- using Azure.Cosmos;
- 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
- {
- public static class ActivityService
- {
- /// <summary>
- /// 活动委托
- /// </summary>
- /// <param name="data"></param>
- /// <returns></returns>
- 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();
- if (!requert.TryGetProperty("stime", out JsonElement stime))
- {
- if (stime.TryGetInt64(out long data))
- {
- stimestamp = data;
- }
- }
- if (!requert.TryGetProperty("type", out JsonElement type))
- {
- //if (stime.TryGetInt64(out long data))
- //{
- // stimestamp = data;
- //}
- }
- //默认当前时间, 未开始的不能查询
- var etimestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
- //if (!requert.TryGetProperty("etime", out JsonElement etime))
- //{
- // if (etime.TryGetInt64(out long data))
- // {
- // etimestamp = data;
- // };
- //};
- string continuationToken = null;
- //默认不指定返回大小
- int? topcout = null;
- if (!requert.TryGetProperty("count", out JsonElement jcount))
- {
- if (jcount.TryGetInt32(out int data))
- {
- topcout = data;
- }
- }
- //是否需要进行分页查询,默认不分页
- bool iscontinuation = false;
- //如果指定了返回大小
- if (!requert.TryGetProperty("continuationToken", out JsonElement continuation))
- {
- //指定了cancellationToken 表示需要进行分页
- if (!continuation.ValueKind.Equals(JsonValueKind.Null) && continuation.ValueKind.Equals(JsonValueKind.String))
- {
- continuationToken = continuation.GetString();
- iscontinuation = true;
- }
- }
- //班级
- List<string> classes=null;
- if (!requert.TryGetProperty("classes", out JsonElement jclasses))
- {
- if (jclasses.ValueKind is JsonValueKind.Array ) {
- classes = jclasses.ToObject<List<string>>();
- }
- }
- string query = null;
- if (classes.IsNotEmpty() )
- {
- StringBuilder insql =new StringBuilder();
- classes.ForEach( x => { insql.Append($"'{x}',"); });
- string sql = insql.ToString();
- sql = sql.ToString().Substring(0, sql.Length - 2);
- query = $" SELECT distinct value c FROM c JOIN A0 IN c.classes join A1 in c.tmdids where c.startTime >= {stimestamp} and c.startTime <= {etimestamp} and c.pk='Activity' and (A0 in('{sql}') or A1 in ('{id}')) ";
- }
- else {
- query = $" SELECT distinct value c FROM c join A1 in c.tmdids where c.startTime >= {stimestamp} and c.startTime <= {etimestamp} and c.pk='Activity' and A1 ='{id}' ";
- }
-
- 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}") }))
- {
- using var json = await JsonDocument.ParseAsync(item.ContentStream);
- if (json.RootElement.TryGetProperty("_count", out JsonElement count) && count.GetUInt16() > 0)
- {
- foreach (var obj in json.RootElement.GetProperty("Documents").EnumerateArray())
- {
- datas.Add(obj.ToObject<ActivityData>());
- }
- //如果需要分页则跳出
- if (iscontinuation)
- {
- continuationToken = item.GetContinuationToken();
- break;
- }
- }
- }
- bool tips = false;
- if (!requert.TryGetProperty("tips", out JsonElement jtips))
- {
- //指定了cancellationToken 表示需要进行分页
- if (!jtips.ValueKind.Equals(JsonValueKind.Null) && !jtips.ValueKind.Equals(JsonValueKind.True))
- {
- tips = jtips.GetBoolean();
- }
- }
- if (tips)
- {
- DoActivityTips activityTips;
- dynamic res = default;
- foreach (var data in datas)
- {
- switch (data.type)
- {
- //投票
- case "vote":
- activityTips = DoVoteTips;
- //msgid, //0不能投票,1可以投票,2不在时间范围内,3周期内的可投票数不足
- //voteCount 可用投票数
- res = activityTips(data, _azureCosmos, id, _azureRedis);
- break;
- //问卷
- case "survey":
- //msgid 0 已作答, 1未作答,2,未完成
- activityTips = DoSurveyTips;
- res = activityTips(data, _azureCosmos, id, _azureRedis);
- break;
- //评测
- case "exam":
- //msgid 0 已作答, 1未作答,2,未完成, 用时间控制 相关发布状态,并且展示相应的结果
- activityTips = DoExamTips;
- res = activityTips(data, _azureCosmos, id, _azureRedis);
- break;
- //学习活动
- case "learn":
- //msgid 0 已完成, 1未开始,2,未完成
- activityTips = DoLearnTips;
- res = activityTips(data, _azureCosmos, id, _azureRedis);
- 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 userid,AzureRedisFactory _azureRedis)
- {
- Vote vote=null;
- var client = _azureCosmos.GetCosmosClient();
- 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;
- }
- 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, AzureRedisFactory _azureRedis)
- {
- return null;
- }
- private static dynamic DoLearnTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
- {
- return null;
- }
- private static dynamic DoExamTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
- {
- return null;
- }
- private static dynamic DoSurveyTips(ActivityData commonData, AzureCosmosFactory _azureCosmos, string id, AzureRedisFactory _azureRedis)
- {
- return null;
- }
-
- }
- }
|