123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249 |
- using Azure.Messaging.ServiceBus;
- using HTEXLib.COMM.Helpers;
- using Microsoft.Extensions.Configuration;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.Json;
- using System.Threading.Tasks;
- using TEAMModelOS.SDK.DI;
- using TEAMModelOS.SDK.Extension;
- using TEAMModelOS.SDK.Models;
- using TEAMModelOS.SDK;
- using Azure.Cosmos;
- using TEAMModelOS.SDK.Models.Cosmos.Common;
- using Azure.Core;
- using TEAMModelOS.SDK.Models.Service;
- namespace TEAMModelOS.FunctionV4.CosmosDB
- {
- internal class TriggerArt
- {
- public static async Task Trigger(CoreAPIHttpService _coreAPIHttpService, AzureServiceBusFactory _serviceBus, AzureStorageFactory _azureStorage, DingDing _dingDing,
- CosmosClient client, JsonElement input, TriggerData tdata, AzureRedisFactory _azureRedis, IConfiguration _configuration)
- {
- try
- {
- if ((tdata.status != null && tdata.status.Value == 404))
- {
- await client.GetContainer(Constant.TEAMModelOS, "Common").DeleteItemStreamAsync(tdata.id, new PartitionKey(tdata.code));
- ActivityList data = input.ToObject<ActivityList>();
- await ActivityService.DeleteActivity(_coreAPIHttpService, client, _dingDing, data);
- var table_cancel = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
- List<ChangeRecord> records = await table_cancel.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id } });
- foreach (var record in records)
- {
- try
- {
- await table_cancel.DeleteSingle<ChangeRecord>(record.PartitionKey, record.RowKey);
- await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), record.sequenceNumber);
- }
- catch (Exception)
- {
- continue;
- }
- }
- return;
- }
- var table = _azureStorage.GetCloudTableClient().GetTableReference("ChangeRecord");
- var adid = tdata.id;
- var adcode = "";
- string blobcntr = null;
- if (tdata.scope.Equals("school"))
- {
- adcode = $"Activity-{tdata.school}";
- blobcntr = tdata.school;
- }
- else
- {
- adcode = $"Activity-{tdata.creatorId}";
- blobcntr = tdata.creatorId;
- }
- ArtEvaluation art = await client.GetContainer("TEAMModelOS", "Common").ReadItemAsync<ArtEvaluation>(tdata.id, new Azure.Cosmos.PartitionKey($"{tdata.code}"));
- if (art != null)
- {
- string PartitionKey = string.Format("{0}{1}{2}", art.code, "-", art.progress);
- List<ChangeRecord> voteRecords = await table.FindListByDict<ChangeRecord>(new Dictionary<string, object>() { { "RowKey", tdata.id }, { "PartitionKey", PartitionKey } });
- switch (art.progress)
- {
- case "pending":
- var messageVote = new ServiceBusMessage(new { id = tdata.id, progress = "going", code = tdata.code }.ToJsonString());
- messageVote.ApplicationProperties.Add("name", "Art");
- if (voteRecords.Count > 0)
- {
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
- try
- {
- await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
- }
- catch (Exception)
- {
- }
- voteRecords[0].sequenceNumber = start;
- await table.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
- }
- else
- {
- long start = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVote, DateTimeOffset.FromUnixTimeMilliseconds(tdata.startTime));
- ChangeRecord changeRecord = new ChangeRecord
- {
- RowKey = tdata.id,
- PartitionKey = PartitionKey,
- sequenceNumber = start,
- msgId = messageVote.MessageId
- };
- await table.Save<ChangeRecord>(changeRecord);
- }
- break;
- case "going":
- List<string> classes = ExamService.getClasses(art.classes, art.stuLists);
- (List<RMember> tmdIds, List<RGroupList> classLists) = await GroupListService.GetMemberByListids(_coreAPIHttpService, client, _dingDing, classes, art.school, null);
- var addStudentsCls = tmdIds.FindAll(x => x.type == 2);
- var addTmdidsCls = tmdIds.FindAll(x => x.type == 1);
- List<string> tmds = new List<string>();
- if (addTmdidsCls.IsNotEmpty())
- {
- tmds.AddRange(addTmdidsCls.Select(x => x.id).ToList());
- }
- List<StuActivity> stuActivities = new List<StuActivity>();
- List<StuActivity> tmdActivities = new List<StuActivity>();
- List<StuActivity> tchActivities = new List<StuActivity>();
- List<string> sub = new();
- if (art.subjects.Count > 0)
- {
- foreach (var course in art.subjects)
- {
- sub.Add(course.id);
- }
- }
- if (tmds.IsNotEmpty())
- {
- tmds.ForEach(x =>
- {
- HashSet<string> classIds = new HashSet<string>();
- classLists.ForEach(z =>
- {
- z.members.ForEach(y =>
- {
- if (y.id.Equals(x) && y.type == 1)
- {
- classIds.Add(z.id);
- }
- });
- });
- tmdActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = art.id,
- code = $"Activity-{x}",
- type = "Art",
- name = art.name,
- startTime = art.startTime,
- endTime = art.endTime,
- scode = art.code,
- scope = art.scope,
- school = art.school,
- creatorId = art.creatorId,
- subjects = sub,
- blob = null,
- owner = art.owner,
- createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- ext = new Dictionary<string, JsonElement>() { { "subjects", art.subjects.ToJsonString().ToObject<JsonElement>() } },
- taskStatus = -1,
- classIds = classIds.ToList()
- });
- });
- }
- if (addStudentsCls.IsNotEmpty())
- {
- addStudentsCls.ForEach(x =>
- {
- HashSet<string> classIds = new HashSet<string>();
- classLists.ForEach(z =>
- {
- z.members.ForEach(y =>
- {
- if (y.id.Equals(x.id) && y.code.Equals(art.school) && y.type == 2)
- {
- classIds.Add(z.id);
- }
- });
- });
- stuActivities.Add(new StuActivity
- {
- pk = "Activity",
- id = art.id,
- code = $"Activity-{x.code.Replace("Base-", "")}-{x.id}",
- type = "Atr",
- name = art.name,
- startTime = art.startTime,
- endTime = art.endTime,
- scode = art.code,
- scope = art.scope,
- school = art.school,
- creatorId = art.creatorId,
- subjects = sub,
- blob = null,
- owner = art.owner,
- createTime = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
- ext = new Dictionary<string, JsonElement>() {{ "subjects", art.subjects.ToJsonString().ToObject<JsonElement>() } },
- taskStatus = -1,
- classIds = classIds.ToList()
- });
- });
- }
- await ActivityService.SaveStuActivity(client, _dingDing, stuActivities, tmdActivities, tchActivities);
- //await StatisticsService.SendServiceBus(list, _configuration, _serviceBus, client);
- var messageVoteEnd = new ServiceBusMessage(new { id = tdata.id, progress = "finish", code = tdata.code }.ToJsonString());
- messageVoteEnd.ApplicationProperties.Add("name", "Art");
- if (voteRecords.Count > 0)
- {
- long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
- try
- {
- await _serviceBus.GetServiceBusClient().CancelMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), voteRecords[0].sequenceNumber);
- }
- catch (Exception)
- {
- }
- voteRecords[0].sequenceNumber = end;
- await table.SaveOrUpdate<ChangeRecord>(voteRecords[0]);
- }
- else
- {
- long end = await _serviceBus.GetServiceBusClient().SendScheduleMessageAsync(Environment.GetEnvironmentVariable("Azure:ServiceBus:ActiveTask"), messageVoteEnd, DateTimeOffset.FromUnixTimeMilliseconds(tdata.endTime));
- ChangeRecord changeRecord = new()
- {
- RowKey = tdata.id,
- PartitionKey = PartitionKey,
- sequenceNumber = end,
- msgId = messageVoteEnd.MessageId
- };
- await table.Save<ChangeRecord>(changeRecord);
- }
- break;
- case "finish":
- break;
- }
- }
- }
- catch (CosmosException e)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-CosmosDB异常{e.Message}\n{e.StackTrace}\n{e.Status}", GroupNames.醍摩豆服務運維群組);
- }
- catch (Exception ex)
- {
- await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}艺术评价异常{ex.Message}\n{ex.StackTrace}", GroupNames.醍摩豆服務運維群組);
- }
- }
- }
- }
|