using Azure.Cosmos;
using Microsoft.Azure.WebJobs;
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;
using TEAMModelOS.SDK.DI;
using TEAMModelOS.SDK.Extension;
using TEAMModelOS.SDK.Models.Cosmos.Common;
namespace TEAMModelFunction
{
public class CourseServiceBus
{
private readonly AzureCosmosFactory _azureCosmos;
private readonly DingDing _dingDing;
public CourseServiceBus(AzureCosmosFactory azureCosmos, DingDing dingDing)
{
_azureCosmos = azureCosmos;
_dingDing = dingDing;
}
///
/// 完善课程变更
///
///
/// CourseChange
/////
///
///
[FunctionName("Course")]
public async Task Course([ServiceBusTrigger("active-task", "course", Connection = "Azure:ServiceBus:ConnectionString")] string msg)
{
var client = _azureCosmos.GetCosmosClient();
try
{
await _dingDing.SendBotMsg($"ServiceBus,CourseChange:{msg}", GroupNames.醍摩豆服務運維群組);
var jsonMsg = JsonDocument.Parse(msg);
CourseChange courseChange = msg.ToObject();
//根据新增名单获取 新增的学生id 及timdid
(List addTmdids, List addStudents) = await TriggerStuActivity.GetStuList(client, courseChange.addList, courseChange.school);
//根据删除名单获取 新增的学生id 及timdid
(List delTmdids, List delStudents) = await TriggerStuActivity.GetStuList(client, courseChange.delList, courseChange.school);
foreach (var addStu in addStudents)
{
var course = new StuCourse
{
id = courseChange.id,
scode = courseChange.code,
name = courseChange.name,
code = $"Course-{courseChange.school}-{addStu.id}",
scope = courseChange.scope,
school = courseChange.school,
creatorId = courseChange.creatorId,
pk = "Course"
};
await client.GetContainer("TEAMModelOS", "Student").UpsertItemAsync(course, new PartitionKey(course.code));
}
foreach (var addTmd in addTmdids)
{
var course = new StuCourse
{
id = courseChange.id,
scode = courseChange.code,
name = courseChange.name,
code = $"Course-{addTmd}",
scope = courseChange.scope,
//school = courseChange.school,
creatorId = courseChange.creatorId,
pk = "Course"
};
await client.GetContainer("TEAMModelOS", "Teacher").UpsertItemAsync(course, new PartitionKey(course.code));
}
foreach (var delStu in delStudents)
{
await client.GetContainer("TEAMModelOS", "Student").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{courseChange.school}-{delStu.id}"));
}
foreach (var delTmd in delTmdids)
{
await client.GetContainer("TEAMModelOS", "Teacher").DeleteItemStreamAsync(courseChange.id, new PartitionKey($"Course-{delTmd}"));
}
}
catch (Exception ex)
{
await _dingDing.SendBotMsg($"CourseServiceBus-Course\n{ex.Message}{ex.StackTrace}", GroupNames.成都开发測試群組);
}
}
}
}